Advertisement
946ben

MyDump - Include v1.0

Aug 11th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.82 KB | None | 0 0
  1. #if defined MYDUMP
  2.     #endinput
  3. #endif
  4. #define MYDUMP
  5. #if defined MAX_TABLES
  6.     #if MAX_TABLES > 300
  7.         #error MAX_TABLES can't be bigger than 300
  8.     #endif
  9. #else
  10.     #define MAX_TABLES 100
  11. #endif
  12. #define INVALID_TIMER 0xFFFF
  13.  
  14. new MyDump_Table[MAX_TABLES][32];
  15. new MyDump_Temp[4000];
  16. new SMyDump_Temp[1000];
  17. new WMyDump_Temp[1000];
  18. new AMyDump_Temp[5000];
  19. new FMyDump_Temp[1000];
  20.  
  21. new MyDump_DB[MyDump:4][50];
  22.  
  23. new AutoExportTimer[MyDump:4] = INVALID_TIMER;
  24. new AutoExportTime[MyDump:4] = 43200;
  25. new AutoExportIfNotExists[MyDump:4] = 1;
  26.  
  27. new MyDump_find1;
  28. new MyDump_find2;
  29.  
  30. stock MyDump:MyDumpExportInit(host[], user[], pw[], db[])
  31. {
  32.     new MySQL:id;
  33.     new MyDump:MyID;
  34.     id = mysql_init();
  35.     mysql_connect(host, user, pw, db, id);
  36.     MyID = MyDump:id;
  37.     format(MyDump_DB[MyID], 50, "%s", db);
  38.     return MyDump:id;
  39. }
  40.  
  41. stock MyDumpExportExit(MyDump:handle)
  42. {
  43.     mysql_close(MySQL:handle);
  44.     return 1;
  45. }
  46.  
  47. stock SaveTables(MyDump:handle)
  48. {
  49.     format(MyDump_Temp, sizeof(MyDump_Temp), "SHOW TABLES FROM `%s`;", MyDump_DB[handle]);
  50.     mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
  51.     mysql_store_result(MySQL:handle);
  52.     new data_table[32];
  53.     new i;
  54.     for(; i<MAX_TABLES; i++) MyDump_Table[i] = "\0";
  55.     new bool:con;
  56.     i = 0;
  57.     while(mysql_fetch_row(data_table))
  58.     {
  59.         if(con) continue;
  60.         format(MyDump_Table[i], 32, "%s", data_table);
  61.         i++;
  62.         printf(">> %s", data_table);
  63.         if(i == MAX_TABLES) con = true;
  64.     }
  65.     mysql_free_result(MySQL:handle);
  66.     return 1;
  67. }
  68.  
  69. stock ExportTablesToFile(file[], MyDump:handle, IfNotExists = 1)
  70. {
  71.     SaveTables(handle);
  72.     for(new i; i<MAX_TABLES; i++) {
  73.         if(strlen(MyDump_Table[i]) == 0) continue;
  74.         new File:fhandle;
  75.         format(MyDump_Temp, sizeof(MyDump_Temp), "%s", ExportTable(MyDump_Table[i], handle));
  76.         strdel(MyDump_Temp, 42+strlen(MyDump_Table[i]), strlen(MyDump_Table[i])+1+42+strlen(MyDump_Table[i]));
  77.         format(MyDump_Temp, sizeof(MyDump_Temp), "%s\n", MyDump_Temp);
  78.         if(IfNotExists) strins(MyDump_Temp, " IF NOT EXISTS", 12+42+strlen(MyDump_Table[i]), 20);
  79.         if(!fexist(file)) fhandle = fopen(file, io_write);
  80.         else fhandle = fopen(file, io_append);
  81.         fwrite(fhandle, MyDump_Temp);
  82.         fclose(fhandle);
  83.         fhandle = fopen(file, io_append);
  84.         MyDump_find1 = strfind(MyDump_Temp, "(", true);
  85.         MyDump_find2 = strfind(MyDump_Temp, ") ENGINE=", true);
  86.         strdel(MyDump_Temp, 0, MyDump_find1+1);
  87.         strdel(MyDump_Temp, MyDump_find2-MyDump_find1+1, strlen(MyDump_Temp));
  88.         MyDump_find1 = strfind(MyDump_Temp, "PRIMARY", true);
  89.         MyDump_find2 = strfind(MyDump_Temp, ")", true, MyDump_find1);
  90.         strdel(MyDump_Temp, MyDump_find1, MyDump_find2+3);
  91.         MyDump_find1 = strfind(MyDump_Temp, "`", true);
  92.         strdel(MyDump_Temp, 0, MyDump_find1+1);
  93.         format(SMyDump_Temp, sizeof(SMyDump_Temp), "\n--\n-- Daten für Tabelle `%s`\n--\n\nINSERT INTO `%s` (", MyDump_Table[i], MyDump_Table[i]);
  94.         while(MyDump_find2 != -1) {
  95.             MyDump_find1 = strfind(MyDump_Temp, "`", true);
  96.             format(WMyDump_Temp, sizeof(WMyDump_Temp), "%s", MyDump_Temp);
  97.             strdel(WMyDump_Temp, MyDump_find1, strlen(WMyDump_Temp));
  98.             MyDump_find2 = strfind(MyDump_Temp, "`", true, MyDump_find1+1);
  99.             strdel(MyDump_Temp, 0, MyDump_find2+1);
  100.             printf("> %s", WMyDump_Temp);
  101.             format(SMyDump_Temp, sizeof(SMyDump_Temp), "%s,`%s`", SMyDump_Temp, WMyDump_Temp);
  102.         }
  103.         strdel(SMyDump_Temp, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+16+32, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+17+32);
  104.         fwrite(fhandle, SMyDump_Temp);
  105.         ExportContents(MyDump_Table[i], fhandle, handle);
  106.         fclose(fhandle);
  107.     }
  108.     return 1;
  109. }
  110.  
  111. stock ExportContents(table[], File:fhandle, MyDump:handle)
  112. {
  113.     format(MyDump_Temp, sizeof(MyDump_Temp), "SELECT * FROM `%s`;", table);
  114.     mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
  115.     mysql_store_result(MySQL:handle);
  116.     fwrite(fhandle, ") VALUES");
  117.     new rows = mysql_num_rows(MySQL:handle);
  118.     new row;
  119.     while(mysql_fetch_row(MyDump_Temp, "|", MySQL:handle))
  120.     {
  121.         row++;
  122.         new num = mysql_num_fields(MySQL:handle);
  123.         fwrite(fhandle, "\n(");
  124.         AMyDump_Temp = "\0";
  125.         for(new o; o<num; o++)
  126.         {
  127.             mysql_fetch_field_num(o, FMyDump_Temp, MySQL:handle);
  128.             format(AMyDump_Temp, sizeof(AMyDump_Temp), "%s'%s',", AMyDump_Temp, FMyDump_Temp);
  129.         }
  130.         strdel(AMyDump_Temp, strlen(AMyDump_Temp)-1, strlen(AMyDump_Temp));
  131.         fwrite(fhandle, AMyDump_Temp);
  132.         if(rows == row) fwrite(fhandle, ");\n\n");
  133.         else fwrite(fhandle, "),");
  134.     }
  135.     mysql_free_result(MySQL:handle);
  136.     return 1;
  137. }
  138.  
  139. stock ExportTable(data_table[], MyDump:handle)
  140. {
  141.     format(MyDump_Temp, sizeof(MyDump_Temp), "SHOW CREATE TABLE `%s`;", data_table);
  142.     mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
  143.     mysql_store_result(MySQL:handle);
  144.     mysql_fetch_row(MyDump_Temp, "|", MySQL:handle);
  145.     mysql_free_result(MySQL:handle);
  146.     format(MyDump_Temp, sizeof(MyDump_Temp), "--\n-- Tabellenstruktur für Tabelle `%s`\n--\n\n%s", data_table, MyDump_Temp);
  147.     return MyDump_Temp;
  148. }
  149.  
  150. stock SetAutoExportTime(sec, MyDump:handle)
  151. {
  152.     AutoExportTime[handle] = sec;
  153.     return 1;
  154. }
  155.  
  156. stock SetAutoExportIfNotExists(num, MyDump:handle)
  157. {
  158.     AutoExportIfNotExists[handle] = num;
  159.     return 1;
  160. }
  161.  
  162. stock AutoExportTimerStart(MyDump:handle)
  163. {
  164.     if(AutoExportTimer[handle] != INVALID_TIMER) return 0;
  165.     AutoExportTimer[handle] = SetTimerEx("AutoExport", AutoExportTimer[handle]*1000, 1, "dd", int:handle, int:AutoExportIfNotExists[handle]);
  166.     return 1;
  167. }
  168.  
  169. stock AutoExportTimerStop(MyDump:handle)
  170. {
  171.     KillTimer(AutoExportTimer[handle]);
  172.     AutoExportTimer[handle] = INVALID_TIMER;
  173.     return 1;
  174. }
  175.  
  176. forward AutoExport(MyDump:handle, ifnotexists);
  177. public AutoExport(MyDump:handle, ifnotexists)
  178. {
  179.     new stamp, h, m, s;
  180.     stamp = getdate(h, m, s);
  181.     new str[50];
  182.     format(str, sizeof(str), "%d.sql", stamp);
  183.     ExportTablesToFile(str, handle, ifnotexists);
  184.     return 1;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement