-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRMSQLiteConnection.cs
287 lines (249 loc) · 9.63 KB
/
RMSQLiteConnection.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using System.Web;
namespace RandM.RMLib
{
public class RMSQLiteConnection : IDisposable
{
private bool _Disposed = false;
private SQLiteConnection _Connection = null;
private SQLiteTransaction _Transaction = null;
private List<Parameter> _Parameters = new List<Parameter>();
public SQLiteDataReader Reader = null;
// Constructor -- loads CURRENTEXE.sqlite from application's directory, with optional transaction
public RMSQLiteConnection(bool transaction)
{
if (HttpContext.Current == null)
{
Init(Path.ChangeExtension(ProcessUtils.ExecutablePath, ".sqlite"), transaction);
}
else
{
Init(StringUtils.PathCombine(HttpContext.Current.Server.MapPath(".\\App_Data"), "default.sqlite"), transaction);
}
}
// Constructor -- loads ADBile from application's directory, with optional transaction
public RMSQLiteConnection(string fileName, bool transaction)
{
if (HttpContext.Current == null)
{
if (!Path.IsPathRooted(fileName))
{
fileName = StringUtils.PathCombine(ProcessUtils.StartupPath, fileName);
}
Init(fileName, transaction);
}
else
{
if (!Path.IsPathRooted(fileName))
{
fileName = StringUtils.PathCombine(HttpContext.Current.Server.MapPath(".\\App_Data"), fileName);
}
Init(fileName, transaction);
}
}
// Constructor -- loads ADBFile from ADBPath, with optional transaction
public RMSQLiteConnection(string directoryName, string fileName, bool transaction)
{
Init(StringUtils.PathCombine(directoryName, fileName), transaction);
}
~RMSQLiteConnection()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if (!_Disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing)
{
// Dispose managed resources.
Close();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
// Note disposing has been done.
_Disposed = true;
}
}
public string AddDateTimeParameter(DateTime value)
{
_Parameters.Add(new Parameter("@P" + (_Parameters.Count + 1).ToString(), value));
return " @P" + (_Parameters.Count).ToString() + " ";
}
public string AddDoubleParameter(double value)
{
_Parameters.Add(new Parameter("@P" + (_Parameters.Count + 1).ToString(), value));
return " @P" + (_Parameters.Count).ToString() + " ";
}
public string AddIntParameter(int value)
{
_Parameters.Add(new Parameter("@P" + (_Parameters.Count + 1).ToString(), value));
return " @P" + (_Parameters.Count).ToString() + " ";
}
public string AddVarCharParameter(string value)
{
_Parameters.Add(new Parameter("@P" + (_Parameters.Count + 1).ToString(), value));
return " @P" + (_Parameters.Count).ToString() + " ";
}
public void Close()
{
CloseReader();
if (_Connection != null)
{
_Connection.Close();
_Connection.Dispose();
_Connection = null;
}
}
private void CloseReader()
{
if ((Reader != null) && (!Reader.IsClosed))
{
Reader.Close();
}
Reader = null;
}
public void Commit()
{
CloseReader();
_Transaction.Commit();
_Transaction.Dispose();
Close();
}
// ExecuteNonQuery executes a query and returns the number of rows affected
// Useful for INSERT/UPDATE/DELETE operations
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public int ExecuteNonQuery(string sqlText)
{
using (SQLiteCommand Cmd = new SQLiteCommand(sqlText, _Connection, _Transaction))
{
for (int I = 0; I < _Parameters.Count; I++)
{
Cmd.Parameters.Add(_Parameters[I].Name, _Parameters[I].Type, _Parameters[I].Length).Value = _Parameters[I].Value;
}
_Parameters.Clear();
return Cmd.ExecuteNonQuery();
}
}
// ExecuteReader executes a query and returns an SQLDataReader object
// Useful for SELECT operations returning multiple columns/rows
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public bool ExecuteReader(string sqlText)
{
using (SQLiteCommand Cmd = new SQLiteCommand(sqlText, _Connection, _Transaction))
{
for (int I = 0; I < _Parameters.Count; I++)
{
Cmd.Parameters.Add(_Parameters[I].Name, _Parameters[I].Type, _Parameters[I].Length).Value = _Parameters[I].Value;
}
_Parameters.Clear();
Reader = Cmd.ExecuteReader();
return Reader.HasRows;
}
}
// ExecuteScalar executes a query and returns a plain old Object
// Useful for SELECT operations returning 1 row with 1 column
// Don't forget to Convert.ToWhatever the return value
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public object ExecuteScalar(string sqlText)
{
using (SQLiteCommand Cmd = new SQLiteCommand(sqlText, _Connection, _Transaction))
{
for (int I = 0; I < _Parameters.Count; I++)
{
Cmd.Parameters.Add(_Parameters[I].Name, _Parameters[I].Type, _Parameters[I].Length).Value = _Parameters[I].Value;
}
_Parameters.Clear();
return Cmd.ExecuteScalar();
}
}
private void Init(string fileName, bool transaction)
{
// Open connection and initiate transaction (if requested)
_Connection = new SQLiteConnection("Data Source=" + fileName);
_Connection.Open();
if (transaction)
{
_Transaction = _Connection.BeginTransaction();
}
}
public void Rollback()
{
CloseReader();
_Transaction.Rollback();
_Transaction.Dispose();
Close();
}
private struct Parameter
{
public string Name;
public string Value;
public DbType Type;
public int Length;
public Parameter(string parameterName, DateTime value)
{
Name = parameterName;
Value = value.ToString("yyyy-MM-dd HH:mm:ss.fffffff");
Type = DbType.DateTime;
Length = 8;
}
public Parameter(string parameterName, double value)
{
Name = parameterName;
Value = value.ToString();
Type = DbType.Double;
Length = 16;
}
public Parameter(string parameterName, int value)
{
Name = parameterName;
Value = value.ToString();
Type = DbType.Int64;
Length = 8;
}
public Parameter(string parameterName, string value)
{
Name = parameterName;
Value = value;
Type = DbType.String;
Length = value.Length;
}
}
}
}