Ejemplo n.º 1
0
        private string GetDataType(SQLiteDB.DB_DataType id)
        {
            string result = string.Empty;

            switch (id)
            {
            case SQLiteDB.DB_DataType.DB_INT:
                result = "INT";
                break;

            case SQLiteDB.DB_DataType.DB_STRING:
                result = "STRING";
                break;

            case SQLiteDB.DB_DataType.DB_TEXT:
                result = "TEXT";
                break;

            case SQLiteDB.DB_DataType.DB_VARCHAR:
                result = "VARCHAR";
                break;

            case SQLiteDB.DB_DataType.DB_FLOAT:
                result = "REAL";
                break;
            }
            return(result);
        }
Ejemplo n.º 2
0
 public void AddField(string fieldName, SQLiteDB.DB_DataType fieldType, int size, bool isNotNull, bool isPrimaryKey, bool isUnique)
 {
     SQLiteDB.DB_Field item = default(SQLiteDB.DB_Field);
     item.name         = fieldName;
     item.type         = fieldType;
     item.isNotNull    = isNotNull;
     item.isPrimaryKey = isPrimaryKey;
     item.isUnique     = isUnique;
     item.size         = size;
     this.fieldList.Add(item);
 }
Ejemplo n.º 3
0
    void RunMigrations()
    {
        // TODO if add/drop migrations or incremental

        LogHelper.Log(LoggingLevel.TRACE, _config, "Creating and Migrating");

        foreach (var connectionField in this.GetType().GetFields())
        {
            if (connectionField.FieldType.Name.Equals("SQLiteTable`1"))
            {
                // rename variable for explicitness, now that it represents something else
                FieldInfo tableField = connectionField;

                // get the generic type argument that the SQLiteTable is using, as that's the type of the table
                Type tableFieldGenericType = tableField.FieldType.GetGenericArguments()[0];
                LogHelper.Log(LoggingLevel.TRACE, _config, String.Concat("Considering object of type: " + tableFieldGenericType.Name));

                // get the name of the field to be the name of the table, unless it has a custom table name attribute, then use that
                string tableName          = tableField.Name;
                var    tableNameAttribute = tableFieldGenericType.GetCustomAttributes(typeof(TableNameAttribute), false).FirstOrDefault() as TableNameAttribute;
                if (tableNameAttribute != null)
                {
                    // cleanse by replacing all spaces with underscores
                    // TODO rip out all symbols and other invalid characters
                    tableName = tableNameAttribute.TableName.Replace(" ", "_");
                }
                LogHelper.Log(LoggingLevel.TRACE, _config, String.Concat("Mapping object to database table named: " + tableName));

                // Create the schema for the new table to be created
                DBSchema schema = new DBSchema(tableName);

                // Only supporting a single primary key at the moment
                bool schemaHasPrimary = false;

                foreach (var tableProperty in tableFieldGenericType.GetProperties())
                {
                    if (tableProperty.GetCustomAttributes(typeof(ColumnIgnoreAttribute), false).Length == 0)
                    {
                        string columnName         = tableProperty.Name;
                        var    fieldNameAttribute = tableProperty.GetCustomAttributes(typeof(FieldNameAttribute), false).FirstOrDefault() as FieldNameAttribute;
                        if (fieldNameAttribute != null)
                        {
                            // cleanse by replacing all spaces with underscores
                            // TODO rip out all symbols and other invalid characters
                            columnName = fieldNameAttribute.FieldName.Replace(" ", "_");
                        }

                        SQLiteDB.DB_DataType dataType = 0;
                        int size = 0;
                        // If it's an int type...
                        if (tableProperty.PropertyType.Equals(typeof(Int16)) ||
                            tableProperty.PropertyType.Equals(typeof(Int32)) ||
                            tableProperty.PropertyType.Equals(typeof(Int64)))
                        {
                            dataType = SQLiteDB.DB_DataType.DB_INT;
                        }
                        else
                        {
                            // if not an int, then store as text unless a size was specified, then store as varchar
                            var sizeAttribute = tableProperty.GetCustomAttributes(typeof(SizeAttribute), false).FirstOrDefault() as SizeAttribute;
                            if (sizeAttribute != null)
                            {
                                dataType = SQLiteDB.DB_DataType.DB_VARCHAR;
                                size     = sizeAttribute.FieldSize;
                            }
                            else
                            {
                                dataType = SQLiteDB.DB_DataType.DB_TEXT;
                            }
                        }

                        bool isPrimary = false;
                        if (tableProperty.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).Length != 0)
                        {
                            if (schemaHasPrimary)
                            {
                                // TODO custom exception
                                throw new Exception("Multiple primary keys are not supported at this time");
                            }

                            isPrimary        = true;
                            schemaHasPrimary = true;
                        }

                        bool isNotNull = false;
                        if (tableProperty.GetCustomAttributes(typeof(NotNullAttribute), false).Length != 0)
                        {
                            isNotNull = true;
                        }

                        bool isUnique = false;
                        if (tableProperty.GetCustomAttributes(typeof(UniqueAttribute), false).Length != 0 || isPrimary)
                        {
                            isUnique = true;
                        }

                        schema.AddField(columnName, dataType, size, isNotNull, isPrimary, isUnique);
                    }
                }

                if (_database.IsTableExists(schema.TableName))
                {
                    LogHelper.Log(LoggingLevel.TRACE, _config, "The " + schema.TableName + " table already exists.");
                    // TODO.. migration update?
                }
                else
                {
                    LogHelper.Log(LoggingLevel.TRACE, _config, "Creating the " + schema.TableName + " table.");
                    _database.CreateTable(schema);
                }

                // Set the value of the table back in this calss
                Type typeArgument     = tableFieldGenericType;
                Type genericClass     = typeof(SQLiteTable <>);
                Type constructedClass = genericClass.MakeGenericType(typeArgument);

                tableField.SetValue(this, Activator.CreateInstance(constructedClass, _database, tableName, _config));
            }
        }

        // Depending on type of migration, seed?
    }