コード例 #1
0
 public static IDbCommand CreateCommand(string query, BulkTransaction transaction)
 {
     try {
         return(transaction.CreateCommand(query));
     } catch (Exception e) {
         System.IO.File.AppendAllText("MySQL_error.log", DateTime.Now + " " + query + "\r\n");
         Server.ErrorLog(e);
         return(null);
     }
 }
コード例 #2
0
            internal static void fillDatabase(Stream stream)
            {
                //Backup
                using (FileStream backup = File.Create("backup.sql"))
                {
                    CopyDatabase(new StreamWriter(backup));
                }
                //Delete old
                List <string> tables = getTables();

                foreach (string name in tables)
                {
                    executeQuery(String.Format("DROP TABLE `{0}`", name));
                }
                //Make new
                string script = new StreamReader(stream).ReadToEnd();

                string[]      cmds = script.Split(';');
                StringBuilder sb   = new StringBuilder();

                using (BulkTransaction helper = BulkTransaction.Create())
                {
                    foreach (string cmd in cmds)
                    {
                        String newCmd = cmd.Trim(" \r\n\t".ToCharArray());
                        int    index  = newCmd.ToUpper().IndexOf("CREATE TABLE");
                        if (index > -1)
                        {
                            newCmd = newCmd.Remove(0, index);
                            //Do we have a primary key?
                            try
                            {
                                if (Server.useMySQL)
                                {
                                    int    priIndex  = newCmd.ToUpper().IndexOf(" PRIMARY KEY AUTOINCREMENT");
                                    int    priCount  = " PRIMARY KEY AUTOINCREMENT".Length;
                                    int    attIdx    = newCmd.Substring(0, newCmd.Substring(0, priIndex - 1).LastIndexOfAny("` \n".ToCharArray())).LastIndexOfAny("` \n".ToCharArray()) + 1;
                                    int    attIdxEnd = newCmd.IndexOfAny("` \n".ToCharArray(), attIdx) - 1;
                                    string attName   = newCmd.Substring(attIdx, attIdxEnd - attIdx + 1).Trim(' ', '\n', '`', '\r');
                                    //For speed, we just delete this, and add it to the attribute name, then delete the auto_increment clause.
                                    newCmd = newCmd.Remove(priIndex, priCount);
                                    newCmd = newCmd.Insert(newCmd.LastIndexOf(")"), ", PRIMARY KEY (`" + attName + "`)");
                                    newCmd = newCmd.Insert(newCmd.IndexOf(',', priIndex), " AUTO_INCREMENT");
                                }
                                else
                                {
                                    int    priIndex    = newCmd.ToUpper().IndexOf(",\r\nPRIMARY KEY");
                                    int    priIndexEnd = newCmd.ToUpper().IndexOf(')', priIndex);
                                    int    attIdx      = newCmd.IndexOf("(", priIndex) + 1;
                                    int    attIdxEnd   = priIndexEnd - 1;
                                    string attName     = newCmd.Substring(attIdx, attIdxEnd - attIdx + 1);
                                    newCmd = newCmd.Remove(priIndex, priIndexEnd - priIndex + 1);
                                    int start = newCmd.IndexOf(attName) + attName.Length;
                                    int end   = newCmd.IndexOf(',');
                                    newCmd = newCmd.Remove(start, end - start);
                                    newCmd = newCmd.Insert(newCmd.IndexOf(attName) + attName.Length, " INTEGER PRIMARY KEY AUTOINCREMENT");
                                    newCmd = newCmd.Replace(" auto_increment", "").Replace(" AUTO_INCREMENT", "").Replace("True", "1").Replace("False", "0");
                                }
                            }
                            catch (ArgumentOutOfRangeException) { } // If we don't, just ignore it.
                        }
                        //Run the command in the transaction.
                        helper.Execute(newCmd.Replace(" unsigned", "").Replace(" UNSIGNED", "") + ";");
                        //                        sb.Append(newCmd).Append(";\n");
                    }
                    helper.Commit();
                }
                //Not sure if order matters.
                //AUTO_INCREMENT is changed to AUTOINCREMENT for MySQL -> SQLite
                //AUTOINCREMENT is changed to AUTO_INCREMENT for SQLite -> MySQL
                // All we should have in the script file is CREATE TABLE and INSERT INTO commands.
                //executeQuery(sb.ToString().Replace(" unsigned", "").Replace(" UNSIGNED", ""));
            }