Skip to content

Commit 0a2c9dd

Browse files
committed
Add Support to Set Default Value for Table Column.
1 parent 545a04c commit 0a2c9dd

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/SQLite.cs

+31-2
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ public class ColumnInfo
900900

901901
public int notnull { get; set; }
902902

903-
// public string dflt_value { get; set; }
903+
public string dflt_value { get; set; }
904904

905905
// public int pk { get; set; }
906906

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

928929
foreach (var p in map.Columns) {
929930
var found = false;
931+
var defaulted = false;
930932
foreach (var c in existingCols) {
931933
found = (string.Compare (p.Name, c.Name, StringComparison.OrdinalIgnoreCase) == 0);
932-
if (found)
934+
if (found) {
935+
defaulted = c.dflt_value != null;
933936
break;
937+
}
934938
}
935939
if (!found) {
936940
toBeAdded.Add (p);
937941
}
942+
else if (!defaulted && p.HasDefaultValue) {
943+
toBeDefaulted.Add (p);
944+
}
938945
}
939946

940947
foreach (var p in toBeAdded) {
941948
var addCol = "alter table \"" + map.TableName + "\" add column " + Orm.SqlDecl (p, StoreDateTimeAsTicks, StoreTimeSpanAsTicks);
942949
Execute (addCol);
943950
}
951+
952+
foreach (var p in toBeDefaulted) {
953+
var updateCol = "update \"" + map.TableName + "\" set \"" + p.Name + "\" = \"" + p.DefaultValue?.ToString () + "\" where \"" + p.Name + "\" is null";
954+
Execute (updateCol);
955+
}
944956
}
945957

946958
/// <summary>
@@ -2431,10 +2443,18 @@ public class ColumnAttribute : Attribute
24312443
{
24322444
public string Name { get; set; }
24332445

2446+
public object DefaultValue { get; private set; }
2447+
24342448
public ColumnAttribute (string name)
24352449
{
24362450
Name = name;
24372451
}
2452+
2453+
public ColumnAttribute (string name, object defaultValue)
2454+
{
2455+
Name = name;
2456+
DefaultValue = defaultValue;
2457+
}
24382458
}
24392459

24402460
[AttributeUsage (AttributeTargets.Property)]
@@ -2703,6 +2723,9 @@ public class Column
27032723

27042724
public bool StoreAsText { get; private set; }
27052725

2726+
public bool HasDefaultValue => DefaultValue != null;
2727+
public object DefaultValue { get; private set; }
2728+
27062729
public Column (MemberInfo member, CreateFlags createFlags = CreateFlags.None)
27072730
{
27082731
_member = member;
@@ -2717,6 +2740,9 @@ public Column (MemberInfo member, CreateFlags createFlags = CreateFlags.None)
27172740
colAttr.ConstructorArguments[0].Value?.ToString () :
27182741
member.Name;
27192742
#endif
2743+
DefaultValue = colAttr != null && colAttr.ConstructorArguments.Count > 1 ?
2744+
colAttr.ConstructorArguments[1].Value : null;
2745+
27202746
//If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead
27212747
ColumnType = Nullable.GetUnderlyingType (memberType) ?? memberType;
27222748
Collation = Orm.Collation (member);
@@ -2877,6 +2903,9 @@ public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks,
28772903
if (!string.IsNullOrEmpty (p.Collation)) {
28782904
decl += "collate " + p.Collation + " ";
28792905
}
2906+
if (p.HasDefaultValue) {
2907+
decl += "default \"" + p.DefaultValue?.ToString() + "\"";
2908+
}
28802909

28812910
return decl;
28822911
}

0 commit comments

Comments
 (0)