[9dba08]: System.Data.SQLite / SQLiteCommandBuilder.cs Maximize Restore History

Download this file

SQLiteCommandBuilder.cs    316 lines (281 with data), 12.0 kB

  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
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
using System.ComponentModel;
using System.Data.Common;
using System.Globalization;
namespace System.Data.SQLite
{
/// <summary>
/// SQLite implementation of DbCommandBuilder.
/// </summary>
public sealed class SQLiteCommandBuilder : DbCommandBuilder
{
/// <summary>
/// Default constructor
/// </summary>
public SQLiteCommandBuilder() : this(null)
{
}
/// <summary>
/// Initializes the command builder and associates it with the specified data adapter.
/// </summary>
/// <param name="adp"></param>
public SQLiteCommandBuilder(SQLiteDataAdapter adp)
{
QuotePrefix = "[";
QuoteSuffix = "]";
DataAdapter = adp;
}
/// <summary>
/// Minimal amount of parameter processing. Primarily sets the DbType for the parameter equal to the provider type in the schema
/// </summary>
/// <param name="parameter">The parameter to use in applying custom behaviors to a row</param>
/// <param name="row">The row to apply the parameter to</param>
/// <param name="statementType">The type of statement</param>
/// <param name="whereClause">Whether the application of the parameter is part of a WHERE clause</param>
protected override void ApplyParameterInfo(DbParameter parameter, DataRow row, StatementType statementType,
bool whereClause)
{
var param = (SQLiteParameter) parameter;
param.DbType = (DbType) row[SchemaTableColumn.ProviderType];
}
/// <summary>
/// Returns a valid named parameter
/// </summary>
/// <param name="parameterName">The name of the parameter</param>
/// <returns>Error</returns>
protected override string GetParameterName(string parameterName)
{
return String.Format(CultureInfo.InvariantCulture, "@{0}", parameterName);
}
/// <summary>
/// Returns a named parameter for the given ordinal
/// </summary>
/// <param name="parameterOrdinal">The i of the parameter</param>
/// <returns>Error</returns>
protected override string GetParameterName(int parameterOrdinal)
{
return String.Format(CultureInfo.InvariantCulture, "@param{0}", parameterOrdinal);
}
/// <summary>
/// Returns a placeholder character for the specified parameter i.
/// </summary>
/// <param name="parameterOrdinal">The index of the parameter to provide a placeholder for</param>
/// <returns>Returns a named parameter</returns>
protected override string GetParameterPlaceholder(int parameterOrdinal)
{
return GetParameterName(parameterOrdinal);
}
/// <summary>
/// Sets the handler for receiving row updating events. Used by the DbCommandBuilder to autogenerate SQL
/// statements that may not have previously been generated.
/// </summary>
/// <param name="adapter">A data adapter to receive events on.</param>
protected override void SetRowUpdatingHandler(DbDataAdapter adapter)
{
if (adapter == base.DataAdapter)
{
((SQLiteDataAdapter) adapter).RowUpdating -= RowUpdatingEventHandler;
}
else
{
((SQLiteDataAdapter) adapter).RowUpdating += RowUpdatingEventHandler;
}
}
private void RowUpdatingEventHandler(object sender, RowUpdatingEventArgs e)
{
base.RowUpdatingHandler(e);
}
/// <summary>
/// Gets/sets the DataAdapter for this CommandBuilder
/// </summary>
public new SQLiteDataAdapter DataAdapter
{
get { return (SQLiteDataAdapter) base.DataAdapter; }
set { base.DataAdapter = value; }
}
/// <summary>
/// Returns the automatically-generated SQLite command to delete rows from the database
/// </summary>
/// <returns></returns>
public new SQLiteCommand GetDeleteCommand()
{
return (SQLiteCommand) base.GetDeleteCommand();
}
/// <summary>
/// Returns the automatically-generated SQLite command to delete rows from the database
/// </summary>
/// <param name="useColumnsForParameterNames"></param>
/// <returns></returns>
public new SQLiteCommand GetDeleteCommand(bool useColumnsForParameterNames)
{
return (SQLiteCommand) base.GetDeleteCommand(useColumnsForParameterNames);
}
/// <summary>
/// Returns the automatically-generated SQLite command to update rows in the database
/// </summary>
/// <returns></returns>
public new SQLiteCommand GetUpdateCommand()
{
return (SQLiteCommand) base.GetUpdateCommand();
}
/// <summary>
/// Returns the automatically-generated SQLite command to update rows in the database
/// </summary>
/// <param name="useColumnsForParameterNames"></param>
/// <returns></returns>
public new SQLiteCommand GetUpdateCommand(bool useColumnsForParameterNames)
{
return (SQLiteCommand) base.GetUpdateCommand(useColumnsForParameterNames);
}
/// <summary>
/// Returns the automatically-generated SQLite command to insert rows into the database
/// </summary>
/// <returns></returns>
public new SQLiteCommand GetInsertCommand()
{
return (SQLiteCommand) base.GetInsertCommand();
}
/// <summary>
/// Returns the automatically-generated SQLite command to insert rows into the database
/// </summary>
/// <param name="useColumnsForParameterNames"></param>
/// <returns></returns>
public new SQLiteCommand GetInsertCommand(bool useColumnsForParameterNames)
{
return (SQLiteCommand) base.GetInsertCommand(useColumnsForParameterNames);
}
/// <summary>
/// Overridden to hide its property from the designer
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false)]
#endif
public override CatalogLocation CatalogLocation
{
get { return base.CatalogLocation; }
set { base.CatalogLocation = value; }
}
/// <summary>
/// Overridden to hide its property from the designer
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false)]
#endif
public override string CatalogSeparator
{
get { return base.CatalogSeparator; }
set { base.CatalogSeparator = value; }
}
/// <summary>
/// Overridden to hide its property from the designer
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false)]
#endif
[DefaultValue("[")]
public override string QuotePrefix
{
get { return base.QuotePrefix; }
set { base.QuotePrefix = value; }
}
/// <summary>
/// Overridden to hide its property from the designer
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false)]
#endif
public override string QuoteSuffix
{
get { return base.QuoteSuffix; }
set { base.QuoteSuffix = value; }
}
/// <summary>
/// Places brackets around an identifier
/// </summary>
/// <param name="unquotedIdentifier">The identifier to quote</param>
/// <returns>The bracketed identifier</returns>
public override string QuoteIdentifier(string unquotedIdentifier)
{
if (String.IsNullOrEmpty(QuotePrefix)
|| String.IsNullOrEmpty(QuoteSuffix)
|| String.IsNullOrEmpty(unquotedIdentifier))
return unquotedIdentifier;
return QuotePrefix + unquotedIdentifier.Replace(QuoteSuffix, QuoteSuffix + QuoteSuffix) + QuoteSuffix;
}
/// <summary>
/// Removes brackets around an identifier
/// </summary>
/// <param name="quotedIdentifier">The quoted (bracketed) identifier</param>
/// <returns>The undecorated identifier</returns>
public override string UnquoteIdentifier(string quotedIdentifier)
{
if (String.IsNullOrEmpty(QuotePrefix)
|| String.IsNullOrEmpty(QuoteSuffix)
|| String.IsNullOrEmpty(quotedIdentifier))
return quotedIdentifier;
if (quotedIdentifier.StartsWith(QuotePrefix, StringComparison.OrdinalIgnoreCase) == false
|| quotedIdentifier.EndsWith(QuoteSuffix, StringComparison.OrdinalIgnoreCase) == false)
return quotedIdentifier;
return
quotedIdentifier.Substring(QuotePrefix.Length,
quotedIdentifier.Length - (QuotePrefix.Length + QuoteSuffix.Length)).Replace(
QuoteSuffix + QuoteSuffix, QuoteSuffix);
}
/// <summary>
/// Overridden to hide its property from the designer
/// </summary>
#if !PLATFORM_COMPACTFRAMEWORK
[Browsable(false)]
#endif
public override string SchemaSeparator
{
get { return base.SchemaSeparator; }
set { base.SchemaSeparator = value; }
}
/// <summary>
/// Override helper, which can help the base command builder choose the right keys for the given query
/// </summary>
/// <param name="sourceCommand"></param>
/// <returns></returns>
protected override DataTable GetSchemaTable(DbCommand sourceCommand)
{
using (
IDataReader reader = sourceCommand.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
{
DataTable schema = reader.GetSchemaTable();
// If the query contains a primary key, turn off the IsUnique property
// for all the non-key columns
if (HasSchemaPrimaryKey(schema))
ResetIsUniqueSchemaColumn(schema);
// if table has no primary key we use unique columns as a fall back
return schema;
}
}
private bool HasSchemaPrimaryKey(DataTable schema)
{
DataColumn IsKeyColumn = schema.Columns[SchemaTableColumn.IsKey];
foreach (DataRow schemaRow in schema.Rows)
{
if ((bool) schemaRow[IsKeyColumn])
return true;
}
return false;
}
private void ResetIsUniqueSchemaColumn(DataTable schema)
{
DataColumn IsUniqueColumn = schema.Columns[SchemaTableColumn.IsUnique];
DataColumn IsKeyColumn = schema.Columns[SchemaTableColumn.IsKey];
foreach (DataRow schemaRow in schema.Rows)
{
if ((bool) schemaRow[IsKeyColumn] == false)
schemaRow[IsUniqueColumn] = false;
}
schema.AcceptChanges();
}
}
}