bool CreateTable() { // database created successfuly then create table if (db.Exists) { // Create table :: schema // you can create table when you want DBSchema schema = new DBSchema("Users"); schema.AddField("Id", SQLiteDB.DB_DataType.DB_INT, 0, false, true, true); schema.AddField("Name", SQLiteDB.DB_DataType.DB_VARCHAR, 100, false, false, false); return(db.CreateTable(schema)); } return(false); }
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? }