-
Notifications
You must be signed in to change notification settings - Fork 0
/
NpgsqlHelper.cs
441 lines (382 loc) · 19.8 KB
/
NpgsqlHelper.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System.Data;
using System.Text;
namespace Npgsql
{
/// <summary>
/// Helper class that makes it easier to work with the provider.
/// </summary>
public static class NpgsqlHelper
{
enum CharClass : byte
{
None,
Quote,
Backslash
}
private static string stringOfBackslashChars = "\u005c\u00a5\u0160\u20a9\u2216\ufe68\uff3c";
private static string stringOfQuoteChars = "\u0022\u0027\u0060\u00b4\u02b9\u02ba\u02bb\u02bc\u02c8\u02ca\u02cb\u02d9\u0300\u0301\u2018\u2019\u201a\u2032\u2035\u275b\u275c\uff07";
private static CharClass[] charClassArray = makeCharClassArray();
#region ExecuteNonQuery
/// <summary>
/// Executes a single command against a Npgsql database. The <see cref="NpgsqlConnection"/> is assumed to be
/// open when the method is called and remains open after the method completes.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use</param>
/// <param name="commandText">Npgsql command to be executed</param>
/// <param name="commandParameters">Array of <see cref="NpgsqlParameter"/> objects to use with the command.</param>
/// <returns></returns>
public static int ExecuteNonQuery(this NpgsqlConnection connection, string commandText, params NpgsqlParameter[] commandParameters)
{
//create a command and prepare it for execution
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
if (commandParameters != null)
foreach (NpgsqlParameter p in commandParameters)
cmd.Parameters.Add(p);
int result = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return result;
}
/// <summary>
/// Executes a single command against a Npgsql database. A new <see cref="NpgsqlConnection"/> is created
/// using the <see cref="NpgsqlConnection.ConnectionString"/> given.
/// </summary>
/// <param name="connectionString"><see cref="NpgsqlConnection.ConnectionString"/> to use</param>
/// <param name="commandText">Npgsql command to be executed</param>
/// <param name="parms">Array of <see cref="NpgsqlParameter"/> objects to use with the command.</param>
/// <returns></returns>
public static int ExecuteNonQuery(string connectionString, string commandText, params NpgsqlParameter[] parms)
{
//create & open a NpgsqlConnection, and dispose of it after we are done.
using (NpgsqlConnection cn = new NpgsqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the connection string
return ExecuteNonQuery(cn, commandText, parms);
}
}
#endregion
#region ExecuteDataSet
/// <summary>
/// Executes a single Npgsql command and returns the first row of the resultset. A new NpgsqlConnection object
/// is created, opened, and closed during this method.
/// </summary>
/// <param name="connectionString">Settings to be used for the connection</param>
/// <param name="commandText">Command to execute</param>
/// <param name="parms">Parameters to use for the command</param>
/// <returns>DataRow containing the first row of the resultset</returns>
public static DataRow ExecuteDataRow(string connectionString, string commandText, params NpgsqlParameter[] parms)
{
DataSet ds = ExecuteDataset(connectionString, commandText, parms);
if (ds == null) return null;
if (ds.Tables.Count == 0) return null;
if (ds.Tables[0].Rows.Count == 0) return null;
return ds.Tables[0].Rows[0];
}
/// <summary>
/// Executes a single Npgsql command and returns the resultset in a <see cref="DataSet"/>.
/// A new NpgsqlConnection object is created, opened, and closed during this method.
/// </summary>
/// <param name="connectionString">Settings to be used for the connection</param>
/// <param name="commandText">Command to execute</param>
/// <returns><see cref="DataSet"/> containing the resultset</returns>
public static DataSet ExecuteDataset(string connectionString, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteDataset(connectionString, commandText, (NpgsqlParameter[])null);
}
/// <summary>
/// Executes a single Npgsql command and returns the resultset in a <see cref="DataSet"/>.
/// A new NpgsqlConnection object is created, opened, and closed during this method.
/// </summary>
/// <param name="connectionString">Settings to be used for the connection</param>
/// <param name="commandText">Command to execute</param>
/// <param name="commandParameters">Parameters to use for the command</param>
/// <returns><see cref="DataSet"/> containing the resultset</returns>
public static DataSet ExecuteDataset(string connectionString, string commandText, params NpgsqlParameter[] commandParameters)
{
//create & open a NpgsqlConnection, and dispose of it after we are done.
using (NpgsqlConnection cn = new NpgsqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the connection string
return ExecuteDataset(cn, commandText, commandParameters);
}
}
/// <summary>
/// Executes a single Npgsql command and returns the resultset in a <see cref="DataSet"/>.
/// The state of the <see cref="NpgsqlConnection"/> object remains unchanged after execution
/// of this method.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use</param>
/// <param name="commandText">Command to execute</param>
/// <returns><see cref="DataSet"/> containing the resultset</returns>
public static DataSet ExecuteDataset(this NpgsqlConnection connection, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteDataset(connection, commandText, (NpgsqlParameter[])null);
}
/// <summary>
/// Executes a single Npgsql command and returns the resultset in a <see cref="DataSet"/>.
/// The state of the <see cref="NpgsqlConnection"/> object remains unchanged after execution
/// of this method.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use</param>
/// <param name="commandText">Command to execute</param>
/// <param name="commandParameters">Parameters to use for the command</param>
/// <returns><see cref="DataSet"/> containing the resultset</returns>
public static DataSet ExecuteDataset(this NpgsqlConnection connection, string commandText, params NpgsqlParameter[] commandParameters)
{
//create a command and prepare it for execution
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
if (commandParameters != null)
foreach (NpgsqlParameter p in commandParameters)
cmd.Parameters.Add(p);
//create the DataAdapter & DataSet
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataSet ds = new DataSet();
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds);
// detach the NpgsqlParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
//return the dataset
return ds;
}
/// <summary>
/// Updates the given table with data from the given <see cref="DataSet"/>
/// </summary>
/// <param name="connectionString">Settings to use for the update</param>
/// <param name="commandText">Command text to use for the update</param>
/// <param name="ds"><see cref="DataSet"/> containing the new data to use in the update</param>
/// <param name="tablename">Tablename in the dataset to update</param>
public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename)
{
NpgsqlConnection cn = new NpgsqlConnection(connectionString);
cn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(commandText, cn);
NpgsqlCommandBuilder cb = new NpgsqlCommandBuilder(da);
cb.ToString();
da.Update(ds, tablename);
cn.Close();
}
#endregion
#region ExecuteDataReader
/// <summary>
/// Executes a single command against a Npgsql database, possibly inside an existing transaction.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use for the command</param>
/// <param name="transaction"><see cref="NpgsqlTransaction"/> object to use for the command</param>
/// <param name="commandText">Command text to use</param>
/// <param name="commandParameters">Array of <see cref="NpgsqlParameter"/> objects to use with the command</param>
/// <param name="ExternalConn">True if the connection should be preserved, false if not</param>
/// <returns><see cref="NpgsqlDataReader"/> object ready to read the results of the command</returns>
private static NpgsqlDataReader ExecuteReader(this NpgsqlConnection connection, NpgsqlTransaction transaction, string commandText, NpgsqlParameter[] commandParameters, bool ExternalConn)
{
//create a command and prepare it for execution
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.Transaction = transaction;
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
if (commandParameters != null)
foreach (NpgsqlParameter p in commandParameters)
cmd.Parameters.Add(p);
//create a reader
NpgsqlDataReader dr;
// call ExecuteReader with the appropriate CommandBehavior
if (ExternalConn)
{
dr = cmd.ExecuteReader();
}
else
{
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
// detach the NpgsqlParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
return dr;
}
/// <summary>
/// Executes a single command against a Npgsql database.
/// </summary>
/// <param name="connectionString">Settings to use for this command</param>
/// <param name="commandText">Command text to use</param>
/// <returns><see cref="NpgsqlDataReader"/> object ready to read the results of the command</returns>
public static NpgsqlDataReader ExecuteReader(string connectionString, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteReader(connectionString, commandText, (NpgsqlParameter[])null);
}
/// <summary>
/// Executes a single command against a Npgsql database.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use for the command</param>
/// <param name="commandText">Command text to use</param>
/// <returns><see cref="NpgsqlDataReader"/> object ready to read the results of the command</returns>
public static NpgsqlDataReader ExecuteReader(this NpgsqlConnection connection, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteReader(connection, null, commandText, (NpgsqlParameter[])null, true);
}
/// <summary>
/// Executes a single command against a Npgsql database.
/// </summary>
/// <param name="connectionString">Settings to use for this command</param>
/// <param name="commandText">Command text to use</param>
/// <param name="commandParameters">Array of <see cref="NpgsqlParameter"/> objects to use with the command</param>
/// <returns><see cref="NpgsqlDataReader"/> object ready to read the results of the command</returns>
public static NpgsqlDataReader ExecuteReader(string connectionString, string commandText, params NpgsqlParameter[] commandParameters)
{
//create & open a NpgsqlConnection
NpgsqlConnection cn = new NpgsqlConnection(connectionString);
cn.Open();
//call the private overload that takes an internally owned connection in place of the connection string
return ExecuteReader(cn, null, commandText, commandParameters, false);
}
/// <summary>
/// Executes a single command against a Npgsql database.
/// </summary>
/// <param name="connection">Connection to use for the command</param>
/// <param name="commandText">Command text to use</param>
/// <param name="commandParameters">Array of <see cref="NpgsqlParameter"/> objects to use with the command</param>
/// <returns><see cref="NpgsqlDataReader"/> object ready to read the results of the command</returns>
public static NpgsqlDataReader ExecuteReader(this NpgsqlConnection connection, string commandText, params NpgsqlParameter[] commandParameters)
{
//call the private overload that takes an internally owned connection in place of the connection string
return ExecuteReader(connection, null, commandText, commandParameters, true);
}
#endregion
#region ExecuteScalar
/// <summary>
/// Execute a single command against a Npgsql database.
/// </summary>
/// <param name="connectionString">Settings to use for the update</param>
/// <param name="commandText">Command text to use for the update</param>
/// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
public static object ExecuteScalar(string connectionString, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteScalar(connectionString, commandText, (NpgsqlParameter[])null);
}
/// <summary>
/// Execute a single command against a Npgsql database.
/// </summary>
/// <param name="connectionString">Settings to use for the command</param>
/// <param name="commandText">Command text to use for the command</param>
/// <param name="commandParameters">Parameters to use for the command</param>
/// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
public static object ExecuteScalar(string connectionString, string commandText, params NpgsqlParameter[] commandParameters)
{
//create & open a NpgsqlConnection, and dispose of it after we are done.
using (NpgsqlConnection cn = new NpgsqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the connection string
return ExecuteScalar(cn, commandText, commandParameters);
}
}
/// <summary>
/// Execute a single command against a Npgsql database.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use</param>
/// <param name="commandText">Command text to use for the command</param>
/// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
public static object ExecuteScalar(this NpgsqlConnection connection, string commandText)
{
//pass through the call providing null for the set of NpgsqlParameters
return ExecuteScalar(connection, commandText, (NpgsqlParameter[])null);
}
/// <summary>
/// Execute a single command against a Npgsql database.
/// </summary>
/// <param name="connection"><see cref="NpgsqlConnection"/> object to use</param>
/// <param name="commandText">Command text to use for the command</param>
/// <param name="commandParameters">Parameters to use for the command</param>
/// <returns>The first column of the first row in the result set, or a null reference if the result set is empty.</returns>
public static object ExecuteScalar(this NpgsqlConnection connection, string commandText, params NpgsqlParameter[] commandParameters)
{
//create a command and prepare it for execution
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
if (commandParameters != null)
foreach (NpgsqlParameter p in commandParameters)
cmd.Parameters.Add(p);
//execute the command & return the results
object retval = cmd.ExecuteScalar();
// detach the NpgsqlParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
return retval;
}
#endregion
#region Utility methods
private static CharClass[] makeCharClassArray()
{
CharClass[] a = new CharClass[65536];
foreach (char c in stringOfBackslashChars)
{
a[c] = CharClass.Backslash;
}
foreach (char c in stringOfQuoteChars)
{
a[c] = CharClass.Quote;
}
return a;
}
private static bool needsQuoting(string s)
{
foreach (char c in s)
{
if (charClassArray[c] != CharClass.None)
{
return true;
}
}
return false;
}
/// <summary>
/// Escapes the string.
/// </summary>
/// <param name="value">The string to escape</param>
/// <returns>The string with all quotes escaped.</returns>
public static string EscapeString(string value)
{
if (!needsQuoting(value))
return value;
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
CharClass charClass = charClassArray[c];
if (charClass != CharClass.None)
{
sb.Append("\\");
}
sb.Append(c);
}
return sb.ToString();
}
public static string DoubleQuoteString(string value)
{
if (!needsQuoting(value))
return value;
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
CharClass charClass = charClassArray[c];
if (charClass == CharClass.Quote)
sb.Append(c);
else if (charClass == CharClass.Backslash)
sb.Append("\\");
sb.Append(c);
}
return sb.ToString();
}
#endregion
}
}