/* * Because the user should not be able to directly create a * DataReader object, the constructors are * marked as internal. */ internal VirtuosoDataReader ( VirtuosoConnection connection, IInnerCommand innerCommand, VirtuosoCommand command, CommandBehavior commandBehavior, bool schemaOnly) { Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoDataReader.ctor()"); Debug.Assert (connection != null); Debug.Assert (innerCommand != null); this.connection = connection; this.command = command; this.innerCommand = innerCommand; this.commandBehavior = commandBehavior; InitializeResultInfo (schemaOnly); if (schemaOnly) { over = true; last = true; } }
private void DisposeStatement () { if (innerCommand != null) { innerCommand.Dispose (); innerCommand = null; } }
public static void DeriveParameters(VirtuosoCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (command.CommandType != CommandType.StoredProcedure) { throw new InvalidOperationException("DeriveParameters supports only stored procedures."); } VirtuosoConnection connection = (VirtuosoConnection)command.Connection; if (connection == null) { throw new InvalidOperationException("The Connection property is not set."); } if (connection.State == ConnectionState.Closed) { throw new InvalidOperationException("The connection is closed."); } IInnerCommand innerCommand = null; VirtuosoDataReader reader = null; try { innerCommand = connection.innerConnection.CreateInnerCommand(null); innerCommand.GetProcedureColumns(command.CommandText); reader = new VirtuosoDataReader(connection, innerCommand, null, CommandBehavior.Default, false); command.Parameters.Clear(); while (reader.Read()) { CLI.InOutType iotype = (CLI.InOutType)reader.GetInt16(reader.GetOrdinal("COLUMN_TYPE")); ParameterDirection direction; switch (iotype) { case CLI.InOutType.SQL_PARAM_INPUT: direction = ParameterDirection.Input; break; case CLI.InOutType.SQL_PARAM_OUTPUT: direction = ParameterDirection.Output; break; case CLI.InOutType.SQL_PARAM_INPUT_OUTPUT: direction = ParameterDirection.InputOutput; break; case CLI.InOutType.SQL_PARAM_RETURN_VALUE: direction = ParameterDirection.ReturnValue; break; default: #if MONO direction = ParameterDirection.Input; #endif continue; } string name = reader.GetString(reader.GetOrdinal("COLUMN_NAME")); if (name == "" && iotype == CLI.InOutType.SQL_PARAM_RETURN_VALUE) { name = "ReturnValue"; } CLI.SqlType sqlType = (CLI.SqlType)reader.GetInt16(reader.GetOrdinal("DATA_TYPE")); DataType type = DataTypeInfo.MapSqlType(sqlType); if (type == null) { throw new SystemException("Unknown data type"); } int sizeOrdinal = reader.GetOrdinal("COLUMN_SIZE"); if (sizeOrdinal < 0) { sizeOrdinal = reader.GetOrdinal("PRECISION"); } int size = reader.IsDBNull(sizeOrdinal) ? 0 : reader.GetInt32(sizeOrdinal); int scaleOrdinal = reader.GetOrdinal("DECIMAL_DIGITS"); if (scaleOrdinal < 0) { scaleOrdinal = reader.GetOrdinal("SCALE"); } short scale = reader.IsDBNull(scaleOrdinal) ? (short)0 : reader.GetInt16(scaleOrdinal); int nullableOrdinal = reader.GetOrdinal("NULLABLE"); CLI.Nullable nullable = (CLI.Nullable)reader.GetInt16(nullableOrdinal); bool isNullable = (nullable != CLI.Nullable.SQL_NO_NULLS); VirtuosoParameter parameter = (VirtuosoParameter)command.CreateParameter(); parameter.ParameterName = name; parameter.Direction = direction; parameter.VirtDbType = type.vdbType; parameter.Size = type.GetFieldSize(size); parameter.Precision = type.GetPrecision(size); parameter.Scale = (byte)scale; parameter.IsNullable = isNullable; command.Parameters.Add(parameter); } } finally { if (reader != null) { reader.Close(); } if (innerCommand != null) { innerCommand.Dispose(); } } }
public VirtuosoDataReader ExecuteReader (CommandBehavior behavior) #endif { /* * ExecuteReader should retrieve results from the data source * and return a DataReader that allows the user to process * the results. */ Debug.WriteLineIf (CLI.FnTrace.Enabled, "VirtuosoCommand.ExecuteReader()"); // There must be a valid and open connection. if (connection == null || connection.State != ConnectionState.Open) throw new InvalidOperationException ("Connection must be valid and open"); VirtuosoConnection.permission.Demand (); CheckState (); if (innerCommand == null) innerCommand = connection.innerConnection.CreateInnerCommand (this); innerCommand.SetTimeout (timeout); innerCommand.SetCommandBehavior (behavior); bool schemaOnly = SchemaOnlyDataReader (behavior); string text = GetCommandText (); bool isSparql = text.TrimStart(null).StartsWith("sparql", StringComparison.OrdinalIgnoreCase); if (schemaOnly) { if (!isPrepared) { if (!isSparql) text = replaceNamedParams (text); innerCommand.Prepare (text); } } else { if (!isSparql && parameters != null && parameters.Count > 0) { VirtuosoParameterCollection _parameters = handleNamedParams (text, parameters); Debug.Assert (_parameters.Count == parameters.Count, "Count mismatch in reordered parameter array"); text = replaceNamedParams(text); } innerCommand.SetParameters (parameters); if (!isPrepared) { try { isExecuting = true; innerCommand.Execute (text); if (executeSecondaryStmt) { innerCommand.CloseCursor(false); innerCommand.Execute (secondaryStmt); } } finally { isExecuting = false; } } else { try { isExecuting = true; innerCommand.Execute (); if (executeSecondaryStmt) { innerCommand.CloseCursor(false); innerCommand.Execute (secondaryStmt); } } finally { isExecuting = false; } } isExecuted = true; } VirtuosoDataReader reader = new VirtuosoDataReader (connection, innerCommand, this, behavior, schemaOnly); dataReaderWeakRef = new WeakReference (reader); isFetching = true; return reader; }