Skip to content

Commit 840b4a8

Browse files
authored
0.9.4
1 parent 2de4e3b commit 840b4a8

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

Bases/Common.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ private static string ReplaceFirst(this string text, string search, string repla
2626
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
2727
}
2828

29-
public static string _PrepareClear(string q, List<string> args)
29+
public static string _PrepareClear(string q, List<string> args, Func<string,string> escape_func = null)
3030
{
3131
var new_q = q;
32-
if(args != null) foreach (var arg in args.ToList())
33-
{
34-
var new_q2 = ReplaceFirst(new_q, "{ARG}", _PrepareArg(arg));
35-
36-
if (new_q2 == new_q) throw new Exception("Mailformed query [Too many args in params]");
37-
new_q = new_q2;
38-
}
32+
if(args != null)
33+
foreach (string arg in args.ToList())
34+
{
35+
var new_q2 = "";
36+
if (escape_func == null)
37+
new_q2 = ReplaceFirst(new_q, "{ARG}", _PrepareArg(arg));
38+
else
39+
new_q2 = ReplaceFirst(new_q, "{ARG}", escape_func(arg));
40+
41+
if (new_q2 == new_q) throw new Exception("Mailformed query [Too many args in params]");
42+
new_q = new_q2;
43+
}
3944
if (new_q.Contains("{ARG}")) throw new Exception("Mailformed query [Not enough args in params]");
4045
return new_q;
4146
}
@@ -44,23 +49,33 @@ public static string _PrepareArg(string arg)
4449
{
4550
if (arg == null) return "";
4651

47-
var new_arg = arg;
52+
//var new_arg = arg;
4853

4954
//string[] escapes = ["'", "\"", "`", "%", "-", "_"];
50-
string[] escapes = ["'", "\"", "`", "%", "\\"];
55+
string[] escapes = ["\\","'", "\"", "`", "%"];
56+
//string[] escapes = ["\\","'", "`", "%"];
5157

52-
foreach (var escape in escapes)
58+
59+
60+
//foreach (var escape in escapes)
61+
//{
62+
var new_arg = "";
63+
foreach(var ch in arg)
5364
{
54-
new_arg = new_arg.Replace(escape, $"\\{escape}");
65+
if(escapes.Contains(ch.ToString()))
66+
new_arg += "\\";
67+
new_arg += ch.ToString();
5568
}
56-
69+
//new_arg = new_arg.Replace(escape, $"\\{escape}");
70+
5771
return new_arg;
5872
}
5973

6074

6175
public static List<List<string>> _Query(DbConnection conn, string q, bool non_query)
6276
{
6377
if (conn.State != ConnectionState.Open) conn.Open();
78+
6479
var sql = conn.CreateCommand();
6580
sql.CommandText = q;
6681
if (!non_query)

Bases/SQLite.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ private string _FixForSQLite(string q)
4646
return q.Replace("PRIMARY KEY AUTO_INCREMENT", "PRIMARY KEY AUTOINCREMENT").Replace("UNIX_TIMESTAMP()", "UNIXEPOCH()");
4747
}
4848

49+
public static string _PrepareArg(string arg)
50+
{
51+
if (arg == null) return "";
52+
53+
//var new_arg = arg;
54+
55+
//string[] escapes = ["'", "\"", "`", "%", "-", "_"];
56+
string[] escapes = ["\\", "`", "%"];
57+
//string[] escapes = ["\\","'", "`", "%"];
58+
59+
60+
61+
//foreach (var escape in escapes)
62+
//{
63+
var new_arg = "";
64+
foreach (var ch in arg)
65+
{
66+
if (escapes.Contains(ch.ToString()))
67+
new_arg += "\\";
68+
new_arg += ch.ToString();
69+
}
70+
//new_arg = new_arg.Replace(escape, $"\\{escape}");
71+
72+
return new_arg.Replace("\"","\"\"").Replace("'","''");
73+
}
4974

5075
public List<List<string>> Query(string q, List<string> args = null, bool non_query = false)
5176
{
@@ -58,12 +83,13 @@ public List<List<string>> Query(string q, List<string> args = null, bool non_que
5883
}
5984
}
6085

61-
return Common.Query(dbConn, Common._PrepareClear(_FixForSQLite(q), args), non_query);
86+
return Common.Query(dbConn, Common._PrepareClear(_FixForSQLite(q), args, _PrepareArg), non_query);
6287
}
6388

6489
public void QueryAsync(string q, List<string> args, Action<List<List<string>>> action = null, bool non_query = false)
6590
{
66-
Common.QueryAsync(dbConn, Common._PrepareClear(_FixForSQLite(q), args), action, non_query, false);
91+
Common.QueryAsync(dbConn, Common._PrepareClear(_FixForSQLite(q), args, _PrepareArg), action, non_query, false);
92+
6793
}
6894
/*
6995
public void QueryDapperAsync(Type type, string q, List<string> args = null, Action<object> action = null)

0 commit comments

Comments
 (0)