public override void DescribeParameters() { lock (this.db) { // Marshal structures to pointer XsqldaMarshaler marshaler = XsqldaMarshaler.Instance; this.parameters = new Descriptor(1); IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, parameters); int[] statusVector = ExtConnection.GetNewStatusVector(); int stmtHandle = this.handle; SafeNativeMethods.isc_dsql_describe_bind( statusVector, ref stmtHandle, IscCodes.SQLDA_VERSION1, sqlda); Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda); // Parse status vector this.db.ParseStatusVector(statusVector); if (descriptor.ActualCount != 0 && descriptor.Count != descriptor.ActualCount) { short n = descriptor.ActualCount; descriptor = new Descriptor(n); // Fre memory marshaler.CleanUpNativeData(ref sqlda); // Marshal new structure sqlda = marshaler.MarshalManagedToNative(this.db.Charset, descriptor); SafeNativeMethods.isc_dsql_describe_bind( statusVector, ref stmtHandle, IscCodes.SQLDA_VERSION1, sqlda); descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda); // Free memory marshaler.CleanUpNativeData(ref sqlda); // Parse status vector this.db.ParseStatusVector(statusVector); } else { if (descriptor.ActualCount == 0) { descriptor = new Descriptor(0); } } // Free memory if (sqlda != IntPtr.Zero) { marshaler.CleanUpNativeData(ref sqlda); } // Update parameter descriptor this.parameters = descriptor; } }
public override void Execute() { if (this.state == StatementState.Deallocated) { throw new InvalidOperationException("Statment is not correctly created."); } lock (this.db) { // Marshal structures to pointer XsqldaMarshaler marshaler = XsqldaMarshaler.Instance; IntPtr inSqlda = IntPtr.Zero; IntPtr outSqlda = IntPtr.Zero; if (this.parameters != null) { inSqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.parameters); } if (this.statementType == DbStatementType.StoredProcedure) { this.Fields.ResetValues(); outSqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.fields); } int[] statusVector = ExtConnection.GetNewStatusVector(); int trHandle = this.transaction.Handle; int stmtHandle = this.handle; SafeNativeMethods.isc_dsql_execute2( statusVector, ref trHandle, ref stmtHandle, IscCodes.SQLDA_VERSION1, inSqlda, outSqlda); if (outSqlda != IntPtr.Zero) { Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, outSqlda); // This would be an Execute procedure DbValue[] values = new DbValue[descriptor.Count]; for (int i = 0; i < values.Length; i++) { values[i] = new DbValue(this, descriptor[i]); } this.outputParams.Enqueue(values); } // Free memory marshaler.CleanUpNativeData(ref inSqlda); marshaler.CleanUpNativeData(ref outSqlda); this.db.ParseStatusVector(statusVector); this.UpdateRecordsAffected(); this.state = StatementState.Executed; } }
public override DbValue[] Fetch() { DbValue[] row = null; if (this.state == StatementState.Deallocated) { throw new InvalidOperationException("Statement is not correctly created."); } if (this.statementType != DbStatementType.Select && this.statementType != DbStatementType.SelectForUpdate) { return(null); } lock (this.db) { if (!this.allRowsFetched) { // Get the XSQLDA Marshaler XsqldaMarshaler marshaler = XsqldaMarshaler.Instance; // Reset actual field values this.fields.ResetValues(); // Marshal structures to pointer IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, fields); // Creta a new status vector int[] statusVector = ExtConnection.GetNewStatusVector(); // Statement handle to be passed to the fetch method int stmtHandle = this.handle; // Fetch data int status = SafeNativeMethods.isc_dsql_fetch(statusVector, ref stmtHandle, IscCodes.SQLDA_VERSION1, sqlda); // Obtain values Descriptor rowDesc = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda); if (this.fields.Count == rowDesc.Count) { // Try to preserve Array Handle information for (int i = 0; i < this.fields.Count; i++) { if (this.fields[i].IsArray() && this.fields[i].ArrayHandle != null) { rowDesc[i].ArrayHandle = this.fields[i].ArrayHandle; } } } this.fields = rowDesc; // Free memory marshaler.CleanUpNativeData(ref sqlda); // Parse status vector this.db.ParseStatusVector(statusVector); if (status == 100) { this.allRowsFetched = true; } else { // Set row values row = new DbValue[this.fields.ActualCount]; for (int i = 0; i < row.Length; i++) { row[i] = new DbValue(this, this.fields[i]); } } } } return(row); }
public override void Prepare(string commandText) { // Clear data this.ClearAll(); lock (this.db) { if (this.state == StatementState.Deallocated) { // Allocate statement this.Allocate(); } // Marshal structures to pointer XsqldaMarshaler marshaler = XsqldaMarshaler.Instance; // Setup fields structure this.fields = new Descriptor(1); IntPtr sqlda = marshaler.MarshalManagedToNative(this.db.Charset, this.fields); int[] statusVector = ExtConnection.GetNewStatusVector(); int trHandle = this.transaction.Handle; int stmtHandle = this.handle; byte[] buffer = this.db.Charset.GetBytes(commandText); SafeNativeMethods.isc_dsql_prepare( statusVector, ref trHandle, ref stmtHandle, (short)buffer.Length, buffer, this.db.Dialect, sqlda); // Marshal Pointer Descriptor descriptor = marshaler.MarshalNativeToManaged(this.db.Charset, sqlda); // Free memory marshaler.CleanUpNativeData(ref sqlda); // Parse status vector this.db.ParseStatusVector(statusVector); // Describe fields this.fields = descriptor; if (this.fields.ActualCount > 0 && this.fields.ActualCount != this.fields.Count) { this.Describe(); } else { if (this.fields.ActualCount == 0) { this.fields = new Descriptor(0); } } // Reset actual field values this.fields.ResetValues(); // Get Statement type this.statementType = this.GetStatementType(); // Update state this.state = StatementState.Prepared; } }