Advertisement
BinayVoice

Stuff

Dec 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.05 KB | None | 0 0
  1. #include "AppBase.Console.HXX"
  2.  
  3. #include "MySQL/SQL.HXX"
  4.  
  5. #include "ConfigSQL.Description.TXT"
  6.  
  7.  
  8. class CAPPLICATION : public CAPPLICATION_BASECLASS
  9. {
  10.     public:
  11.         bool    ProcessInit(CCONFIG&);
  12.         void    ProcessExec();
  13.         void    CreateDBTables(CStringList & tables);
  14.  
  15.     private:
  16.         CSLIST  DataFiles;
  17.         CPATH   outputpath;
  18.         CSTR    outputname;
  19.         SQL_Connect sql_connect;
  20. };
  21.  
  22. CAPPLICATION application;
  23.  
  24. CXXAPP * cxxapp = &application;
  25.  
  26.  
  27.  
  28. bool AddDBNames = true;
  29.  
  30. typedef struct
  31. {
  32.   char * table;
  33.   uint * fields;
  34.   uint   fieldcount;
  35.   CSLIST * fieldmap;
  36. } DB_TABLE_INFO;
  37.  
  38. CHAR * db_table_name[1];
  39. CHAR * db_field_name[1];
  40. DB_TABLE_INFO db_table_info[1];
  41.  
  42. uint    db_table_name_count = 0;
  43. uint    db_field_name_count = 0;
  44. uint    db_table_info_count = 0;
  45.  
  46. #include <MySQL/DB_TABLES.INC>
  47.  
  48.  
  49. bool CAPPLICATION::ProcessInit(CCONFIG & config)
  50. {
  51.     description = DESCRIPTION;
  52.  
  53.     if(!CAPPLICATION_BASECLASS::ProcessInit(config) )
  54.     {
  55.         return false;
  56.     }
  57.     Output(EOL);
  58.  
  59.     if( !sql_connect.Config(config) )
  60.     {
  61.         return false;
  62.     }
  63.     Output(EOL);
  64.  
  65.     if( !ConfigOptionalPath (config,"OutputPath",outputpath) )
  66.     {
  67.         outputpath.SetName(configfile);
  68.         outputpath.SetNamePart();
  69.     }
  70.     if( !ConfigOptionalEntry(config,"OutputName",outputname) )
  71.     {
  72.         outputname = "DB_TABLES";
  73.     }
  74.     if( !ConfigMustHaveEntry(config,"DataFiles",DataFiles) )
  75.     {
  76.         return false;
  77.     }
  78.     Output(EOL);
  79.     return true;
  80. }
  81.  
  82.  
  83. void CAPPLICATION::ProcessExec()
  84. {
  85.     for(char * s; DataFiles.Next(&s); )
  86.     {
  87.         CStringList database;
  88.  
  89.         if( CommaFields(s,database) )
  90.         {
  91.             CreateDBTables(database);
  92.         }
  93.     }
  94. }
  95.  
  96.  
  97. class CSQLTable
  98. {
  99. public:
  100.     CSTR64 id;
  101.     SQL_Table table;
  102. };
  103.  
  104. CREATECLASS_CPOINTERARRAY(CSQLTable)
  105.  
  106.  
  107. int __cdecl sortcomp_CSQLTable(CSQLTable ** t1,CSQLTable ** t2,void * unused)
  108. {
  109.     int comp = sz_comp((*t1)->table.db,(*t2)->table.db);
  110.  
  111.     if( comp == 0 )
  112.     {
  113.         comp = sz_comp((*t1)->table.table,(*t2)->table.table);
  114.     }
  115.     return comp;
  116. }
  117.  
  118.  
  119. void CAPPLICATION::CreateDBTables(CStringList & database)
  120. {
  121.     CSTR256 s1,s2; CHAR * s;
  122.  
  123.     CPOINTERARRAY_CSQLTable tables; CSLIST db_fields;
  124.  
  125.     SQL_Select sql_select(sql_connect);
  126.  
  127.     for( CHAR * db = NULL; database.Next(&db); )
  128.     {
  129.         if(!sql_connect.Query(s1.Format("USE `%s`",db)) )
  130.         {
  131.             continue;
  132.         }
  133.  
  134.         if( sql_select.SelectBegin("SHOW TABLES") )
  135.         {
  136.             CHAR * field = sql_select.GetFieldName(0);
  137.  
  138.             while( sql_select.SelectNext() )
  139.             {
  140.                 CSQLTable * table = tables.Append();
  141.  
  142.                 table->table.db = db;
  143.  
  144.                 table->table.table = sql_select.GetFieldValue((UINT)0);
  145.  
  146.                 CHAR * t = s2.Copy(table->table.table);
  147.  
  148.                 while( (s = sz_token(&t,"-")) != NULL )
  149.                 {
  150.                     table->id.Concat(ch_toupper(*s));
  151.                     table->id.Concat(++s);
  152.                 }
  153.             }
  154.             sql_select.SelectClear();
  155.         }
  156.     }
  157.  
  158.     tables.ArraySort((SORTCOMP)sortcomp_CSQLTable);
  159.  
  160.     db_fields.Add("0");
  161.  
  162.     for( UINT i = 0; i < tables.GetCount(); ++i )
  163.     {
  164.         CSQLTable * table = tables.GetAt(i);
  165.  
  166.         if(!sql_connect.Query(s1.Format("USE `%s`",sz(table->table.db))) )
  167.         {
  168.             continue;
  169.         }
  170.         if( SQL_GetTableInfo(sql_connect,table->table) )
  171.         {
  172.             db_fields.Concat(table->table.fields);
  173.         }
  174.     }
  175.  
  176.     CString64 filename(outputname);
  177.  
  178.     for( CHAR * db = NULL; database.Next(&db); )
  179.     {
  180.         filename.Concat("-",db);
  181.     }
  182.  
  183.     CFILEOUTPUT output(outputpath,filename,"HXX");
  184.  
  185.     if( output.OpenWRITE() )
  186.     {
  187.         output.PutEOL();
  188.         output.PutLine("#define DB_TABLE(id) db_table_##id,");
  189.         output.PutEOL();
  190.         output.PutLine("typedef enum");
  191.         output.PutLine("{");
  192.  
  193.         for( UINT i = 0; i < tables.GetCount(); ++i )
  194.         {
  195.             CSQLTable * table = tables.GetAt(i);
  196.  
  197.             output.PrintLine("  DB_TABLE(%s)",(CHAR*)table->id);
  198.         }
  199.         output.PutLine("} DB_TABLE_enum;");
  200.         output.PutEOL();
  201.         output.PutLine("#undef  DB_TABLE");
  202.         output.PutLine("#define DB_TABLE(id) db_table_##id");
  203.         output.PutLine("#define DB_Table(id) db_table_name[db_table_##id]");
  204.         output.PutEOL();
  205.         output.PutLine("extern CHAR * db_table_name[];");
  206.         output.PutLine("extern uint   db_table_name_count;");
  207.  
  208.        
  209.         output.PutEOL();
  210.         output.PutLine("#define DB_FIELD(id) db_field_##id,");
  211.         output.PutEOL();
  212.         output.PutLine("typedef enum");
  213.         output.PutLine("{");
  214.  
  215.         for( s = NULL; db_fields.Next(&s); )
  216.         {
  217.             output.PrintLine("  DB_FIELD(%s)",sz_strip(s1.Copy(s)," "));
  218.         }
  219.         output.PutLine("} DB_FIELD_enum;");
  220.         output.PutEOL();
  221.         output.PutLine("#undef  DB_FIELD");
  222.         output.PutLine("#define DB_FIELD(id) db_field_##id");
  223.         output.PutLine("#define DB_Field(id) db_field_name[db_field_##id]");
  224.         output.PutEOL();
  225.         output.PutLine("extern CHAR * db_field_name[];");
  226.         output.PutLine("extern uint   db_field_name_count;");
  227.         output.Close();
  228.     }
  229.  
  230.     output.SetName(outputpath,filename,"CXX");
  231.  
  232.     if( output.OpenWRITE() )
  233.     {
  234.         output.PutEOL();
  235.         output.PutLine("#include \"$CXXLIB.HXX\"");
  236.         output.PutEOL();
  237.         output.PutLine(s1.Copycat("#include \"",filename,".HXX\""));
  238.         output.PutEOL();
  239.         output.PutLine("CHAR * db_table_name[] =");
  240.         output.PutLine("{");
  241.  
  242.         for( UINT i = 0; i < tables.GetCount(); ++i )
  243.         {
  244.             CSQLTable * table = tables.GetAt(i);
  245.  
  246.             output.PrintLine("  \"%s.%s\",",(CHAR*)table->table.db,(CHAR*)table->table.table);
  247.         }
  248.  
  249.         output.PutLine("};");
  250.         output.PutLine("uint db_table_name_count = (sizeof(db_table_name)/sizeof(db_table_name[0]));");
  251.         output.PutEOL();
  252.  
  253.  
  254.         output.PutEOL();
  255.         output.PutLine("CHAR * db_field_name[] =");
  256.         output.PutLine("{");
  257.  
  258.         for( s = NULL; db_fields.Next(&s); )
  259.         {
  260.             output.PrintLine("  \"%s\",",s);
  261.         }
  262.  
  263.         output.PutLine("};");
  264.         output.PutLine("uint db_field_name_count = (sizeof(db_field_name)/sizeof(db_field_name[0]));");
  265.         output.PutEOL();
  266.  
  267.  
  268.         for( i = 0; i < tables.GetCount(); ++i )
  269.         {
  270.             CSQLTable * table = tables.GetAt(i);
  271.  
  272.             sz_substitute(s1.Copy(table->table.table),'-','_');
  273.  
  274.             output.PrintLine("static uint required_%s[]=",(CHAR*)s1);
  275.  
  276.             output.PutLine("{");
  277.  
  278.             for( CHAR * s = NULL; table->table.fields.Next(&s); )
  279.             {
  280.                 output.PrintLine("  DB_FIELD(%s),",sz_strip(s1.Copy(s)," "));
  281.             }
  282.             output.PutLine("};"EOL);
  283.         }
  284.  
  285.         output.PutEOL();
  286.         output.PutLine("typedef struct");
  287.         output.PutLine("{");
  288.         output.PutLine("  char * table;");
  289.         output.PutLine("  uint * fields;");
  290.         output.PutLine("  uint   fieldcount;");
  291.         output.PutLine("  CSLIST * fieldmap;");
  292.         output.PutLine("} DB_TABLE_INFO;");
  293.         output.PutEOL();
  294.         output.PutLine("DB_TABLE_INFO db_table_info[] =");
  295.         output.PutLine("{");
  296.  
  297.         for( i = 0; i < tables.GetCount(); ++i )
  298.         {
  299.             CSQLTable * table = tables.GetAt(i);
  300.  
  301.             sz_substitute(s2.Copy(table->table.table),'-','_');
  302.  
  303.             output.PrintLine("  {\"%s\",required_%s,(sizeof(required_%s)/sizeof(required_%s[0]))},",(CHAR*)table->id,(CHAR*)s2,(CHAR*)s2,(CHAR*)s2);
  304.         }
  305.         output.PutLine("};"EOL);
  306.         output.PutLine("uint db_table_info_count = (sizeof(db_table_info)/sizeof(db_table_info[0]));");
  307.         output.PutEOL();
  308.         output.PutLine("#include <MySQL/DB_TABLES.INC>");
  309.         output.PutEOL();
  310.  
  311.         output.Close();
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement