private void SetVariable(MemoryDbCommand command, SqlSetAssignmentStatement assignment) { var scalarAssignment = (SqlScalarVariableAssignment)(assignment.VariableAssignment); var param = Helper.GetParameter(command, scalarAssignment.Variable); param.Value = Helper.GetValue(scalarAssignment.Value, param.NetDataType, new RawData(command), new List <RawTableRow>( )); }
public MemoryDbDataReader.ResultBatch Execute(RawData rawData, Dictionary <string, Table> tables, SqlQueryExpression expression, SqlOrderByClause orderByClause = null) { switch (expression) { case SqlBinaryQueryExpression binaryQueryExpression: { var commandLeft = new MemoryDbCommand(_Command); var rawDataLeft = new RawData(commandLeft); var batchLeft = Execute(rawDataLeft, tables, binaryQueryExpression.Left); var commandRight = new MemoryDbCommand(_Command); var rawDataRight = new RawData(commandRight); var batchRight = Execute(rawDataRight, tables, binaryQueryExpression.Right); var batch = MergeBatches(batchLeft, batchRight, binaryQueryExpression.Operator); if (orderByClause != null) { var resultRawData = new RawData(_Command, batch); new QueryResultBuilder(resultRawData).AddOrderedResultRows(batch, orderByClause.Items); } return(batch); } case SqlQuerySpecification sqlQuery: { return(Execute(tables, rawData, sqlQuery, orderByClause)); } default: throw new NotImplementedException($"Query expressions of type {expression} are currently not implemented."); } }
public object ExecuteSqlScalar(string commandText, MemoryDbCommand command) { var result = Parser.Parse(commandText); if (result.Errors.Any()) { throw new SqlServerParserException(result.Errors); } command.DataReader = new MemoryDbDataReader(CommandBehavior.SingleResult); foreach (var batch in result.Script.Batches) { command.LastIdentitySet = null; foreach (var child in batch.Children) { ExecuteStatement(command, child); } if (command.DataReader.IsScalarResult) { command.DataReader.Read( ); var value = command.DataReader.GetValue(0); command.DataReader.Dispose( ); command.DataReader = null; return(value); } throw new SqlNoScalarResultException( ); } return(null); }
private void SetStoredProcedureParameters(MemoryDbCommand command, SqlCreateAlterProcedureStatementBase storedProcedure, SqlExecuteArgumentCollection arguments) { if (arguments != null) { _MemoryDatabase.AddParameters(command, storedProcedure.Definition.Parameters); for (int argumentIndex = 0; argumentIndex < arguments.Count; argumentIndex++) { var parameter = (MemoryDbParameter)command.Parameters[argumentIndex]; var argument = arguments[argumentIndex]; if (argument.Parameter != null) { parameter.Value = Helper.GetParameter(_Command, argument.Parameter).Value; } else { parameter.Value = Helper.GetValue(argument.Value, parameter.NetDataType, new RawData(_Command), new List <RawTableRow>( )); } } } else { foreach (MemoryDbParameter parameter in _Command.Parameters) { command.Parameters.Add(parameter); } } }
public RawData(MemoryDbCommand command, MemoryDbDataReader.ResultBatch batch = null) { Command = command; Parameters = command.Parameters; Database = ((MemoryDbConnection)Command.Connection).GetMemoryDatabase( ); Batch = batch; }
public void AddTablesFromCommonTableExpressions(SqlCommonTableExpressionCollection commonTableExpressions, Dictionary <string, Table> tables) { foreach (var commonTableExpression in commonTableExpressions) { var command = new MemoryDbCommand(Command.Connection, Command.Parameters, Command.Variables); var rawData = new RawData(command); var batch = new ExecuteQueryStatement(Database, command).Execute(Database.Tables, rawData, (SqlQuerySpecification)commonTableExpression.QueryExpression); var name = commonTableExpression.Name.Value; var table = CreateTable(name, null, batch, commonTableExpression.ColumnList); table.Rows.AddRange(batch.ResultRows); _CommonTableList.Add(table); } }
private List <RawTableJoinRow> GetAllViewRows(SqlCreateAlterViewStatementBase view, string name) { var command = new MemoryDbCommand(Command.Connection, Command.Parameters, Command.Variables); var rawData = new RawData(command); var batch = new ExecuteQueryStatement(Database, command).Execute(Database.Tables, rawData, (SqlQuerySpecification)view.Definition.QueryExpression); var identifier = view.Definition.Name; if (TableAliasList.ContainsKey(name) == false) { var table = CreateTable(name, identifier, batch, null); TableAliasList.Add(name, table); } return(ResultBatch2RowList(TableAliasList[name], batch)); }
private void AddVariable(MemoryDbCommand command, SqlVariableDeclareStatement variableDeclaration) { foreach (var declaration in variableDeclaration.Declarations) { var column = new Column(null, declaration.Name, declaration.Type, UserDataTypes, 1); var variable = new MemoryDbParameter { ParameterName = column.Name, DbType = column.DbDataType, NetDataType = column.NetDataType, Precision = ( byte )column.Precision, Scale = ( byte )column.Scale, Size = column.Size, IsNullable = true }; command.Variables.Add(variable); } }
private void AddRowsFromSelect(Table table, List <Column> columns, SqlSelectSpecificationInsertSource selectSource) { var command = new MemoryDbCommand(_Command); var rawData = new RawData(command); var select = selectSource.SelectSpecification; var batch = new ExecuteQueryStatement(_Database, command, columns).Execute(rawData, _Database.Tables, select.QueryExpression, select.OrderByClause); foreach (var resultRow in batch.ResultRows) { var row = InitializeNewRow(table, columns); for (int index = 0; index < columns.Count; index++) { row[columns[index].Order] = resultRow[index]; } AddRowToTable(table, row); } }
public DbDataReader ExecuteSqlReader(string commandText, MemoryDbCommand command, CommandBehavior behavior) { var result = Parser.Parse(commandText); if (result.Errors.Any()) { throw new SqlServerParserException(result.Errors); } command.DataReader = new MemoryDbDataReader(behavior); foreach (var batch in result.Script.Batches) { command.LastIdentitySet = null; foreach (var child in batch.Children) { ExecuteStatement(command, child); } } return(command.DataReader); }
public void Execute(SqlExecuteModuleStatement executeProcedure) { var name = Helper.GetQualifiedName(executeProcedure.Module.ObjectIdentifier); if (_MemoryDatabase.StoredProcedures.ContainsKey(name) == false) { throw new SqlInvalidObjectNameException(name); } var storedProcedure = _MemoryDatabase.StoredProcedures[name]; var command = new MemoryDbCommand(_Command.Connection) { DataReader = _Command.DataReader }; SetStoredProcedureParameters(command, storedProcedure, executeProcedure.Arguments); foreach (var statement in storedProcedure.Statements) { _MemoryDatabase.ExecuteStatement(command, statement); } }
public int ExecuteSqlStatement(string commandText, MemoryDbCommand command) { var result = Parser.Parse(commandText); if (result.Errors.Any()) { throw new SqlServerParserException(result.Errors); } foreach (var batch in result.Script.Batches) { command.LastIdentitySet = null; foreach (var child in batch.Children) { ExecuteStatement(command, child); } } return(1); }
public void AddParameters(MemoryDbCommand command, SqlParameterDeclarationCollection parameterDeclarations) { if (parameterDeclarations != null) { foreach (var parameterDeclaration in parameterDeclarations) { var column = new Column(null, parameterDeclaration.Name, parameterDeclaration.Type, UserDataTypes, 1); var parameter = new MemoryDbParameter { ParameterName = column.Name.TrimStart(new [] { '@' }), DbType = column.DbDataType, NetDataType = column.NetDataType, Precision = ( byte )column.Precision, Scale = ( byte )column.Scale, Size = column.Size, IsNullable = true }; command.Parameters.Add(parameter); } } }
public ExecuteNullStatement(MemoryDatabase memoryDatabase, MemoryDbCommand command) { _Database = memoryDatabase; _Command = command; }
public MemoryDbCommand(MemoryDbCommand command) { DbConnection = command.Connection; DbParameterCollection = command.Parameters; Variables = command.Variables; }
public ExecuteQueryStatement(MemoryDatabase memoryDatabase, MemoryDbCommand command, List <Column> insertColumns = null) { _Command = command; _InsertColumns = insertColumns; }
public ExecuteView(MemoryDatabase memoryDatabase, MemoryDbCommand command) { _MemoryDatabase = memoryDatabase; _Command = command; }
public ExecuteProcedure(MemoryDatabase memoryDatabase, MemoryDbCommand command) { _MemoryDatabase = memoryDatabase; _Command = command; }
public void ExecuteStatement(MemoryDbCommand command, SqlCodeObject child) { switch (child) { case SqlCreateTableStatement createTable: new CreateTable(createTable).AddToDatabase(this, (MemoryDbConnection)(command.Connection)); break; case SqlInsertStatement insertStatement: new ExecuteNonQueryStatement(this, command).Execute(Tables, insertStatement); break; case SqlUpdateStatement updateStatement: new ExecuteUpdateStatement(this, command).Execute(Tables, updateStatement); break; case SqlNullStatement nullStatement: new ExecuteNullStatement(this, command).Execute(Tables, nullStatement); break; case SqlIfElseStatement ifElseStatement: new ExecuteNonQueryStatement(this, command).Execute(Tables, ifElseStatement); break; case SqlDeleteStatement deleteStatement: new ExecuteNonQueryStatement(this, command).Execute(Tables, deleteStatement); break; case SqlSelectStatement selectStatement: new ExecuteQueryStatement(this, command).Execute(Tables, selectStatement, command.DataReader); break; case SqlCompoundStatement compoundStatement: foreach (var compoundChild in compoundStatement.Children) { ExecuteStatement(command, compoundChild); } break; case SqlVariableDeclareStatement variableDeclaration: AddVariable(command, variableDeclaration); break; case SqlSetAssignmentStatement assignment: SetVariable(command, assignment); break; case SqlCreateProcedureStatement createProcedure: { new ExecuteProcedure(this, command).Execute(createProcedure); break; } case SqlAlterProcedureStatement alterProcedure: { new ExecuteProcedure(this, command).Execute(alterProcedure); break; } case SqlDropProcedureStatement dropProcedure: { new ExecuteProcedure(this, command).Execute(dropProcedure); break; } case SqlExecuteModuleStatement executeModule: { new ExecuteProcedure(this, command).Execute(executeModule); break; } case SqlCreateIndexStatement createIndex: case SqlDropExistingIndexOption dropIndex: { // We will never implement this and function without it break; } case SqlCreateViewStatement createView: { new ExecuteView(this, command).Execute(createView); break; } case SqlDropViewStatement dropView: { new ExecuteView(this, command).Execute(dropView); break; } case SqlAlterViewStatement alterView: { new ExecuteView(this, command).Execute(alterView); break; } case SqlCreateUserDefinedDataTypeStatement createUserDefinedDataType: { AddUserDefinedDataType(createUserDefinedDataType); break; } default: throw new NotImplementedException($"Statements of type {child.GetType( )} are not implemented yet"); } }