OpenSource » Database Migrator » Migrator2
Clone URL:  

Migrator * updated the documentation and variable names on the AddForeignKey methods
* Added a new, safer, method for inserting rows into a table

* Deprecated the old less safe way of inserting rows into a table

Changeset ea784fee4395

Parent 1cbd2faebee5

by gareth.farrington

Changes to 5 files · Browse files at ea784fee4395 Showing diff from parent 1cbd2faebee5 Diff from another changeset...

 
302
303
304
 
 
 
 
 
305
306
 
302
303
304
305
306
307
308
309
310
311
@@ -302,5 +302,10 @@
  {   throw new NotImplementedException();   } + + public override int Insert(string table, string[] columns, params object[] values) + { + throw new NotImplementedException(); + }   }  }
 
292
293
294
 
 
 
 
 
295
296
 
292
293
294
295
296
297
298
299
300
301
@@ -292,5 +292,10 @@
  {   throw new NotImplementedException();   } + + public override int Insert(string table, string[] columns, params object[] values) + { + throw new NotImplementedException(); + }   }  }
 
70
71
72
73
 
74
75
76
 
77
78
79
 
80
81
82
83
 
84
85
 
86
87
88
 
92
93
94
95
96
 
97
98
99
 
102
103
104
105
106
 
107
108
109
110
111
 
112
113
114
115
 
 
 
116
117
118
 
354
355
356
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
358
359
 
70
71
72
 
73
74
 
 
75
76
77
 
78
79
80
81
 
82
83
 
84
85
86
87
 
91
92
93
 
 
94
95
96
97
 
100
101
102
 
 
103
104
105
106
107
 
108
109
 
 
 
110
111
112
113
114
115
 
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
@@ -70,19 +70,18 @@
  ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY ({2}) ", table, name, string.Join(",", columns)));   }   - public override void AddForeignKey(string name, string primaryTable, string[] primaryColumns, string refTable, string[] refColumns) + public override void AddForeignKey(string name, string foreignKeyTable, string[] foreignKeyColumn, string entityTable, string[] primaryKeyColumn)   { - AddForeignKey(name, primaryTable, primaryColumns, refTable, refColumns, CascadeBehavior.CascadeNone); - + AddForeignKey(name, foreignKeyTable, foreignKeyColumn, entityTable, primaryKeyColumn, CascadeBehavior.CascadeNone);   }   - public override void AddForeignKey(string name, string primaryTable, string[] primaryColumns, string refTable, string[] refColumns, CascadeBehavior cascade) + public override void AddForeignKey(string name, string foreignKeyTable, string[] foreignKeyColumns, string entityTable, string[] primaryKeyColumns, CascadeBehavior cascade)   {   string cascadeString = "";   - if (ConstraintExists(name, primaryTable)) + if (ConstraintExists(name, foreignKeyTable))   { - Logger.Warn("The contraint {0} already exists", name); + Logger.Warn("The constraint {0} already exists", name);   return;   }   @@ -92,8 +91,7 @@
  switch (cascade)   {   case CascadeBehavior.CascadeAll: - cascadeString = "ON DELETE CASCADE "; - cascadeString += "ON UPDATE CASCADE"; + cascadeString = "ON DELETE CASCADE ON UPDATE CASCADE";   break;   case CascadeBehavior.CascadeDelete:   cascadeString = "ON DELETE CASCADE"; @@ -102,17 +100,16 @@
  cascadeString = "ON UPDATE CASCADE";   break;   case CascadeBehavior.CascadeNone: - cascadeString = "ON DELETE NO ACTION "; - cascadeString += "ON UPDATE NO ACTION"; + cascadeString = "ON DELETE NO ACTION ON UPDATE NO ACTION";   break;   }     ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD CONSTRAINT {1} FOREIGN KEY ({2}) REFERENCES {3} ({4}) {5}", - primaryTable, + foreignKeyTable,   name, - string.Join(",", primaryColumns), - refTable, - string.Join(",", refColumns), + string.Join(",", foreignKeyColumns), + entityTable, + string.Join(",", primaryKeyColumns),   cascadeString));   }   public override void RemoveForeignKey(string name, string table) @@ -354,6 +351,20 @@
  return (Column[])columns.ToArray(typeof(Column));   }   + public override int Insert(string table, string[] columns, params object[] values) + { + if (columns.Length != values.Length) + throw new ArgumentException("The Number of Columns and the number of Values are not the same!"); + + List<string> sqlValues = new List<string>(); + foreach (var value in values) + { + sqlValues.Add(this.ToSqlValue(value.GetType(), value)); + } + + return ExecuteNonQuery(string.Format("INSERT INTO {0} ({1}) VALUES ({2})", table, string.Join(", ", columns), string.Join(", ", sqlValues.ToArray()))); + } +   #region Indexes   public void CreateClusteredIndex(string indexName,string onTableName, Column column)   {
 
178
179
180
181
182
183
184
185
 
 
 
 
 
186
187
 
188
189
190
 
 
 
 
 
 
 
 
 
 
191
192
 
193
194
195
 
 
 
 
 
 
 
 
 
 
196
197
198
 
202
203
204
205
 
206
207
208
 
390
391
392
 
 
 
 
 
393
394
395
 
399
400
401
402
 
 
 
 
403
404
 
405
406
407
408
409
410
411
 
440
441
442
 
443
444
445
 
456
457
458
 
 
 
 
 
 
 
 
 
459
460
461
 
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
 
220
221
222
 
223
224
225
226
 
408
409
410
411
412
413
414
415
416
417
418
 
422
423
424
 
425
426
427
428
429
 
430
431
432
433
 
434
435
436
 
465
466
467
468
469
470
471
 
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
@@ -178,21 +178,39 @@
  /// Append a foreign key (relation) between two tables.   /// </summary>   /// <param name="name">Constraint name</param> - /// <param name="primaryTable">Table name containing the primary key</param> - /// <param name="primaryColumn">Primary key column name</param> - /// <param name="refTable">Foreign table name</param> - /// <param name="refColumn">Foreign column name</param> - public virtual void AddForeignKey(string name, string primaryTable, string primaryColumn, string refTable, string refColumn) + /// <param name="foreignKeyTable">Table to add the constraint to. Usually the table with the reference to an Entity in another table.</param> + /// <param name="foreignKeyColumn">The Foreign Key column name</param> + /// <param name="entityTable">The table that contains the Entity the Foreign Key references</param> + /// <param name="primaryKeyColumn">The Primary Key column in the Entity table</param> + public virtual void AddForeignKey(string name, string foreignKeyTable, string foreignKeyColumn, string entityTable, string primaryKeyColumn)   { - AddForeignKey(name, primaryTable, new string[] { primaryColumn }, refTable, new string[] { refColumn }); + AddForeignKey(name, foreignKeyTable, new string[] { foreignKeyColumn }, entityTable, new string[] { primaryKeyColumn });   }   - public virtual void AddForeignKey(string name, string primaryTable, string primaryColumn, string refTable, string refColumn, CascadeBehavior cascade) + /// <summary> + /// Append a foreign key (relation) between two tables. + /// </summary> + /// <param name="name">Constraint name</param> + /// <param name="foreignKeyTable">Table to add the constraint to. Usually the table with the reference to an Entity in another table.</param> + /// <param name="foreignKeyColumn">The Foreign Key column name</param> + /// <param name="entityTable">The table that contains the Entity the Foreign Key references</param> + /// <param name="primaryKeyColumn">The Primary Key column in the Entity table</param> + /// <param name="cascade">What Cascading should be performed</param> + public virtual void AddForeignKey(string name, string foreignKeyTable, string foreignKeyColumn, string entityTable, string primaryKeyColumn, CascadeBehavior cascade)   { - AddForeignKey(name, primaryTable, new string[] { primaryColumn }, refTable, new string[] { refColumn }, cascade); + AddForeignKey(name, foreignKeyTable, new string[] { foreignKeyColumn }, entityTable, new string[] { primaryKeyColumn }, cascade);   }   - public virtual void AddForeignKey(string name, string primaryTable, string[] primaryColumns, string refTable, string[] refColumns, CascadeBehavior cascade) + /// <summary> + /// Append a foreign key (relation) between two tables. + /// </summary> + /// <param name="name">Constraint name</param> + /// <param name="foreignKeyTable">Table to add the constraint to. Usually the table with the reference to an Entity in another table.</param> + /// <param name="ForeignKeyColumn">The Foreign Key column name</param> + /// <param name="entityTableName">The table that contains the Entity the Foreign Key references</param> + /// <param name="primaryKeyColumns">The Primary Key column in the Entity table</param> + /// <param name="cascade"></param> + public virtual void AddForeignKey(string name, string foreignKeyTable, string[] foreignKeyColumns, string entityTableName, string[] primaryKeyColumns, CascadeBehavior cascade)   {   throw new NotImplementedException();   } @@ -202,7 +220,7 @@
  /// AddForeignKey(string, string, string, string, string)   /// </see>   /// </summary> - public abstract void AddForeignKey(string name, string primaryTable, string[] primaryColumns, string refTable, string[] refColumns); + public abstract void AddForeignKey(string name, string foreignKeyTable, string[] foreignKeyColumns, string entityTableName, string[] primaryKeyColumns);     /// <summary>   /// Add unique constraint @@ -390,6 +408,11 @@
  }   }   + /// <summary> + /// Execute a raw SQl statement that will not produce a result set (e.g. Update, Insert, Delete etc.) + /// </summary> + /// <param name="sql">Raw SQL statement</param> + /// <returns>the number of rows affected</returns>   public int ExecuteNonQuery(string sql)   {   this.Logger.Trace(sql); @@ -399,13 +422,15 @@
  }   }   - /// <param name="format">Format String</param> + /// <summary> + /// Execute a raw SQl statement that will not produce a result set (e.g. Update, Insert, Delete etc.) + /// </summary> + /// <param name="format">Raw SQL string to format into</param>   /// <param name="args">Arguments to use against the formatted string</param> - /// <returns>Int</returns> + /// <returns>The number of rows affected</returns>   public int ExecuteNonQuery(string format, params object[] args)   {   return ExecuteNonQuery(string.Format(format, args)); -   }     /// <summary> @@ -440,6 +465,7 @@
  return (int)ExecuteScalar(String.Format("SELECT TOP 1 Version FROM SchemaInfo WHERE AssemblyId='{0}'", assemblyId));   }   + [Obsolete("Use the other Insert variant, this one is not safe!")]   public virtual int Insert(string table, params string[] columnValues)   {   string[] columns = new string[columnValues.Length]; @@ -456,6 +482,15 @@
  return ExecuteNonQuery(string.Format("INSERT INTO {0} ({1}) VALUES ({2})", table, string.Join(", ", columns), string.Join(", ", values)));   }   + /// <summary> + /// Insert a row into a table + /// </summary> + /// <param name="table">The name of the table</param> + /// <param name="columns">A list of column names</param> + /// <param name="values">A list of primitive values that can be converted to SQL by the TransformationProvider</param> + /// <returns></returns> + public abstract int Insert(string table, string[] columns, params object[] values); +   protected virtual void InsertVersionRecord(String assemblyId)   {   ExecuteNonQuery(string.Format("INSERT INTO SchemaInfo (Version, AssemblyId) VALUES (0, '{0}')", assemblyId));
 
189
190
191
 
 
 
 
 
192
193
 
189
190
191
192
193
194
195
196
197
198
@@ -189,5 +189,10 @@
  {   throw new NotImplementedException();   } + + public override int Insert(string table, string[] columns, params object[] values) + { + throw new NotImplementedException(); + }   }  }