/
Query.cs
85 lines (72 loc) · 2.36 KB
/
Query.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using DataTables.DatabaseUtil;
namespace DataTables.DatabaseUtil.Sqlite
{
/// <summary>
/// SQLite Query class
/// </summary>
public class Query : DataTables.Query
{
/// <summary>
/// Create a query instance specifically for SQLite
/// </summary>
/// <param name="db">Host database</param>
/// <param name="type">Query type</param>
public Query(Database db, string type)
: base(db, type)
{
}
/// <summary>
/// Bind parameters to the SQL statement
/// </summary>
/// <param name="sql">SQL command</param>
override protected void _Prepare(string sql)
{
DbProviderFactory provider = DbProviderFactories.GetFactory(_db.Adapter());
DbParameter param;
DbCommand cmd = provider.CreateCommand();
cmd.CommandText = sql;
cmd.Connection = _db.Conn();
cmd.Transaction = _db.DbTransaction;
if (_db.CommandTimeout != -1) {
cmd.CommandTimeout = _db.CommandTimeout;
}
// Bind values
for (int i = 0, ien = _bindings.Count; i < ien; i++)
{
var binding = _bindings[i];
param = cmd.CreateParameter();
param.ParameterName = binding.Name;
param.Value = binding.Value ?? DBNull.Value;
if (binding.Type != null)
{
param.DbType = binding.Type;
}
cmd.Parameters.Add(param);
}
_stmt = cmd;
_db.DebugInfo(sql, _bindings);
}
/// <summary>
/// Execute the SQL command
/// </summary>
/// <returns>Result instance</returns>
protected override DataTables.Result _Exec()
{
var dt = new System.Data.DataTable();
using (var dr = _stmt.ExecuteReader())
{
try {
dt.Load(dr);
}
catch {}
}
return new Sqlite.Result(_db, dt, this);
}
}
}