From 545a04c59d98c0ade5ca30e71620bd8ff7102d0f Mon Sep 17 00:00:00 2001 From: microspaze Date: Tue, 16 Jan 2024 17:04:46 +0800 Subject: [PATCH 1/3] Add Contains method with StringComparison parameter. --- src/SQLite.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 5b6941c3..3a189dfe 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -4064,6 +4064,9 @@ private SQLiteCommand GenerateCommand (string selectionList) } cmdText += " offset " + _offset.Value; } + + //Remove all StringComparison arguments from args, otherwise the created command may unmatch with the real arguments. + args.RemoveAll (x => x is StringComparison); return Connection.CreateCommand (cmdText, args.ToArray ()); } } @@ -4133,7 +4136,24 @@ private CompileResult CompileExpr (Expression expr, List queryArgs) sqlCall = "(" + args[0].CommandText + " like " + args[1].CommandText + ")"; } else if (call.Method.Name == "Contains" && args.Length == 2) { - sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")"; + //Add Contains method with StringComparison parameter. + if (call.Object != null && call.Object.Type == typeof (string) && args[1].Value is StringComparison comparison) { + switch (comparison) { + case StringComparison.Ordinal: + case StringComparison.CurrentCulture: + case StringComparison.InvariantCulture: + sqlCall = "(instr(" + obj.CommandText + "," + args[0].CommandText + ") > 0)"; + break; + case StringComparison.OrdinalIgnoreCase: + case StringComparison.CurrentCultureIgnoreCase: + case StringComparison.InvariantCultureIgnoreCase: + sqlCall = "(" + obj.CommandText + " like ('%' ||" + args[0].CommandText + "|| '%'))"; + break; + } + } + else { + sqlCall = "(" + args[1].CommandText + " in " + args[0].CommandText + ")"; + } } else if (call.Method.Name == "Contains" && args.Length == 1) { if (call.Object != null && call.Object.Type == typeof (string)) { From 0a2c9dd9fdef5d4546ebe3bc64e56c98ea5a4970 Mon Sep 17 00:00:00 2001 From: microspaze Date: Mon, 18 Mar 2024 13:55:48 +0800 Subject: [PATCH 2/3] Add Support to Set Default Value for Table Column. --- src/SQLite.cs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 3a189dfe..ffae8cbe 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -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; } @@ -924,23 +924,35 @@ public List GetTableInfo (string tableName) void MigrateTable (TableMapping map, List existingCols) { var toBeAdded = new List (); + var toBeDefaulted = new List (); 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); + } } /// @@ -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)] @@ -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; @@ -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 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); @@ -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; } From 09aa2719c47e2e3200d23c72798c4d59e8e70041 Mon Sep 17 00:00:00 2001 From: microspaze Date: Thu, 28 Mar 2024 10:50:58 +0800 Subject: [PATCH 3/3] Revert "Add Support to Set Default Value for Table Column." This reverts commit 0a2c9dd9fdef5d4546ebe3bc64e56c98ea5a4970. --- src/SQLite.cs | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index ffae8cbe..3a189dfe 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -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; } @@ -924,35 +924,23 @@ public List GetTableInfo (string tableName) void MigrateTable (TableMapping map, List existingCols) { var toBeAdded = new List (); - var toBeDefaulted = new List (); 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) { - defaulted = c.dflt_value != null; + if (found) 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); - } } /// @@ -2443,18 +2431,10 @@ 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)] @@ -2723,9 +2703,6 @@ 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; @@ -2740,9 +2717,6 @@ 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 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); @@ -2903,9 +2877,6 @@ 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; }