public void GetDeleteCommand_Creates_Correct_SQL_Commands() { IList <string> commandList = new List <string>(); DataSet ds = _commandBuilder.GetSchema(); foreach (DataTable dataTable in ds.Tables) { IDbCommand dbCommand = _commandBuilder.GetDeleteCommand(dataTable.TableName); commandList.Add(dbCommand.CommandText); Console.WriteLine("Table '" + dataTable.TableName + "' delete command"); Console.WriteLine("\t" + dbCommand.CommandText); } Assert.AreEqual(EXPECTED_COUNT_OF_COMMANDS, commandList.Count, string.Format("Should be {0} commands", EXPECTED_COUNT_OF_COMMANDS)); Assert.That(ExpectedDeleteCommands, Is.EquivalentTo(commandList)); }
protected override int Update(DataRow[] dataRows, DataTableMapping tableMapping) { // generate commands by first row table schema if (dataRows.Length > 0) { var tbl = dataRows[0].Table; var changeset = GetChangeset(tbl); InsertCommand = (DbCommand)CmdBuilder.GetInsertCommand(tbl.TableName, changeset); InitDbCmd(InsertCommand, tbl); if (tbl.PrimaryKey != null && tbl.PrimaryKey.Length > 0) { var pkQuery = new Query(tbl.TableName, ComposePkCondition(tbl)); UpdateCommand = (DbCommand)CmdBuilder.GetUpdateCommand(pkQuery, changeset); InitDbCmd(UpdateCommand, tbl); DeleteCommand = (DbCommand)CmdBuilder.GetDeleteCommand(pkQuery); InitDbCmd(DeleteCommand, tbl); } } return(base.Update(dataRows, tableMapping)); }
private void deleteRecursive(DataSet ds, DataTable dataTableSchema, IDbCommandBuilder dbCommandBuilder, IDbTransaction dbTransaction, Hashtable deletedTableColl, bool deleteAll) { // Table has already been deleted from. if (deletedTableColl.ContainsKey(dataTableSchema.TableName)) { return; } // [20060724 - sdh] Move here (from end of method) to avoid infinite-loop when package has relation to itself // Table was deleted from in the database. deletedTableColl[dataTableSchema.TableName] = null; DataRelationCollection childRelations = dataTableSchema.ChildRelations; // The table has children. if (null != childRelations) { foreach (DataRelation childRelation in childRelations) { // Must delete the child table first. deleteRecursive(ds, childRelation.ChildTable, dbCommandBuilder, dbTransaction, deletedTableColl, deleteAll); } } if (deleteAll) { IDbCommand dbCommand = dbCommandBuilder.GetDeleteAllCommand(dataTableSchema.TableName); try { OnDeleteAll(dbCommand, dbTransaction); } catch (DBConcurrencyException) { // Swallow deletion of zero records. } } else { DataTable dataTable = ds.Tables[dataTableSchema.TableName]; DataTable dataTableCopy = dataTable.Copy(); dataTableCopy.AcceptChanges(); foreach (DataRow dataRow in dataTableCopy.Rows) { // Delete the row. dataRow.Delete(); } IDbCommand dbCommand = dbCommandBuilder.GetDeleteCommand(dataTableSchema.TableName); try { OnDelete(dataTableCopy, dbCommand, dbTransaction); } catch (DBConcurrencyException) { // Swallow deletion of zero records. } } }
private void deleteRecursive(DataSet ds, DataTable dataTableSchema, IDbCommandBuilder dbCommandBuilder, DbTransaction dbTransaction, Hashtable deletedTableColl, bool deleteAll) { // Table has already been deleted from. if (deletedTableColl.ContainsKey(dataTableSchema.TableName)) { return; } // [20060724 - sdh] Move here (from end of method) to avoid infinite-loop when package has relation to itself // Table was deleted from in the database. deletedTableColl[dataTableSchema.TableName] = null; DataRelationCollection childRelations = dataTableSchema.ChildRelations; // The table has children. if (null != childRelations) { foreach (DataRelation childRelation in childRelations) { // Must delete the child table first. deleteRecursive(ds, childRelation.ChildTable, dbCommandBuilder, dbTransaction, deletedTableColl, deleteAll); } } if (deleteAll) { using (DbCommand dbCommand = dbCommandBuilder.GetDeleteAllCommand(dbTransaction, dataTableSchema.TableName)) { try { OnDeleteAll(dbCommand, dbTransaction); } catch (DBConcurrencyException) { // Swallow deletion of zero records. } } } else { DataTable dataTable = ds.Tables[dataTableSchema.TableName]; DataTable dataTableCopy = dataTable.Copy(); dataTableCopy.AcceptChanges(); foreach (DataRow dataRow in dataTableCopy.Rows) { // Delete the row. dataRow.Delete(); } using (DbCommand dbCommand = dbCommandBuilder.GetDeleteCommand(dbTransaction, dataTableSchema.TableName)) { try { OnDelete(dataTableCopy, dbCommand, dbTransaction); } catch (DBConcurrencyException) { // Swallow deletion of zero records. } } } }