public SQLiteColumnHeader(string name, SQLiteDataType databaseType) { if (name == null) throw new ArgumentNullException("name"); _name = name; _databaseType = databaseType; }
public ColumnObject(string name, string type, bool notNull, object defaultContent, bool primaryKey) { this.name = name; this.type = type; this.notNull = notNull; this.content = SQLiteDataTypeLoader.GetType (type, defaultContent); if (hasDefault = defaultContent != null) { this.defaultContent = SQLiteDataTypeLoader.GetType (type, defaultContent); } else { } this.primaryKey = primaryKey; }
/// <summary> /// Converts SQLite data type enum to string /// </summary> /// <param name="type"></param> /// <returns></returns> static string ConvertToStringType(SQLiteDataType type) { // Try to find if (FieldTypeMap.ContainsKey(type)) { return(FieldTypeMap[type]); } // if not found return TEXT type else { return(FieldTypeMap[SQLiteDataType.TEXT]); } }
private object Parse(SqliteDataReader reader, SQLiteDataType dataType, int index) { switch (dataType) { case SQLiteDataType.BLOB: break; case SQLiteDataType.INTEGER: return(reader.GetInt32(index)); case SQLiteDataType.REAL: return(reader.GetFloat(index)); case SQLiteDataType.TEXT: return(reader.GetString(index)); } return(null); }
/// <summary> /// Creates the column with the specified name and data type (e.g. id, INT) /// and options (e.g. NOT NULL). /// </summary> public Column(string name, SQLiteDataType dataType, string options) : this(name, dataType.ToString(), options) { }
/// <summary> /// Creates the column with the specified name and data type (e.g. id, INT). /// Options default to NOT NULL. /// </summary> public Column(string name, SQLiteDataType dataType) : this(name, dataType.ToString(), "NOT NULL") { }
/// <summary> /// This show the controls for adding or changing a column /// </summary> /// <param name="editable">If set to <c>true</c> editable.</param> public void EditorFieldSetup(string[] types, bool editable=true) { string oldType = type; GUI.enabled = editable; name = EditorGUILayout.TextField (name); type = types [typeIndex = EditorGUILayout.Popup (typeIndex, types)]; primaryKey = EditorGUILayout.Toggle ("Primary Key", primaryKey); autoIncrement = EditorGUILayout.Toggle ("AutoIncrement", autoIncrement); notNull = EditorGUILayout.Toggle ("Not null", notNull); if (defaultContent == null || oldType != type) { defaultContent = SQLiteDataTypeLoader.GetType (type, null); } if (hasDefault = EditorGUILayout.Toggle ("Default", hasDefault)) { defaultContent.EditorField (false, ""); } GUI.enabled = true; }
public SQLiteTableField(string name, SQLiteDataType type) : this(name, type, false) { }
public SQLiteTableField(string name, SQLiteDataType type, bool primaryKeyAutoIncrement) : this(name, ConvertToStringType(type), primaryKeyAutoIncrement) { }
/// <summary> /// By passing an Entity type, this method will use the Attribute's /// attached to each of the entities properties to generate an /// SQL command, that will create a table on the database. /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="flags">Additional flags for SQL generation</param> public static void CreateTable <TEntity>(this SQLiteContext context, TableCreationOptions flags = TableCreationOptions.None) where TEntity : class { // Get our table mapping Type entityType = typeof(TEntity); TableMapping table = EntityCache.GetTableMap(entityType); // Column defined foreign keys List <AttributeInfo> withFKs = new List <AttributeInfo>(); // ----------------------------------------- // Begin the SQL generation // ----------------------------------------- StringBuilder sql = new StringBuilder("CREATE "); sql.AppendIf(flags.HasFlag(TableCreationOptions.Temporary), "TEMP "); sql.Append("TABLE "); sql.AppendIf(flags.HasFlag(TableCreationOptions.IfNotExists), "IF NOT EXISTS "); sql.AppendLine($"{context.QuoteIdentifier(table.TableName)} ("); // ----------------------------------------- // Append attributes // ----------------------------------------- foreach (var colData in table.Columns) { // Get attribute data AttributeInfo info = colData.Value; Type propertyType = info.Property.PropertyType; SQLiteDataType pSqlType = GetSQLiteType(propertyType); // Start appending column definition SQL sql.Append($"\t{context.QuoteIdentifier(colData.Key)} {pSqlType}"); // Primary Key and Unique column definition if (info.AutoIncrement || (table.HasRowIdAlias && info.PrimaryKey)) { sql.AppendIf(table.HasRowIdAlias && info.PrimaryKey, $" PRIMARY KEY"); sql.AppendIf(info.AutoIncrement && pSqlType == SQLiteDataType.INTEGER, " AUTOINCREMENT"); } else if (info.Unique) { // Unique column definition sql.Append(" UNIQUE"); } // Collation sql.AppendIf( info.Collation != Collation.Default && pSqlType == SQLiteDataType.TEXT, " COLLATE " + info.Collation.ToString().ToUpperInvariant() ); // Nullable definition bool canBeNull = !propertyType.IsValueType || (Nullable.GetUnderlyingType(propertyType) != null); if (info.HasRequiredAttribute || (!info.PrimaryKey && !canBeNull)) { sql.Append(" NOT NULL"); } // Default value if (info.DefaultValue != null) { sql.Append($" DEFAULT "); // Do we need to quote this? SQLiteDataType type = info.DefaultValue.SQLiteDataType; if (type == SQLiteDataType.INTEGER && info.DefaultValue.Value is Boolean) { // Convert bools to integers int val = ((bool)info.DefaultValue.Value) ? 1 : 0; sql.Append($"{val}"); } else if (info.DefaultValue.Quote) { sql.Append($"\"{info.DefaultValue.Value}\""); } else { sql.Append($"{info.DefaultValue.Value}"); } } // Add last comma sql.AppendLine(","); // For later use if (info.ForeignKey != null) { withFKs.Add(info); } } // ----------------------------------------- // Composite Keys // ----------------------------------------- string[] keys = table.PrimaryKeys.ToArray(); if (!table.HasRowIdAlias && keys.Length > 0) { sql.Append($"\tPRIMARY KEY("); sql.Append(String.Join(", ", keys.Select(x => context.QuoteIdentifier(x)))); sql.AppendLine("),"); } // ----------------------------------------- // Composite Unique Constraints // ----------------------------------------- foreach (var cu in table.UniqueConstraints) { sql.Append($"\tUNIQUE("); sql.Append(String.Join(", ", cu.Attributes.Select(x => context.QuoteIdentifier(x)))); sql.AppendLine("),"); } // ----------------------------------------- // Foreign Keys // ----------------------------------------- foreach (ForeignKeyConstraint info in table.ForeignKeys) { // Primary table attributes ForeignKeyAttribute fk = info.ForeignKey; string attrs1 = String.Join(", ", fk.Attributes.Select(x => context.QuoteIdentifier(x))); string attrs2 = String.Join(", ", info.InverseKey.Attributes.Select(x => context.QuoteIdentifier(x))); // Build sql command TableMapping map = EntityCache.GetTableMap(info.ParentEntityType); sql.Append('\t'); sql.Append($"FOREIGN KEY({context.QuoteIdentifier(attrs1)}) "); sql.Append($"REFERENCES {context.QuoteIdentifier(map.TableName)}({attrs2})"); // Add integrety options sql.AppendIf(fk.OnUpdate != ReferentialIntegrity.NoAction, $" ON UPDATE {ToSQLite(fk.OnUpdate)}"); sql.AppendIf(fk.OnDelete != ReferentialIntegrity.NoAction, $" ON DELETE {ToSQLite(fk.OnDelete)}"); // Finish the line sql.AppendLine(","); } // ----------------------------------------- // SQL wrap up // ----------------------------------------- string sqlLine = String.Concat( sql.ToString().TrimEnd(new char[] { '\r', '\n', ',' }), Environment.NewLine, ")" ); // Without row id? if (table.WithoutRowID) { sqlLine += " WITHOUT ROWID;"; } // ----------------------------------------- // Execute the command on the database // ----------------------------------------- using (SQLiteCommand command = context.CreateCommand(sqlLine)) { command.ExecuteNonQuery(); } }
public SQLiteField(string name, SQLiteDataType type, params string[] suffix) { Name = name; Type = type; Suffix = MergeSuffix(suffix); }
public SQLiteData(string name, SQLiteDataType dataType) { this.name = name; this.dataType = dataType; }