Example #1
0
        public override void SetCurrentCatalog(string name)
        {
            IntPtr buffer = IntPtr.Zero;

            try
            {
                int length = (name.Length + 1) * Platform.WideCharSize;
                buffer = Platform.MarshalAlloc(length);
                Platform.StringToWideChars(name, buffer, length);
                CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetConnectAttr(
                    hdbc,
                    (int)CLI.ConnectionAttribute.SQL_ATTR_CURRENT_CATALOG,
                    buffer,
                    (int)CLI.LengthCode.SQL_NTS);
                if (rc != CLI.ReturnCode.SQL_SUCCESS)
                {
                    HandleConnectionErrors(rc);
                }
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    Platform.MarshalFree(buffer);
                }
            }

            GC.KeepAlive(this);
        }
Example #2
0
        public bool IsDBNull(int i, ColumnData[] columns)
        {
            if (dataBuffer == null)
            {
                dataBuffer = new MemoryHandle(2048);
            }

            ColumnData column = columns[i];
            BufferType type   = column.bufferType;

            IntPtr length;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLGetData(
                hstmt,
                (ushort)(i + 1),
                (short)type.sqlCType,
                dataBuffer.Handle,
                (IntPtr)0,
                out length);
            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }

            // rewind so that another IsDBNull() won't get SQL_NO_DATA
            RewindData(columns);

            return(length == (IntPtr)(int)CLI.LengthCode.SQL_NULL_DATA);
        }
Example #3
0
        public void Execute(string query)
        {
            if (pendingFuture != null)
            {
                throw new InvalidOperationException("The command is already active.");
            }

            IMarshal marshaledQuery = ExplicitString.CreateExecString(connection, query);

            queryType    = QueryType.QT_UNKNOWN;
            affectedRows = prefetchedRows = 0;
            isLastResult = isLastRow = isLastInBatch = false;
            prefetchRow  = currentRow = null;

            object[] parameterRows = new object[1];
            parameterRows[0] = parameterValues;
            Future future = new Future(Service.Execute, GetId(), marshaledQuery, GetId(), parameterRows, null, GetOptions());

            SendRequest(future);
            CLI.ReturnCode rc = ProcessResult(true);

            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
            {
                if (rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
                {
                    pendingFuture = null;
                    connection.futures.Remove(future);
                }
                Diagnostics.HandleResult(rc, this, connection.OuterConnection);
            }
        }
Example #4
0
        private static VirtuosoErrorCollection CreateErrors(CLI.ReturnCode rc)
        {
            string message = null;

            switch (rc)
            {
            case CLI.ReturnCode.SQL_INVALID_HANDLE:
                message = "Invalid Handle";
                break;

            case CLI.ReturnCode.SQL_NEED_DATA:
                message = "Need Data";
                break;

            case CLI.ReturnCode.SQL_STILL_EXECUTING:
                message = "Still Executing";
                break;

            default:
                message = "Unexpected Return Code";
                break;
            }

            VirtuosoErrorCollection errors = new VirtuosoErrorCollection();
            VirtuosoError           error  = new VirtuosoError(message, "");

            errors.Add(error);
            return(errors);
        }
Example #5
0
        public override IInnerCommand CreateInnerCommand(VirtuosoCommand outerCommand)
        {
            IntPtr hstmt;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLAllocHandle(
                (short)CLI.HandleType.SQL_HANDLE_STMT,
                hdbc,
                out hstmt);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }

            try
            {
                //statementList.Add (hstmt, outerCommand);
                Add(hstmt, outerCommand);
            }
            catch (Exception)
            {
                CLI.SQLFreeHandle((short)CLI.HandleType.SQL_HANDLE_STMT, hstmt);
                throw;
            }

            GC.KeepAlive(this);
            return(new OdbcCommand(hstmt, outerCommand));
        }
Example #6
0
 public void Cancel()
 {
     CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLCancel(hstmt);
     if (rc != CLI.ReturnCode.SQL_SUCCESS)
     {
         Diagnostics.HandleResult(rc, this, outerCommand.Connection);
     }
     GC.KeepAlive(this);
 }
        internal static VirtuosoErrorCollection CreateErrors(CLI.HandleType handleType, IntPtr handle)
        {
            VirtuosoErrorCollection errors = new VirtuosoErrorCollection();

            MemoryHandle sqlState    = null;
            MemoryHandle messageText = null;

            try
            {
                sqlState    = new MemoryHandle((CLI.SQL_SQLSTATE_SIZE + 1) * Platform.WideCharSize);
                messageText = new MemoryHandle((CLI.SQL_MAX_MESSAGE_LEN + 1) * Platform.WideCharSize);

                for (short recNumber = 1; ; recNumber++)
                {
                    int            nativeError;
                    short          textLength;
                    CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLGetDiagRec(
                        (short)handleType,
                        handle,
                        recNumber,
                        sqlState.Handle,
                        out nativeError,
                        messageText.Handle,
                        (short)(messageText.Length / Platform.WideCharSize),
                        out textLength);
                    if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
                    {
                        break;
                    }

#if false
                    //System.Console.WriteLine ("length: {0}", textLength);
                    string sMessageText = Platform.WideCharsToString(messageText.Handle, textLength);
                    string sSqlState    = Platform.WideCharsToString(sqlState.Handle, CLI.SQL_SQLSTATE_SIZE);
#else
                    string sMessageText = Marshal.PtrToStringAnsi(messageText.Handle, textLength);
                    string sSqlState    = Marshal.PtrToStringAnsi(sqlState.Handle, CLI.SQL_SQLSTATE_SIZE);
#endif
                    VirtuosoError error = new VirtuosoError(sMessageText, sSqlState);
                    errors.Add(error);
                }
            }
            finally
            {
                if (sqlState != null)
                {
                    sqlState.Dispose();
                }
                if (messageText != null)
                {
                    messageText.Dispose();
                }
            }

            return(errors);
        }
Example #8
0
 public bool GetNextResult()
 {
     CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLMoreResults(hstmt);
     if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
     {
         Diagnostics.HandleResult(rc, this, outerCommand.Connection);
     }
     GC.KeepAlive(this);
     return(rc == CLI.ReturnCode.SQL_SUCCESS);
 }
Example #9
0
        public int GetRowCount()
        {
            IntPtr count;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLRowCount(hstmt, out count);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }

            GC.KeepAlive(this);
            return((int)count);
        }
Example #10
0
 public void CloseCursor(bool isExecuted)
 {
     if (!isExecuted)
     {
         return;
     }
     CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLFreeStmt(hstmt, (ushort)CLI.FreeStmtOption.SQL_CLOSE);
     if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
     {
         Diagnostics.HandleResult(rc, this, outerCommand.Connection);
     }
     GC.KeepAlive(this);
 }
Example #11
0
 public void Execute()
 {
     CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLExecute(hstmt);
     if (rc != CLI.ReturnCode.SQL_SUCCESS)
     {
         if (rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
         {
             DisposeParameters();
         }
         Diagnostics.HandleResult(rc, this, outerCommand.Connection);
     }
     GC.KeepAlive(this);
 }
Example #12
0
        private int GetResultColumns()
        {
            short count;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLNumResultCols(hstmt, out count);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }

            GC.KeepAlive(this);
            return(count);
        }
Example #13
0
 public void SetTimeout(int timeout)
 {
     CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetStmtAttr(
         hstmt,
         (int)CLI.StatementAttribute.SQL_ATTR_QUERY_TIMEOUT,
         (IntPtr)timeout,
         (int)CLI.LengthCode.SQL_IS_SMALLINT);
     if (rc != CLI.ReturnCode.SQL_SUCCESS)
     {
         Diagnostics.HandleResult(rc, this, outerCommand.Connection);
     }
     GC.KeepAlive(this);
 }
Example #14
0
 internal static void HandleResult(
     CLI.ReturnCode returnCode,
     ICreateErrors source,
     IDbConnection connection)
 {
     if (returnCode == CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
     {
         HandleWarnings(source, connection);
     }
     else if (returnCode != CLI.ReturnCode.SQL_SUCCESS)
     {
         HandleErrors(returnCode, source);
     }
 }
Example #15
0
        private void RewindData(ColumnData[] columns)
        {
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetPos(hstmt, 1, (ushort)CLI.SetPosOp.SQL_POSITION, (ushort)CLI.LockOption.SQL_LOCK_NO_CHANGE);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }
            GC.KeepAlive(this);

            for (int i = 0; i < columns.Length; i++)
            {
                columns[i].lobOffset = 0;
            }
        }
Example #16
0
        internal static void HandleErrors(CLI.ReturnCode returnCode, ICreateErrors source)
        {
            VirtuosoErrorCollection errors = null;

            if (returnCode == CLI.ReturnCode.SQL_ERROR)
            {
                errors = source.CreateErrors();
            }
            else
            {
                errors = CreateErrors(returnCode);
            }
            throw new VirtuosoException(returnCode, errors);
        }
Example #17
0
        private void DisposeParameters()
        {
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLFreeStmt(hstmt, (ushort)CLI.FreeStmtOption.SQL_RESET_PARAMS);
            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }
            GC.KeepAlive(this);

            if (parameterData != null)
            {
                parameterData.Dispose();
                parameterData = null;
            }
        }
Example #18
0
 // deprecated
 internal static void HandleResult(
     CLI.ReturnCode returnCode,
     CLI.HandleType handleType,
     IntPtr handle,
     VirtuosoConnection connection)
 {
     if (returnCode == CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
     {
         HandleWarnings(handleType, handle, connection);
     }
     else if (returnCode != CLI.ReturnCode.SQL_SUCCESS)
     {
         HandleErrors(returnCode, handleType, handle);
     }
 }
Example #19
0
        public bool Fetch()
        {
#if false
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLFetch(hstmt);
#else
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLFetchScroll(hstmt, (short)CLI.FetchOrientation.SQL_FETCH_NEXT, (IntPtr)0);
#endif
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                if (rc == CLI.ReturnCode.SQL_NO_DATA)
                {
                    return(false);
                }
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }
            return(true);
        }
Example #20
0
        // deprecated
        internal static void HandleErrors(
            CLI.ReturnCode returnCode,
            CLI.HandleType handleType,
            IntPtr handle)
        {
            VirtuosoErrorCollection errors = null;

            if (returnCode == CLI.ReturnCode.SQL_ERROR)
            {
                errors = OdbcErrors.CreateErrors(handleType, handle);
            }
            else
            {
                errors = CreateErrors(returnCode);
            }
            throw new VirtuosoException(returnCode, errors);
        }
Example #21
0
        public override string GetCurrentCatalog()
        {
            MemoryHandle buffer = new MemoryHandle((CLI.SQL_MAX_COLUMN_NAME_LEN + 1) * Platform.WideCharSize);
            int          length;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLGetConnectAttr(
                hdbc,
                (int)CLI.ConnectionAttribute.SQL_ATTR_CURRENT_CATALOG,
                buffer.Handle,
                buffer.Length,
                out length);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }

            GC.KeepAlive(this);
            return(Platform.WideCharsToString(buffer.Handle, length));
        }
Example #22
0
        public void SetCommandBehavior(CommandBehavior behavior)
        {
            int unique_rows = 0;

            if ((behavior & CommandBehavior.KeyInfo) != 0)
            {
                unique_rows = 1;
            }

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetStmtAttr(
                hstmt,
                (int)CLI.StatementAttribute.SQL_UNIQUE_ROWS,
                (IntPtr)unique_rows,
                (int)CLI.LengthCode.SQL_IS_SMALLINT);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }
            GC.KeepAlive(this);
        }
Example #23
0
        public bool GetNextResult()
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ManagedCommand.GetNextResult ()");

            while (!isLastRow)
            {
                Fetch();
            }
            if (isLastResult)
            {
                return(false);
            }

            CLI.ReturnCode rc = ProcessResult(true);
            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
            {
                Diagnostics.HandleResult(rc, this, connection.OuterConnection);
            }
            return(rc == CLI.ReturnCode.SQL_SUCCESS || rc == CLI.ReturnCode.SQL_SUCCESS_WITH_INFO);
        }
Example #24
0
        public override void BeginTransaction(CLI.IsolationLevel level)
        {
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetConnectAttr(
                hdbc,
                (int)CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
                (IntPtr)(int)CLI.AutoCommit.SQL_AUTOCOMMIT_OFF,
                (int)CLI.LengthCode.SQL_IS_UINTEGER);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }
            rc = (CLI.ReturnCode)CLI.SQLSetConnectAttr(
                hdbc,
                (int)CLI.ConnectionAttribute.SQL_ATTR_TXN_ISOLATION,
                (IntPtr)(int)level,
                (int)CLI.LengthCode.SQL_IS_UINTEGER);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                if (rc == CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
                {
                    Diagnostics.HandleWarnings(this, OuterConnection);
                }
                else
                {
                    try
                    {
                        Diagnostics.HandleErrors(rc, CLI.HandleType.SQL_HANDLE_DBC, hdbc);
                    }
                    finally
                    {
                        CLI.SQLSetConnectAttr(
                            hdbc,
                            (int)CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
                            (IntPtr)(int)CLI.AutoCommit.SQL_AUTOCOMMIT_ON,
                            (int)CLI.LengthCode.SQL_IS_UINTEGER);
                    }
                }
            }

            GC.KeepAlive(this);
        }
Example #25
0
        public bool Fetch()
        {
            Debug.WriteLineIf(CLI.FnTrace.Enabled, "ManagedCommand.Fetch ()");

            for (;;)
            {
                Debug.WriteLineIf(Switch.Enabled, "isLastRow: " + isLastRow);
                if (isLastRow)
                {
                    currentRow = null;
                    return(false);
                }
                if (prefetchRow != null)
                {
                    prefetchedRows++;
                    currentRow  = prefetchRow;
                    prefetchRow = null;
                    return(true);
                }
                Debug.WriteLineIf(Switch.Enabled, "prefetchedRows: " + prefetchedRows + " / " + prefetchSize);
                Debug.WriteLineIf(Switch.Enabled, "isLastInBatch: " + isLastInBatch);
                if ((prefetchedRows == prefetchSize || isLastInBatch) &&
                    queryType == QueryType.QT_SELECT)
                {
                    Future future = new Future(Service.Fetch, id, pendingFuture.RequestNo);
                    future.SendRequest(connection.Session, timeout);
                    prefetchedRows = 0;
                }

                CLI.ReturnCode rc = ProcessResult(true);
                if (rc != CLI.ReturnCode.SQL_SUCCESS)
                {
                    if (rc == CLI.ReturnCode.SQL_NO_DATA)
                    {
                        return(false);
                    }
                    Diagnostics.HandleResult(rc, this, connection.OuterConnection);
                }
            }
        }
Example #26
0
        public override void EndTransaction(bool commit)
        {
            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLEndTran(
                (short)CLI.HandleType.SQL_HANDLE_DBC, hdbc,
                (short)(commit ? CLI.CompletionType.SQL_COMMIT : CLI.CompletionType.SQL_ROLLBACK));
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }

            rc = (CLI.ReturnCode)CLI.SQLSetConnectAttr(
                hdbc,
                (int)CLI.ConnectionAttribute.SQL_ATTR_AUTOCOMMIT,
                (IntPtr)(int)CLI.AutoCommit.SQL_AUTOCOMMIT_ON,
                (int)CLI.LengthCode.SQL_IS_UINTEGER);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }

            GC.KeepAlive(this);
        }
Example #27
0
        public void Prepare(string query)
        {
            if (pendingFuture != null)
            {
                throw new InvalidOperationException("The command is already active.");
            }

            IMarshal marshaledQuery = ExplicitString.CreateExecString(connection, query);

            Future future = new Future(Service.Prepare, GetId(), marshaledQuery, 0, GetOptions());

            SendRequest(future);
            CLI.ReturnCode rc = ProcessResult(false);
            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_NO_DATA)
            {
                if (rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
                {
                    pendingFuture = null;
                    connection.futures.Remove(future);
                }
                Diagnostics.HandleResult(rc, this, connection.OuterConnection);
            }
        }
Example #28
0
        public override void Enlist(object distributedTransaction)
        {
            ITransaction transaction = (ITransaction)distributedTransaction;

            IntPtr pIUnknown = IntPtr.Zero;

            if (transaction != null)
            {
                pIUnknown = Marshal.GetIUnknownForObject(transaction);
            }

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLSetConnectAttr(
                hdbc,
                (int)CLI.ConnectionAttribute.SQL_ATTR_ENLIST_IN_DTC,
                pIUnknown,
                (int)CLI.LengthCode.SQL_IS_POINTER);
            if (rc != CLI.ReturnCode.SQL_SUCCESS)
            {
                HandleConnectionErrors(rc);
            }

            GC.KeepAlive(this);
        }
		internal VirtuosoException (CLI.ReturnCode returnCode, VirtuosoErrorCollection errors)
		{
			this.returnCode = returnCode;
			this.errors = errors;
		}
Example #30
0
 internal VirtuosoException(CLI.ReturnCode returnCode, VirtuosoErrorCollection errors)
 {
     this.returnCode = returnCode;
     this.errors     = errors;
 }
Example #31
0
        public object GetColumnData(int i, ColumnData[] columns)
        {
            if (dataBuffer == null)
            {
                dataBuffer = new MemoryHandle(2048);
            }

            ColumnData column = columns[i];
            BufferType type   = column.bufferType;
            object     columnData;        // = column.data;

            IntPtr length;

            CLI.ReturnCode rc = (CLI.ReturnCode)CLI.SQLGetData(
                hstmt,
                (ushort)(i + 1),
                (short)type.sqlCType,
                dataBuffer.Handle,
                (IntPtr)dataBuffer.Length,
                out length);
            if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
            {
                Diagnostics.HandleResult(rc, this, outerCommand.Connection);
            }

            if (length == (IntPtr)(int)CLI.LengthCode.SQL_NULL_DATA)
            {
                columnData = DBNull.Value;
            }
            else if ((int)length <= (dataBuffer.Length - type.NullTermSize) && (int)length >= 0)
            {
                columnData = type.NativeToManaged(dataBuffer.Handle, (int)length);
            }
            else
            {
                column.lobOffset = 0;
                column.lobLength = (int)length;
                columnData       = type.NativeSizeToManaged(column.lobLength);
                type.NativePartToManaged(dataBuffer.Handle, dataBuffer.Length - type.NullTermSize, columnData, ref column.lobOffset);
                for (;;)
                {
                    rc = (CLI.ReturnCode)CLI.SQLGetData(
                        hstmt,
                        (ushort)(i + 1),
                        (short)column.bufferType.sqlCType,
                        dataBuffer.Handle,
                        (IntPtr)dataBuffer.Length,
                        out length);
                    if (rc == CLI.ReturnCode.SQL_NO_DATA)
                    {
                        break;
                    }
                    if (rc != CLI.ReturnCode.SQL_SUCCESS && rc != CLI.ReturnCode.SQL_SUCCESS_WITH_INFO)
                    {
                        Diagnostics.HandleResult(rc, this, outerCommand.Connection);
                    }

                    int copyLength = (int)length;
                    if (copyLength > dataBuffer.Length - type.NullTermSize)
                    {
                        copyLength = dataBuffer.Length - type.NullTermSize;
                    }
                    type.NativePartToManaged(dataBuffer.Handle, copyLength, columnData, ref column.lobOffset);
                }
                if (column.columnType.GetType() == typeof(DataTypeChar) ||
                    column.columnType.GetType() == typeof(DataTypeWide))
                {
                    columnData = new String((char [])columnData);
                }
            }

            GC.KeepAlive(this);
            return(columnData);
        }