Example #1
0
 public void Dispose()
 {
     if (sql != null)
     {
         sql.Dispose();
         sql = null;
     }
 }
Example #2
0
        public static void  OpenDB()
        {
 #if MONO
            if (detailsDB == null)
            {
#if ANDROID
                detailsDB = new SqliteConnection("DbLinqProvider=Sqlite;Data Source=" + DBFullFilename);
#else
                detailsDB = new SqliteConnection("DbLinqProvider=Sqlite;Data Source=Details.db");
#endif
                detailsDB.Open();
            }
#else
            if (sqlDetailsDB == null)
            {
                sqlDetailsDB = new SQL_Lite();
                sqlDetailsDB.SkipHeaderRow = true;
                sqlDetailsDB.Open(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "details.db"));
            }
#endif
        }
Example #3
0
        private bool TableMatchesDescription(SQL_Lite sql, DBTableDesc table)
#endif
        {
            RowsRet ret = sql.ExecuteCommand("PRAGMA table_info(" + table.Name + ");");

            Dictionary <string, DBFieldDesc> fieldInfo = new Dictionary <string, DBFieldDesc>();

            foreach (var field in table.Fields)
            {
                fieldInfo.Add(field.Name, field);
            }



            bool tableMatches = true;

            foreach (Row row in ret.Rows)
            {
                string name = row["name"];
                if (fieldInfo.ContainsKey(row["name"]))
                {
                    fieldInfo.Remove(row["name"]);
                }
                else
                {
                    if (name != "ID" && name != "OwnerID")
                    {
                        tableMatches = false;
                        break;
                    }
                }
            }

            if (fieldInfo.Count > 0)
            {
                tableMatches = false;
            }

            return(tableMatches);
        }
Example #4
0
 private static void CopyTable(SQL_Lite sql, SQL_Lite sql2, DBTableDesc table)
Example #5
0
        public DBLoader(String filename)
        {
            Type type = typeof(T);

#if ANDROID
            string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#else
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
#endif
            path = Path.Combine(path, "Combat Manager");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }


            try
            {
                string fullfilename = Path.Combine(path, filename);

#if !MONO
                sql = new SQL_Lite();
                sql.SkipHeaderRow = true;
                sql.Open(fullfilename);
                string backtext = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                string backname = fullfilename + backtext + ".db";
#else
#if ANDROID
                Android.Util.Log.Error("DBLoader", "fullfilename " + fullfilename);
#endif
                sql = new SqliteConnection("DbLinqProvider=Sqlite;Data Source=" + fullfilename);

                sql.Open();
#endif



#if !MONO
                //make a backup
                if (File.Exists(fullfilename) && !(File.Exists(backname)))
                {
                    try
                    {
                        File.Copy(fullfilename, backname);
                    }
                    catch (IOException ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
#endif



                bool needsCopy = false;

#if !MONO
                try
                {
                    sql.ExecuteCommand("SELECT name FROM sqlite_master");
                }
                catch (SQL_Lite_Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                    sql.Dispose();
                    sql = null;

                    string errorname = fullfilename + ".error.db";

                    if (File.Exists(errorname))
                    {
                        File.Delete(errorname);
                    }
                    File.Move(fullfilename, errorname);

                    sql = new SQL_Lite();
                    sql.SkipHeaderRow = true;

                    sql.Open(fullfilename);
                }
#endif

                List <DBTableDesc> tables = GetTablesForType(type);

                foreach (DBTableDesc desc in tables)
                {
                    RowsRet ret = sql.ExecuteCommand("SELECT name FROM sqlite_master WHERE type='table' AND name=?",
                                                     new object[] { desc.Name });

                    if (ret.Rows.Count == 0)
                    {
                        String str = CreateTableStatementForDesc(desc);
                        sql.ExecuteCommand(str);
                    }
                    else
                    {
                        if (!TableMatchesDescription(sql, desc))
                        {
                            needsCopy = true;
                            break;
                        }
                    }
                }

                if (needsCopy)
                {
#if ANDROID
                    Android.Util.Log.Error("DBLoader", "DBL needs copy");
#endif
                    string newfile = fullfilename + ".tmp";
                    if (File.Exists(newfile))
                    {
#if ANDROID
                        Android.Util.Log.Error("DBLoader", "DBL new file exists");
#endif
                        File.Delete(newfile);

#if ANDROID
                        Android.Util.Log.Error("DBLoader", "DBL new file delete");
#endif
                    }

#if !MONO
                    SQL_Lite sql2 = new SQL_Lite();
                    sql2.SkipHeaderRow = true;

                    sql2.Open(newfile);
#else
                    LogError("DBLoader", "NewFile " + newfile);


                    SqliteConnection sql2 = new SqliteConnection("DbLinqProvider=Sqlite;Data Source=" + newfile);
                    sql2.Open();
#endif

                    foreach (DBTableDesc table in tables)
                    {
                        CopyTable(sql, sql2, table);
                    }

                    sql.Dispose();
                    sql2.Dispose();

                    File.Delete(fullfilename);
                    File.Move(newfile, fullfilename);

#if !MONO
                    sql = new SQL_Lite();

                    sql.SkipHeaderRow = true;
                    sql.Open(fullfilename);
#else
                    sql = new SqliteConnection("DbLinqProvider=Sqlite;Data Source=" + fullfilename);
                    sql.Open();
#endif
                }


                LoadTableNextIndexes(tables);
                LoadDBItems();
            }
#if !MONO
            catch (SQL_Lite_Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
#else
            finally
            {
            }
#endif
        }