Exemplo n.º 1
0
        internal static IntPtr SetupPGConn(PqsqlConnectionStringBuilder connStringBuilder, out ConnStatusType connStatus, out PGTransactionStatusType tranStatus)
        {
#if CODECONTRACTS
            Contract.Requires <ArgumentNullException>(connStringBuilder != null);
#else
            if (connStringBuilder == null)
            {
                throw new ArgumentNullException(nameof(connStringBuilder));
            }
#endif

            // setup null-terminated key-value arrays for the connection
            string[] keys = new string[connStringBuilder.Keys.Count + 1];
            string[] vals = new string[connStringBuilder.Values.Count + 1];

            // get keys and values from PqsqlConnectionStringBuilder
            connStringBuilder.Keys.CopyTo(keys, 0);
            connStringBuilder.Values.CopyTo(vals, 0);

            // now create connection
            IntPtr conn = PqsqlWrapper.PQconnectdbParams(keys, vals, 0);

            if (conn == IntPtr.Zero)
            {
                connStatus = ConnStatusType.CONNECTION_BAD;
                tranStatus = PGTransactionStatusType.PQTRANS_UNKNOWN;
                return(conn);
            }

            // always force client_encoding to utf8
            int client_encoding = PqsqlWrapper.PQclientEncoding(conn);

            if (client_encoding == (int)PgEnc.PG_UTF8)              // done
            {
                connStatus = PqsqlWrapper.PQstatus(conn);
                tranStatus = PqsqlWrapper.PQtransactionStatus(conn);
            }
            else if (client_encoding == -1)             // bail out, but keep connection object
            {
                connStatus = ConnStatusType.CONNECTION_BAD;
                tranStatus = PGTransactionStatusType.PQTRANS_UNKNOWN;
            }
            else                                                                    // try to set client_encoding to utf8
            {
                if (PqsqlWrapper.PQsetClientEncoding(conn, PgEncName.PG_UTF8) == 0) // success
                {
                    connStatus = PqsqlWrapper.PQstatus(conn);
                    tranStatus = PqsqlWrapper.PQtransactionStatus(conn);
                }
                else                 // bail out, but keep connection object
                {
                    connStatus = ConnStatusType.CONNECTION_BAD;
                    tranStatus = PGTransactionStatusType.PQTRANS_UNKNOWN;
                }
            }

            return(conn);
        }
Exemplo n.º 2
0
        public void PqsqlConnectionTest10()
        {
            PqsqlConnectionPool.Clear();

            PqsqlConnection connection = new PqsqlConnection(connectionString);

            connection.Open();
            Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state");

            IntPtr pgConn          = connection.PGConnection;
            int    client_encoding = PqsqlWrapper.PQclientEncoding(pgConn);

            Assert.AreEqual((int)PgEnc.PG_UTF8, client_encoding, "wrong client_encoding");

            // setup client_encoding to sqlascii, and leave it like that

            byte[] encoding = PqsqlUTF8Statement.CreateUTF8Statement("sqlascii");
            client_encoding = PqsqlWrapper.PQsetClientEncoding(pgConn, encoding);
            Assert.AreEqual(0, client_encoding, "could not set client_encoding");

            client_encoding = PqsqlWrapper.PQclientEncoding(pgConn);
            Assert.AreEqual(0, client_encoding, "wrong client_encoding");

            connection.Close();

            // now try to get the same connection again

            connection = new PqsqlConnection(connectionString);

            connection.Open();
            Assert.AreEqual(ConnectionState.Open, connection.State, "wrong connection state");

            pgConn          = connection.PGConnection;
            client_encoding = PqsqlWrapper.PQclientEncoding(pgConn);
            Assert.AreEqual((int)PgEnc.PG_UTF8, client_encoding, "wrong client_encoding");
        }
Exemplo n.º 3
0
        private static bool CheckOrRelease(IntPtr pgConn, out ConnStatusType connStatus, out PGTransactionStatusType tranStatus)
        {
            if (pgConn == IntPtr.Zero)
            {
                connStatus = ConnStatusType.CONNECTION_BAD;
                tranStatus = PGTransactionStatusType.PQTRANS_UNKNOWN;
                return(false);
            }

            // is connection reusable?
            connStatus = PqsqlWrapper.PQstatus(pgConn);
            if (connStatus != ConnStatusType.CONNECTION_OK)
            {
                goto broken;
            }

            tranStatus = PqsqlWrapper.PQtransactionStatus(pgConn);
            if (tranStatus != PGTransactionStatusType.PQTRANS_IDLE)
            {
                goto broken;
            }

            //
            // now check the connection: first try to fix the client encoding if it is not utf8 for some reason.
            // if client_encoding is utf8, we send the empty query to check whether the socket is still usable
            // for the backend communiction. here, we could make use of tcp_keepalive settings.
            //
            int client_encoding = PqsqlWrapper.PQclientEncoding(pgConn);

            if (client_encoding == -1)
            {
                goto broken;
            }

            if (client_encoding == (int)PgEnc.PG_UTF8)              // client_encoding == utf8
            {
                // send empty query to test whether we are really connected (tcp_keepalive might have closed socket)
                unsafe
                {
                    byte[] empty = { 0 };                   // empty query string

                    fixed(byte *eq = empty)
                    {
                        if (PqsqlWrapper.PQsendQuery(pgConn, eq) == 0)                         // could not send query
                        {
                            goto broken;
                        }
                    }
                }

                // Reading result: consume and clear remaining results until we reach the NULL result.
                // PQgetResult will block here
                IntPtr         res;
                ExecStatusType st = ExecStatusType.PGRES_EMPTY_QUERY;
                while ((res = PqsqlWrapper.PQgetResult(pgConn)) != IntPtr.Zero)
                {
                    ExecStatusType st0 = PqsqlWrapper.PQresultStatus(res);

                    if (st0 != ExecStatusType.PGRES_EMPTY_QUERY)
                    {
                        st = st0;
                    }

                    // always free res
                    PqsqlWrapper.PQclear(res);
                }

                if (st != ExecStatusType.PGRES_EMPTY_QUERY)                 // received wrong exec status
                {
                    goto broken;
                }
            }
            else             // set client_encoding to utf8
            {
                // set client_encoding to test whether we are really connected (tcp_keepalive might have closed socket)
                if (PqsqlWrapper.PQsetClientEncoding(pgConn, PgEncName.PG_UTF8) != 0)
                {
                    goto broken;
                }
            }

            return(true);            // successfully reused connection

broken:
            // reconnect with current connection setting
            PqsqlWrapper.PQreset(pgConn);

            connStatus = PqsqlWrapper.PQstatus(pgConn);
            if (connStatus == ConnStatusType.CONNECTION_OK)
            {
                tranStatus = PqsqlWrapper.PQtransactionStatus(pgConn);
                if (tranStatus == PGTransactionStatusType.PQTRANS_IDLE)
                {
                    return(true);                    // successfully reconnected
                }
            }
            else
            {
                tranStatus = PGTransactionStatusType.PQTRANS_UNKNOWN;
            }

            // could not reconnect: finally give up and clean up memory
            PqsqlWrapper.PQfinish(pgConn);
            return(false);
        }