Beispiel #1
0
        /// <summary>
        /// Creates/loads a table in the relevant SQLite DB with the required structure for storing the Model
        /// </summary>
        /// <param name="DBConnection">A database connection connected to the SQLite DB this model is to be stored in</param>
        public ListDBHook(SQLiteConnection DBConnection)
        {
            Model             = typeof(T);
            DBLock            = new object();
            this.DBConnection = DBConnection;
            object[] attributes = Model.GetCustomAttributes(typeof(TableAttribute), true);
            if (attributes.Length == 0)
            {
                throw new Exception(Model.Name + " is not a valid table binding type!");
            }
            TableAttribute attribute = attributes.First() as TableAttribute;

            TableName  = attribute.tableName;
            Properties = new List <AttributeProperty>();
            foreach (PropertyInfo property in Model.GetProperties())
            {
                DatabaseValueAttribute valueAttribute = property.GetCustomAttribute <DatabaseValueAttribute>();
                if (valueAttribute == null)
                {
                    continue;                         //user messed up
                }
                Properties.Add(new AttributeProperty(property, valueAttribute));
            }
            IDField  = Model.BaseType.GetField("id", BindingFlags.NonPublic | BindingFlags.Instance);
            OpenData = new TrackingList <T>();
        }
Beispiel #2
0
        /// <summary>
        /// Creates a fresh empty table using the specified Model
        /// </summary>
        /// <typeparam name="T">Model type to be creating a table for</typeparam>
        /// <param name="db">Database path to create the Model table in</param>
        public static void CreateTable <T>(String db) where T : Model <T>
        {
            TableAttribute   attribute    = GetTableInfoFrom <T>();
            SQLiteConnection dbConnection = GetDatabase(db);
            String           SQLString    = "CREATE TABLE " + attribute.tableName + " (";
            List <String>    columns      = new List <String>();

            columns.Add(" id INT");
            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                DatabaseValueAttribute valueAttribute = property.GetCustomAttribute <DatabaseValueAttribute>();
                if (valueAttribute == null)
                {
                    continue;
                }
                columns.Add(valueAttribute.column + " VARCHAR(" + valueAttribute.maxLength + ")");
            }
            SQLString += String.Join(",", columns) + ")";
            new SQLiteCommand(SQLString, dbConnection).ExecuteNonQuery();
        }