Advertisement
Ladies_Man

cdb 2nd stage done

Mar 27th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Threading.Tasks;
  7. using System.Collections;
  8. using System.Diagnostics;
  9.  
  10. using System.Data.SQLite;
  11.  
  12. using MySql.Data;
  13. using MySql.Data.MySqlClient;
  14.  
  15. namespace adonet_4
  16. {
  17.     public class Globals
  18.         {
  19.             private static SortedDictionary<string, List<String>> _mapNameToDataList = new SortedDictionary<string, List<String>>();
  20.             private static List<string> _listTablenames;
  21.  
  22.             public static SortedDictionary<string, List<String>> mapNameToDataList
  23.             {
  24.                 get { return _mapNameToDataList; }
  25.                 set { _mapNameToDataList = value; }
  26.             }
  27.  
  28.             public static List<string> listTableNames
  29.             {
  30.                 get { return _listTablenames; }
  31.                 set { _listTablenames = value; }
  32.             }
  33.         }
  34.  
  35.     class Program
  36.     {
  37.  
  38.         static void execReaderSqlite(SQLiteCommand command)
  39.         {
  40.             try
  41.             {
  42.                 SQLiteDataReader reader = command.ExecuteReader();
  43.                 Console.WriteLine("Executing Sqlite reader for query:" + command.CommandText);
  44.                 do
  45.                 {
  46.                     while (reader.Read())
  47.                     {
  48.                         Object[] values = new Object[reader.FieldCount];
  49.                         int fieldCount = reader.GetValues(values);
  50.  
  51.                         for (int i = 0; i < fieldCount; i++)
  52.                         {
  53.                             Console.Write("\t{0}", values[i]);
  54.                         }
  55.                         Console.WriteLine();
  56.                     }
  57.  
  58.                 } while (reader.NextResult());
  59.  
  60.                 reader.Close();
  61.  
  62.             }
  63.             catch (Exception e)
  64.             {
  65.                 Console.WriteLine(e.Message);
  66.             }
  67.  
  68.             Console.WriteLine();
  69.         }
  70.  
  71.  
  72.         //      P E R E G R O O Z K A
  73.         static void execReaderMysql(MySqlCommand command)
  74.         {
  75.             try
  76.             {
  77.                 MySqlDataReader reader = command.ExecuteReader();
  78.                 Console.WriteLine("Executing Mysql reader for query:" + command.CommandText);
  79.                 do
  80.                 {
  81.                     while (reader.Read())
  82.                     {
  83.                         Object[] values = new Object[reader.FieldCount];
  84.                         int fieldCount = reader.GetValues(values);
  85.  
  86.                         for (int i = 0; i < fieldCount; i++)
  87.                         {
  88.                             Console.Write("\t{0}", values[i]);
  89.                            
  90.                         }
  91.                         Console.WriteLine();
  92.                     }
  93.  
  94.                 } while (reader.NextResult());
  95.  
  96.                 reader.Close();
  97.  
  98.             }
  99.             catch (Exception e)
  100.             {
  101.                 Console.WriteLine(e.Message);
  102.             }
  103.  
  104.             Console.WriteLine();
  105.         }
  106.  
  107.  
  108.  
  109.  
  110.         static List<string> retrieveTablenames(SQLiteConnection connection)
  111.         {
  112.             List<string> tmpList = new List<string>();
  113.  
  114.             try
  115.             {
  116.                
  117.                 string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
  118.                 SQLiteCommand cmd = new SQLiteCommand(q, connection);
  119.  
  120.                 SQLiteDataReader reader = cmd.ExecuteReader();
  121.  
  122.                 if (reader.HasRows)
  123.                 {
  124.                     int j = 0;
  125.                     while (reader.Read())
  126.                     {
  127.                         for (int i = 0; i < reader.FieldCount; i++)
  128.                         {
  129.                             string tableName = reader.GetValue(i).ToString();
  130.  
  131.                             tmpList.Add(tableName);
  132.                         }
  133.                         j++;
  134.                     }
  135.                 }
  136.             }
  137.             catch (Exception e)
  138.             {
  139.                 Console.WriteLine(e.Message);
  140.                 Console.ReadLine();
  141.             }
  142.  
  143.             return tmpList;
  144.         }
  145.  
  146.        
  147.         static List<String> retrieveData(string tableName, SQLiteConnection connection)
  148.         {
  149.  
  150.             string q = string.Format("SELECT * FROM {0}", tableName);
  151.             SQLiteCommand cmd = new SQLiteCommand(q, connection);
  152.  
  153.             List<String> data = new List<String>();
  154.  
  155.             try {
  156.                 Console.WriteLine("\treading table [" + tableName + "]");
  157.  
  158.                 if (null != connection && ConnectionState.Open != connection.State)
  159.                 {
  160.                     connection.Open();
  161.                 }
  162.                 //string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
  163.                 //q = "select * from Persons";
  164.                 //SQLiteCommand cmd = new SQLiteCommand(query, connection);
  165.  
  166.                 SQLiteDataReader reader = cmd.ExecuteReader();
  167.  
  168.                
  169.                 int entryNum = 0;
  170.  
  171.                 if (reader.HasRows)
  172.                 {
  173.                    
  174.                     while (reader.Read())
  175.                     {
  176.                        
  177.                         String entry = "(";
  178.  
  179.                         for (int i = 0; i < reader.FieldCount; i++)
  180.                         {
  181.                             //Console.Write("\t{0}", Convert.ToString(reader[i]));
  182.                             try
  183.                             {
  184.                                 string cellType = reader.GetDataTypeName(i).ToString();
  185.                                 switch (cellType)
  186.                                 {
  187.                                     case "INTEGER":
  188.                                         break;
  189.                                     case "DATETIME":
  190.                                         entry += "'";
  191.                                         break;
  192.                                     default:
  193.                                         entry += "'"; //\"
  194.                                         break;
  195.                                 }
  196.  
  197.                                 string cellValue = reader.GetValue(i).ToString();
  198.                                 cellValue = cellValue.Replace("'", "''");
  199.                                 if (cellValue.Equals(""))
  200.                                 {
  201.                                     entry = entry.Remove(entry.Length - 1);
  202.                                     entry += "null";
  203.                                     goto fuck;
  204.                                 }
  205.  
  206.                                 entry += cellValue;
  207.  
  208.                                 switch (cellType)
  209.                                 {
  210.                                     case "INTEGER":
  211.                                         break;
  212.                                     case "DATETIME":
  213.                                         entry += "'";
  214.                                         break;
  215.                                     default:
  216.                                         entry += "'"; //\"
  217.                                         break;
  218.                                 }
  219.  
  220.                                
  221.                                 fuck:
  222.                                 entry += ", ";
  223.  
  224.                                
  225.                             }
  226.                             catch (Exception e)
  227.                             {
  228.                                 Console.WriteLine(e.Message);
  229.                             }
  230.                    
  231.                         }
  232.  
  233.                         entry = entry.Remove(entry.Length - 2);
  234.                         entry += ")";
  235.  
  236.                         data.Add(entry);
  237.  
  238.                         entryNum++;
  239.  
  240.  
  241.                         if (1 == entryNum)
  242.                         {
  243.                             Console.WriteLine("\tEntry example: " + entry);
  244.                         }
  245.                     }
  246.                 }
  247.  
  248.                 reader.Close();
  249.  
  250.                 Console.WriteLine("\t" + entryNum + " entries for table [" + tableName + "] has been processed");
  251.  
  252.             }
  253.             catch (SQLiteException e)
  254.             {
  255.                 Console.WriteLine(e.Message);
  256.                 Console.ReadLine();
  257.             }
  258.             finally
  259.             {
  260.                 connection.Close();
  261.             }
  262.  
  263.             return data;
  264.         }
  265.  
  266.  
  267.  
  268.  
  269.  
  270.         static void insertData(String tableName, List<String> data, MySqlConnection connection)
  271.         {
  272.             try
  273.             {
  274.                 Console.WriteLine("\treading table [" + tableName + "]");
  275.  
  276.                 var dataAsString = String.Join(", ", data);
  277.                 Console.WriteLine("\tquery length: " + dataAsString.Length);
  278.  
  279.  
  280.                 if (null != connection && ConnectionState.Open != connection.State)
  281.                 {
  282.                     connection.Open();
  283.                 }
  284.  
  285.                 String q = String.Format("insert into {0} values {1}", tableName, dataAsString);
  286.  
  287.                 MySqlCommand cmd = new MySqlCommand(q, connection);
  288.  
  289.                 cmd.ExecuteNonQuery();
  290.  
  291.             }
  292.             catch (MySqlException e)
  293.             {
  294.                 Console.WriteLine(e.Message);
  295.                 Console.ReadLine();
  296.             }
  297.             finally
  298.             {
  299.                 connection.Close();
  300.             }
  301.         }
  302.  
  303.  
  304.  
  305.  
  306.         static void Main(string[] args)
  307.         {
  308.             int tableNumSqlite = 0, tableNumMysql = 0;
  309.  
  310.             Console.WriteLine("============================================================================");
  311.             Console.WriteLine("---------------------------------SQLITE-------------------------------------");
  312.             Console.WriteLine("============================================================================");
  313.  
  314.             string connStringSqlite = @"Data Source=C:\User
  315.             using (SQLiteConnection conn1 = new SQLiteConnection(connStringSqlite))
  316.             {
  317.                 try
  318.                 {
  319.                     conn1.Open();
  320.                     Console.WriteLine("connection to SQLite has been set\n");
  321.                    
  322.                     Console.Write("retrieving tablenames: ");
  323.  
  324.                     Globals.listTableNames = retrieveTablenames(conn1);
  325.                        
  326.                     Console.WriteLine("successfully retrieved\n");
  327.                        
  328.  
  329.                     Stopwatch stopWatch = new Stopwatch();
  330.                     stopWatch.Start();
  331.                     foreach (string table in Globals.listTableNames)
  332.                     {
  333.                         Console.WriteLine("retrieving data for table " + ++tableNumSqlite);
  334.                            
  335.                         List<String> data = retrieveData(table, conn1);
  336.                         Globals.mapNameToDataList.Add(table, data);
  337.                        
  338.                         Console.WriteLine("\tdata has been added to map");
  339.                     }
  340.                     stopWatch.Stop();
  341.  
  342.  
  343.  
  344.                     Console.WriteLine("\nelapsed time for " + tableNumSqlite + " tables: " + stopWatch.Elapsed.ToString() + "\n");
  345.  
  346.                 }
  347.                 catch (Exception e)
  348.                 {
  349.                     Console.WriteLine(e.Message);
  350.                 }
  351.             }
  352.  
  353.  
  354.             Console.WriteLine("============================================================================");
  355.             Console.WriteLine("----------------------------------MYSQL-------------------------------------");
  356.             Console.WriteLine("============================================================================\n");
  357.  
  358.            
  359.             string connStringMysql = "Server=localho
  360.             using (MySqlConnection conn2 = new MySqlConnection(connStringMysql))
  361.             {
  362.                 try
  363.                 {
  364.                     conn2.Open();
  365.                     Console.WriteLine("connection to MySQL has been set\n");
  366.  
  367.  
  368.                     Stopwatch stopWatch = new Stopwatch();
  369.                     stopWatch.Start();
  370.                     foreach (string table in Globals.listTableNames)
  371.                     {
  372.                         //if (!table.Equals("Sessions"))
  373.                         //{
  374.                             Console.WriteLine("inserting data into table " + ++tableNumMysql + ": " + table);
  375.  
  376.                             if (table.Equals("RelTasksModules") || table.Equals("FailedTests"))
  377.                             {
  378.                                 Console.WriteLine("\t-skipping-");
  379.                                 continue;
  380.                             }
  381.  
  382.                             if (table.Equals("Comments"))
  383.                             {
  384.  
  385.                                 int divisionParts = 200;
  386.                                 int count = Globals.mapNameToDataList["Comments"].Count - (Globals.mapNameToDataList["Comments"].Count % 10);
  387.                                 int partSize = (int)(count / divisionParts);
  388.                                 int from = 0;
  389.  
  390.                                 //List<String> dataInRange = Globals.mapNameToDataList["Comments"].GetRange(7000, 10000);
  391.                                 //insertData("Comments", dataInRange, conn2);
  392.  
  393.                                 Console.WriteLine("\tall data count:" + count);
  394.  
  395.                                 int f = 0;
  396.                                 while (from < count)
  397.                                 {
  398.                                    
  399.  
  400.  
  401.                                     List<String> dataInRange = Globals.mapNameToDataList["Comments"].GetRange(from, partSize);
  402.  
  403.                                     if (dataInRange.Contains("'2013-04-04 20:10:17'"))
  404.                                     {
  405.                                         Console.WriteLine("found it");
  406.                                         from += partSize;
  407.                                         continue;
  408.                                     }
  409.  
  410.                                     Console.WriteLine("\t" + f++ + " data range:" + from + ".." + (from + partSize));
  411.                                     insertData("Comments", dataInRange, conn2);
  412.                                    
  413.  
  414.                                     from += partSize;
  415.                                 }
  416.                                 Console.WriteLine("\tdone");
  417.  
  418.                                 //Console.WriteLine("count:" + Globals.mapNameToDataList["Comments"].Count / 10);
  419.                                 //List<String> first50k = Globals.mapNameToDataList["Comments"].
  420.                                 //  GetRange(0, Globals.mapNameToDataList["Comments"].Count / 10);
  421.                                 //List<String> rest50k = Globals.mapNameToDataList["Comments"].
  422.                                     //GetRange(Globals.mapNameToDataList["Comments"].Count / 2,
  423.                                         //   Globals.mapNameToDataList["Comments"].Count - 1);
  424.  
  425.                                 //insertData("Comments", first50k, conn2);
  426.                                 //Console.WriteLine("\t30k");
  427.                                 //insertData("Comments", rest50k, conn2);
  428.                                 //Console.WriteLine("\trest");
  429.  
  430.                                
  431.  
  432.                                 continue;
  433.                             }
  434.  
  435.  
  436.  
  437.                             List<String> data = Globals.mapNameToDataList[table];
  438.                             insertData(table, data, conn2);
  439.  
  440.                             if (table.Equals("RelLanguagesModules"))
  441.                             {
  442.                                 //insert RelTasksModules first
  443.                                 List<String> specData = Globals.mapNameToDataList["RelTasksModules"];
  444.                                 insertData("RelTasksModules", specData, conn2);
  445.                                 Console.WriteLine("\tpre-added RelTasksModules");
  446.                                 continue;
  447.                             }
  448.  
  449.                            
  450.  
  451.                            
  452.                         //}
  453.                        
  454.  
  455.  
  456.                     }
  457.                     stopWatch.Stop();
  458.                    
  459.  
  460.  
  461.                     Console.WriteLine("\nelapsed time for " + tableNumMysql + " tables: " + stopWatch.Elapsed.ToString() + "\n");
  462.  
  463.  
  464.                 }
  465.                 catch (Exception e)
  466.                 {
  467.                     Console.WriteLine(e.Message);
  468.                 }
  469.  
  470.  
  471.                 //MySqlCommand cmd2 = new MySqlCommand("select table_name from information_schema.tables", conn2);
  472.                 //execReaderMysql(cmd2);
  473.                 //Console.WriteLine("cmd2 done");
  474.  
  475.                 MySqlCommand cmd3 = new MySqlCommand("select * from persons", conn2);
  476.                 execReaderMysql(cmd3);
  477.  
  478.                 Console.WriteLine("cmd3 done");
  479.  
  480.                 /*string q4 = "insert into persons(personid, firstname, lastname, login, password, avatar, recoversession, banned) values " + Globals.mapNameToData["Persons"];
  481.                 Console.WriteLine(q4);
  482.                 MySqlCommand cmd4 = new MySqlCommand(q4, conn2);
  483.                 cmd4.ExecuteNonQuery();
  484.                 Console.WriteLine("cmd4 done");*/
  485.  
  486.                 execReaderMysql(cmd3);
  487.  
  488.                 conn2.Dispose();
  489.  
  490.  
  491.                 Console.WriteLine("all done");
  492.                 Console.ReadLine();
  493.             }
  494.         }
  495.        
  496.     }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement