/// <summary> /// Executes the non query with primary key constraints. /// </summary> /// <param name="database">The database.</param> /// <param name="connection">The connection.</param> /// <param name="command">The command.</param> /// <param name="listQueries">The list queries.</param> /// <param name="listPrimaryKeyValues">The list primary key values.</param> /// <returns></returns> public int ExecuteNonQueryWithPrimaryKeyConstraints(DatabaseServer database, ref DbConnection connection, ref DbCommand command, List <ExecutionQuery> listQueries, out List <object> listPrimaryKeyValues) { List <object> listValues = new List <object>(); int modifiedRows = 0; foreach (ExecutionQuery executionQuery in listQueries) { if (executionQuery.Query.StartsWith(PRIMARY_KEY_CONSTRAINT)) { PrimaryKeyConstraint keyConstraint = ConstraintManager.ParsePrimaryKeyConstraint(executionQuery); ExecutionQuery currentQuery = ConstraintManager.StripPrimaryKeyConstraint(executionQuery); object pkValue = null; modifiedRows += ExecuteNonQueryWithPrimaryKey(database, ref connection, ref command, currentQuery, keyConstraint.PrimaryKeyFieldName, keyConstraint.TableName, ref pkValue); listValues.Add(pkValue); } } listPrimaryKeyValues = listValues; return(modifiedRows); }
public int ExecuteNonQueryConstrained(ref DbConnection connection, ref DbCommand command, List <ExecutionQuery> listQueries) { //NOTE : the Queue<T> doesn't have an indexer so we can't modify the ForeignConstraint. //That's why will use a List<T>. List <ForeignKeyConstraint> fkConstraints = new List <ForeignKeyConstraint>(); List <PrimaryKeyConstraint> pkConstraints = new List <PrimaryKeyConstraint>(); //the value of the primary key. object primaryKeyValue = null; //total number of modified rows. int modifiedRows = 0; //log data (new ExecutionEngineLogger()).LogContext(listQueries); for (int i = 0; i < listQueries.Count; i++) { //check for a constraints first if (listQueries[i].Query.StartsWith(FOREIGN_KEY_CONSTRAINT)) { ForeignKeyConstraint fk = ConstraintManager.ParseForeignKeyConstraint(listQueries[i]); fkConstraints.Add(fk); } else if (listQueries[i].Query.StartsWith(PRIMARY_KEY_CONSTRAINT)) { PrimaryKeyConstraint pk = ConstraintManager.ParsePrimaryKeyConstraint(listQueries[i]); pkConstraints.Add(pk); List <object> listGeneratedPks = new List <object>(); List <ExecutionQuery> listExecQueries = new List <ExecutionQuery>(); listExecQueries.Add(listQueries[i]); modifiedRows += ExecuteNonQueryWithPrimaryKeyConstraints(database, ref connection, ref command, listExecQueries, out listGeneratedPks); primaryKeyValue = listGeneratedPks[0]; } else { //we have no constraints so just run the query. if (fkConstraints.Count == 0) { command.Parameters.Clear(); if (listQueries[i].Parameters != null) { for (int j = 0; j < listQueries[i].Parameters.Length; j++) { command.Parameters.Add(listQueries[i].Parameters[j]); } } command.CommandType = CommandType.Text; command.CommandText = listQueries[i].Query; modifiedRows += command.ExecuteNonQuery(); primaryKeyValue = null; } else if (fkConstraints.Count > 0) { //we have constraints enabled. //get the current query and modify it string currentQuery = listQueries[i].Query; //get the current constraint. ForeignKeyConstraint fk = fkConstraints[0]; //check if we have already a primary key. if (primaryKeyValue != null) { //replace the value with the value of the primary key. ReplaceForeignKeyValue(ref currentQuery, primaryKeyValue.ToString()); //we have modified the running query. Now decresed the number of running queries. --fk.NumerOfRunningQueries; //check if it's the last running query. if (fk.NumerOfRunningQueries == 0) { //remove the constraint fkConstraints.RemoveAt(0); } else { //set back the constraint with the new value fkConstraints[0] = fk; } } ExecutionQuery execQuery = new ExecutionQuery(currentQuery, listQueries[i].Parameters); //run the query and return the value of the primary key modifiedRows += ExecuteNonQueryWithPrimaryKey(database, ref connection, ref command, execQuery, fk.NameOfPrimaryKeyField, fk.TableName, ref primaryKeyValue); } } } return(modifiedRows); }