// Generated by Haxe 4.3.1
package database;

import haxe.root.*;

@SuppressWarnings(value={"rawtypes", "unchecked"})
public class DBPostgreSQL extends haxe.lang.HxObject
{
	public DBPostgreSQL(haxe.lang.EmptyObject empty)
	{
	}
	
	
	public DBPostgreSQL()
	{
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		database.DBPostgreSQL.__hx_ctor_database_DBPostgreSQL(this);
	}
	
	
	protected static void __hx_ctor_database_DBPostgreSQL(database.DBPostgreSQL __hx_this)
	{
	}
	
	
	public static java.lang.String generateSQL(model.MLD mld)
	{
		//line 18 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		java.lang.String sql = "";
		//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		java.lang.String sqlrefs = "";
		//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		{
			//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.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/DBPostgreSQL.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/DBPostgreSQL.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/DBPostgreSQL.hx"
				boolean hasPK = false;
				//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
				sql += ( ( "CREATE TABLE IF NOT EXISTS " + r1.name ) + " (" );
				//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
				boolean first = true;
				//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
				{
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
					int _g = 0;
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
					haxe.root.Array<model.Field> _g1 = r1.fields;
					//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
					while (( _g < _g1.length ))
					{
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						model.Field f = _g1.__get(_g);
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						 ++ _g;
						//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						java.lang.Object fk = ((java.lang.Object) (haxe.root.Lambda.find(((java.lang.Object) (r1.foreignKeys) ), ((haxe.lang.Function) (new database.DBPostgreSQL_generateSQL_25__Fun(f)) ))) );
						//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						if (first) 
						{
							//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
							first = false;
						}
						else
						{
							//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
							sql += ", ";
						}
						
						//line 27 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						sql += ( f.name + " " );
						//line 29 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						if (haxe.lang.Runtime.valEq(f.type.toUpperCase(), "COUNTER")) 
						{
							//line 30 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
							if (( ( r1.key.length == 1 ) && haxe.lang.Runtime.valEq(r1.key.__get(0), f.name) )) 
							{
								//line 31 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
								sql += "SERIAL PRIMARY KEY";
								//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
								hasPK = true;
							}
							else
							{
								//line 34 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
								sql += "INT";
							}
							
						}
						else
						{
							//line 37 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
							sql += f.dbType;
						}
						
						//line 40 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						if (( (  ! (hasPK)  && ( r1.key.length == 1 ) ) && haxe.lang.Runtime.valEq(r1.key.__get(0), f.name) )) 
						{
							//line 41 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
							sql += " PRIMARY KEY";
						}
						
						//line 43 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
						if (( ! (( fk == null )) )) 
						{
							//line 44 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.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 47 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
				if (( r1.key.length != 1 )) 
				{
					//line 48 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
					sql += ( ( ", PRIMARY KEY(" + r1.key.join(",") ) + ")" );
				}
				
				//line 50 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
				sql += ");\n";
			}
			
		}
		
		//line 52 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		sql += sqlrefs;
		//line 53 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		return sql;
	}
	
	
	public static sys.db.Connection getConnection(java.lang.String paramStr)
	{
		//line 65 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		return null;
	}
	
	
	public static void createDatabase(java.lang.String paramStr, java.lang.String sqlFile)
	{
	}
	
	
	public static model.MLD analyse(java.lang.String paramStr)
	{
		//line 73 "/home/frank/Documents/Developpements/dbconcept-0.5.0/src/database/DBPostgreSQL.hx"
		return null;
	}
	
	
}


