Beispiel #1
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);
        }
Beispiel #2
0
        private List <object> LoadValues(DBTableDesc table, int owner)
        {
            List <object> valueList = new List <object>();


            RowsRet ret;


            if (table.Primary)
            {
                ret = sql.ExecuteCommand("SELECT * FROM " + table.Name + ";");
            }
            else
            {
                ret = sql.ExecuteCommand("SELECT * FROM " + table.Name + " WHERE OwnerID = ?;", new object[] { owner });
            }

            foreach (Row row in ret.Rows)
            {
                ConstructorInfo info = table.Type.GetConstructor(new Type[] { });
                Object          item = info.Invoke(new object[] {});

                int index = row.IntValue("ID");


                foreach (DBFieldDesc field in table.ValueFields)
                {
                    string stringVal = row[field.Name];

                    object obVal = ParseObjectForType(stringVal, field.Info.PropertyType);


                    field.Info.GetSetMethod().Invoke(item, new object[] { obVal });
                }

                foreach (DBFieldDesc field in table.SubtableFields)
                {
                    if (IsEnumerationTableType(field.Info.PropertyType))
                    {
                        List <object> list;

                        if (IsSimpleValueEnumeration(field.Info.PropertyType))
                        {
                            list = LoadSimpleValues(field.Subtable, index);
                        }
                        else
                        {
                            list = LoadValues(field.Subtable, index);
                        }


                        ConstructorInfo cons    = field.Info.PropertyType.GetConstructor(new Type[] { });
                        IList           newList = (IList)cons.Invoke(new object[0]);
                        foreach (object listitem in list)
                        {
                            newList.Add(listitem);
                        }

                        field.Info.GetSetMethod().Invoke(item, new object[] { newList });
                    }
                    else
                    {
                        List <object> list = LoadValues(field.Subtable, index);

                        if (list.Count > 0)
                        {
                            field.Info.GetSetMethod().Invoke(item, new object[] { list[0] });
                        }
                    }
                }

                if (table.Primary)
                {
                    IDBLoadable loadable = (IDBLoadable)item;
                    loadable.DBLoaderID = index;
                }

                valueList.Add(item);
            }

            return(valueList);
        }
Beispiel #3
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
        }
Beispiel #4
0
        public static Dictionary <string, string> LoadDetails(string ID, string table, List <string> fields)
        {
            var dict = new Dictionary <string, string>();

            string selectFields = "";
            bool   first        = true;

            foreach (string s in fields)
            {
                if (!first)
                {
                    selectFields += ", ";
                }
                first         = false;
                selectFields += s;
            }


            string commandText = "Select " + selectFields + " from " + table + " where ID=?";

            try
            {
                OpenDB();
#if MONO
                var cm = detailsDB.CreateCommand();
                cm.CommandText = commandText;
                var p = cm.CreateParameter();
                p.Value = ID;
                cm.Parameters.Add(p);

                var r = cm.ExecuteReader();

                r.Read();
                foreach (string s in fields)
                {
                    object obj = r.GetValue(r.GetOrdinal(s));
                    if (obj != null)
                    {
                        dict[s] = obj.ToString();
                    }
                    else
                    {
                        dict[s] = null;
                    }
                }
                r.Close();
#else
                RowsRet ret = null;

                ret = sqlDetailsDB.ExecuteCommand(commandText, new object[] { ID });


                if (ret == null || ret.Count() == 0)
                {
                    return(dict);
                }

                foreach (string s in fields)
                {
                    dict[s] = ret.Rows[0][s];
                }
#endif
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }


            return(dict);
        }