Advertisement
946ben

MyDump - Include v1.1

Aug 11th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.66 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.     i = 0;
  56.     while(mysql_fetch_row(data_table))
  57.     {
  58.         format(MyDump_Table[i], 32, "%s", data_table);
  59.         i++;
  60.         printf(">> %s", data_table);
  61.         if(i == MAX_TABLES) break;
  62.     }
  63.     mysql_free_result(MySQL:handle);
  64.     return 1;
  65. }
  66.  
  67. stock ExportTablesToFile(file[], MyDump:handle, IfNotExists = 1)
  68. {
  69.     SaveTables(handle);
  70.     for(new i; i<MAX_TABLES; i++) {
  71.         if(strlen(MyDump_Table[i]) == 0) continue;
  72.         new File:fhandle;
  73.         format(MyDump_Temp, sizeof(MyDump_Temp), "%s", ExportTable(MyDump_Table[i], handle));
  74.         strdel(MyDump_Temp, 42+strlen(MyDump_Table[i]), strlen(MyDump_Table[i])+1+42+strlen(MyDump_Table[i]));
  75.         format(MyDump_Temp, sizeof(MyDump_Temp), "%s\n", MyDump_Temp);
  76.         if(IfNotExists) strins(MyDump_Temp, " IF NOT EXISTS", 12+42+strlen(MyDump_Table[i]), 20);
  77.         fhandle = fopen(file, io_append);
  78.         fwrite(fhandle, MyDump_Temp);
  79.         fclose(fhandle);
  80.         fhandle = fopen(file, io_append);
  81.         MyDump_find1 = strfind(MyDump_Temp, "(", true);
  82.         MyDump_find2 = strfind(MyDump_Temp, ") ENGINE=", true);
  83.         strdel(MyDump_Temp, 0, MyDump_find1+1);
  84.         strdel(MyDump_Temp, MyDump_find2-MyDump_find1+1, strlen(MyDump_Temp));
  85.         MyDump_find1 = strfind(MyDump_Temp, "PRIMARY", true);
  86.         MyDump_find2 = strfind(MyDump_Temp, ")", true, MyDump_find1);
  87.         strdel(MyDump_Temp, MyDump_find1, MyDump_find2+3);
  88.         MyDump_find1 = strfind(MyDump_Temp, "`", true);
  89.         strdel(MyDump_Temp, 0, MyDump_find1+1);
  90.         format(SMyDump_Temp, sizeof(SMyDump_Temp), "\n--\n-- Daten für Tabelle `%s`\n--\n\nINSERT INTO `%s` (", MyDump_Table[i], MyDump_Table[i]);
  91.         while(MyDump_find2 != -1) {
  92.             MyDump_find1 = strfind(MyDump_Temp, "`", true);
  93.             format(WMyDump_Temp, sizeof(WMyDump_Temp), "%s", MyDump_Temp);
  94.             strdel(WMyDump_Temp, MyDump_find1, strlen(WMyDump_Temp));
  95.             MyDump_find2 = strfind(MyDump_Temp, "`", true, MyDump_find1+1);
  96.             strdel(MyDump_Temp, 0, MyDump_find2+1);
  97.             format(SMyDump_Temp, sizeof(SMyDump_Temp), "%s,`%s`", SMyDump_Temp, WMyDump_Temp);
  98.         }
  99.         strdel(SMyDump_Temp, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+16+32, strlen(MyDump_Table[i])+strlen(MyDump_Table[i])+17+32);
  100.         fwrite(fhandle, SMyDump_Temp);
  101.         ExportContents(MyDump_Table[i], fhandle, handle);
  102.         fclose(fhandle);
  103.     }
  104.     return 1;
  105. }
  106.  
  107. stock ExportContents(table[], File:fhandle, MyDump:handle)
  108. {
  109.     format(MyDump_Temp, sizeof(MyDump_Temp), "SELECT * FROM `%s`;", table);
  110.     mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
  111.     mysql_store_result(MySQL:handle);
  112.     fwrite(fhandle, ") VALUES");
  113.     new rows = mysql_num_rows(MySQL:handle);
  114.     new row;
  115.     while(mysql_fetch_row(MyDump_Temp, "|", MySQL:handle))
  116.     {
  117.         row++;
  118.         new num = mysql_num_fields(MySQL:handle);
  119.         fwrite(fhandle, "\n(");
  120.         AMyDump_Temp = "\0";
  121.         for(new o; o<num; o++)
  122.         {
  123.             mysql_fetch_field_num(o, FMyDump_Temp, MySQL:handle);
  124.             format(AMyDump_Temp, sizeof(AMyDump_Temp), "%s'%s',", AMyDump_Temp, FMyDump_Temp);
  125.         }
  126.         strdel(AMyDump_Temp, strlen(AMyDump_Temp)-1, strlen(AMyDump_Temp));
  127.         fwrite(fhandle, AMyDump_Temp);
  128.         fwrite(fhandle, (rows == row)?(");\n\n"):("),"));
  129.     }
  130.     mysql_free_result(MySQL:handle);
  131.     return 1;
  132. }
  133.  
  134. stock ExportTable(data_table[], MyDump:handle)
  135. {
  136.     format(MyDump_Temp, sizeof(MyDump_Temp), "SHOW CREATE TABLE `%s`;", data_table);
  137.     mysql_query(MyDump_Temp, -1, 0, MySQL:handle);
  138.     mysql_store_result(MySQL:handle);
  139.     mysql_fetch_row(MyDump_Temp, "|", MySQL:handle);
  140.     mysql_free_result(MySQL:handle);
  141.     format(MyDump_Temp, sizeof(MyDump_Temp), "--\n-- Tabellenstruktur für Tabelle `%s`\n--\n\n%s", data_table, MyDump_Temp);
  142.     return MyDump_Temp;
  143. }
  144.  
  145. stock SetAutoExportTime(sec, MyDump:handle)
  146. {
  147.     AutoExportTime[handle] = sec;
  148.     return 1;
  149. }
  150.  
  151. stock SetAutoExportIfNotExists(num, MyDump:handle)
  152. {
  153.     AutoExportIfNotExists[handle] = num;
  154.     return 1;
  155. }
  156.  
  157. stock AutoExportTimerStart(MyDump:handle)
  158. {
  159.     if(AutoExportTimer[handle] != INVALID_TIMER) return 0;
  160.     AutoExportTimer[handle] = SetTimerEx("AutoExport", AutoExportTimer[handle]*1000, 1, "dd", int:handle, int:AutoExportIfNotExists[handle]);
  161.     return 1;
  162. }
  163.  
  164. stock AutoExportTimerStop(MyDump:handle)
  165. {
  166.     KillTimer(AutoExportTimer[handle]);
  167.     AutoExportTimer[handle] = INVALID_TIMER;
  168.     return 1;
  169. }
  170.  
  171. forward AutoExport(MyDump:handle, ifnotexists);
  172. public AutoExport(MyDump:handle, ifnotexists)
  173. {
  174.     new stamp, h, m, s;
  175.     stamp = getdate(h, m, s);
  176.     new str[50];
  177.     format(str, sizeof(str), "%d.sql", stamp);
  178.     ExportTablesToFile(str, handle, ifnotexists);
  179.     return 1;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement