/// <summary> /// Releases, if present, the underlying <c>HsqlStatement</c> and /// makes eligible for garbage collection any related resources. /// </summary> internal void InvalidateStatement() { try { // localize member references to minimize // potential race conditions regarding // null status of instance variables. HsqlConnection connection = m_dbConnection; HsqlStatement statement = m_statement; // Don't leak compiled statement handles if (connection != null && connection.State == ConnectionState.Open && statement != null) { statement.Free(Session); } } finally { m_statement = null; m_tokenList = null; m_storedProcedureCommandText = null; m_tableDirectCommandText = null; } }
internal void ClearBatchInternal() { HsqlStatement statement = m_statement; if (statement != null) { statement.ClearBatch(); } m_commandTextBatch = null; }
/// <summary> /// Provides the core logic for the <see cref="Prepare()"/> method. /// </summary> internal void PrepareInternal() { if (!IsPrepared) { string sql; HsqlSession session = this.Session; switch (m_commandType) { case CommandType.StoredProcedure: { sql = StoredProcedureCommandText; break; } case CommandType.Text: { sql = DefaultParameterMarkerCommandText; break; } case CommandType.TableDirect: { sql = TableDirectCommandText; break; } default: { throw new HsqlDataSourceException( "CommandType not supported: " + m_commandType); // NOI18N } } m_statement = session.PrepareStatement(sql); // no longer valid m_commandTextBatch = null; } }
internal void DeriveParametersInternal() { if (CommandType != CommandType.StoredProcedure) { throw new InvalidOperationException(string.Format( "Operation not supported for CommandType: " + CommandType.ToString())); } Prepare(); HsqlStatement statement = m_statement; ParameterMetaData pmd = statement.ParameterDescriptor.metaData; string[] parameterNames = pmd.colNames; int count = parameterNames.Length; HsqlParameter[] parameters = new HsqlParameter[count]; for (int i = 0; i < count; i++) { string name = parameterNames[i]; ParameterMode mode = (ParameterMode)pmd.paramMode[i]; int type = pmd.colTypes[i]; int precision = pmd.colSizes[i]; int scale = pmd.colScales[i]; int nullability = pmd.colNullable[i]; HsqlProviderType providerType = (HsqlProviderType)type; DbType dbType = HsqlConvert.ToDbType(providerType); ParameterDirection?direction = HsqlConvert.ToParameterDirection(mode); bool?isNullable = IsNullable(nullability); bool isCharacter = IsCharacterType(type); bool isNumber = (!isCharacter) && IsNumberType(type); bool isTemporal = !(isCharacter || isNumber) && IsTemporalType(type); int size = ToBufferSize(type, precision); if (isCharacter) { precision = 0; scale = 0; } else if (isNumber || isTemporal) { if (precision == 0) { precision = ToDefaultPrecision(type); } } HsqlParameter parameter = new HsqlParameter(); parameter.DbType = dbType; if (direction != null) { parameter.Direction = direction.Value; } if (isNullable != null) { parameter.IsNullable = isNullable.Value; } parameter.ParameterName = name; parameter.Precision = (byte)Math.Min(byte.MaxValue, precision); parameter.ProviderType = providerType; parameter.Scale = (byte)Math.Min(byte.MaxValue, scale); parameter.Size = size; parameter.SourceVersion = DataRowVersion.Default; parameters[i] = parameter; } HsqlParameterCollection pc = Parameters; pc.Clear(); foreach (HsqlParameter parameter in parameters) { pc.Add(parameter); } }
/// <summary> /// Applies the current values in this object's parameter collection /// to the internal statement object, if any, that represents the /// prepared form of this command. /// </summary> /// <remarks> /// <para> /// If this command is not presently prepared, no action is taken. /// </para> /// <para> /// On the other hand, this operation is invoked internally whenever /// an <c>ExecuteXXX</c> operation is invoked on a prepared /// <c>HsqlCommand</c> instance. /// </para> /// </remarks> /// <param name="batch"> /// <c>true</c> to apply the parameters toward batch execution /// </param> /// <exception cref="HsqlDataSourceException"> /// When unbound parameters exist. An unbound parameter condition /// occurs when a parameter's existence is declared in the command /// text using a marker or can be inferred from /// the signature of the stored procedure to be executed, and /// the parameter collection of this <c>HsqlCommand</c> contains /// no corresponding <c>HsqlParameter</c> instance or the /// corresponding <c>HsqlParameter</c> instance exists, but either /// it represents an explicitly required (non-defaulted) value binding /// site and the value has not explicitly been set or it represents a /// non-nullable binding site and its present value is either /// implicitly null or has explicily been set null. /// </exception> internal void ApplyParameters(bool batch) { HsqlStatement statement = m_statement; if (statement == null) { return; } HsqlParameterCollection parameters = m_dbParameterCollection; if (parameters == null || parameters.Count == 0) { int expectedCount = statement.ParameterCount; if (expectedCount == 0) { statement.SetParameterValues(s_noParameters); return; } throw new HsqlDataSourceException(string.Format( "{0} unbound parameters exist.", expectedCount)); // NOI18N } TokenList tokenList = TokenList; int[] bindTypes = statement.ParameterTypes; object[] values = new object[tokenList.ParameterCount]; int boundValueCount = 0; foreach (HsqlParameter parameter in parameters) { switch (parameter.Direction) { case ParameterDirection.Input: case ParameterDirection.InputOutput: { string name = parameter.ParameterName; int[] bindPositions = tokenList .GetNamedParameterBindPositionsInternal(name); for (int i = 0; i < bindPositions.Length; i++) { int bindPosition = bindPositions[i]; int bindType = bindTypes[bindPosition]; object value = HsqlConvert.FromDotNet.ToObject( parameter.Value, bindType); values[bindPosition] = value; boundValueCount++; } break; } } } if (boundValueCount < values.Length) { int unboundCount = (values.Length - boundValueCount); throw new HsqlDataSourceException(string.Format( "{0} unbound Parameters Exist.", unboundCount)); // NOI18N } if (batch) { statement.AddBatch(values); } else { statement.SetParameterValues(values); } }