/// <summary> /// Create a column schema from TypeInfoItem /// </summary> public static IColumn FromInfo(TypeInfo info, TypeItemInfo pi) { SqliteType dataType = mapType(pi); //Props bool isKey = pi.Is(DatabaseWrapper.ColumnAttributes.PrimaryKey); // Auto select bool allowNulls = dataType == SqliteType.TEXT || dataType == SqliteType.BLOB; // was specified ? if (pi.Is(DatabaseWrapper.ColumnAttributes.AllowNull)) { allowNulls = true; } if (pi.Is(DatabaseWrapper.ColumnAttributes.NotNull)) { allowNulls = false; } bool isUnique = pi.Is(DatabaseWrapper.ColumnAttributes.Unique); object defVal = null; foreach (var attr in pi.DBAttributes) { if (attr.Attribute is DefaultValueAttribute def) { defVal = def; break; } } // create return(new Column() { ColumnName = pi.Name, AllowNulls = allowNulls, NativeType = pi.Type, SqliteType = dataType, DefaultValue = defVal, IsPK = isKey, IsAI = isKey && dataType == SqliteType.INTEGER, IsUnique = isUnique, }); }
internal static void adjustInsertValue(ref object value, TypeItemInfo p, object parameters) { if (value is Uri uri) { value = uri.ToString(); } if (!p.Is(DatabaseWrapper.ColumnAttributes.PrimaryKey)) { return; } //if (p.Type == typeof(int) || p.Type == typeof(long)) //{ // if (!value.Equals(0)) return; // // PK ints are AI // value = null; //} // Check for INT or LONGs equals to zero if (p.Type == typeof(int)) { if ((int)value != 0) { return; } value = null; } else if (p.Type == typeof(long)) { if ((long)value != 0) { return; } value = null; } else if (p.Type == typeof(Guid)) { if ((Guid)value != Guid.Empty) { return; } value = Guid.NewGuid(); // write new guid on object p.SetValue(parameters, value); } }