public object Clone() { Query query = new Query((string) text.Clone(), style); foreach(QueryParameter parameter in Parameters) { query.Parameters.Add((QueryParameter) parameter.Clone()); } query.readOnly = readOnly; return query; }
public ITable ExecQuery(Query query) { // TODO: Look up the query in a cache to see if we parsed it // before. // Parse it SqlParser parser = new SqlParser(new StringReader(query.Text)); // Determine that type of expression and dispatch as necessary, // If it's a SELECT query, Expression expression; try { expression = parser.Statement(); } catch (ParseException e) { // Parse error, report it back as an sql exception. throw new ApplicationException(e.Message); } // commit and rollback are special case operations, string fun_name = ""; if (expression is FunctionExpression) { fun_name = expression.GetArgument("name").ToString(); } if (fun_name.Equals("transaction_commit")) { lastSpecialCommand = "commit"; return null; } if (fun_name.Equals("transaction_rollback")) { lastSpecialCommand = "rollback"; return null; } if (fun_name.Equals("schema_assignment")) { lastSpecialCommand = "set_schema"; specialCommandArgument = expression; return null; } // Put the operation through the interpreter return Execute(query, expression); }
public ITable Execute(Query query, Expression expression) { // Create the QueryProcessor QueryProcessor processor = new QueryProcessor(transaction); // If it's a select, if (expression is SelectExpression) { QueryOptimizer optimizer = new QueryOptimizer(transaction); expression = optimizer.SubstituteParameters(expression, query); expression = optimizer.Qualify(expression); expression = optimizer.Optimize(expression); // Execute the query, return processor.Execute(expression); } // Set the parameter as the base table, and the base rowid (the // parameters table only has 1 row). processor.PushTable(new QueryParametersTable(query)); processor.UpdateTableRow(new RowId(0)); // Otherwise it must be an interpretable function if (expression is FunctionExpression) { string fun_name = (string)expression.GetArgument("name"); if (fun_name.Equals("create_table")) return CreateTable(expression); /* TODO: if (fun_name.Equals("drop_table")) return DropTable(processor, expression); if (fun_name.Equals("create_index")) return CreateIndex(processor, expression); if (fun_name.Equals("drop_index")) return DropIndex(processor, expression); if (fun_name.Equals("explain_expression")) return ExplainExpression(expression); */ } throw new NotSupportedException(); }
private IQueryContext ExecuteQuery(Query query) { lock (syncObject) { // Make the statement interpreter SqlInterpreter interpreter = new SqlInterpreter(this, transaction); // Execute the query ITable resultSet = interpreter.ExecQuery(query); // Was the last command a transactional operation (commit or rollback)? if (interpreter.HasSpecialCommand) { if (interpreter.SpecialCommand.Equals("set_schema")) { string schemaName = SqlInterpreter.GetStaticString(interpreter.SpecialCommandArgument); transaction.ChangeSchema(schemaName); resultSet = SqlInterpreter.DMLResult(1); } else { try { // Commit it (if it's a commit op) and dispose if (interpreter.SpecialCommand.Equals("commit")) session.CommitTransaction(transaction); } finally { // We must ensure that dispose is always called regardless SystemTransaction to_dispose = transaction; transaction = null; session.DisposeTransaction(to_dispose); } int result = (transaction == null) ? 0 : 1; resultSet = SqlInterpreter.DMLResult(result); } } // Map the result table into a form the JDBC driver wants return new EmbeddedQueryContext(resultSet, transaction, syncObject); } }
public QueryContainerTable(SystemTransaction transaction, Query query) { this.transaction = transaction; parameters = new List<SqlObject>(); if (query != null) { queryString = query.Text; foreach (QueryParameter parameter in query.Parameters) parameters.Add(parameter.Value); } columns = new ColumnCollection(this); columns.Add("#QUERY", SqlType.String, true); }
public Expression SubstituteParameters(Expression expression, Query query) { return WalkGraph(expression, new ParameterSubstitutor(query)); }
public IQueryContext Execute(Query query) { // Synchronize over this object to enforce the single thread per session // rule. Note that this does not prevent other sessions from interacting // with the database concurrently. lock (syncObject) { // Create a new transaction if there isn't already one open. if (transaction == null) transaction = session.CreateTransaction(username); // Execute the query and return the result return ExecuteQuery(query); } }
public QueryParametersTable(Query query) { this.query = query; columns = new ParameterColumnCollection(this); }
public ParameterSubstitutor(Query query) { this.query = query; }
public ParameterRow(QueryParametersTable table, RowId id) : base(table, id) { query = table.query; }
public ParameterColumnCollection(QueryParametersTable table) : base(table) { query = table.query; }
public Query Deserialize(Stream input) { if (input == null) throw new ArgumentNullException("input"); if (!input.CanRead) throw new ArgumentException("The input stream cannot be read."); BinaryReader reader = new BinaryReader(input); byte version = reader.ReadByte(); if (version != 1) throw new FormatException("Invalid version."); string text = reader.ReadString(); ParameterStyle style = (ParameterStyle) reader.ReadByte(); Query query = new Query(text, style); int paramCount = reader.ReadInt32(); for (int i = 0; i < paramCount; i++) { ParameterDirection direction = (ParameterDirection) reader.ReadByte(); int id = -1; string name = null; if (style == ParameterStyle.Named) { name = reader.ReadString(); } else { id = reader.ReadInt32(); } byte valueType = reader.ReadByte(); QueryParameter parameter; if (valueType == 0) { parameter = style == ParameterStyle.Marker ? new QueryParameter(id, null, direction) : new QueryParameter(name, null, direction); } else { int typeBufferLength = reader.ReadInt32(); byte[] typeBuffer = new byte[typeBufferLength]; reader.Read(typeBuffer, 0, typeBufferLength); SqlType type = SqlType.FromBinary(typeBuffer); int valueLength = reader.ReadInt32(); byte[] valueBuffer = new byte[valueLength]; reader.Read(valueBuffer, 0, valueLength); SqlValue value = new SqlBinaryValue(valueBuffer); parameter = style == ParameterStyle.Marker ? new QueryParameter(id, new SqlObject(type, value)) : new QueryParameter(name, new SqlObject(type, value)); } parameter.Direction = direction; query.Parameters.Add(parameter); } return query; }
public void Serialize(Query query, Stream output) { if (query == null) throw new ArgumentNullException("query"); if (output == null) throw new ArgumentNullException("output"); if (!output.CanWrite) throw new ArgumentException(); BinaryWriter writer = new BinaryWriter(output, encoding); writer.Write((byte)1); writer.Write(query.Text); writer.Write((byte)query.ParameterStyle); int paramCount = query.Parameters.Count; writer.Write(paramCount); for (int i = 0; i < paramCount; i++) { QueryParameter parameter = query.Parameters[i]; writer.Write((byte)parameter.Direction); if (query.ParameterStyle == ParameterStyle.Marker) { writer.Write(parameter.Id); } else { writer.Write(parameter.Name); } SqlObject value = parameter.Value; if (value == null) { writer.Write((byte)0); } else { writer.Write((byte)1); SqlType type = value.Type; byte[] typeBuffer = type.ToBinary(); writer.Write(typeBuffer.Length); writer.Write(typeBuffer); SqlValueInputStream valueInput = new SqlValueInputStream(value.Value); byte[] valueBuffer = new byte[valueInput.Length]; valueInput.Read(valueBuffer, 0, valueBuffer.Length); writer.Write(valueBuffer); } } }
internal void OnTransactionCommand(Query query) { if (sessionCommands == null) throw new SystemException(); sessionCommands.Add(query); }