Skip to content

Commit

Permalink
Add Support to Set Default Value for Table Column.
Browse files Browse the repository at this point in the history
  • Loading branch information
microspaze committed Mar 18, 2024
1 parent 545a04c commit 0a2c9dd
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ public class ColumnInfo

public int notnull { get; set; }

// public string dflt_value { get; set; }
public string dflt_value { get; set; }

// public int pk { get; set; }

Expand All @@ -924,23 +924,35 @@ public List<ColumnInfo> GetTableInfo (string tableName)
void MigrateTable (TableMapping map, List<ColumnInfo> existingCols)
{
var toBeAdded = new List<TableMapping.Column> ();
var toBeDefaulted = new List<TableMapping.Column> ();

foreach (var p in map.Columns) {
var found = false;
var defaulted = false;
foreach (var c in existingCols) {
found = (string.Compare (p.Name, c.Name, StringComparison.OrdinalIgnoreCase) == 0);
if (found)
if (found) {
defaulted = c.dflt_value != null;
break;
}
}
if (!found) {
toBeAdded.Add (p);
}
else if (!defaulted && p.HasDefaultValue) {
toBeDefaulted.Add (p);
}
}

foreach (var p in toBeAdded) {
var addCol = "alter table \"" + map.TableName + "\" add column " + Orm.SqlDecl (p, StoreDateTimeAsTicks, StoreTimeSpanAsTicks);
Execute (addCol);
}

foreach (var p in toBeDefaulted) {
var updateCol = "update \"" + map.TableName + "\" set \"" + p.Name + "\" = \"" + p.DefaultValue?.ToString () + "\" where \"" + p.Name + "\" is null";
Execute (updateCol);
}
}

/// <summary>
Expand Down Expand Up @@ -2431,10 +2443,18 @@ public class ColumnAttribute : Attribute
{
public string Name { get; set; }

public object DefaultValue { get; private set; }

public ColumnAttribute (string name)
{
Name = name;
}

public ColumnAttribute (string name, object defaultValue)
{
Name = name;
DefaultValue = defaultValue;
}
}

[AttributeUsage (AttributeTargets.Property)]
Expand Down Expand Up @@ -2703,6 +2723,9 @@ public class Column

public bool StoreAsText { get; private set; }

public bool HasDefaultValue => DefaultValue != null;
public object DefaultValue { get; private set; }

public Column (MemberInfo member, CreateFlags createFlags = CreateFlags.None)
{
_member = member;
Expand All @@ -2717,6 +2740,9 @@ public Column (MemberInfo member, CreateFlags createFlags = CreateFlags.None)
colAttr.ConstructorArguments[0].Value?.ToString () :
member.Name;
#endif
DefaultValue = colAttr != null && colAttr.ConstructorArguments.Count > 1 ?
colAttr.ConstructorArguments[1].Value : null;

//If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead
ColumnType = Nullable.GetUnderlyingType (memberType) ?? memberType;
Collation = Orm.Collation (member);
Expand Down Expand Up @@ -2877,6 +2903,9 @@ public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks,
if (!string.IsNullOrEmpty (p.Collation)) {
decl += "collate " + p.Collation + " ";
}
if (p.HasDefaultValue) {
decl += "default \"" + p.DefaultValue?.ToString() + "\"";
}

return decl;
}
Expand Down

0 comments on commit 0a2c9dd

Please sign in to comment.