// Generated by Haxe 4.3.1
package database;

import haxe.root.*;

@SuppressWarnings(value={"rawtypes", "unchecked"})
public class DBMariaDB extends haxe.lang.HxObject
{
	public DBMariaDB(haxe.lang.EmptyObject empty)
	{
	}
	
	
	public DBMariaDB()
	{
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		database.DBMariaDB.__hx_ctor_database_DBMariaDB(this);
	}
	
	
	protected static void __hx_ctor_database_DBMariaDB(database.DBMariaDB __hx_this)
	{
	}
	
	
	public static java.lang.String generateSQL(model.MLD mld)
	{
		//line 18 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		java.lang.String sql = "";
		//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		java.lang.String sqlrefs = "";
		//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		{
			//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
			{
				//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
				//line 21 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				boolean hasPK = false;
				//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				sql += ( ( "CREATE TABLE IF NOT EXISTS " + r1.name ) + " (" );
				//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				boolean first = true;
				//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				{
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					int _g = 0;
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					haxe.root.Array<model.Field> _g1 = r1.fields;
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					while (( _g < _g1.length ))
					{
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						model.Field f = _g1.__get(_g);
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						 ++ _g;
						//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						java.lang.Object fk = ((java.lang.Object) (haxe.root.Lambda.find(((java.lang.Object) (r1.foreignKeys) ), ((haxe.lang.Function) (new database.DBMariaDB_generateSQL_25__Fun(f)) ))) );
						//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (first) 
						{
							//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							first = false;
						}
						else
						{
							//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							sql += ", ";
						}
						
						//line 27 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						sql += ( f.name + " " );
						//line 28 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (haxe.lang.Runtime.valEq(f.type.toUpperCase(), "COUNTER")) 
						{
							//line 29 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							if (( ( r1.key.length == 1 ) && haxe.lang.Runtime.valEq(r1.key.__get(0), f.name) )) 
							{
								//line 30 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								sql += "INT PRIMARY KEY AUTO_INCREMENT";
								//line 31 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								hasPK = true;
							}
							else
							{
								//line 33 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								sql += "INT";
							}
							
						}
						else
						{
							//line 36 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							sql += f.dbType;
						}
						
						//line 39 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (( (  ! (hasPK)  && ( r1.key.length == 1 ) ) && haxe.lang.Runtime.valEq(r1.key.__get(0), f.name) )) 
						{
							//line 40 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							sql += " PRIMARY KEY";
						}
						
						//line 42 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (( ! (( fk == null )) )) 
						{
							//line 43 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							sqlrefs += ( ( ( ( ( ( ( ( "ALTER TABLE " + r1.name ) + " ADD FOREIGN KEY (" ) + f.name ) + ") REFERENCES " ) + ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).name ) + "(" ) + haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(fk, "key", true)) ) + ");\n" );
						}
						
					}
					
				}
				
				//line 46 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				if (( r1.key.length != 1 )) 
				{
					//line 47 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					sql += ( ( ", PRIMARY KEY(" + r1.key.join(",") ) + ")" );
				}
				
				//line 49 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				sql += ");\n";
			}
			
		}
		
		//line 51 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		sql += sqlrefs;
		//line 52 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		return sql;
	}
	
	
	public static sys.db.Connection getConnection(java.lang.String paramStr)
	{
		//line 62 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		haxe.root.Array<java.lang.String> params = haxe.lang.StringExt.split(paramStr, ",");
		//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		java.lang.Object __temp_stmt5 = null;
		//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		{
			//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.String __temp_odecl1 = params.__get(2);
			//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.String __temp_odecl2 = params.__get(3);
			//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.String __temp_odecl3 = params.__get(0);
			//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.String __temp_odecl4 = params.__get(1);
			//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			__temp_stmt5 = new haxe.lang.DynamicObject(new java.lang.String[]{"database", "host", "pass", "user"}, new java.lang.Object[]{__temp_odecl4, __temp_odecl3, __temp_odecl2, __temp_odecl1}, new java.lang.String[]{}, new double[]{});
		}
		
		//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		return sys.db.Mysql.connect(__temp_stmt5);
	}
	
	
	public static void createDatabase(java.lang.String paramStr, java.lang.String sqlFile)
	{
		//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		sys.db.Connection db = database.DBMariaDB.getConnection(paramStr);
		//line 68 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		if (( db != null )) 
		{
			//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			{
				//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				int _g = 0;
				//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				haxe.root.Array<java.lang.String> _g1 = haxe.lang.StringExt.split(sys.io.File.getContent(sqlFile), "\n");
				//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				while (( _g < _g1.length ))
				{
					//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					java.lang.String r = _g1.__get(_g);
					//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					 ++ _g;
					//line 70 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					if ( ! (haxe.lang.Runtime.valEq(r, "")) ) 
					{
						//line 71 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						db.request(r);
					}
					
				}
				
			}
			
			//line 74 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			db.close();
		}
		
	}
	
	
	public static model.MLD analyse(java.lang.String paramStr)
	{
		//line 79 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		model.MLD mld = new model.MLD();
		//line 80 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		sys.db.Connection db = database.DBMariaDB.getConnection(paramStr);
		//line 81 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		java.lang.String database1 = haxe.lang.StringExt.split(paramStr, ",").__get(1);
		//line 82 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		sys.db.ResultSet sqlrs = db.request(( ( "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = \'" + database1 ) + "\'" ));
		//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		{
			//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			sys.db.ResultSet sqlr = sqlrs;
			//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			while (sqlr.hasNext())
			{
				//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				java.lang.Object sqlr1 = sqlr.next();
				//line 84 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				mld.relations.set(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlr1, "TABLE_NAME", true)), new model.Relation(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlr1, "TABLE_NAME", true))));
			}
			
		}
		
		//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		{
			//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
			{
				//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
				//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				sqlrs = db.request(( ( ( ( ( "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, COLUMN_KEY, EXTRA FROM information_schema.COLUMNS " + "WHERE TABLE_SCHEMA = \'" ) + database1 ) + "\' AND TABLE_NAME = \'" ) + r1.name ) + "\' ORDER BY ORDINAL_POSITION" ));
				//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				{
					//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					sys.db.ResultSet sqlf = sqlrs;
					//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					while (sqlf.hasNext())
					{
						//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						java.lang.Object sqlf1 = sqlf.next();
						//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (( haxe.lang.Runtime.valEq(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "DATA_TYPE", true)), "int") && haxe.lang.Runtime.valEq(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "EXTRA", true)), "auto_increment") )) 
						{
							//line 91 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							r1.fields.push(new model.Field(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "COLUMN_NAME", true)), haxe.lang.Runtime.toString("COUNTER"), ((java.lang.Object) (null) ), ((java.lang.Object) (null) )));
						}
						else
						{
							//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							r1.fields.push(new model.Field(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "COLUMN_NAME", true)), haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "COLUMN_TYPE", true)), ((java.lang.Object) (null) ), ((java.lang.Object) (null) )));
						}
						
						//line 95 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (haxe.lang.Runtime.valEq(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "COLUMN_KEY", true)), "PRI")) 
						{
							//line 96 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							r1.key.push(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "COLUMN_NAME", true)));
						}
						
					}
					
				}
				
				//line 99 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				sqlrs = db.request(( ( ( ( ( "SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME  FROM information_schema.KEY_COLUMN_USAGE " + "WHERE TABLE_SCHEMA = \'" ) + database1 ) + "\' AND TABLE_NAME = \'" ) + r1.name ) + "\' AND REFERENCED_TABLE_NAME IS NOT NULL" ));
				//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
				{
					//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					sys.db.ResultSet sqlfk = sqlrs;
					//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
					while (sqlfk.hasNext())
					{
						//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						java.lang.Object sqlfk1 = sqlfk.next();
						//line 102 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						model.Relation t = mld.relations.get(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "REFERENCED_TABLE_NAME", true)));
						//line 103 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
						if (( t != null )) 
						{
							//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							java.lang.Object __temp_stmt3 = null;
							//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							{
								//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								java.lang.String __temp_odecl1 = haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "COLUMN_NAME", true));
								//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								java.lang.String __temp_odecl2 = haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "REFERENCED_COLUMN_NAME", true));
								//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
								__temp_stmt3 = new haxe.lang.DynamicObject(new java.lang.String[]{"key", "name", "relation"}, new java.lang.Object[]{__temp_odecl2, __temp_odecl1, t}, new java.lang.String[]{}, new double[]{});
							}
							
							//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
							r1.foreignKeys.push(__temp_stmt3);
						}
						
					}
					
				}
				
			}
			
		}
		
		//line 108 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		db.close();
		//line 109 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBMariaDB.hx"
		return mld;
	}
	
	
}


