+++ /dev/null
-/* File: libpqconnection.h
- *
- * Description: See "libpqconnection.c"
- *
- * Comments: See "notice.txt" for copyright and license information.
- *
- */
-
-#ifdef USE_LIBPQ
-
-#ifndef __LIBPQCONNECTION_H__
-#define __LIBPQCONNECTION_H__
-
-#include "psqlodbc.h"
-#include <libpq-fe.h>
-#include <stdlib.h>
-#include <string.h>
-#include "descriptor.h"
-
-#if defined (POSIX_MULTITHREAD_SUPPORT)
-#include <pthread.h>
-#endif
-
-#if !defined WIN32 && defined HAVE_SYS_UN_H && !defined HAVE_UNIX_SOCKETS
-#define HAVE_UNIX_SOCKETS
-#endif
-
-/* These errors have general sql error state */
-#define CONNECTION_SERVER_NOT_REACHED 101
-#define CONNECTION_MSG_TOO_LONG 103
-#define CONNECTION_COULD_NOT_SEND 104
-#define CONNECTION_NO_SUCH_DATABASE 105
-#define CONNECTION_BACKEND_CRAZY 106
-#define CONNECTION_NO_RESPONSE 107
-#define CONNECTION_SERVER_REPORTED_ERROR 108
-#define CONNECTION_COULD_NOT_RECEIVE 109
-#define CONNECTION_SERVER_REPORTED_WARNING 110
-#define CONNECTION_NEED_PASSWORD 112
-#define CONNECTION_COULD_NOT_ESTABLISH 113
-
-/* These errors correspond to specific SQL states */
-#define CONN_INIREAD_ERROR 201
-#define CONN_OPENDB_ERROR 202
-#define CONN_STMT_ALLOC_ERROR 203
-#define CONN_IN_USE 204
-#define CONN_UNSUPPORTED_OPTION 205
-/* Used by SetConnectoption to indicate unsupported options */
-#define CONN_INVALID_ARGUMENT_NO 206
-/* SetConnectOption: corresponds to ODBC--"S1009" */
-#define CONN_TRANSACT_IN_PROGRES 207
-#define CONN_NO_MEMORY_ERROR 208
-#define CONN_NOT_IMPLEMENTED_ERROR 209
-#define CONN_INVALID_AUTHENTICATION 210
-#define CONN_AUTH_TYPE_UNSUPPORTED 211
-#define CONN_UNABLE_TO_LOAD_DLL 212
-
-#define CONN_OPTION_VALUE_CHANGED 213
-#define CONN_VALUE_OUT_OF_RANGE 214
-
-#define CONN_TRUNCATED 215
-
-
-#define CONN_MEMORY_ALLOCATION_FAILED 301
-#define COULD_NOT_GET_RESULT_BACK 302
-
-/* Conn_status defines */
-#define CONN_IN_AUTOCOMMIT 1L
-#define CONN_IN_TRANSACTION (1L<<1)
-#define CONN_IN_MANUAL_TRANSACTION (1L<<2)
-#define CONN_IN_ERROR_BEFORE_IDLE (1L<<3)
-
-/* AutoCommit functions */
-#define CC_set_autocommit_off(x) (x->transact_status &= ~CONN_IN_AUTOCOMMIT)
-#define CC_set_autocommit_on(x) (x->transact_status |= CONN_IN_AUTOCOMMIT)
-#define CC_is_in_autocommit(x) (x->transact_status & CONN_IN_AUTOCOMMIT)
-
-/* Transaction in/not functions */
-#define CC_set_in_trans(x) (x->transact_status |= CONN_IN_TRANSACTION)
-#define CC_set_no_trans(x) (x->transact_status &= ~(CONN_IN_TRANSACTION | CONN_IN_ERROR_BEFORE_IDLE))
-#define CC_is_in_trans(x) (x->transact_status & CONN_IN_TRANSACTION)
-
-/* Manual transaction in/not functions */
-#define CC_set_in_manual_trans(x) (x->transact_status |= CONN_IN_MANUAL_TRANSACTION)
-#define CC_set_no_manual_trans(x) (x->transact_status &= ~CONN_IN_MANUAL_TRANSACTION)
-#define CC_is_in_manual_trans(x) (x->transact_status & CONN_IN_MANUAL_TRANSACTION)
-
-/* Error waiting for ROLLBACK */
-#define CC_set_in_error_trans(x) (x->transact_status |= CONN_IN_ERROR_BEFORE_IDLE)
-#define CC_set_no_error_trans(x) (x->transact_status &= ~CONN_IN_ERROR_BEFORE_IDLE)
-#define CC_is_in_error_trans(x) (x->transact_status & CONN_IN_ERROR_BEFORE_IDLE)
-
-#define CC_get_errornumber(x) (x->__error_number)
-#define CC_get_errormsg(x) (x->__error_message)
-#define CC_set_errornumber(x, n) (x->__error_number = n)
-
-#define CC_MALLOC_return_with_error(t, tp, s, x, m, ret) \
- { \
- if (t = malloc(s), NULL == t) \
- { \
- CC_set_error(x, CONN_NO_MEMORY_ERROR, m); \
- return ret; \
- } \
- }
-#define CC_REALLOC_return_with_error(t, tp, s, x, m, ret) \
- { \
- if (t = (tp *) realloc(t, s), NULL == t) \
- { \
- CC_set_error(x, CONN_NO_MEMORY_ERROR, m); \
- return ret; \
- } \
- }
-
-/* For Multi-thread */
-#if defined(WIN_MULTITHREAD_SUPPORT)
-#define INIT_CONN_CS(x) InitializeCriticalSection(&((x)->cs))
-#define ENTER_CONN_CS(x) EnterCriticalSection(&((x)->cs))
-#define ENTER_INNER_CONN_CS(x, entered) \
- { EnterCriticalSection(&((x)->cs)); entered++; }
-#define LEAVE_CONN_CS(x) LeaveCriticalSection(&((x)->cs))
-#define DELETE_CONN_CS(x) DeleteCriticalSection(&((x)->cs))
-#elif defined(POSIX_THREADMUTEX_SUPPORT)
-#define INIT_CONN_CS(x) pthread_mutex_init(&((x)->cs), getMutexAttr())
-#define ENTER_CONN_CS(x) pthread_mutex_lock(&((x)->cs))
-#define ENTER_INNER_CONN_CS(x, entered) \
- { \
- if (getMutexAttr()) \
- { \
- if (pthread_mutex_lock(&((x)->cs)) == 0) \
- entered++; \
- else \
- -1; \
- } \
- else \
- 0; \
- }
-#define LEAVE_CONN_CS(x) pthread_mutex_unlock(&((x)->cs))
-#define DELETE_CONN_CS(x) pthread_mutex_destroy(&((x)->cs))
-#else
-#define INIT_CONN_CS(x)
-#define ENTER_CONN_CS(x)
-#define ENTER_INNER_CONN_CS(x, entered) (0)
-#define LEAVE_CONN_CS(x)
-#define DELETE_CONN_CS(x)
-#endif /* WIN_MULTITHREAD_SUPPORT */
-
-#define LEAVE_INNER_CONN_CS(entered, conn) \
- { \
- if (entered > 0) \
- { \
- LEAVE_CONN_CS(conn); \
- entered--; \
- } \
- }
-#define CLEANUP_FUNC_CONN_CS(entered, conn) \
- while (entered > 0) \
- { \
- LEAVE_CONN_CS(conn); \
- entered--; \
- }
-
-
-#define AUTH_REQ_OK 0
-#define AUTH_REQ_KRB4 1
-#define AUTH_REQ_KRB5 2
-#define AUTH_REQ_PASSWORD 3
-#define AUTH_REQ_CRYPT 4
-#define AUTH_REQ_MD5 5
-#define AUTH_REQ_SCM_CREDS 6
-
-/* Old 6.2 protocol defines */
-#define NO_AUTHENTICATION 7
-#define PATH_SIZE 64
-#define ARGV_SIZE 64
-#define USRNAMEDATALEN 16
-
-typedef unsigned int ProtocolVersion;
-
-#define PG_PROTOCOL(major, minor) (((major) << 16) | (minor))
-#define PG_PROTOCOL_LATEST PG_PROTOCOL(2, 0)
-#define PG_PROTOCOL_63 PG_PROTOCOL(1, 0)
-#define PG_PROTOCOL_62 PG_PROTOCOL(0, 0)
-
-
-typedef enum
-{
- CONN_NOT_CONNECTED, /* Connection has not been established */
- CONN_CONNECTED, /* Connection is up and has been
- * established */
- CONN_DOWN, /* Connection is broken */
- CONN_EXECUTING /* the connection is currently executing a
- * statement */
-} CONN_Status;
-
-/* Transferred from pqcomm.h: */
-
-
-typedef ProtocolVersion MsgType;
-
-/* Structure to hold all the connection attributes for a specific
- connection (used for both registry and file, DSN and DRIVER) */
-
-typedef struct
-{
- char dsn[MEDIUM_REGISTRY_LEN];
- char desc[MEDIUM_REGISTRY_LEN];
- char drivername[MEDIUM_REGISTRY_LEN];
- char server[MEDIUM_REGISTRY_LEN];
- char database[MEDIUM_REGISTRY_LEN];
- char username[MEDIUM_REGISTRY_LEN];
- char password[MEDIUM_REGISTRY_LEN];
- char conn_settings[LARGE_REGISTRY_LEN];
- char protocol[SMALL_REGISTRY_LEN];
- char port[SMALL_REGISTRY_LEN];
- char sslmode[MEDIUM_REGISTRY_LEN];
- char onlyread[SMALL_REGISTRY_LEN];
- char fake_oid_index[SMALL_REGISTRY_LEN];
- char show_oid_column[SMALL_REGISTRY_LEN];
- char row_versioning[SMALL_REGISTRY_LEN];
- char show_system_tables[SMALL_REGISTRY_LEN];
- char translation_dll[MEDIUM_REGISTRY_LEN];
- char translation_option[SMALL_REGISTRY_LEN];
- char focus_password;
- signed char disallow_premature;
- signed char allow_keyset;
- signed char updatable_cursors;
- signed char lf_conversion;
- signed char true_is_minus1;
- signed char int8_as;
- signed char bytea_as_longvarbinary;
- signed char use_server_side_prepare;
- signed char lower_case_identifier;
- GLOBAL_VALUES drivers; /* moved from driver's option */
-} ConnInfo;
-
-
-/* Macro to determine is the connection using 6.2 protocol? */
-#define PROTOCOL_62(conninfo_) (strncmp((conninfo_)->protocol, PG62, strlen(PG62)) == 0)
-
-/* Macro to determine is the connection using 6.3 protocol? */
-#define PROTOCOL_63(conninfo_) (strncmp((conninfo_)->protocol, PG63, strlen(PG63)) == 0)
-
-/*
- * Macros to compare the server's version with a specified version
- * 1st parameter: pointer to a ConnectionClass object
- * 2nd parameter: major version number
- * 3rd parameter: minor version number
- */
-#define SERVER_VERSION_GT(conn, major, minor) \
- ((conn)->pg_version_major > major || \
- ((conn)->pg_version_major == major && (conn)->pg_version_minor > minor))
-#define SERVER_VERSION_GE(conn, major, minor) \
- ((conn)->pg_version_major > major || \
- ((conn)->pg_version_major == major && (conn)->pg_version_minor >= minor))
-#define SERVER_VERSION_EQ(conn, major, minor) \
- ((conn)->pg_version_major == major && (conn)->pg_version_minor == minor)
-#define SERVER_VERSION_LE(conn, major, minor) (! SERVER_VERSION_GT(conn, major, minor))
-#define SERVER_VERSION_LT(conn, major, minor) (! SERVER_VERSION_GE(conn, major, minor))
-/*#if ! defined(HAVE_CONFIG_H) || defined(HAVE_STRINGIZE)*/
-#define STRING_AFTER_DOT(string) (strchr(#string, '.') + 1)
-/*#else
-#define STRING_AFTER_DOT(str) (strchr("str", '.') + 1)
-#endif*/
-/*
- * Simplified macros to compare the server's version with a
- * specified version
- * Note: Never pass a variable as the second parameter.
- * It must be a decimal constant of the form %d.%d .
- */
-#define PG_VERSION_GT(conn, ver) \
- (SERVER_VERSION_GT(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
-#define PG_VERSION_GE(conn, ver) \
- (SERVER_VERSION_GE(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
-#define PG_VERSION_EQ(conn, ver) \
- (SERVER_VERSION_EQ(conn, (int) ver, atoi(STRING_AFTER_DOT(ver))))
-#define PG_VERSION_LE(conn, ver) (! PG_VERSION_GT(conn, ver))
-#define PG_VERSION_LT(conn, ver) (! PG_VERSION_GE(conn, ver))
-
-/* This is used to store cached table information in the connection */
-struct col_info
-{
- QResultClass *result;
- char *schema;
- char name[TABLE_NAME_STORAGE_LEN + 1];
-};
-
- /* Translation DLL entry points */
-#ifdef WIN32
-#define DLLHANDLE HINSTANCE
-#else
-#define WINAPI CALLBACK
-#define DLLHANDLE void *
-#define HINSTANCE void *
-#endif
-
-typedef BOOL (FAR WINAPI * DataSourceToDriverProc)
- (UDWORD,SWORD,PTR,SDWORD,PTR,SDWORD,SDWORD FAR *,UCHAR FAR *,SWORD,SWORD FAR *);
-
-typedef BOOL (FAR WINAPI * DriverToDataSourceProc)
- (UDWORD,SWORD,PTR,SDWORD,PTR,SDWORD,SDWORD FAR *,UCHAR FAR *,SWORD,SWORD FAR *);
-
- /******* The Connection handle ************/
-struct ConnectionClass_
-{
- HENV henv; /* environment this connection was created
- * on */
- StatementOptions stmtOptions;
- ARDFields ardOptions;
- APDFields apdOptions;
- char *__error_message;
- int __error_number;
- CONN_Status status;
- ConnInfo connInfo;
- StatementClass **stmts;
- int num_stmts;
- PGconn *pgconn;
- int lobj_type;
- int ntables;
- COL_INFO **col_info;
- UDWORD translation_option;
- HINSTANCE translation_handle;
- DataSourceToDriverProc DataSourceToDriver;
- DriverToDataSourceProc DriverToDataSource;
- Int2 driver_version; /* prepared for ODBC3.0 */
- char transact_status;/* Is a transaction is currently in
- * progress */
- char errormsg_created; /* has an informative error msg
- * been created? */
- char pg_version[MAX_INFO_STRING]; /* Version of PostgreSQL
- * we're connected to -
- * DJP 25-1-2001 */
- float pg_version_number;
- Int2 pg_version_major;
- Int2 pg_version_minor;
- char ms_jet;
- char unicode;
- char result_uncommitted;
- char schema_support;
- char *client_encoding;
- char *server_encoding;
- int ccsc;
- int be_pid; /* pid returned by backend */
- int be_key; /* auth code needed to send cancel */
- UInt4 isolation;
- char *current_schema;
- int num_discardp;
- char **discardp;
- int num_descs;
- DescriptorClass **descs;
-#if defined(WIN_MULTITHREAD_SUPPORT)
- CRITICAL_SECTION cs;
-#elif defined(POSIX_THREADMUTEX_SUPPORT)
- pthread_mutex_t cs;
-#endif /* WIN_MULTITHREAD_SUPPORT */
-};
-
-
-/* Accessor functions */
-#define CC_get_database(x) (x->connInfo.database)
-#define CC_get_server(x) (x->connInfo.server)
-#define CC_get_DSN(x) (x->connInfo.dsn)
-#define CC_get_username(x) (x->connInfo.username)
-#define CC_is_onlyread(x) (x->connInfo.onlyread[0] == '1')
-
-/* for CC_DSN_info */
-#define CONN_DONT_OVERWRITE 0
-#define CONN_OVERWRITE 1
-
-
-/* prototypes */
-ConnectionClass *CC_Constructor(void);
-void CC_conninfo_init(ConnInfo *conninfo);
-char CC_Destructor(ConnectionClass *self);
-int CC_cursor_count(ConnectionClass *self);
-char CC_cleanup(ConnectionClass *self);
-char CC_begin(ConnectionClass *self);
-char CC_commit(ConnectionClass *self);
-char CC_abort(ConnectionClass *self);
-int CC_set_translation(ConnectionClass *self);
-char CC_connect(ConnectionClass *self, char password_req, char *salt);
-char CC_add_statement(ConnectionClass *self, StatementClass *stmt);
-char CC_remove_statement(ConnectionClass *self, StatementClass *stmt);
-char CC_add_descriptor(ConnectionClass *self, DescriptorClass *desc);
-char CC_remove_descriptor(ConnectionClass *self, DescriptorClass *desc);
-void CC_set_error(ConnectionClass *self, int number, const char *message);
-void CC_set_errormsg(ConnectionClass *self, const char *message);
-char CC_get_error(ConnectionClass *self, int *number, char **message);
-QResultClass *CC_send_query(ConnectionClass *self, char *query, QueryInfo *qi, UDWORD flag);
-void CC_clear_error(ConnectionClass *self);
-char *CC_create_errormsg(ConnectionClass *self);
-int CC_send_function(ConnectionClass *conn, int fnid, void *result_buf, int *actual_result_len, int result_is_int, LO_ARG *argv, int nargs);
-char CC_send_settings(ConnectionClass *self);
-void CC_lookup_lo(ConnectionClass *conn);
-void CC_lookup_pg_version(ConnectionClass *conn);
-void CC_initialize_pg_version(ConnectionClass *conn);
-void CC_log_error(const char *func, const char *desc, const ConnectionClass *self);
-int CC_get_max_query_len(const ConnectionClass *self);
-int CC_send_cancel_request(const ConnectionClass *conn);
-void CC_on_commit(ConnectionClass *conn);
-void CC_on_abort(ConnectionClass *conn, UDWORD opt);
-void ProcessRollback(ConnectionClass *conn, BOOL undo);
-const char *CC_get_current_schema(ConnectionClass *conn);
-int CC_mark_a_plan_to_discard(ConnectionClass *conn, const char *plannm);
-int CC_discard_marked_plans(ConnectionClass *conn);
-
-/* Accessor functions*/
-PGconn *LIBPQ_Constructor();
-void LIBPQ_Destructor(PGconn *pgconn);
-int LIBPQ_connect(ConnectionClass *self);
-QResultClass *LIBPQ_execute_query(ConnectionClass *self,char *query);
-QResultClass *CC_mapping(ConnectionClass *self,PGresult *pgres,QResultClass *qres);
-void CC_is_server_alive(ConnectionClass *conn);
-/* CC_send_query options */
-#define CLEAR_RESULT_ON_ABORT 1L
-#define CREATE_KEYSET (1L << 1) /* create keyset for updatable curosrs */
-#define GO_INTO_TRANSACTION (1L << 2) /* issue begin in advance */
-/* CC_on_abort options */
-#define NO_TRANS 1L
-#define CONN_DEAD (1L << 1) /* connection is no longer valid */
-
-#endif /* __LIBPQCONNECTION_H__ */
-
-#endif /* USE_LIBPQ */
+++ /dev/null
-/*--------
- * Module: lobj.c
- *
- * Description: This module contains routines related to manipulating
- * large objects.
- *
- * Classes: none
- *
- * API functions: none
- *
- * Comments: See "notice.txt" for copyright and license information.
- *--------
- */
-
-#ifndef USE_LIBPQ
-
-#include "lobj.h"
-
-#include "connection.h"
-
-
-Oid
-lo_creat(ConnectionClass *conn, int mode)
-{
- LO_ARG argv[1];
- int retval,
- result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = mode;
-
- if (!CC_send_function(conn, LO_CREAT, &retval, &result_len, 1, argv, 1))
- return 0; /* invalid oid */
- else
- return retval;
-}
-
-
-int
-lo_open(ConnectionClass *conn, int lobjId, int mode)
-{
- int fd;
- int result_len;
- LO_ARG argv[2];
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = lobjId;
-
- argv[1].isint = 1;
- argv[1].len = 4;
- argv[1].u.integer = mode;
-
- if (!CC_send_function(conn, LO_OPEN, &fd, &result_len, 1, argv, 2))
- return -1;
-
- if (fd >= 0 && lo_lseek(conn, fd, 0L, SEEK_SET) < 0)
- return -1;
-
- return fd;
-}
-
-
-int
-lo_close(ConnectionClass *conn, int fd)
-{
- LO_ARG argv[1];
- int retval,
- result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = fd;
-
- if (!CC_send_function(conn, LO_CLOSE, &retval, &result_len, 1, argv, 1))
- return -1;
- else
- return retval;
-}
-
-
-int
-lo_read(ConnectionClass *conn, int fd, char *buf, int len)
-{
- LO_ARG argv[2];
- int result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = fd;
-
- argv[1].isint = 1;
- argv[1].len = 4;
- argv[1].u.integer = len;
-
- if (!CC_send_function(conn, LO_READ, (int *) buf, &result_len, 0, argv, 2))
- return -1;
- else
- return result_len;
-}
-
-
-int
-lo_write(ConnectionClass *conn, int fd, char *buf, int len)
-{
- LO_ARG argv[2];
- int retval,
- result_len;
-
- if (len <= 0)
- return 0;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = fd;
-
- argv[1].isint = 0;
- argv[1].len = len;
- argv[1].u.ptr = (char *) buf;
-
- if (!CC_send_function(conn, LO_WRITE, &retval, &result_len, 1, argv, 2))
- return -1;
- else
- return retval;
-}
-
-
-int
-lo_lseek(ConnectionClass *conn, int fd, int offset, int whence)
-{
- LO_ARG argv[3];
- int retval,
- result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = fd;
-
- argv[1].isint = 1;
- argv[1].len = 4;
- argv[1].u.integer = offset;
-
- argv[2].isint = 1;
- argv[2].len = 4;
- argv[2].u.integer = whence;
-
- if (!CC_send_function(conn, LO_LSEEK, &retval, &result_len, 1, argv, 3))
- return -1;
- else
- return retval;
-}
-
-
-int
-lo_tell(ConnectionClass *conn, int fd)
-{
- LO_ARG argv[1];
- int retval,
- result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = fd;
-
- if (!CC_send_function(conn, LO_TELL, &retval, &result_len, 1, argv, 1))
- return -1;
- else
- return retval;
-}
-
-
-int
-lo_unlink(ConnectionClass *conn, Oid lobjId)
-{
- LO_ARG argv[1];
- int retval,
- result_len;
-
- argv[0].isint = 1;
- argv[0].len = 4;
- argv[0].u.integer = lobjId;
-
- if (!CC_send_function(conn, LO_UNLINK, &retval, &result_len, 1, argv, 1))
- return -1;
- else
- return retval;
-}
-
-#endif /* USE_LIBPQ */
+++ /dev/null
-/* File: lobj.h
- *
- * Description: See "lobj.c"
- *
- * Comments: See "notice.txt" for copyright and license information.
- *
- */
-
-#ifndef __LOBJ_H__
-#define __LOBJ_H__
-
-
-#include "psqlodbc.h"
-
-struct lo_arg
-{
- int isint;
- int len;
- union
- {
- int integer;
- char *ptr;
- } u;
-};
-
-#define LO_CREAT 957
-#define LO_OPEN 952
-#define LO_CLOSE 953
-#define LO_READ 954
-#define LO_WRITE 955
-#define LO_LSEEK 956
-#define LO_TELL 958
-#define LO_UNLINK 964
-
-#define INV_WRITE 0x00020000
-#define INV_READ 0x00040000
-
-#ifndef USE_LIBPQ
-
-Oid lo_creat(ConnectionClass *conn, int mode);
-int lo_open(ConnectionClass *conn, int lobjId, int mode);
-int lo_close(ConnectionClass *conn, int fd);
-int lo_read(ConnectionClass *conn, int fd, char *buf, int len);
-int lo_write(ConnectionClass *conn, int fd, char *buf, int len);
-int lo_lseek(ConnectionClass *conn, int fd, int offset, int len);
-int lo_tell(ConnectionClass *conn, int fd);
-int lo_unlink(ConnectionClass *conn, Oid lobjId);
-
-#endif /*USE_LIBPQ */
-
-#endif
+++ /dev/null
-/*-------
- * Module: socket.c
- *
- * Description: This module contains functions for low level socket
- * operations (connecting/reading/writing to the backend)
- *
- * Classes: SocketClass (Functions prefix: "SOCK_")
- *
- * API functions: none
- *
- * Comments: See "notice.txt" for copyright and license information.
- *-------
- */
-
-#ifndef USE_LIBPQ
-
-#include "socket.h"
-
-#include "connection.h"
-
-#ifndef WIN32
-#include <stdlib.h>
-#include <string.h> /* for memset */
-#endif /* WIN32 */
-
-extern GLOBAL_VALUES globals;
-
-#ifndef BOOL
-#define BOOL int
-#endif
-#ifndef TRUE
-#define TRUE (BOOL)1
-#endif
-#ifndef FALSE
-#define FALSE (BOOL)0
-#endif
-
-
-void
-SOCK_clear_error(SocketClass *self)
-{
- self->errornumber = 0;
- self->errormsg = NULL;
-}
-
-
-SocketClass *
-SOCK_Constructor(const ConnectionClass *conn)
-{
- SocketClass *rv;
-
- rv = (SocketClass *) malloc(sizeof(SocketClass));
-
- if (rv != NULL)
- {
- rv->socket = (SOCKETFD) - 1;
- rv->buffer_filled_in = 0;
- rv->buffer_filled_out = 0;
- rv->buffer_read_in = 0;
-
- if (rv)
- rv->buffer_size = conn->connInfo.drivers.socket_buffersize;
- else
- rv->buffer_size = globals.socket_buffersize;
- rv->buffer_in = (UCHAR *) malloc(rv->buffer_size);
- if (!rv->buffer_in)
- {
- free(rv);
- return NULL;
- }
-
- rv->buffer_out = (UCHAR *) malloc(rv->buffer_size);
- if (!rv->buffer_out)
- {
- free(rv->buffer_in);
- free(rv);
- return NULL;
- }
- rv->sadr = NULL;
- rv->errormsg = NULL;
- rv->errornumber = 0;
- rv->reverse = FALSE;
- }
- return rv;
-}
-
-
-void
-SOCK_Destructor(SocketClass *self)
-{
- mylog("SOCK_Destructor\n");
- if (!self)
- return;
- if (self->socket != -1)
- {
- SOCK_put_char(self, 'X');
- SOCK_flush_output(self);
- closesocket(self->socket);
- }
-
- if (self->buffer_in)
- free(self->buffer_in);
-
- if (self->buffer_out)
- free(self->buffer_out);
- if (self->sadr != (struct sockaddr *) &(self->sadr_in))
- free(self->sadr);
-
- free(self);
-}
-
-
-char
-SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname
-#ifdef HAVE_UNIX_SOCKETS
- , char *uds /* unix domain socket path */
-#endif
- )
-{
-#if defined (POSIX_MULTITHREAD_SUPPORT)
- const int bufsz = 8192;
- char buf[bufsz];
- int error = 0;
- struct hostent host;
- struct hostent* hp = &host;
-#else
- struct hostent* hp;
-#endif /* POSIX_MULTITHREAD_SUPPORT */
- struct sockaddr_in *in;
-#ifdef HAVE_UNIX_SOCKETS
- struct sockaddr_un *un;
-#endif /* HAVE_UNIX_SOCKETS */
- int family, sLen;
-#ifdef WIN32
- UInt4 iaddr;
-#else
- in_addr_t iaddr;
-#endif
-
- if (self->socket != -1)
- {
- self->errornumber = SOCKET_ALREADY_CONNECTED;
- self->errormsg = "Socket is already connected";
- return 0;
- }
-
-
- /*
- * If it is a valid IP address, use it. Otherwise use AF_UNIX socket.
- */
- if (hostname && hostname[0])
- {
- iaddr = inet_addr(hostname);
- memset((char *) &(self->sadr_in), 0, sizeof(self->sadr_in));
- in = &(self->sadr_in);
- in->sin_family = family = AF_INET;
- in->sin_port = htons(port);
- sLen = sizeof(self->sadr_in);
- if (iaddr == INADDR_NONE)
- {
-#if defined (POSIX_MULTITHREAD_SUPPORT)
- #if defined (HAVE_GETIPNODEBYNAME) /* Free-BSD ? */
- hp = getipnodebyname(hostname, AF_INET, 0, &error);
- #elif defined (PGS_REENTRANT_API_1) /* solaris, irix */
- hp = gethostbyname_r(hostname, hp, buf, bufsz, &error);
- #elif defined (PGS_REENTRANT_API_2) /* linux */
- int result = 0;
- result = gethostbyname_r(hostname, hp, buf, bufsz, &hp, &error);
- if (result)
- hp = NULL;
- #else
- hp = gethostbyname(hostname);
- #endif /* HAVE_GETIPNODEBYNAME */
-#else
- hp = gethostbyname(hostname);
-#endif /* POSIX_MULTITHREAD_SUPPORT */
- if (hp == NULL)
- {
- self->errornumber = SOCKET_HOST_NOT_FOUND;
- self->errormsg = "Could not resolve hostname.";
- return 0;
- }
- memcpy(&(in->sin_addr), hp->h_addr, hp->h_length);
- }
- else
- memcpy(&(in->sin_addr), (struct in_addr *) & iaddr, sizeof(iaddr));
- self->sadr = (struct sockaddr *) in;
-
-#if defined (HAVE_GETIPNODEBYNAME)
- freehostent(hp);
-#endif /* HAVE_GETIPNODEBYNAME */
- }
- else
-#ifdef HAVE_UNIX_SOCKETS
- {
- un = (struct sockaddr_un *) malloc(sizeof(struct sockaddr_un));
- if (!un)
- {
- self->errornumber = SOCKET_COULD_NOT_CREATE_SOCKET;
- self->errormsg = "coulnd't allocate memory for un.";
- return 0;
- }
- un->sun_family = family = AF_UNIX;
- /* passing NULL means that this only suports the pg default "/tmp" */
- UNIXSOCK_PATH(un, port, uds);
- sLen = UNIXSOCK_LEN(un);
- self->sadr = (struct sockaddr *) un;
- }
-#else
- {
- self->errornumber = SOCKET_HOST_NOT_FOUND;
- self->errormsg = "Hostname isn't specified.";
- return 0;
- }
-#endif /* HAVE_UNIX_SOCKETS */
-
- self->socket = socket(family, SOCK_STREAM, 0);
- if (self->socket == -1)
- {
- self->errornumber = SOCKET_COULD_NOT_CREATE_SOCKET;
- self->errormsg = "Could not create Socket.";
- return 0;
- }
-#ifdef TCP_NODELAY
- if (family == AF_INET)
- {
- int i, len;
-
- i = 1;
- len = sizeof(i);
- if (setsockopt(self->socket, IPPROTO_TCP, TCP_NODELAY, (char *) &i, len) < 0)
- {
- self->errornumber = SOCKET_COULD_NOT_CONNECT;
- self->errormsg = "Could not set socket to NODELAY.";
- closesocket(self->socket);
- self->socket = (SOCKETFD) - 1;
- return 0;
- }
- }
-#endif /* TCP_NODELAY */
-
- self->sadr_len = sLen;
- if (connect(self->socket, self->sadr, sLen) < 0)
- {
- self->errornumber = SOCKET_COULD_NOT_CONNECT;
- self->errormsg = "Could not connect to remote socket.";
- closesocket(self->socket);
- self->socket = (SOCKETFD) - 1;
- return 0;
- }
-
- return 1;
-}
-
-
-void
-SOCK_get_n_char(SocketClass *self, char *buffer, int len)
-{
- int lf;
-
- if (!self)
- return;
- if (!buffer)
- {
- self->errornumber = SOCKET_NULLPOINTER_PARAMETER;
- self->errormsg = "get_n_char was called with NULL-Pointer";
- return;
- }
-
- for (lf = 0; lf < len; lf++)
- buffer[lf] = SOCK_get_next_byte(self);
-}
-
-
-void
-SOCK_put_n_char(SocketClass *self, char *buffer, int len)
-{
- int lf;
-
- if (!self)
- return;
- if (!buffer)
- {
- self->errornumber = SOCKET_NULLPOINTER_PARAMETER;
- self->errormsg = "put_n_char was called with NULL-Pointer";
- return;
- }
-
- for (lf = 0; lf < len; lf++)
- SOCK_put_next_byte(self, (UCHAR) buffer[lf]);
-}
-
-
-/*
- * bufsize must include room for the null terminator
- * will read at most bufsize-1 characters + null.
- * returns TRUE if truncation occurs.
- */
-BOOL
-SOCK_get_string(SocketClass *self, char *buffer, int bufsize)
-{
- register int lf = 0;
-
- for (lf = 0; lf < bufsize - 1; lf++)
- if (!(buffer[lf] = SOCK_get_next_byte(self)))
- return FALSE;
-
- buffer[bufsize - 1] = '\0';
- return TRUE;
-}
-
-
-void
-SOCK_put_string(SocketClass *self, char *string)
-{
- register int lf;
- int len;
-
- len = strlen(string) + 1;
-
- for (lf = 0; lf < len; lf++)
- SOCK_put_next_byte(self, (UCHAR) string[lf]);
-}
-
-
-int
-SOCK_get_int(SocketClass *self, short len)
-{
- if (!self)
- return 0;
- switch (len)
- {
- case 2:
- {
- unsigned short buf;
-
- SOCK_get_n_char(self, (char *) &buf, len);
- if (self->reverse)
- return buf;
- else
- return ntohs(buf);
- }
-
- case 4:
- {
- unsigned int buf;
-
- SOCK_get_n_char(self, (char *) &buf, len);
- if (self->reverse)
- return buf;
- else
- return ntohl(buf);
- }
-
- default:
- self->errornumber = SOCKET_GET_INT_WRONG_LENGTH;
- self->errormsg = "Cannot read ints of that length";
- return 0;
- }
-}
-
-
-void
-SOCK_put_int(SocketClass *self, int value, short len)
-{
- unsigned int rv;
-
- if (!self)
- return;
- switch (len)
- {
- case 2:
- rv = self->reverse ? value : htons((unsigned short) value);
- SOCK_put_n_char(self, (char *) &rv, 2);
- return;
-
- case 4:
- rv = self->reverse ? value : htonl((unsigned int) value);
- SOCK_put_n_char(self, (char *) &rv, 4);
- return;
-
- default:
- self->errornumber = SOCKET_PUT_INT_WRONG_LENGTH;
- self->errormsg = "Cannot write ints of that length";
- return;
- }
-}
-
-
-void
-SOCK_flush_output(SocketClass *self)
-{
- int written, pos = 0;
-
- if (!self)
- return;
- do
- {
- written = send(self->socket, (char *) self->buffer_out + pos, self->buffer_filled_out, 0);
- if (written < 0)
- {
- if (SOCK_ERRNO == EINTR)
- continue;
- self->errornumber = SOCKET_WRITE_ERROR;
- self->errormsg = "Could not flush socket buffer.";
- break;
- }
- pos += written;
- self->buffer_filled_out -= written;
- } while (self->buffer_filled_out > 0);
-}
-
-
-UCHAR
-SOCK_get_next_byte(SocketClass *self)
-{
- if (!self)
- return 0;
- if (self->buffer_read_in >= self->buffer_filled_in)
- {
- /*
- * there are no more bytes left in the buffer so reload the buffer
- */
- self->buffer_read_in = 0;
-retry:
- self->buffer_filled_in = recv(self->socket, (char *) self->buffer_in, self->buffer_size, 0);
-
- mylog("read %d, global_socket_buffersize=%d\n", self->buffer_filled_in, self->buffer_size);
-
- if (self->buffer_filled_in < 0)
- {
- if (SOCK_ERRNO == EINTR)
- goto retry;
- self->errornumber = SOCKET_READ_ERROR;
- self->errormsg = "Error while reading from the socket.";
- self->buffer_filled_in = 0;
- return 0;
- }
- if (self->buffer_filled_in == 0)
- {
- self->errornumber = SOCKET_CLOSED;
- self->errormsg = "Socket has been closed.";
- self->buffer_filled_in = 0;
- return 0;
- }
- }
- return self->buffer_in[self->buffer_read_in++];
-}
-
-
-void
-SOCK_put_next_byte(SocketClass *self, UCHAR next_byte)
-{
- int bytes_sent, pos = 0;
-
- if (!self)
- return;
- self->buffer_out[self->buffer_filled_out++] = next_byte;
-
- if (self->buffer_filled_out == self->buffer_size)
- {
- /* buffer is full, so write it out */
- do
- {
- bytes_sent = send(self->socket, (char *) self->buffer_out + pos, self->buffer_filled_out, 0);
- if (bytes_sent < 0)
- {
- if (SOCK_ERRNO == EINTR)
- continue;
- self->errornumber = SOCKET_WRITE_ERROR;
- self->errormsg = "Error while writing to the socket.";
- break;
- }
- pos += bytes_sent;
- self->buffer_filled_out -= bytes_sent;
- } while (self->buffer_filled_out > 0);
- }
-}
-
-
-#endif /* USE_LIBPQ */
+++ /dev/null
-/* File: socket.h
- *
- * Description: See "socket.c"
- *
- * Comments: See "notice.txt" for copyright and license information.
- *
- */
-
-#ifndef __SOCKET_H__
-#define __SOCKET_H__
-
-#include "psqlodbc.h"
-#include <errno.h>
-
-#ifndef WIN32
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#define closesocket(xxx) close(xxx)
-#define SOCKETFD int
-
-#ifndef INADDR_NONE
-#ifndef _IN_ADDR_T
-#define _IN_ADDR_T
-typedef unsigned int in_addr_t;
-#endif /* _IN_ADDR_T */
-#define INADDR_NONE ((in_addr_t)-1)
-#endif /* _IN_ADDR_NONE */
-
-#define SOCK_ERRNO errno
-#define SOCK_ERRNO_SET(e) (errno = e)
-#if defined HAVE_SYS_UN_H && !defined HAVE_UNIX_SOCKETS
-#define HAVE_UNIX_SOCKETS
-#endif /* HAVE_SYS_UN_H */
-#else
-#include <winsock.h>
-#define SOCKETFD SOCKET
-#define SOCK_ERRNO (WSAGetLastError())
-#define SOCK_ERRNO_SET(e) WSASetLastError(e)
-#endif /* WIN32 */
-
-#define SOCKET_ALREADY_CONNECTED 1
-#define SOCKET_HOST_NOT_FOUND 2
-#define SOCKET_COULD_NOT_CREATE_SOCKET 3
-#define SOCKET_COULD_NOT_CONNECT 4
-#define SOCKET_READ_ERROR 5
-#define SOCKET_WRITE_ERROR 6
-#define SOCKET_NULLPOINTER_PARAMETER 7
-#define SOCKET_PUT_INT_WRONG_LENGTH 8
-#define SOCKET_GET_INT_WRONG_LENGTH 9
-#define SOCKET_CLOSED 10
-
-
-struct SocketClass_
-{
-
- int buffer_size;
- int buffer_filled_in;
- int buffer_filled_out;
- int buffer_read_in;
- UCHAR *buffer_in;
- UCHAR *buffer_out;
-
- SOCKETFD socket;
-
- char *errormsg;
- int errornumber;
- struct sockaddr *sadr; /* Used for handling connections for cancel */
- int sadr_len;
- struct sockaddr_in sadr_in; /* Used for INET connections */
-
- char reverse; /* used to handle Postgres 6.2 protocol
- * (reverse byte order) */
-
-};
-
-#define SOCK_get_char(self) (SOCK_get_next_byte(self))
-#define SOCK_put_char(self, c) (SOCK_put_next_byte(self, c))
-
-
-/* error functions */
-#define SOCK_get_errcode(self) (self ? self->errornumber : SOCKET_CLOSED)
-#define SOCK_get_errmsg(self) (self ? self->errormsg : "socket closed")
-
-/*
- * code taken from postgres libpq et al.
- */
-#ifndef WIN32
-#define DEFAULT_PGSOCKET_DIR "/tmp"
-#define UNIXSOCK_PATH(sun, port, defpath) \
- snprintf((sun)->sun_path, sizeof((sun)->sun_path), "%s/.s.PGSQL.%d", \
- ((defpath) && *(defpath) != '\0') ? (defpath) : \
- DEFAULT_PGSOCKET_DIR, \
- (port))
-
-/*
- * We do this because sun_len is in BSD's struct, while others don't.
- * We never actually set BSD's sun_len, and I can't think of a
- * platform-safe way of doing it, but the code still works. bjm
- */
-#ifndef offsetof
-#define offsetof(type, field) ((long) &((type *)0)->field)
-#endif /* offsetof */
-#if defined(SUN_LEN)
-#define UNIXSOCK_LEN(sun) SUN_LEN(sun)
-#else
-#define UNIXSOCK_LEN(sun) \
- (strlen((sun)->sun_path) + offsetof(struct sockaddr_un, sun_path))
-#endif /* SUN_LEN */
-#endif /* WIN32 */
-/*
- * END code taken from postgres libpq et al.
- */
-
-
-/* Socket prototypes */
-SocketClass *SOCK_Constructor(const ConnectionClass *conn);
-void SOCK_Destructor(SocketClass *self);
-char SOCK_connect_to(SocketClass *self, unsigned short port,
- char *hostname
-#ifdef HAVE_UNIX_SOCKETS
- , char *uds
-#endif
- );
-void SOCK_get_n_char(SocketClass *self, char *buffer, int len);
-void SOCK_put_n_char(SocketClass *self, char *buffer, int len);
-BOOL SOCK_get_string(SocketClass *self, char *buffer, int bufsize);
-void SOCK_put_string(SocketClass *self, char *string);
-int SOCK_get_int(SocketClass *self, short len);
-void SOCK_put_int(SocketClass *self, int value, short len);
-void SOCK_flush_output(SocketClass *self);
-UCHAR SOCK_get_next_byte(SocketClass *self);
-void SOCK_put_next_byte(SocketClass *self, UCHAR next_byte);
-void SOCK_clear_error(SocketClass *self);
-
-#endif /* __SOCKET_H__ */