// Generated by Haxe 4.3.1
package database;

import haxe.root.*;

@SuppressWarnings(value={"rawtypes", "unchecked"})
public class DBSqlite extends haxe.lang.HxObject
{
	public DBSqlite(haxe.lang.EmptyObject empty)
	{
	}
	
	
	public DBSqlite()
	{
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		database.DBSqlite.__hx_ctor_database_DBSqlite(this);
	}
	
	
	protected static void __hx_ctor_database_DBSqlite(database.DBSqlite __hx_this)
	{
	}
	
	
	public static java.lang.String generateSQL(model.MLD mld)
	{
		//line 18 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		java.lang.String sql = "";
		//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		{
			//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
			{
				//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
				//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				boolean hasPK = false;
				//line 21 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				sql += ( ( "CREATE TABLE IF NOT EXISTS " + r1.name ) + " (" );
				//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				boolean first = true;
				//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				{
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					int _g = 0;
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					haxe.root.Array<model.Field> _g1 = r1.fields;
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					while (( _g < _g1.length ))
					{
						//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						model.Field f = _g1.__get(_g);
						//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						 ++ _g;
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						java.lang.Object fk = ((java.lang.Object) (haxe.root.Lambda.find(((java.lang.Object) (r1.foreignKeys) ), ((haxe.lang.Function) (new database.DBSqlite_generateSQL_24__Fun(f)) ))) );
						//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (first) 
						{
							//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							first = false;
						}
						else
						{
							//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							sql += ", ";
						}
						
						//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						sql += ( f.name + " " );
						//line 28 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (haxe.lang.Runtime.valEq(f.type.toUpperCase(), "COUNTER")) 
						{
							//line 29 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.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/DBSqlite.hx"
								sql += "INTEGER PRIMARY KEY AUTOINCREMENT";
								//line 31 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
								hasPK = true;
							}
							else
							{
								//line 33 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
								sql += "INT";
							}
							
						}
						else
						{
							//line 36 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							sql += f.dbType;
						}
						
						//line 39 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.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/DBSqlite.hx"
							sql += " PRIMARY KEY";
						}
						
						//line 42 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (( ! (( fk == null )) )) 
						{
							//line 43 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							sql += ( ( ( ( " REFERENCES " + ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).name ) + "(" ) + haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(fk, "key", true)) ) + ")" );
						}
						
					}
					
				}
				
				//line 46 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				if (( r1.key.length != 1 )) 
				{
					//line 47 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					sql += ( ( ", PRIMARY KEY(" + r1.key.join(",") ) + ")" );
				}
				
				//line 49 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				sql += ");\n";
			}
			
		}
		
		//line 51 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		return sql;
	}
	
	
	public static sys.db.Connection getConnection(java.lang.String filename)
	{
		//line 61 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		return sys.db.Sqlite.open(filename);
	}
	
	
	public static void createDatabase(java.lang.String filename, java.lang.String sqlFile)
	{
		//line 65 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		sys.db.Connection db = database.DBSqlite.getConnection(filename);
		//line 66 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		if (( db != null )) 
		{
			//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			{
				//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				int _g = 0;
				//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				haxe.root.Array<java.lang.String> _g1 = haxe.lang.StringExt.split(sys.io.File.getContent(sqlFile), "\n");
				//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				while (( _g < _g1.length ))
				{
					//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					java.lang.String r = _g1.__get(_g);
					//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					 ++ _g;
					//line 68 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					if ( ! (haxe.lang.Runtime.valEq(r, "")) ) 
					{
						//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						db.request(r);
					}
					
				}
				
			}
			
			//line 72 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			db.close();
		}
		
	}
	
	
	public static model.MLD analyse(java.lang.String filename)
	{
		//line 77 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		model.MLD mld = new model.MLD();
		//line 78 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		sys.db.Connection db = sys.db.Sqlite.open(filename);
		//line 79 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		sys.db.ResultSet sqlrs = db.request("SELECT name FROM sqlite_master WHERE type=\'table\' ORDER BY name");
		//line 80 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		{
			//line 80 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			sys.db.ResultSet sqlr = sqlrs;
			//line 80 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			while (sqlr.hasNext())
			{
				//line 80 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				java.lang.Object sqlr1 = sqlr.next();
				//line 81 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				if ( ! (haxe.lang.Runtime.valEq(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlr1, "name", true)), "sqlite_sequence")) ) 
				{
					//line 82 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					mld.relations.set(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlr1, "name", true)), new model.Relation(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlr1, "name", true))));
				}
				
			}
			
		}
		
		//line 85 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		{
			//line 85 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 85 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
			{
				//line 85 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
				//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				sqlrs = db.request(( ( "PRAGMA table_info(" + r1.name ) + ")" ));
				//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				{
					//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					sys.db.ResultSet sqlf = sqlrs;
					//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					while (sqlf.hasNext())
					{
						//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						java.lang.Object sqlf1 = sqlf.next();
						//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (( ( haxe.lang.Runtime.valEq(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "type", true)), "INTEGER") && ( ((int) (haxe.lang.Runtime.getField_f(sqlf1, "pk", true)) ) != 0 ) ) && ( db.request(( ( "SELECT * FROM sqlite_sequence WHERE name = \'" + r1.name ) + "\'" )).get_length() != 0 ) )) 
						{
							//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							r1.fields.push(new model.Field(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "name", true)), haxe.lang.Runtime.toString("COUNTER"), ((java.lang.Object) (null) ), ((java.lang.Object) (null) )));
						}
						else
						{
							//line 91 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							r1.fields.push(new model.Field(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "name", true)), haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "type", true)), ((java.lang.Object) (null) ), ((java.lang.Object) (null) )));
						}
						
						//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (( ((int) (haxe.lang.Runtime.getField_f(sqlf1, "pk", true)) ) != 0 )) 
						{
							//line 94 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							r1.key.push(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlf1, "name", true)));
						}
						
					}
					
				}
				
				//line 97 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				sqlrs = db.request(( ( "PRAGMA foreign_key_list(" + r1.name ) + ")" ));
				//line 98 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
				{
					//line 98 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					sys.db.ResultSet sqlfk = sqlrs;
					//line 98 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
					while (sqlfk.hasNext())
					{
						//line 98 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						java.lang.Object sqlfk1 = sqlfk.next();
						//line 99 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						model.Relation t = mld.relations.get(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "table", true)));
						//line 100 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
						if (( t != null )) 
						{
							//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							java.lang.Object __temp_stmt3 = null;
							//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							{
								//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
								java.lang.String __temp_odecl1 = haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "from", true));
								//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
								java.lang.String __temp_odecl2 = haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(sqlfk1, "to", true));
								//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.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 101 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
							r1.foreignKeys.push(__temp_stmt3);
						}
						
					}
					
				}
				
			}
			
		}
		
		//line 105 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		db.close();
		//line 106 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBSqlite.hx"
		return mld;
	}
	
	
}


