diff --git a/src/interfaces/odbc/bind.c b/src/interfaces/odbc/bind.c index c01f61ab7f..945d23d64d 100644 --- a/src/interfaces/odbc/bind.c +++ b/src/interfaces/odbc/bind.c @@ -1,17 +1,17 @@ - -/* Module: bind.c - * - * Description: This module contains routines related to binding - * columns and parameters. - * - * Classes: BindInfoClass, ParameterInfoClass - * - * API functions: SQLBindParameter, SQLBindCol, SQLDescribeParam, SQLNumParams, - * SQLParamOptions(NI) - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: bind.c + * + * Description: This module contains routines related to binding + * columns and parameters. + * + * Classes: BindInfoClass, ParameterInfoClass + * + * API functions: SQLBindParameter, SQLBindCol, SQLDescribeParam, SQLNumParams, + * SQLParamOptions(NI) + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "bind.h" #include "environ.h" #include "statement.h" @@ -19,8 +19,8 @@ #include "pgtypes.h" #include #include -#include -#include +#include +#include // Bind parameters on a statement handle @@ -77,10 +77,11 @@ StatementClass *stmt = (StatementClass *) hstmt; stmt->parameters[i].CType = 0; stmt->parameters[i].SQLType = 0; stmt->parameters[i].precision = 0; - stmt->parameters[i].scale = 0; - stmt->parameters[i].data_at_exec = FALSE; - stmt->parameters[i].EXEC_used = NULL; - stmt->parameters[i].EXEC_buffer = NULL; + stmt->parameters[i].scale = 0; + stmt->parameters[i].data_at_exec = FALSE; + stmt->parameters[i].lobj_oid = 0; + stmt->parameters[i].EXEC_used = NULL; + stmt->parameters[i].EXEC_buffer = NULL; } } @@ -94,26 +95,28 @@ StatementClass *stmt = (StatementClass *) hstmt; stmt->parameters[ipar].CType = fCType; stmt->parameters[ipar].SQLType = fSqlType; stmt->parameters[ipar].precision = cbColDef; - stmt->parameters[ipar].scale = ibScale; - - /* If rebinding a parameter that had data-at-exec stuff in it, - then free that stuff - */ - if (stmt->parameters[ipar].EXEC_used) { - free(stmt->parameters[ipar].EXEC_used); - stmt->parameters[ipar].EXEC_used = NULL; - } - - if (stmt->parameters[ipar].EXEC_buffer) { - free(stmt->parameters[ipar].EXEC_buffer); - stmt->parameters[ipar].EXEC_buffer = NULL; - } - - if (pcbValue && *pcbValue <= SQL_LEN_DATA_AT_EXEC_OFFSET) - stmt->parameters[ipar].data_at_exec = TRUE; - else - stmt->parameters[ipar].data_at_exec = FALSE; - + stmt->parameters[ipar].scale = ibScale; + + /* If rebinding a parameter that had data-at-exec stuff in it, + then free that stuff + */ + if (stmt->parameters[ipar].EXEC_used) { + free(stmt->parameters[ipar].EXEC_used); + stmt->parameters[ipar].EXEC_used = NULL; + } + + if (stmt->parameters[ipar].EXEC_buffer) { + free(stmt->parameters[ipar].EXEC_buffer); + stmt->parameters[ipar].EXEC_buffer = NULL; + } + + if (pcbValue && *pcbValue <= SQL_LEN_DATA_AT_EXEC_OFFSET) + stmt->parameters[ipar].data_at_exec = TRUE; + else + stmt->parameters[ipar].data_at_exec = FALSE; + + mylog("SQLBindParamater: ipar = %d, *pcbValue = %d, data_at_exec = %d\n", + ipar, pcbValue ? *pcbValue: -777, stmt->parameters[ipar].data_at_exec); return SQL_SUCCESS; } @@ -188,6 +191,8 @@ mylog("**** SQLBindCol: stmt = %u, icol = %d\n", stmt, icol); stmt->bindings[icol].buffer = rgbValue; stmt->bindings[icol].used = pcbValue; stmt->bindings[icol].returntype = fCType; + + mylog(" bound buffer[%d] = %u\n", icol, stmt->bindings[icol].buffer); } return SQL_SUCCESS; @@ -228,7 +233,7 @@ StatementClass *stmt = (StatementClass *) hstmt; *pibScale = stmt->parameters[ipar].scale; if(pfNullable) - *pfNullable = pgtype_nullable(stmt->parameters[ipar].paramType); + *pfNullable = pgtype_nullable(stmt, stmt->parameters[ipar].paramType); return SQL_SUCCESS; } @@ -247,37 +252,25 @@ RETCODE SQL_API SQLParamOptions( // - - - - - - - - - -// Returns the number of parameter markers. +// Returns the number of parameters in an SQL statement RETCODE SQL_API SQLNumParams( HSTMT hstmt, SWORD FAR *pcpar) { StatementClass *stmt = (StatementClass *) hstmt; -unsigned int i; - // I guess this is the number of actual parameter markers - // in the statement, not the number of parameters that are bound. - // why does this have to be driver-specific? if(!stmt) return SQL_INVALID_HANDLE; - if(!stmt->statement) { - // no statement has been allocated - *pcpar = 0; - stmt->errormsg = "SQLNumParams called with no statement ready."; - stmt->errornumber = STMT_SEQUENCE_ERROR; - return SQL_ERROR; - } else { - *pcpar = 0; - for(i=0; i < strlen(stmt->statement); i++) { - if(stmt->statement[i] == '?') - (*pcpar)++; - } + // If the statement does not have parameters, it should just return 0. - return SQL_SUCCESS; + if (pcpar) { + *pcpar = stmt->parameters_allocated; } + + return SQL_SUCCESS; } /******************************************************************** @@ -309,7 +302,7 @@ extend_bindings(StatementClass *stmt, int num_columns) BindInfoClass *new_bindings; int i; - mylog("in extend_bindings\n"); +mylog("in extend_bindings: stmt=%u, bindings_allocated=%d, num_columns=%d\n", stmt, stmt->bindings_allocated, num_columns); /* if we have too few, allocate room for more, and copy the old */ /* entries into the new structure */ @@ -325,6 +318,7 @@ int i; } stmt->bindings = new_bindings; // null indicates error + stmt->bindings_allocated = num_columns; } else { /* if we have too many, make sure the extra ones are emptied out */ diff --git a/src/interfaces/odbc/bind.h b/src/interfaces/odbc/bind.h index 7ce3321a7a..7d19064e76 100644 --- a/src/interfaces/odbc/bind.h +++ b/src/interfaces/odbc/bind.h @@ -1,20 +1,20 @@ - -/* File: bind.h - * - * Description: See "bind.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: bind.h + * + * Description: See "bind.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __BIND_H__ #define __BIND_H__ #include "psqlodbc.h" -/* - * BindInfoClass -- stores information about a bound column - */ +/* + * BindInfoClass -- stores information about a bound column + */ struct BindInfoClass_ { Int4 buflen; /* size of buffer */ char *buffer; /* pointer to the buffer */ @@ -33,10 +33,11 @@ struct ParameterInfoClass_ { Int2 CType; Int2 SQLType; UInt4 precision; - Int2 scale; - Int4 *EXEC_used; - char *EXEC_buffer; - char data_at_exec; + Int2 scale; + Oid lobj_oid; + Int4 *EXEC_used; /* amount of data OR the oid of the large object */ + char *EXEC_buffer; /* the data or the FD of the large object */ + char data_at_exec; }; BindInfoClass *create_empty_bindings(int num_columns); diff --git a/src/interfaces/odbc/columninfo.c b/src/interfaces/odbc/columninfo.c index 4f029e9c38..54fc06a1c3 100644 --- a/src/interfaces/odbc/columninfo.c +++ b/src/interfaces/odbc/columninfo.c @@ -1,18 +1,18 @@ - -/* Module: columninfo.c - * - * Description: This module contains routines related to - * reading and storing the field information from a query. - * - * Classes: ColumnInfoClass (Functions prefix: "CI_") - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ -#include "columninfo.h" +/* Module: columninfo.c + * + * Description: This module contains routines related to + * reading and storing the field information from a query. + * + * Classes: ColumnInfoClass (Functions prefix: "CI_") + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ + +#include "columninfo.h" #include "socket.h" #include #include @@ -29,6 +29,7 @@ ColumnInfoClass *rv; rv->name = NULL; rv->adtid = NULL; rv->adtsize = NULL; + rv->display_size = NULL; } return rv; @@ -54,7 +55,7 @@ int new_num_fields; Oid new_adtid; Int2 new_adtsize; char new_field_name[MAX_MESSAGE_LEN+1]; - + /* at first read in the number of fields that are in the query */ new_num_fields = (Int2) SOCK_get_int(sock, sizeof(Int2)); @@ -93,11 +94,12 @@ int num_fields = self->num_fields; if( self->name[lf]) free (self->name[lf]); } - + /* Safe to call even if null */ free(self->name); free(self->adtid); free(self->adtsize); + free(self->display_size); } void @@ -110,6 +112,7 @@ CI_set_num_fields(ColumnInfoClass *self, int new_num_fields) self->name = (char **) malloc (sizeof(char *) * self->num_fields); self->adtid = (Oid *) malloc (sizeof(Oid) * self->num_fields); self->adtsize = (Int2 *) malloc (sizeof(Int2) * self->num_fields); + self->display_size = (Int2 *) malloc(sizeof(Int2) * self->num_fields); } void @@ -126,34 +129,7 @@ CI_set_field_info(ColumnInfoClass *self, int field_num, char *new_name, self->name[field_num] = strdup(new_name); self->adtid[field_num] = new_adtid; self->adtsize[field_num] = new_adtsize; -} - -char * -CI_get_fieldname(ColumnInfoClass *self, Int2 which) -{ -char *rv = NULL; - - if ( ! self->name) - return NULL; - - if ((which >= 0) && (which < self->num_fields)) - rv = self->name[which]; - - return rv; -} - - -Int2 -CI_get_fieldsize(ColumnInfoClass *self, Int2 which) -{ -Int2 rv = 0; - - if ( ! self->adtsize) - return 0; - - if ((which >= 0) && (which < self->num_fields)) - rv = self->adtsize[which]; - - return rv; + + self->display_size[field_num] = 0; } diff --git a/src/interfaces/odbc/columninfo.h b/src/interfaces/odbc/columninfo.h index 05d3dce6f2..8fcf2ad3c7 100644 --- a/src/interfaces/odbc/columninfo.h +++ b/src/interfaces/odbc/columninfo.h @@ -1,11 +1,11 @@ - -/* File: columninfo.h - * - * Description: See "columninfo.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: columninfo.h + * + * Description: See "columninfo.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __COLUMNINFO_H__ #define __COLUMNINFO_H__ @@ -17,14 +17,18 @@ struct ColumnInfoClass_ { char **name; /* list of type names */ Oid *adtid; /* list of type ids */ Int2 *adtsize; /* list type sizes */ + Int2 *display_size; /* the display size (longest row) */ }; -#define CI_get_num_fields(self) (self->num_fields) -#define CI_get_oid(self, col) (self->adtid[col]) - +#define CI_get_num_fields(self) (self->num_fields) +#define CI_get_oid(self, col) (self->adtid[col]) +#define CI_get_fieldname(self, col) (self->name[col]) +#define CI_get_fieldsize(self, col) (self->adtsize[col]) +#define CI_get_display_size(self, col) (self->display_size[col]) ColumnInfoClass *CI_Constructor(); void CI_Destructor(ColumnInfoClass *self); +void CI_free_memory(ColumnInfoClass *self); char CI_read_fields(ColumnInfoClass *self, SocketClass *sock); /* functions for setting up the fields from within the program, */ @@ -33,8 +37,5 @@ void CI_set_num_fields(ColumnInfoClass *self, int new_num_fields); void CI_set_field_info(ColumnInfoClass *self, int field_num, char *new_name, Oid new_adtid, Int2 new_adtsize); -char *CI_get_fieldname(ColumnInfoClass *self, Int2 which); -Int2 CI_get_fieldsize(ColumnInfoClass *self, Int2 which); -void CI_free_memory(ColumnInfoClass *self); #endif diff --git a/src/interfaces/odbc/connection.c b/src/interfaces/odbc/connection.c index e0ab2a9a60..95eceab0c2 100644 --- a/src/interfaces/odbc/connection.c +++ b/src/interfaces/odbc/connection.c @@ -1,30 +1,33 @@ - -/* Module: connection.c - * - * Description: This module contains routines related to - * connecting to and disconnecting from the Postgres DBMS. - * - * Classes: ConnectionClass (Functions prefix: "CC_") - * - * API functions: SQLAllocConnect, SQLConnect, SQLDisconnect, SQLFreeConnect, - * SQLBrowseConnect(NI) - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: connection.c + * + * Description: This module contains routines related to + * connecting to and disconnecting from the Postgres DBMS. + * + * Classes: ConnectionClass (Functions prefix: "CC_") + * + * API functions: SQLAllocConnect, SQLConnect, SQLDisconnect, SQLFreeConnect, + * SQLBrowseConnect(NI) + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "environ.h" #include "connection.h" #include "socket.h" #include "statement.h" #include "qresult.h" +#include "lobj.h" +#include "dlg_specific.h" #include #include #define STMT_INCREMENT 16 /* how many statement holders to allocate at a time */ -extern GLOBAL_VALUES globals; +extern GLOBAL_VALUES globals; +// void CC_test(ConnectionClass *self); RETCODE SQL_API SQLAllocConnect( HENV henv, @@ -70,25 +73,28 @@ RETCODE SQL_API SQLConnect( SWORD cbAuthStr) { ConnectionClass *conn = (ConnectionClass *) hdbc; +ConnInfo *ci; if ( ! conn) return SQL_INVALID_HANDLE; - make_string(szDSN, cbDSN, conn->connInfo.dsn); + ci = &conn->connInfo; + + make_string(szDSN, cbDSN, ci->dsn); /* get the values for the DSN from the registry */ - CC_DSN_info(conn, CONN_OVERWRITE); + getDSNinfo(ci, CONN_OVERWRITE); /* override values from DSN info with UID and authStr(pwd) This only occurs if the values are actually there. */ - make_string(szUID, cbUID, conn->connInfo.username); - make_string(szAuthStr, cbAuthStr, conn->connInfo.password); + make_string(szUID, cbUID, ci->username); + make_string(szAuthStr, cbAuthStr, ci->password); /* fill in any defaults */ - CC_set_defaults(conn); + getDSNdefaults(ci); - qlog("conn = %u, SQLConnect(DSN='%s', UID='%s', PWD='%s')\n", conn->connInfo.dsn, conn->connInfo.username, conn->connInfo.password); + qlog("conn = %u, SQLConnect(DSN='%s', UID='%s', PWD='%s')\n", ci->dsn, ci->username, ci->password); if ( CC_connect(conn, FALSE) <= 0) // Error messages are filled in @@ -124,12 +130,12 @@ ConnectionClass *conn = (ConnectionClass *) hdbc; return SQL_INVALID_HANDLE; qlog("conn=%u, SQLDisconnect\n", conn); - - if (conn->status == CONN_EXECUTING) { - conn->errornumber = CONN_IN_USE; - conn->errormsg = "A transaction is currently being executed"; - return SQL_ERROR; - } + + if (conn->status == CONN_EXECUTING) { + conn->errornumber = CONN_IN_USE; + conn->errormsg = "A transaction is currently being executed"; + return SQL_ERROR; + } mylog("SQLDisconnect: about to CC_cleanup\n"); @@ -155,12 +161,12 @@ ConnectionClass *conn = (ConnectionClass *) hdbc; if ( ! conn) return SQL_INVALID_HANDLE; - /* Remove the connection from the environment */ - if ( ! EN_remove_connection(conn->henv, conn)) { - conn->errornumber = CONN_IN_USE; - conn->errormsg = "A transaction is currently being executed"; - return SQL_ERROR; - } + /* Remove the connection from the environment */ + if ( ! EN_remove_connection(conn->henv, conn)) { + conn->errornumber = CONN_IN_USE; + conn->errormsg = "A transaction is currently being executed"; + return SQL_ERROR; + } CC_Destructor(conn); @@ -207,6 +213,7 @@ ConnectionClass *rv; rv->num_stmts = STMT_INCREMENT; + rv->lobj_type = PG_TYPE_LO; } return rv; } @@ -239,6 +246,26 @@ CC_Destructor(ConnectionClass *self) return 1; } +/* Return how many cursors are opened on this connection */ +int +CC_cursor_count(ConnectionClass *self) +{ +StatementClass *stmt; +int i, count = 0; + + mylog("CC_cursor_count: self=%u, num_stmts=%d\n", self, self->num_stmts); + + for (i = 0; i < self->num_stmts; i++) { + stmt = self->stmts[i]; + if (stmt && stmt->result && stmt->result->cursor) + count++; + } + + mylog("CC_cursor_count: returning %d\n", count); + + return count; +} + void CC_clear_error(ConnectionClass *self) { @@ -316,75 +343,12 @@ StatementClass *stmt; return TRUE; } -void -CC_set_defaults(ConnectionClass *self) -{ -ConnInfo *ci = &(self->connInfo); - - if (ci->port[0] == '\0') - strcpy(ci->port, DEFAULT_PORT); - - if (ci->readonly[0] == '\0') - strcpy(ci->readonly, DEFAULT_READONLY); -} - -void -CC_DSN_info(ConnectionClass *self, char overwrite) -{ -ConnInfo *ci = &(self->connInfo); -char *DSN = ci->dsn; - - // If a driver keyword was present, then dont use a DSN and return. - // If DSN is null and no driver, then use the default datasource. - if ( DSN[0] == '\0') { - if ( ci->driver[0] != '\0') - return; - else - strcpy(DSN, "DEFAULT"); - } - - // Proceed with getting info for the given DSN. - if ( ci->server[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_SERVER, "", ci->server, sizeof(ci->server), ODBC_INI); - - if ( ci->database[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_DATABASE, "", ci->database, sizeof(ci->database), ODBC_INI); - - if ( ci->username[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_USER, "", ci->username, sizeof(ci->username), ODBC_INI); - - if ( ci->password[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_PASSWORD, "", ci->password, sizeof(ci->password), ODBC_INI); - - if ( ci->port[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_PORT, "", ci->port, sizeof(ci->port), ODBC_INI); - - if ( ci->readonly[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_READONLY, "", ci->readonly, sizeof(ci->readonly), ODBC_INI); - - if ( ci->protocol[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_PROTOCOL, "", ci->protocol, sizeof(ci->protocol), ODBC_INI); - - if ( ci->conn_settings[0] == '\0' || overwrite) - SQLGetPrivateProfileString(DSN, INI_CONNSETTINGS, "", ci->conn_settings, sizeof(ci->conn_settings), ODBC_INI); - - qlog("conn=%u, DSN info(DSN='%s',server='%s',dbase='%s',user='%s',passwd='%s',port='%s',readonly='%s',protocol='%s',conn_settings='%s')\n", - self, DSN, - ci->server, - ci->database, - ci->username, - ci->password, - ci->port, - ci->readonly, - ci->protocol, - ci->conn_settings); -} char CC_connect(ConnectionClass *self, char do_password) { -StartupPacket sp; +StartupPacket sp; StartupPacket6_2 sp62; QResultClass *res; SocketClass *sock; @@ -392,7 +356,7 @@ ConnInfo *ci = &(self->connInfo); int areq = -1; int beresp; char msgbuffer[ERROR_MSG_LENGTH]; -char salt[2]; +char salt[2]; if ( do_password) @@ -400,6 +364,24 @@ char salt[2]; else { + qlog("Global Options: fetch=%d, socket=%d, unknown_sizes=%d, max_varchar_size=%d, max_longvarchar_size=%d\n", + globals.fetch_max, + globals.socket_buffersize, + globals.unknown_sizes, + globals.max_varchar_size, + globals.max_longvarchar_size); + qlog(" disable_optimizer=%d, unique_index=%d, use_declarefetch=%d\n", + globals.disable_optimizer, + globals.unique_index, + globals.use_declarefetch); + qlog(" text_as_longvarchar=%d, unknowns_as_longvarchar=%d, bools_as_char=%d\n", + globals.text_as_longvarchar, + globals.unknowns_as_longvarchar, + globals.bools_as_char); + qlog(" extra_systable_prefixes='%s', conn_settings='%s'\n", + globals.extra_systable_prefixes, + globals.conn_settings); + if (self->status != CONN_NOT_CONNECTED) { self->errormsg = "Already connected."; self->errornumber = CONN_OPENDB_ERROR; @@ -439,18 +421,18 @@ char salt[2]; return 0; } mylog("connection to the server socket succeeded.\n"); - - if ( PROTOCOL_62(ci)) { - sock->reverse = TRUE; /* make put_int and get_int work for 6.2 */ - - memset(&sp62, 0, sizeof(StartupPacket6_2)); - SOCK_put_int(sock, htonl(4+sizeof(StartupPacket6_2)), 4); - sp62.authtype = htonl(NO_AUTHENTICATION); - strncpy(sp62.database, ci->database, PATH_SIZE); - strncpy(sp62.user, ci->username, NAMEDATALEN); - SOCK_put_n_char(sock, (char *) &sp62, sizeof(StartupPacket6_2)); - SOCK_flush_output(sock); - } + + if ( PROTOCOL_62(ci)) { + sock->reverse = TRUE; /* make put_int and get_int work for 6.2 */ + + memset(&sp62, 0, sizeof(StartupPacket6_2)); + SOCK_put_int(sock, htonl(4+sizeof(StartupPacket6_2)), 4); + sp62.authtype = htonl(NO_AUTHENTICATION); + strncpy(sp62.database, ci->database, PATH_SIZE); + strncpy(sp62.user, ci->username, NAMEDATALEN); + SOCK_put_n_char(sock, (char *) &sp62, sizeof(StartupPacket6_2)); + SOCK_flush_output(sock); + } else { memset(&sp, 0, sizeof(StartupPacket)); @@ -465,7 +447,7 @@ char salt[2]; SOCK_put_n_char(sock, (char *) &sp, sizeof(StartupPacket)); SOCK_flush_output(sock); - } + } mylog("sent the authentication block.\n"); @@ -484,9 +466,9 @@ char salt[2]; // *************************************************** // Now get the authentication request from backend - // *************************************************** + // *************************************************** - if ( ! PROTOCOL_62(ci)) do { + if ( ! PROTOCOL_62(ci)) do { if (do_password) beresp = 'R'; @@ -590,15 +572,19 @@ char salt[2]; mylog("empty query seems to be OK.\n"); - - /**********************************************/ - /******* Send any initial settings *********/ - /**********************************************/ - - CC_send_settings(self); - - CC_clear_error(self); /* clear any initial command errors */ + /**********************************************/ + /******* Send any initial settings *********/ + /**********************************************/ + + if ( ! CC_send_settings(self)) + return 0; + + CC_lookup_lo(self); /* a hack to get the oid of our large object oid type */ + +// CC_test(self); + + CC_clear_error(self); /* clear any initial command errors */ self->status = CONN_CONNECTED; return 1; @@ -650,8 +636,8 @@ int i; return FALSE; } -/* Create a more informative error message by concatenating the connection - error message with its socket error message. +/* Create a more informative error message by concatenating the connection + error message with its socket error message. */ char * CC_create_errormsg(ConnectionClass *self) @@ -705,14 +691,14 @@ int rv; return rv; } - -/* The "result_in" is only used by QR_next_tuple() to fetch another group of rows into - the same existing QResultClass (this occurs when the tuple cache is depleted and - needs to be re-filled). - - The "cursor" is used by SQLExecute to associate a statement handle as the cursor name - (i.e., C3326857) for SQL select statements. This cursor is then used in future - 'declare cursor C3326857 for ...' and 'fetch 100 in C3326857' statements. + +/* The "result_in" is only used by QR_next_tuple() to fetch another group of rows into + the same existing QResultClass (this occurs when the tuple cache is depleted and + needs to be re-filled). + + The "cursor" is used by SQLExecute to associate a statement handle as the cursor name + (i.e., C3326857) for SQL select statements. This cursor is then used in future + 'declare cursor C3326857 for ...' and 'fetch 100 in C3326857' statements. */ QResultClass * CC_send_query(ConnectionClass *self, char *query, QResultClass *result_in, char *cursor) @@ -822,9 +808,16 @@ char cmdbuffer[MAX_MESSAGE_LEN+1]; // QR_set_command() dups this string so dont SOCK_put_string(sock, "Q "); SOCK_flush_output(sock); while(!clear) { - SOCK_get_string(sock, cmdbuffer, ERROR_MSG_LENGTH); - mylog("send_query: read command '%s'\n", cmdbuffer); + SOCK_get_string(sock, cmdbuffer, ERROR_MSG_LENGTH); + mylog("send_query: read command '%s'\n", cmdbuffer); clear = (cmdbuffer[0] == 'I'); + + if (cmdbuffer[0] == 'N') + qlog("NOTICE from backend during send_query: '%s'\n", &cmdbuffer[1]); + else if (cmdbuffer[0] == 'E') + qlog("ERROR from backend during send_query: '%s'\n", &cmdbuffer[1]); + else if (cmdbuffer[0] == 'C') + qlog("Command response: '%s'\n", &cmdbuffer[1]); } mylog("send_query: returning res = %u\n", res); @@ -925,55 +918,270 @@ char cmdbuffer[MAX_MESSAGE_LEN+1]; // QR_set_command() dups this string so dont } } } - -char -CC_send_settings(ConnectionClass *self) -{ -char ini_query[MAX_MESSAGE_LEN]; -ConnInfo *ci = &(self->connInfo); -QResultClass *res; - - ini_query[0] = '\0'; - - /* Turn on/off genetic optimizer based on global flag */ - if (globals.optimizer[0] != '\0') - sprintf(ini_query, "set geqo to '%s'", globals.optimizer); - - /* Global settings */ - if (globals.conn_settings[0] != '\0') - sprintf(&ini_query[strlen(ini_query)], "%s%s", - ini_query[0] != '\0' ? "; " : "", - globals.conn_settings); - - /* Per Datasource settings */ - if (ci->conn_settings[0] != '\0') - sprintf(&ini_query[strlen(ini_query)], "%s%s", - ini_query[0] != '\0' ? "; " : "", - ci->conn_settings); - - if (ini_query[0] != '\0') { - mylog("Sending Initial Connection query: '%s'\n", ini_query); - - res = CC_send_query(self, ini_query, NULL, NULL); - if (res && QR_get_status(res) != PGRES_FATAL_ERROR) { - mylog("Initial Query response: '%s'\n", QR_get_notice(res)); - } - - if ( res == NULL || - QR_get_status(res) == PGRES_BAD_RESPONSE || - QR_get_status(res) == PGRES_FATAL_ERROR || - QR_get_status(res) == PGRES_INTERNAL_ERROR) { - - self->errornumber = CONNECTION_COULD_NOT_SEND; - self->errormsg = "Error sending ConnSettings"; - if (res) - QR_Destructor(res); - return 0; - } - - if (res) - QR_Destructor(res); - } - return TRUE; -} - + +int +CC_send_function(ConnectionClass *self, int fnid, void *result_buf, int *actual_result_len, int result_is_int, LO_ARG *args, int nargs) +{ +char id, c, done; +SocketClass *sock = self->sock; +static char msgbuffer[MAX_MESSAGE_LEN+1]; +int i; + + mylog("send_function(): conn=%u, fnid=%d, result_is_int=%d, nargs=%d\n", self, fnid, result_is_int, nargs); +// qlog("conn=%u, func=%d\n", self, fnid); + + if (SOCK_get_errcode(sock) != 0) { + self->errornumber = CONNECTION_COULD_NOT_SEND; + self->errormsg = "Could not send function to backend"; + CC_set_no_trans(self); + return FALSE; + } + + SOCK_put_string(sock, "F "); + if (SOCK_get_errcode(sock) != 0) { + self->errornumber = CONNECTION_COULD_NOT_SEND; + self->errormsg = "Could not send function to backend"; + CC_set_no_trans(self); + return FALSE; + } + + SOCK_put_int(sock, fnid, 4); + SOCK_put_int(sock, nargs, 4); + + + mylog("send_function: done sending function\n"); + + for (i = 0; i < nargs; ++i) { + + mylog(" arg[%d]: len = %d, isint = %d, integer = %d, ptr = %u\n", + i, args[i].len, args[i].isint, args[i].u.integer, args[i].u.ptr); + + SOCK_put_int(sock, args[i].len, 4); + if (args[i].isint) + SOCK_put_int(sock, args[i].u.integer, 4); + else + SOCK_put_n_char(sock, (char *) args[i].u.ptr, args[i].len); + + + } + + mylog(" done sending args\n"); + + SOCK_flush_output(sock); + mylog(" after flush output\n"); + + done = FALSE; + while ( ! done) { + id = SOCK_get_char(sock); + mylog(" got id = %c\n", id); + + switch(id) { + case 'V': + done = TRUE; + break; /* ok */ + + case 'N': + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + mylog("send_function(V): 'N' - %s\n", msgbuffer); + /* continue reading */ + break; + + case 'E': + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + self->errormsg = msgbuffer; + + mylog("send_function(V): 'E' - %s\n", self->errormsg); + qlog("ERROR from backend during send_function: '%s'\n", self->errormsg); + + return FALSE; + + default: + self->errornumber = CONNECTION_BACKEND_CRAZY; + self->errormsg = "Unexpected protocol character from backend"; + CC_set_no_trans(self); + + mylog("send_function: error - %s\n", self->errormsg); + return FALSE; + } + } + + id = SOCK_get_char(sock); + for (;;) { + switch (id) { + case 'G': /* function returned properly */ + mylog(" got G!\n"); + + *actual_result_len = SOCK_get_int(sock, 4); + mylog(" actual_result_len = %d\n", *actual_result_len); + + if (result_is_int) + *((int *) result_buf) = SOCK_get_int(sock, 4); + else + SOCK_get_n_char(sock, (char *) result_buf, *actual_result_len); + + mylog(" after get result\n"); + + c = SOCK_get_char(sock); /* get the last '0' */ + + mylog(" after get 0\n"); + + return TRUE; + + case 'E': + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + self->errormsg = msgbuffer; + + mylog("send_function(G): 'E' - %s\n", self->errormsg); + qlog("ERROR from backend during send_function: '%s'\n", self->errormsg); + + return FALSE; + + case 'N': + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + + mylog("send_function(G): 'N' - %s\n", msgbuffer); + qlog("NOTICE from backend during send_function: '%s'\n", msgbuffer); + + continue; // dont return a result -- continue reading + + case '0': /* empty result */ + return TRUE; + + default: + self->errornumber = CONNECTION_BACKEND_CRAZY; + self->errormsg = "Unexpected protocol character from backend"; + CC_set_no_trans(self); + + mylog("send_function: error - %s\n", self->errormsg); + return FALSE; + } + } +} + +char +CC_send_settings(ConnectionClass *self) +{ +char ini_query[MAX_MESSAGE_LEN]; +ConnInfo *ci = &(self->connInfo); +// QResultClass *res; +HSTMT hstmt; +StatementClass *stmt; +RETCODE result; +SWORD cols = 0; + + result = SQLAllocStmt( self, &hstmt); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + return FALSE; + } + stmt = (StatementClass *) hstmt; + + ini_query[0] = '\0'; + + /* Set the Datestyle to the format the driver expects it to be in */ + sprintf(ini_query, "set DateStyle to 'ISO'"); + + /* Disable genetic optimizer based on global flag */ + if (globals.disable_optimizer) + sprintf(&ini_query[strlen(ini_query)], "%sset geqo to 'OFF'", + ini_query[0] != '\0' ? "; " : ""); + + /* Global settings */ + if (globals.conn_settings[0] != '\0') + sprintf(&ini_query[strlen(ini_query)], "%s%s", + ini_query[0] != '\0' ? "; " : "", + globals.conn_settings); + + /* Per Datasource settings */ + if (ci->conn_settings[0] != '\0') + sprintf(&ini_query[strlen(ini_query)], "%s%s", + ini_query[0] != '\0' ? "; " : "", + ci->conn_settings); + + if (ini_query[0] != '\0') { + mylog("Sending Initial Connection query: '%s'\n", ini_query); + + result = SQLExecDirect(hstmt, ini_query, SQL_NTS); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + SQLFreeStmt(hstmt, SQL_DROP); + return FALSE; + } + + SQLFreeStmt(hstmt, SQL_DROP); + + } + return TRUE; +} + +/* This function is just a hack to get the oid of our Large Object oid type. + If a real Large Object oid type is made part of Postgres, this function + will go away and the define 'PG_TYPE_LO' will be updated. +*/ +void +CC_lookup_lo(ConnectionClass *self) +{ +HSTMT hstmt; +StatementClass *stmt; +RETCODE result; + + result = SQLAllocStmt( self, &hstmt); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + return; + } + stmt = (StatementClass *) hstmt; + + result = SQLExecDirect(hstmt, "select oid from pg_type where typname='" \ + PG_TYPE_LO_NAME \ + "'", SQL_NTS); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + SQLFreeStmt(hstmt, SQL_DROP); + return; + } + + result = SQLFetch(hstmt); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + SQLFreeStmt(hstmt, SQL_DROP); + return; + } + + result = SQLGetData(hstmt, 1, SQL_C_SLONG, &self->lobj_type, sizeof(self->lobj_type), NULL); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + SQLFreeStmt(hstmt, SQL_DROP); + return; + } + + mylog("Got the large object oid: %d\n", self->lobj_type); + qlog(" [ Large Object oid = %d ]\n", self->lobj_type); + + result = SQLFreeStmt(hstmt, SQL_DROP); +} + +/* +void +CC_test(ConnectionClass *self) +{ +HSTMT hstmt1; +RETCODE result; +SDWORD pcbValue; +UDWORD pcrow; +UWORD rgfRowStatus; +char buf[255]; + + result = SQLAllocStmt( self, &hstmt1); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + return; + } + + result = SQLExtendedFetch(hstmt1, SQL_FETCH_ABSOLUTE, -2, &pcrow, &rgfRowStatus); + SQLGetData(hstmt1, 1, SQL_C_CHAR, buf, sizeof(buf), &pcbValue); + qlog("FETCH_ABSOLUTE, -2: result=%d, Col1 = '%s'\n", result, buf); + + result = SQLFetch(hstmt1); + while (result != SQL_NO_DATA_FOUND) { + result = SQLFetch(hstmt1); + qlog("fetch on stmt1\n"); + } + SQLFreeStmt(hstmt1, SQL_DROP); + +} +*/ + diff --git a/src/interfaces/odbc/connection.h b/src/interfaces/odbc/connection.h index 30d567212c..8ffc15b3d3 100644 --- a/src/interfaces/odbc/connection.h +++ b/src/interfaces/odbc/connection.h @@ -1,17 +1,18 @@ - -/* File: connection.h - * - * Description: See "connection.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: connection.h + * + * Description: See "connection.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __CONNECTION_H__ #define __CONNECTION_H__ #include #include +#include #include "psqlodbc.h" typedef enum { @@ -44,9 +45,9 @@ typedef enum { /* SetConnectOption: corresponds to ODBC--"S1009" */ #define CONN_TRANSACT_IN_PROGRES 7 #define CONN_NO_MEMORY_ERROR 8 -#define CONN_NOT_IMPLEMENTED_ERROR 9 +#define CONN_NOT_IMPLEMENTED_ERROR 9 #define CONN_INVALID_AUTHENTICATION 10 -#define CONN_AUTH_TYPE_UNSUPPORTED 11 +#define CONN_AUTH_TYPE_UNSUPPORTED 11 /* Conn_status defines */ @@ -77,12 +78,12 @@ typedef enum { #define SM_OPTIONS 64 #define SM_UNUSED 64 #define SM_TTY 64 - -/* Old 6.2 protocol defines */ -#define NO_AUTHENTICATION 7 -#define PATH_SIZE 64 -#define ARGV_SIZE 64 -#define NAMEDATALEN 16 + +/* Old 6.2 protocol defines */ +#define NO_AUTHENTICATION 7 +#define PATH_SIZE 64 +#define ARGV_SIZE 64 +#define NAMEDATALEN 16 typedef unsigned int ProtocolVersion; @@ -90,7 +91,7 @@ typedef unsigned int ProtocolVersion; #define PG_PROTOCOL_LATEST PG_PROTOCOL(1, 0) #define PG_PROTOCOL_EARLIEST PG_PROTOCOL(0, 0) -/* This startup packet is to support latest Postgres protocol (6.3) */ +/* This startup packet is to support latest Postgres protocol (6.3) */ typedef struct _StartupPacket { ProtocolVersion protoVersion; @@ -100,41 +101,47 @@ typedef struct _StartupPacket char unused[SM_UNUSED]; char tty[SM_TTY]; } StartupPacket; - - + + /* This startup packet is to support pre-Postgres 6.3 protocol */ -typedef struct _StartupPacket6_2 -{ - unsigned int authtype; - char database[PATH_SIZE]; - char user[NAMEDATALEN]; - char options[ARGV_SIZE]; - char execfile[ARGV_SIZE]; - char tty[PATH_SIZE]; -} StartupPacket6_2; - - -/* Structure to hold all the connection attributes for a specific - connection (used for both registry and file, DSN and DRIVER) +typedef struct _StartupPacket6_2 +{ + unsigned int authtype; + char database[PATH_SIZE]; + char user[NAMEDATALEN]; + char options[ARGV_SIZE]; + char execfile[ARGV_SIZE]; + char tty[PATH_SIZE]; +} StartupPacket6_2; + + +/* 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 driver[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 password[MEDIUM_REGISTRY_LEN]; + char conn_settings[LARGE_REGISTRY_LEN]; char protocol[SMALL_REGISTRY_LEN]; char port[SMALL_REGISTRY_LEN]; - char readonly[SMALL_REGISTRY_LEN]; + char readonly[SMALL_REGISTRY_LEN]; +// char unknown_sizes[SMALL_REGISTRY_LEN]; + char fake_oid_index[SMALL_REGISTRY_LEN]; + char show_oid_column[SMALL_REGISTRY_LEN]; + char show_system_tables[SMALL_REGISTRY_LEN]; char focus_password; } ConnInfo; - + /* Macro to determine is the connection using 6.2 protocol? */ -#define PROTOCOL_62(conninfo_) (strncmp((conninfo_)->protocol, PG62, strlen(PG62)) == 0) - - +#define PROTOCOL_62(conninfo_) (strncmp((conninfo_)->protocol, PG62, strlen(PG62)) == 0) + + + /******* The Connection handle ************/ struct ConnectionClass_ { HENV henv; /* environment this connection was created on */ @@ -145,6 +152,7 @@ struct ConnectionClass_ { StatementClass **stmts; int num_stmts; SocketClass *sock; + int lobj_type; char transact_status; /* Is a transaction is currently in progress */ char errormsg_created; /* has an informative error msg been created? */ }; @@ -162,15 +170,14 @@ struct ConnectionClass_ { /* for CC_DSN_info */ #define CONN_DONT_OVERWRITE 0 #define CONN_OVERWRITE 1 - - + + /* prototypes */ ConnectionClass *CC_Constructor(); char CC_Destructor(ConnectionClass *self); +int CC_cursor_count(ConnectionClass *self); char CC_cleanup(ConnectionClass *self); char CC_abort(ConnectionClass *self); -void CC_DSN_info(ConnectionClass *self, char overwrite); -void CC_set_defaults(ConnectionClass *self); char CC_connect(ConnectionClass *self, char do_password); char CC_add_statement(ConnectionClass *self, StatementClass *stmt); char CC_remove_statement(ConnectionClass *self, StatementClass *stmt); @@ -178,6 +185,8 @@ char CC_get_error(ConnectionClass *self, int *number, char **message); QResultClass *CC_send_query(ConnectionClass *self, char *query, QResultClass *result_in, char *cursor); void CC_clear_error(ConnectionClass *self); char *CC_create_errormsg(ConnectionClass *self); -char CC_send_settings(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); #endif diff --git a/src/interfaces/odbc/convert.c b/src/interfaces/odbc/convert.c index e340c83ec0..bad3f1a911 100644 --- a/src/interfaces/odbc/convert.c +++ b/src/interfaces/odbc/convert.c @@ -1,32 +1,52 @@ - -/* Module: convert.c - * - * Description: This module contains routines related to - * converting parameters and columns into requested data types. - * Parameters are converted from their SQL_C data types into - * the appropriate postgres type. Columns are converted from - * their postgres type (SQL type) into the appropriate SQL_C - * data type. - * - * Classes: n/a - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: convert.c + * + * Description: This module contains routines related to + * converting parameters and columns into requested data types. + * Parameters are converted from their SQL_C data types into + * the appropriate postgres type. Columns are converted from + * their postgres type (SQL type) into the appropriate SQL_C + * data type. + * + * Classes: n/a + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include #include #include -#include +#include #include -#include +#include #include #include "convert.h" #include "statement.h" #include "bind.h" #include "pgtypes.h" +#include "lobj.h" +#include "connection.h" + +extern GLOBAL_VALUES globals; + +/* How to map ODBC scalar functions {fn func(args)} to Postgres */ +/* This is just a simple substitution */ +char *mapFuncs[][2] = { + { "CONCAT", "textcat" }, + { "LCASE", "lower" }, + { "LOCATE", "strpos" }, + { "LENGTH", "textlen" }, + { "LTRIM", "ltrim" }, + { "RTRIM", "rtrim" }, + { "SUBSTRING", "substr" }, + { "UCASE", "upper" }, + { "NOW", "now" }, + { 0, 0 } +}; + /******** A Guide for date/time/timestamp conversions ************** @@ -47,23 +67,23 @@ /* This is called by SQLFetch() */ int -copy_and_convert_field_bindinfo(Int4 field_type, void *value, BindInfoClass *bic) +copy_and_convert_field_bindinfo(StatementClass *stmt, Int4 field_type, void *value, int col) { - return copy_and_convert_field(field_type, value, (Int2)bic->returntype, (PTR)bic->buffer, - (SDWORD)bic->buflen, (SDWORD *)bic->used); +BindInfoClass *bic = &(stmt->bindings[col]); + + return copy_and_convert_field(stmt, field_type, value, (Int2)bic->returntype, (PTR)bic->buffer, + (SDWORD)bic->buflen, (SDWORD *)bic->used, FALSE); } /* This is called by SQLGetData() */ int -copy_and_convert_field(Int4 field_type, void *value, Int2 fCType, PTR rgbValue, SDWORD cbValueMax, SDWORD *pcbValue) +copy_and_convert_field(StatementClass *stmt, Int4 field_type, void *value, Int2 fCType, + PTR rgbValue, SDWORD cbValueMax, SDWORD *pcbValue, char multiple) { -Int4 len = 0, nf; -char day[4], mon[4], tz[4]; +Int4 len = 0; SIMPLE_TIME st; time_t t = time(NULL); struct tm *tim; -int bool; - memset(&st, 0, sizeof(SIMPLE_TIME)); @@ -73,8 +93,6 @@ int bool; st.d = tim->tm_mday; st.y = tim->tm_year + 1900; - bool = 0; - mylog("copy_and_convert: field_type = %d, fctype = %d, value = '%s', cbValueMax=%d\n", field_type, fCType, value, cbValueMax); if(value) { @@ -88,7 +106,7 @@ int bool; switch(field_type) { /* $$$ need to add parsing for date/time/timestamp strings in PG_TYPE_CHAR,VARCHAR $$$ */ case PG_TYPE_DATE: - sscanf(value, "%2d-%2d-%4d", &st.m, &st.d, &st.y); + sscanf(value, "%4d-%2d-%2d", &st.y, &st.m, &st.d); break; case PG_TYPE_TIME: @@ -96,32 +114,28 @@ int bool; break; case PG_TYPE_ABSTIME: - case PG_TYPE_DATETIME: - if (strnicmp(value, "invalid", 7) != 0) { - nf = sscanf(value, "%3s %3s %2d %2d:%2d:%2d %4d %3s", &day, &mon, &st.d, &st.hh, &st.mm, &st.ss, &st.y, &tz); - - if (nf == 7 || nf == 8) { - /* convert month name to month number */ - st.m = monthToNumber(mon); - } - } else { /* The timestamp is invalid so set something conspicuous, like the epoch */ - t = 0; - tim = localtime(&t); - st.m = tim->tm_mon + 1; - st.d = tim->tm_mday; - st.y = tim->tm_year + 1900; - st.hh = tim->tm_hour; - st.mm = tim->tm_min; - st.ss = tim->tm_sec; + case PG_TYPE_DATETIME: + if (strnicmp(value, "invalid", 7) != 0) { + sscanf(value, "%4d-%2d-%2d %2d:%2d:%2d", &st.y, &st.m, &st.d, &st.hh, &st.mm, &st.ss); + + } else { /* The timestamp is invalid so set something conspicuous, like the epoch */ + t = 0; + tim = localtime(&t); + st.m = tim->tm_mon + 1; + st.d = tim->tm_mday; + st.y = tim->tm_year + 1900; + st.hh = tim->tm_hour; + st.mm = tim->tm_min; + st.ss = tim->tm_sec; } break; case PG_TYPE_BOOL: { /* change T/F to 1/0 */ char *s = (char *) value; - if (s[0] == 'T' || s[0] == 't' || s[0] == '1') - bool = 1; - else - bool = 0; + if (s[0] == 'T' || s[0] == 't') + s[0] = '1'; + else + s[0] = '0'; } break; @@ -149,11 +163,20 @@ int bool; return COPY_OK; /* dont go any further or the data will be trashed */ } + /* This is a large object OID, which is used to store LONGVARBINARY objects. */ + case PG_TYPE_LO: + + return convert_lo( stmt, value, fCType, rgbValue, cbValueMax, pcbValue, multiple); + + default: + + if (field_type == stmt->hdbc->lobj_type) /* hack until permanent type available */ + return convert_lo( stmt, value, fCType, rgbValue, cbValueMax, pcbValue, multiple); } /* Change default into something useable */ if (fCType == SQL_C_DEFAULT) { - fCType = pgtype_to_ctype(field_type); + fCType = pgtype_to_ctype(stmt, field_type); mylog("copy_and_convert, SQL_C_DEFAULT: fCType = %d\n", fCType); } @@ -165,41 +188,41 @@ int bool; switch(field_type) { case PG_TYPE_DATE: len = 11; - if (cbValueMax > len) + if (cbValueMax >= len) sprintf((char *)rgbValue, "%.4d-%.2d-%.2d", st.y, st.m, st.d); break; case PG_TYPE_TIME: len = 9; - if (cbValueMax > len) + if (cbValueMax >= len) sprintf((char *)rgbValue, "%.2d:%.2d:%.2d", st.hh, st.mm, st.ss); break; case PG_TYPE_ABSTIME: case PG_TYPE_DATETIME: len = 19; - if (cbValueMax > len) + if (cbValueMax >= len) sprintf((char *) rgbValue, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", st.y, st.m, st.d, st.hh, st.mm, st.ss); break; case PG_TYPE_BOOL: len = 1; - if (cbValueMax > len) { - strcpy((char *) rgbValue, bool ? "1" : "0"); - mylog("PG_TYPE_BOOL: rgbValue = '%s'\n", rgbValue); + if (cbValueMax > len) { + strcpy((char *) rgbValue, value); + mylog("PG_TYPE_BOOL: rgbValue = '%s'\n", rgbValue); } - break; - - case PG_TYPE_BYTEA: // convert binary data to hex strings (i.e, 255 = "FF") - len = convert_pgbinary_to_char(value, rgbValue, cbValueMax); break; - - default: - /* convert linefeeds to carriage-return/linefeed */ - convert_linefeeds( (char *) value, rgbValue, cbValueMax); - len = strlen(rgbValue); - + + case PG_TYPE_BYTEA: // convert binary data to hex strings (i.e, 255 = "FF") + len = convert_pgbinary_to_char(value, rgbValue, cbValueMax); + break; + + default: + /* convert linefeeds to carriage-return/linefeed */ + convert_linefeeds( (char *) value, rgbValue, cbValueMax); + len = strlen(rgbValue); + mylog(" SQL_C_CHAR, default: len = %d, cbValueMax = %d, rgbValue = '%s'\n", len, cbValueMax, rgbValue); break; } @@ -233,7 +256,7 @@ int bool; } break; - case SQL_C_TIMESTAMP: + case SQL_C_TIMESTAMP: len = 16; if (cbValueMax >= len) { TIMESTAMP_STRUCT *ts = (TIMESTAMP_STRUCT *) rgbValue; @@ -250,7 +273,7 @@ int bool; case SQL_C_BIT: len = 1; if (cbValueMax >= len || field_type == PG_TYPE_BOOL) { - *((UCHAR *)rgbValue) = (UCHAR) bool; + *((UCHAR *)rgbValue) = atoi(value); mylog("SQL_C_BIT: val = %d, cb = %d, rgb=%d\n", atoi(value), cbValueMax, *((UCHAR *)rgbValue)); } break; @@ -306,12 +329,13 @@ int bool; *((UDWORD *)rgbValue) = atol(value); break; - case SQL_C_BINARY: - // truncate if necessary - // convert octal escapes to bytes - len = convert_from_pgbinary(value, rgbValue, cbValueMax); - mylog("SQL_C_BINARY: len = %d\n", len); - break; + case SQL_C_BINARY: + + // truncate if necessary + // convert octal escapes to bytes + len = convert_from_pgbinary(value, rgbValue, cbValueMax); + mylog("SQL_C_BINARY: len = %d\n", len); + break; default: return COPY_UNSUPPORTED_TYPE; @@ -361,18 +385,19 @@ unsigned int opos, npos; char param_string[128], tmp[256], cbuf[TEXT_FIELD_SIZE+5]; int param_number; Int2 param_ctype, param_sqltype; -char *old_statement = stmt->statement; +char *old_statement = stmt->statement; char *new_statement = stmt->stmt_with_params; SIMPLE_TIME st; time_t t = time(NULL); struct tm *tim; -SDWORD FAR *used; +SDWORD used; char *buffer, *buf; +char in_quote = FALSE; if ( ! old_statement) return SQL_ERROR; - + memset(&st, 0, sizeof(SIMPLE_TIME)); @@ -382,11 +407,13 @@ char *buffer, *buf; st.d = tim->tm_mday; st.y = tim->tm_year + 1900; - + /* If the application hasn't set a cursor name, then generate one */ + if ( stmt->cursor_name[0] == '\0') + sprintf(stmt->cursor_name, "SQL_CUR%u", stmt); // For selects, prepend a declare cursor to the statement - if (stmt->statement_type == STMT_TYPE_SELECT) { - sprintf(new_statement, "declare C%u cursor for ", stmt); + if (stmt->statement_type == STMT_TYPE_SELECT && globals.use_declarefetch) { + sprintf(new_statement, "declare %s cursor for ", stmt->cursor_name); npos = strlen(new_statement); } else { @@ -398,10 +425,10 @@ char *buffer, *buf; for (opos = 0; opos < strlen(old_statement); opos++) { - // Squeeze carriage-returns/linfeed pairs to linefeed only + // Squeeze carriage-returns/linfeed pairs to linefeed only if (old_statement[opos] == '\r' && opos+1= stmt->parameters_allocated) break; - - /* Assign correct buffers based on data at exec param or not */ - if ( stmt->parameters[param_number].data_at_exec) { - used = stmt->parameters[param_number].EXEC_used; - buffer = stmt->parameters[param_number].EXEC_buffer; - } - else { - used = stmt->parameters[param_number].used; - buffer = stmt->parameters[param_number].buffer; - } - - /* Handle NULL parameter data */ - if (used && *used == SQL_NULL_DATA) { - strcpy(&new_statement[npos], "NULL"); - npos += 4; - continue; - } - - /* If no buffer, and its not null, then what the hell is it? - Just leave it alone then. + + /* Assign correct buffers based on data at exec param or not */ + if ( stmt->parameters[param_number].data_at_exec) { + used = stmt->parameters[param_number].EXEC_used ? *stmt->parameters[param_number].EXEC_used : SQL_NTS; + buffer = stmt->parameters[param_number].EXEC_buffer; + } + else { + used = stmt->parameters[param_number].used ? *stmt->parameters[param_number].used : SQL_NTS; + buffer = stmt->parameters[param_number].buffer; + } + + /* Handle NULL parameter data */ + if (used == SQL_NULL_DATA) { + strcpy(&new_statement[npos], "NULL"); + npos += 4; + continue; + } + + /* If no buffer, and its not null, then what the hell is it? + Just leave it alone then. */ if ( ! buffer) { new_statement[npos++] = '?'; continue; - } + } param_ctype = stmt->parameters[param_number].CType; param_sqltype = stmt->parameters[param_number].SQLType; @@ -483,8 +525,8 @@ char *buffer, *buf; /* Convert input C type to a neutral format */ - switch(param_ctype) { - case SQL_C_BINARY: + switch(param_ctype) { + case SQL_C_BINARY: case SQL_C_CHAR: buf = buffer; break; @@ -535,7 +577,7 @@ char *buffer, *buf; case SQL_C_BIT: { int i = *((UCHAR *) buffer); - sprintf(param_string, "'%s'", i ? "t" : "f"); + sprintf(param_string, "%d", i ? 1 : 0); break; } @@ -587,14 +629,14 @@ char *buffer, *buf; switch(param_sqltype) { case SQL_CHAR: case SQL_VARCHAR: - case SQL_LONGVARCHAR: + case SQL_LONGVARCHAR: new_statement[npos++] = '\''; /* Open Quote */ /* it was a SQL_C_CHAR */ if (buf) { - convert_returns(buf, &new_statement[npos], used ? *used : SQL_NTS); - npos += strlen(&new_statement[npos]); + convert_special_chars(buf, &new_statement[npos], used); + npos += strlen(&new_statement[npos]); } /* it was a numeric type */ @@ -605,9 +647,11 @@ char *buffer, *buf; /* it was date,time,timestamp -- use m,d,y,hh,mm,ss */ else { - char *buf = convert_time(&st); - strcpy(&new_statement[npos], buf); - npos += strlen(buf); + sprintf(tmp, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", + st.y, st.m, st.d, st.hh, st.mm, st.ss); + + strcpy(&new_statement[npos], tmp); + npos += strlen(tmp); } new_statement[npos++] = '\''; /* Close Quote */ @@ -615,20 +659,20 @@ char *buffer, *buf; break; case SQL_DATE: - if (buf && used) { /* copy char data to time */ - my_strcpy(cbuf, sizeof(cbuf), buf, *used); + if (buf) { /* copy char data to time */ + my_strcpy(cbuf, sizeof(cbuf), buf, used); parse_datetime(cbuf, &st); } - sprintf(tmp, "'%.2d-%.2d-%.4d'", st.m, st.d, st.y); + sprintf(tmp, "'%.4d-%.2d-%.2d'", st.y, st.m, st.d); strcpy(&new_statement[npos], tmp); npos += strlen(tmp); break; case SQL_TIME: - if (buf && used) { /* copy char data to time */ - my_strcpy(cbuf, sizeof(cbuf), buf, *used); + if (buf) { /* copy char data to time */ + my_strcpy(cbuf, sizeof(cbuf), buf, used); parse_datetime(cbuf, &st); } @@ -638,39 +682,61 @@ char *buffer, *buf; npos += strlen(tmp); break; - case SQL_TIMESTAMP: { - char *tbuf; + case SQL_TIMESTAMP: - if (buf && used) { - my_strcpy(cbuf, sizeof(cbuf), buf, *used); + if (buf) { + my_strcpy(cbuf, sizeof(cbuf), buf, used); parse_datetime(cbuf, &st); } - tbuf = convert_time(&st); + sprintf(tmp, "'%.4d-%.2d-%.2d %.2d:%.2d:%.2d'", + st.y, st.m, st.d, st.hh, st.mm, st.ss); - sprintf(&new_statement[npos], "'%s'", tbuf); - npos += strlen(tbuf) + 2; + strcpy(&new_statement[npos], tmp); + npos += strlen(tmp); break; - } - case SQL_BINARY: - case SQL_VARBINARY: - case SQL_LONGVARBINARY: /* non-ascii characters should be converted to octal */ - - new_statement[npos++] = '\''; /* Open Quote */ - - mylog("SQL_LONGVARBINARY: about to call convert_to_pgbinary, *used = %d\n", *used); - - npos += convert_to_pgbinary(buf, &new_statement[npos], *used); - - new_statement[npos++] = '\''; /* Close Quote */ - - break; + case SQL_BINARY: + case SQL_VARBINARY: /* non-ascii characters should be converted to octal */ + new_statement[npos++] = '\''; /* Open Quote */ - default: /* a numeric type */ + mylog("SQL_LONGVARBINARY: about to call convert_to_pgbinary, used = %d\n", used); + + npos += convert_to_pgbinary(buf, &new_statement[npos], used); + + new_statement[npos++] = '\''; /* Close Quote */ + + break; + case SQL_LONGVARBINARY: + /* the oid of the large object -- just put that in for the + parameter marker -- the data has already been sent to the large object + */ + sprintf(param_string, "%d", stmt->parameters[param_number].lobj_oid); strcpy(&new_statement[npos], param_string); npos += strlen(param_string); + + break; + + // because of no conversion operator for bool and int4, SQL_BIT + // must be quoted (0 or 1 is ok to use inside the quotes) + + default: /* a numeric type or SQL_BIT */ + if (param_sqltype == SQL_BIT) + new_statement[npos++] = '\''; /* Open Quote */ + + if (buf) { + my_strcpy(&new_statement[npos], sizeof(stmt->stmt_with_params) - npos, buf, used); + npos += strlen(&new_statement[npos]); + } + else { + strcpy(&new_statement[npos], param_string); + npos += strlen(param_string); + } + + if (param_sqltype == SQL_BIT) + new_statement[npos++] = '\''; /* Close Quote */ + break; } @@ -683,29 +749,47 @@ char *buffer, *buf; return SQL_SUCCESS; } +char * +mapFunction(char *func) +{ +int i; + + for (i = 0; mapFuncs[i][0]; i++) + if ( ! stricmp(mapFuncs[i][0], func)) + return mapFuncs[i][1]; + + return NULL; +} // This function returns a pointer to static memory! char * convert_escape(char *value) { char key[32], val[256]; -static char escape[256]; -SIMPLE_TIME st; +static char escape[1024]; +char func[32], the_rest[1024]; +char *mapFunc; - sscanf(value, "%[^'] '%[^']'", key, val); + sscanf(value, "%s %[^\r]", key, val); mylog("convert_escape: key='%s', val='%s'\n", key, val); - if ( ! strncmp(key, "d", 1)) { - sscanf(val, "%4d-%2d-%2d", &st.y, &st.m, &st.d); - sprintf(escape, "'%.2d-%.2d-%.4d'", st.m, st.d, st.y); - - } else if (! strncmp(key, "t", 1)) { - sprintf(escape, "'%s'", val); + if ( ! strcmp(key, "d") || + ! strcmp(key, "t") || + ! strcmp(key, "ts")) { + + strcpy(escape, val); + } + else if ( ! strcmp(key, "fn")) { + sscanf(val, "%[^(]%[^\r]", func, the_rest); + mapFunc = mapFunction(func); + if ( ! mapFunc) + return NULL; + else { + strcpy(escape, mapFunc); + strcat(escape, the_rest); + } - } else if (! strncmp(key, "ts", 2)) { - sscanf(val, "%4d-%2d-%2d %2d:%2d:%2d", &st.y, &st.m, &st.d, &st.hh, &st.mm, &st.ss); - strcpy(escape, convert_time(&st)); } else { return NULL; @@ -716,40 +800,6 @@ SIMPLE_TIME st; } -int -monthToNumber(char *mon) -{ -int m = 0; - - if ( ! stricmp(mon, "Jan")) - m = 1; - else if ( ! stricmp(mon, "Feb")) - m = 2; - else if ( ! stricmp(mon, "Mar")) - m = 3; - else if ( ! stricmp(mon, "Apr")) - m = 4; - else if ( ! stricmp(mon, "May")) - m = 5; - else if ( ! stricmp(mon, "Jun")) - m = 6; - else if ( ! stricmp(mon, "Jul")) - m = 7; - else if ( ! stricmp(mon, "Aug")) - m = 8; - else if ( ! stricmp(mon, "Sep")) - m = 9; - else if ( ! stricmp(mon, "Oct")) - m = 10; - else if ( ! stricmp(mon, "Nov")) - m = 11; - else if ( ! stricmp(mon, "Dec")) - m = 12; - - return m; -} - - char * convert_money(char *s) { @@ -767,39 +817,7 @@ size_t i = 0, out = 0; return s; } -/* Convert a discrete time into a localized string */ -char * -convert_time(SIMPLE_TIME *st) -{ -struct tm tim; -static char buf[1024]; -mylog("convert_time: m=%d,d=%d,y=%d,hh=%d,mm=%d,ss=%d\n", - st->m, st->d, st->y, st->hh, st->mm, st->ss); - - memset(&tim, 0, sizeof(tim)); - - tim.tm_mon = st->m - 1; - tim.tm_mday = st->d; - tim.tm_year = st->y - 1900; - tim.tm_hour = st->hh; - tim.tm_min = st->mm; - tim.tm_sec = st->ss; - - /* Dont bother trying to figure out the day of week because - postgres will determine it correctly. However, the timezone - should be taken into account. $$$$ - */ - - // tim.tm_isdst = _daylight; - - strftime(buf, sizeof(buf), "%b %d %H:%M:%S %Y", - &tim); - -mylog("convert_time: buf = '%s'\n", buf); - - return buf; -} /* This function parses a character string for date/time info and fills in SIMPLE_TIME */ /* It does not zero out SIMPLE_TIME in case it is desired to initialize it with a value */ @@ -851,145 +869,214 @@ int nf; return FALSE; } - -/* Change linefeed to carriage-return/linefeed */ -char * -convert_linefeeds(char *si, char *dst, size_t max) -{ -size_t i = 0, out = 0; -static char sout[TEXT_FIELD_SIZE+5]; -char *p; - - if (dst) - p = dst; - else { - p = sout; - max = sizeof(sout); - } - - p[0] = '\0'; - - for (i = 0; i < strlen(si) && out < max-2; i++) { - if (si[i] == '\n') { - p[out++] = '\r'; - p[out++] = '\n'; - } - else - p[out++] = si[i]; - } - p[out] = '\0'; - return p; -} - -/* Change carriage-return/linefeed to just linefeed */ -char * -convert_returns(char *si, char *dst, int used) -{ -size_t i = 0, out = 0, max; -static char sout[TEXT_FIELD_SIZE+5]; -char *p; - - if (dst) - p = dst; - else - p = sout; - - p[0] = '\0'; - - if (used == SQL_NTS) - max = strlen(si); - else - max = used; - - for (i = 0; i < max; i++) { - if (si[i] == '\r' && i+1 < strlen(si) && si[i+1] == '\n') - continue; - else - p[out++] = si[i]; - } - p[out] = '\0'; - return p; -} - -int -convert_pgbinary_to_char(char *value, char *rgbValue, int cbValueMax) -{ - return 0; -} - -unsigned int -conv_from_octal(unsigned char *s) -{ -int i, y=0; - - for (i = 1; i <= 3; i++) { - y += (s[i] - 48) * (int) pow(8, 3-i); - } - - return y; - -} - -// convert octal escapes to bytes -int -convert_from_pgbinary(unsigned char *value, unsigned char *rgbValue, int cbValueMax) -{ -size_t i; -int o=0; - - for (i = 0; i < strlen(value); ) { - if (value[i] == '\\') { - rgbValue[o] = conv_from_octal(&value[i]); - i += 4; - } - else { - rgbValue[o] = value[i++]; - } - mylog("convert_from_pgbinary: i=%d, rgbValue[%d] = %d, %c\n", i, o, rgbValue[o], rgbValue[o]); - o++; - } - return o; -} - - -char * -conv_to_octal(unsigned char val) -{ -int i; -static char x[6]; - - x[0] = '\\'; - x[1] = '\\'; - x[5] = '\0'; - - for (i = 4; i > 1; i--) { - x[i] = (val & 7) + 48; - val >>= 3; - } - - return x; -} - -// convert non-ascii bytes to octal escape sequences -int -convert_to_pgbinary(unsigned char *in, char *out, int len) -{ -int i, o=0; - - - for (i = 0; i < len; i++) { - mylog("convert_to_pgbinary: in[%d] = %d, %c\n", i, in[i], in[i]); - if (in[i] < 32 || in[i] > 126) { - strcpy(&out[o], conv_to_octal(in[i])); - o += 5; - } - else - out[o++] = in[i]; - - } - - mylog("convert_to_pgbinary: returning %d, out='%.*s'\n", o, o, out); - - return o; -} - + +/* Change linefeed to carriage-return/linefeed */ +char * +convert_linefeeds(char *si, char *dst, size_t max) +{ +size_t i = 0, out = 0; +static char sout[TEXT_FIELD_SIZE+5]; +char *p; + + if (dst) + p = dst; + else { + p = sout; + max = sizeof(sout); + } + + p[0] = '\0'; + + for (i = 0; i < strlen(si) && out < max; i++) { + if (si[i] == '\n') { + p[out++] = '\r'; + p[out++] = '\n'; + } + else + p[out++] = si[i]; + } + p[out] = '\0'; + return p; +} + +/* Change carriage-return/linefeed to just linefeed + Plus, escape any special characters. +*/ +char * +convert_special_chars(char *si, char *dst, int used) +{ +size_t i = 0, out = 0, max; +static char sout[TEXT_FIELD_SIZE+5]; +char *p; + + if (dst) + p = dst; + else + p = sout; + + p[0] = '\0'; + + if (used == SQL_NTS) + max = strlen(si); + else + max = used; + + for (i = 0; i < max; i++) { + if (si[i] == '\r' && i+1 < strlen(si) && si[i+1] == '\n') + continue; + if (si[i] == '\'') + p[out++] = '\\'; + + p[out++] = si[i]; + } + p[out] = '\0'; + return p; +} + +/* !!! Need to implement this function !!! */ +int +convert_pgbinary_to_char(char *value, char *rgbValue, int cbValueMax) +{ + return 0; +} + +unsigned int +conv_from_octal(unsigned char *s) +{ +int i, y=0; + + for (i = 1; i <= 3; i++) { + y += (s[i] - 48) * (int) pow(8, 3-i); + } + + return y; + +} + +// convert octal escapes to bytes +int +convert_from_pgbinary(unsigned char *value, unsigned char *rgbValue, int cbValueMax) +{ +size_t i; +int o=0; + + for (i = 0; i < strlen(value); ) { + if (value[i] == '\\') { + rgbValue[o] = conv_from_octal(&value[i]); + i += 4; + } + else { + rgbValue[o] = value[i++]; + } + mylog("convert_from_pgbinary: i=%d, rgbValue[%d] = %d, %c\n", i, o, rgbValue[o], rgbValue[o]); + o++; + } + return o; +} + + +char * +conv_to_octal(unsigned char val) +{ +int i; +static char x[6]; + + x[0] = '\\'; + x[1] = '\\'; + x[5] = '\0'; + + for (i = 4; i > 1; i--) { + x[i] = (val & 7) + 48; + val >>= 3; + } + + return x; +} + +// convert non-ascii bytes to octal escape sequences +int +convert_to_pgbinary(unsigned char *in, char *out, int len) +{ +int i, o=0; + + + for (i = 0; i < len; i++) { + mylog("convert_to_pgbinary: in[%d] = %d, %c\n", i, in[i], in[i]); + if (in[i] < 32 || in[i] > 126) { + strcpy(&out[o], conv_to_octal(in[i])); + o += 5; + } + else + out[o++] = in[i]; + + } + + mylog("convert_to_pgbinary: returning %d, out='%.*s'\n", o, o, out); + + return o; +} + + +/* 1. get oid (from 'value') + 2. open the large object + 3. read from the large object (handle multiple GetData) + 4. close when read less than requested? -OR- + lseek/read each time + handle case where application receives truncated and + decides not to continue reading. + + CURRENTLY, ONLY LONGVARBINARY is handled, since that is the only + data type currently mapped to a PG_TYPE_LO. But, if any other types + are desired to map to a large object (PG_TYPE_LO), then that would + need to be handled here. For example, LONGVARCHAR could possibly be + mapped to PG_TYPE_LO someday, instead of PG_TYPE_TEXT as it is now. +*/ +int +convert_lo(StatementClass *stmt, void *value, Int2 fCType, PTR rgbValue, + SDWORD cbValueMax, SDWORD *pcbValue, char multiple) +{ +Oid oid; +int retval; + + /* if this is the first call for this column, + open the large object for reading + */ + if ( ! multiple) { + oid = atoi(value); + stmt->lobj_fd = lo_open(stmt->hdbc, oid, INV_READ); + if (stmt->lobj_fd < 0) { + stmt->errornumber = STMT_EXEC_ERROR; + stmt->errormsg = "Couldnt open large object for writing."; + return COPY_GENERAL_ERROR; + } + } + + if (stmt->lobj_fd < 0) + return COPY_NO_DATA_FOUND; + + retval = lo_read(stmt->hdbc, stmt->lobj_fd, rgbValue, cbValueMax); + if (retval < 0) { + lo_close(stmt->hdbc, stmt->lobj_fd); + stmt->lobj_fd = -1; + + stmt->errornumber = STMT_EXEC_ERROR; + stmt->errormsg = "Error reading from large object."; + return COPY_GENERAL_ERROR; + } + else if (retval < cbValueMax) { /* success, all done */ + lo_close(stmt->hdbc, stmt->lobj_fd); + stmt->lobj_fd = -1; /* prevent further reading */ + + if (pcbValue) + *pcbValue = retval; + + return COPY_OK; + } + else { /* retval == cbVaueMax -- assume truncated */ + if (pcbValue) + *pcbValue = SQL_NO_TOTAL; + + return COPY_RESULT_TRUNCATED; + + } +} diff --git a/src/interfaces/odbc/convert.h b/src/interfaces/odbc/convert.h index 2826589a6a..56f798f8e8 100644 --- a/src/interfaces/odbc/convert.h +++ b/src/interfaces/odbc/convert.h @@ -1,11 +1,11 @@ - -/* File: convert.h - * - * Description: See "convert.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: convert.h + * + * Description: See "convert.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __CONVERT_H__ #define __CONVERT_H__ @@ -13,10 +13,12 @@ #include "psqlodbc.h" /* copy_and_convert results */ -#define COPY_OK 0 +#define COPY_OK 0 #define COPY_UNSUPPORTED_TYPE 1 #define COPY_UNSUPPORTED_CONVERSION 2 #define COPY_RESULT_TRUNCATED 3 +#define COPY_GENERAL_ERROR 4 +#define COPY_NO_DATA_FOUND 5 typedef struct { int m; @@ -27,21 +29,21 @@ typedef struct { int ss; } SIMPLE_TIME; -int copy_and_convert_field_bindinfo(Int4 field_type, void *value, BindInfoClass *bic); -int copy_and_convert_field(Int4 field_type, void *value, - Int2 fCType, PTR rgbValue, SDWORD cbValueMax, SDWORD *pcbValue); +int copy_and_convert_field_bindinfo(StatementClass *stmt, Int4 field_type, void *value, int col); +int copy_and_convert_field(StatementClass *stmt, Int4 field_type, void *value, Int2 fCType, + PTR rgbValue, SDWORD cbValueMax, SDWORD *pcbValue, char multiple); int copy_statement_with_parameters(StatementClass *stmt); char *convert_escape(char *value); char *convert_money(char *s); -int monthToNumber(char *mon); -char *convert_time(SIMPLE_TIME *st); char parse_datetime(char *buf, SIMPLE_TIME *st); -char *convert_linefeeds(char *s, char *dst, size_t max); -char *convert_returns(char *si, char *dst, int used); - -int convert_pgbinary_to_char(char *value, char *rgbValue, int cbValueMax); -int convert_from_pgbinary(unsigned char *value, unsigned char *rgbValue, int cbValueMax); -int convert_to_pgbinary(unsigned char *in, char *out, int len); +char *convert_linefeeds(char *s, char *dst, size_t max); +char *convert_special_chars(char *si, char *dst, int used); + +int convert_pgbinary_to_char(char *value, char *rgbValue, int cbValueMax); +int convert_from_pgbinary(unsigned char *value, unsigned char *rgbValue, int cbValueMax); +int convert_to_pgbinary(unsigned char *in, char *out, int len); +int convert_lo(StatementClass *stmt, void *value, Int2 fCType, PTR rgbValue, + SDWORD cbValueMax, SDWORD *pcbValue, char multiple); #endif diff --git a/src/interfaces/odbc/dlg_specific.c b/src/interfaces/odbc/dlg_specific.c new file mode 100644 index 0000000000..558fa66926 --- /dev/null +++ b/src/interfaces/odbc/dlg_specific.c @@ -0,0 +1,762 @@ + +/* Module: dlg_specific.c + * + * Description: This module contains any specific code for handling + * dialog boxes such as driver/datasource options. Both the + * ConfigDSN() and the SQLDriverConnect() functions use + * functions in this module. If you were to add a new option + * to any dialog box, you would most likely only have to change + * things in here rather than in 2 separate places as before. + * + * Classes: none + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ + +#include "dlg_specific.h" + +extern GLOBAL_VALUES globals; + +void +SetDlgStuff(HWND hdlg, ConnInfo *ci) +{ + /* If driver attribute NOT present, then set the datasource name and description */ + if (ci->driver[0] == '\0') { + SetDlgItemText(hdlg, IDC_DSNAME, ci->dsn); + SetDlgItemText(hdlg, IDC_DESC, ci->desc); + } + + SetDlgItemText(hdlg, IDC_DATABASE, ci->database); + SetDlgItemText(hdlg, IDC_SERVER, ci->server); + SetDlgItemText(hdlg, IDC_USER, ci->username); + SetDlgItemText(hdlg, IDC_PASSWORD, ci->password); + SetDlgItemText(hdlg, IDC_PORT, ci->port); +} + +void +GetDlgStuff(HWND hdlg, ConnInfo *ci) +{ + GetDlgItemText(hdlg, IDC_DESC, ci->desc, sizeof(ci->desc)); + + GetDlgItemText(hdlg, IDC_DATABASE, ci->database, sizeof(ci->database)); + GetDlgItemText(hdlg, IDC_SERVER, ci->server, sizeof(ci->server)); + GetDlgItemText(hdlg, IDC_USER, ci->username, sizeof(ci->username)); + GetDlgItemText(hdlg, IDC_PASSWORD, ci->password, sizeof(ci->password)); + GetDlgItemText(hdlg, IDC_PORT, ci->port, sizeof(ci->port)); +} + + + +int CALLBACK driver_optionsProc(HWND hdlg, + WORD wMsg, + WPARAM wParam, + LPARAM lParam) +{ + switch (wMsg) { + case WM_INITDIALOG: + + CheckDlgButton(hdlg, DRV_COMMLOG, globals.commlog); + CheckDlgButton(hdlg, DRV_OPTIMIZER, globals.disable_optimizer); + CheckDlgButton(hdlg, DRV_UNIQUEINDEX, globals.unique_index); + CheckDlgButton(hdlg, DRV_READONLY, globals.readonly); + CheckDlgButton(hdlg, DRV_USEDECLAREFETCH, globals.use_declarefetch); + + /* Unknown (Default) Data Type sizes */ + switch(globals.unknown_sizes) { + case UNKNOWNS_AS_DONTKNOW: + CheckDlgButton(hdlg, DRV_UNKNOWN_DONTKNOW, 1); + break; + case UNKNOWNS_AS_LONGEST: + CheckDlgButton(hdlg, DRV_UNKNOWN_LONGEST, 1); + break; + case UNKNOWNS_AS_MAX: + default: + CheckDlgButton(hdlg, DRV_UNKNOWN_MAX, 1); + break; + } + + CheckDlgButton(hdlg, DRV_TEXT_LONGVARCHAR, globals.text_as_longvarchar); + CheckDlgButton(hdlg, DRV_UNKNOWNS_LONGVARCHAR, globals.unknowns_as_longvarchar); + CheckDlgButton(hdlg, DRV_BOOLS_CHAR, globals.bools_as_char); + + SetDlgItemInt(hdlg, DRV_CACHE_SIZE, globals.fetch_max, FALSE); + SetDlgItemInt(hdlg, DRV_VARCHAR_SIZE, globals.max_varchar_size, FALSE); + SetDlgItemInt(hdlg, DRV_LONGVARCHAR_SIZE, globals.max_longvarchar_size, TRUE); + + SetDlgItemText(hdlg, DRV_EXTRASYSTABLEPREFIXES, globals.extra_systable_prefixes); + + /* Driver Connection Settings */ + SetDlgItemText(hdlg, DRV_CONNSETTINGS, globals.conn_settings); + + break; + + case WM_COMMAND: + switch (GET_WM_COMMAND_ID(wParam, lParam)) { + case IDOK: + + globals.commlog = IsDlgButtonChecked(hdlg, DRV_COMMLOG); + globals.disable_optimizer = IsDlgButtonChecked(hdlg, DRV_OPTIMIZER); + globals.unique_index = IsDlgButtonChecked(hdlg, DRV_UNIQUEINDEX); + globals.readonly = IsDlgButtonChecked(hdlg, DRV_READONLY); + globals.use_declarefetch = IsDlgButtonChecked(hdlg, DRV_USEDECLAREFETCH); + + /* Unknown (Default) Data Type sizes */ + if (IsDlgButtonChecked(hdlg, DRV_UNKNOWN_MAX)) + globals.unknown_sizes = UNKNOWNS_AS_MAX; + else if (IsDlgButtonChecked(hdlg, DRV_UNKNOWN_DONTKNOW)) + globals.unknown_sizes = UNKNOWNS_AS_DONTKNOW; + else if (IsDlgButtonChecked(hdlg, DRV_UNKNOWN_LONGEST)) + globals.unknown_sizes = UNKNOWNS_AS_LONGEST; + else + globals.unknown_sizes = UNKNOWNS_AS_MAX; + + globals.text_as_longvarchar = IsDlgButtonChecked(hdlg, DRV_TEXT_LONGVARCHAR); + globals.unknowns_as_longvarchar = IsDlgButtonChecked(hdlg, DRV_UNKNOWNS_LONGVARCHAR); + globals.bools_as_char = IsDlgButtonChecked(hdlg, DRV_BOOLS_CHAR); + + globals.fetch_max = GetDlgItemInt(hdlg, DRV_CACHE_SIZE, NULL, FALSE); + globals.max_varchar_size = GetDlgItemInt(hdlg, DRV_VARCHAR_SIZE, NULL, FALSE); + globals.max_longvarchar_size= GetDlgItemInt(hdlg, DRV_LONGVARCHAR_SIZE, NULL, TRUE); // allows for SQL_NO_TOTAL + + GetDlgItemText(hdlg, DRV_EXTRASYSTABLEPREFIXES, globals.extra_systable_prefixes, sizeof(globals.extra_systable_prefixes)); + + /* Driver Connection Settings */ + GetDlgItemText(hdlg, DRV_CONNSETTINGS, globals.conn_settings, sizeof(globals.conn_settings)); + + updateGlobals(); + + // fall through + + case IDCANCEL: + EndDialog(hdlg, GET_WM_COMMAND_ID(wParam, lParam) == IDOK); + return TRUE; + + case IDDEFAULTS: + CheckDlgButton(hdlg, DRV_COMMLOG, DEFAULT_COMMLOG); + CheckDlgButton(hdlg, DRV_OPTIMIZER, DEFAULT_OPTIMIZER); + CheckDlgButton(hdlg, DRV_UNIQUEINDEX, DEFAULT_UNIQUEINDEX); + CheckDlgButton(hdlg, DRV_READONLY, DEFAULT_READONLY); + CheckDlgButton(hdlg, DRV_USEDECLAREFETCH, DEFAULT_USEDECLAREFETCH); + + /* Unknown Sizes */ + CheckDlgButton(hdlg, DRV_UNKNOWN_DONTKNOW, 0); + CheckDlgButton(hdlg, DRV_UNKNOWN_LONGEST, 0); + CheckDlgButton(hdlg, DRV_UNKNOWN_MAX, 0); + switch(DEFAULT_UNKNOWNSIZES) { + case UNKNOWNS_AS_DONTKNOW: + CheckDlgButton(hdlg, DRV_UNKNOWN_DONTKNOW, 1); + break; + case UNKNOWNS_AS_LONGEST: + CheckDlgButton(hdlg, DRV_UNKNOWN_LONGEST, 1); + break; + case UNKNOWNS_AS_MAX: + CheckDlgButton(hdlg, DRV_UNKNOWN_MAX, 1); + break; + } + + CheckDlgButton(hdlg, DRV_TEXT_LONGVARCHAR, DEFAULT_TEXTASLONGVARCHAR); + CheckDlgButton(hdlg, DRV_UNKNOWNS_LONGVARCHAR, DEFAULT_UNKNOWNSASLONGVARCHAR); + CheckDlgButton(hdlg, DRV_BOOLS_CHAR, DEFAULT_BOOLSASCHAR); + + SetDlgItemInt(hdlg, DRV_CACHE_SIZE, FETCH_MAX, FALSE); + SetDlgItemInt(hdlg, DRV_VARCHAR_SIZE, MAX_VARCHAR_SIZE, FALSE); + SetDlgItemInt(hdlg, DRV_LONGVARCHAR_SIZE, TEXT_FIELD_SIZE, TRUE); + + SetDlgItemText(hdlg, DRV_EXTRASYSTABLEPREFIXES, DEFAULT_EXTRASYSTABLEPREFIXES); + + /* Driver Connection Settings */ + SetDlgItemText(hdlg, DRV_CONNSETTINGS, ""); + + break; + } + + } + + return FALSE; +} + +int CALLBACK ds_optionsProc(HWND hdlg, + WORD wMsg, + WPARAM wParam, + LPARAM lParam) +{ +ConnInfo *ci; +char buf[128]; +// int unknown_sizes; + + switch (wMsg) { + case WM_INITDIALOG: + ci = (ConnInfo *) lParam; + SetWindowLong(hdlg, DWL_USER, lParam); // save for OK + + /* Change window caption */ + if (ci->driver[0]) + SetWindowText(hdlg, "Advanced Options (Connection)"); + else { + sprintf(buf, "Advanced Options (%s)", ci->dsn); + SetWindowText(hdlg, buf); + } + + /* Readonly */ + CheckDlgButton(hdlg, DS_READONLY, atoi(ci->readonly)); + + /* Protocol */ + if (strncmp(ci->protocol, PG62, strlen(PG62)) == 0) + CheckDlgButton(hdlg, DS_PG62, 1); + else + CheckDlgButton(hdlg, DS_PG62, 0); + + /* Unknown Data Type sizes -- currently only needed in Driver options. + switch (atoi(ci->unknown_sizes)) { + case UNKNOWNS_AS_DONTKNOW: + CheckDlgButton(hdlg, DS_UNKNOWN_DONTKNOW, 1); + break; + case UNKNOWNS_AS_LONGEST: + CheckDlgButton(hdlg, DS_UNKNOWN_LONGEST, 1); + break; + case UNKNOWNS_AS_MAX: + default: + CheckDlgButton(hdlg, DS_UNKNOWN_MAX, 1); + break; + } + */ + + CheckDlgButton(hdlg, DS_SHOWOIDCOLUMN, atoi(ci->show_oid_column)); + CheckDlgButton(hdlg, DS_FAKEOIDINDEX, atoi(ci->fake_oid_index)); + CheckDlgButton(hdlg, DS_SHOWSYSTEMTABLES, atoi(ci->show_system_tables)); + + EnableWindow(GetDlgItem(hdlg, DS_FAKEOIDINDEX), atoi(ci->show_oid_column)); + + /* Datasource Connection Settings */ + SetDlgItemText(hdlg, DS_CONNSETTINGS, ci->conn_settings); + break; + + + case WM_COMMAND: + switch (GET_WM_COMMAND_ID(wParam, lParam)) { + case DS_SHOWOIDCOLUMN: + mylog("WM_COMMAND: DS_SHOWOIDCOLUMN\n"); + EnableWindow(GetDlgItem(hdlg, DS_FAKEOIDINDEX), IsDlgButtonChecked(hdlg, DS_SHOWOIDCOLUMN)); + return TRUE; + + + case IDOK: + + ci = (ConnInfo *)GetWindowLong(hdlg, DWL_USER); + mylog("IDOK: got ci = %u\n", ci); + + /* Readonly */ + sprintf(ci->readonly, "%d", IsDlgButtonChecked(hdlg, DS_READONLY)); + + /* Protocol */ + if ( IsDlgButtonChecked(hdlg, DS_PG62)) + strcpy(ci->protocol, PG62); + else + ci->protocol[0] = '\0'; + + + /* Unknown Data Type sizes -- currently only needed in Driver options. + if (IsDlgButtonChecked(hdlg, DS_UNKNOWN_MAX)) + unknown_sizes = UNKNOWNS_AS_MAX; + else if (IsDlgButtonChecked(hdlg, DS_UNKNOWN_DONTKNOW)) + unknown_sizes = UNKNOWNS_AS_DONTKNOW; + else if (IsDlgButtonChecked(hdlg, DS_UNKNOWN_LONGEST)) + unknown_sizes = UNKNOWNS_AS_LONGEST; + else + unknown_sizes = UNKNOWNS_AS_MAX; + + sprintf(ci->unknown_sizes, "%d", unknown_sizes); + */ + + sprintf(ci->show_system_tables, "%d", IsDlgButtonChecked(hdlg, DS_SHOWSYSTEMTABLES)); + + /* OID Options*/ + sprintf(ci->fake_oid_index, "%d", IsDlgButtonChecked(hdlg, DS_FAKEOIDINDEX)); + sprintf(ci->show_oid_column, "%d", IsDlgButtonChecked(hdlg, DS_SHOWOIDCOLUMN)); + + /* Datasource Connection Settings */ + GetDlgItemText(hdlg, DS_CONNSETTINGS, ci->conn_settings, sizeof(ci->conn_settings)); + + + // fall through + + case IDCANCEL: + EndDialog(hdlg, GET_WM_COMMAND_ID(wParam, lParam) == IDOK); + return TRUE; + } + } + + return FALSE; +} + +void +makeConnectString(char *connect_string, ConnInfo *ci) +{ +char got_dsn = (ci->dsn[0] != '\0'); + + sprintf(connect_string, "%s=%s;DATABASE=%s;SERVER=%s;PORT=%s;UID=%s;READONLY=%s;PWD=%s;PROTOCOL=%s;FAKEOIDINDEX=%s;SHOWOIDCOLUMN=%s;SHOWSYSTEMTABLES=%s;CONNSETTINGS=%s", + got_dsn ? "DSN" : "DRIVER", + got_dsn ? ci->dsn : ci->driver, + ci->database, + ci->server, + ci->port, + ci->username, + ci->readonly, + ci->password, + ci->protocol, +// ci->unknown_sizes, -- currently only needed in Driver options. + ci->fake_oid_index, + ci->show_oid_column, + ci->show_system_tables, + ci->conn_settings); +} + +void +copyAttributes(ConnInfo *ci, char *attribute, char *value) +{ + + if(stricmp(attribute, "DSN") == 0) + strcpy(ci->dsn, value); + + else if(stricmp(attribute, "driver") == 0) + strcpy(ci->driver, value); + + else if(stricmp(attribute, INI_DATABASE) == 0) + strcpy(ci->database, value); + + else if(stricmp(attribute, INI_SERVER) == 0 || stricmp(attribute, "server") == 0) + strcpy(ci->server, value); + + else if(stricmp(attribute, INI_USER) == 0 || stricmp(attribute, "uid") == 0) + strcpy(ci->username, value); + + else if(stricmp(attribute, INI_PASSWORD) == 0 || stricmp(attribute, "pwd") == 0) + strcpy(ci->password, value); + + else if(stricmp(attribute, INI_PORT) == 0) + strcpy(ci->port, value); + + else if (stricmp(attribute, INI_READONLY) == 0) + strcpy(ci->readonly, value); + + else if (stricmp(attribute, INI_PROTOCOL) == 0) + strcpy(ci->protocol, value); + + /* + else if (stricmp(attribute, INI_UNKNOWNSIZES) == 0) + strcpy(ci->unknown_sizes, value); + */ + else if (stricmp(attribute, INI_SHOWOIDCOLUMN) == 0) + strcpy(ci->show_oid_column, value); + + else if (stricmp(attribute, INI_FAKEOIDINDEX) == 0) + strcpy(ci->fake_oid_index, value); + + else if (stricmp(attribute, INI_SHOWSYSTEMTABLES) == 0) + strcpy(ci->show_system_tables, value); + + else if (stricmp(attribute, INI_CONNSETTINGS) == 0) + strcpy(ci->conn_settings, value); + + + mylog("copyAttributes: DSN='%s',server='%s',dbase='%s',user='%s',passwd='%s',port='%s',readonly='%s',protocol='%s', conn_settings='%s')\n", + ci->dsn, + ci->server, + ci->database, + ci->username, + ci->password, + ci->port, + ci->readonly, + ci->protocol, + // ci->unknown_sizes, + ci->conn_settings); + +} + +void +getDSNdefaults(ConnInfo *ci) +{ + if (ci->port[0] == '\0') + strcpy(ci->port, DEFAULT_PORT); + + if (ci->readonly[0] == '\0') + sprintf(ci->readonly, "%d", globals.readonly); + +/* -- currently only needed in Driver options. + if (ci->unknown_sizes[0] == '\0') + sprintf(ci->unknown_sizes, "%d", globals.unknown_sizes); +*/ + if (ci->fake_oid_index[0] == '\0') + sprintf(ci->fake_oid_index, "%d", DEFAULT_FAKEOIDINDEX); + + if (ci->show_oid_column[0] == '\0') + sprintf(ci->show_oid_column, "%d", DEFAULT_SHOWOIDCOLUMN); + + if (ci->show_system_tables[0] == '\0') + sprintf(ci->show_system_tables, "%d", DEFAULT_SHOWSYSTEMTABLES); + +} + + +void +getDSNinfo(ConnInfo *ci, char overwrite) +{ +char *DSN = ci->dsn; + + // If a driver keyword was present, then dont use a DSN and return. + // If DSN is null and no driver, then use the default datasource. + if ( DSN[0] == '\0') { + if ( ci->driver[0] != '\0') + return; + else + strcpy(DSN, INI_DSN); + } + + // Proceed with getting info for the given DSN. + + if ( ci->desc[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_KDESC, "", ci->desc, sizeof(ci->desc), ODBC_INI); + + if ( ci->server[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_SERVER, "", ci->server, sizeof(ci->server), ODBC_INI); + + if ( ci->database[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_DATABASE, "", ci->database, sizeof(ci->database), ODBC_INI); + + if ( ci->username[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_USER, "", ci->username, sizeof(ci->username), ODBC_INI); + + if ( ci->password[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_PASSWORD, "", ci->password, sizeof(ci->password), ODBC_INI); + + if ( ci->port[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_PORT, "", ci->port, sizeof(ci->port), ODBC_INI); + + if ( ci->readonly[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_READONLY, "", ci->readonly, sizeof(ci->readonly), ODBC_INI); + + /* -- currently only needed in Driver options. + if ( ci->unknown_sizes[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_UNKNOWNSIZES, "", ci->unknown_sizes, sizeof(ci->unknown_sizes), ODBC_INI); + */ + if ( ci->show_oid_column[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_SHOWOIDCOLUMN, "", ci->show_oid_column, sizeof(ci->show_oid_column), ODBC_INI); + + if ( ci->fake_oid_index[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_FAKEOIDINDEX, "", ci->fake_oid_index, sizeof(ci->fake_oid_index), ODBC_INI); + + if ( ci->show_system_tables[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_SHOWSYSTEMTABLES, "", ci->show_system_tables, sizeof(ci->show_system_tables), ODBC_INI); + + if ( ci->protocol[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_PROTOCOL, "", ci->protocol, sizeof(ci->protocol), ODBC_INI); + + if ( ci->conn_settings[0] == '\0' || overwrite) + SQLGetPrivateProfileString(DSN, INI_CONNSETTINGS, "", ci->conn_settings, sizeof(ci->conn_settings), ODBC_INI); + + qlog("DSN info: DSN='%s',server='%s',port='%s',dbase='%s',user='%s',passwd='%s'\n", + DSN, + ci->server, + ci->port, + ci->database, + ci->username, + ci->password); + qlog(" readonly='%s',protocol='%s',showoid='%s',fakeoidindex='%s',showsystable='%s'\n", + ci->readonly, + ci->protocol, + ci->show_oid_column, + ci->fake_oid_index, + // ci->unknown_sizes, + ci->show_system_tables); + qlog(" conn_settings='%s'\n", + ci->conn_settings); +} + + +/* This is for datasource based options only */ +void +writeDSNinfo(ConnInfo *ci) +{ +char *DSN = ci->dsn; + + SQLWritePrivateProfileString(DSN, + INI_KDESC, + ci->desc, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_DATABASE, + ci->database, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_SERVER, + ci->server, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_PORT, + ci->port, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_USER, + ci->username, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_PASSWORD, + ci->password, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_READONLY, + ci->readonly, + ODBC_INI); + + /* -- currently only needed in Driver options. + SQLWritePrivateProfileString(DSN, + INI_UNKNOWNSIZES, + ci->unknown_sizes, + ODBC_INI); + */ + SQLWritePrivateProfileString(DSN, + INI_SHOWOIDCOLUMN, + ci->show_oid_column, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_FAKEOIDINDEX, + ci->fake_oid_index, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_SHOWSYSTEMTABLES, + ci->show_system_tables, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_PROTOCOL, + ci->protocol, + ODBC_INI); + + SQLWritePrivateProfileString(DSN, + INI_CONNSETTINGS, + ci->conn_settings, + ODBC_INI); +} + + +/* This function reads the ODBCINST.INI portion of + the registry and gets any driver defaults. +*/ +void getGlobalDefaults(void) +{ +char temp[128]; + + + // Fetch Count is stored in driver section + SQLGetPrivateProfileString(DBMS_NAME, INI_FETCH, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] ) { + globals.fetch_max = atoi(temp); + /* sanity check if using cursors */ + if (globals.fetch_max <= 0) + globals.fetch_max = FETCH_MAX; + } + + else + globals.fetch_max = FETCH_MAX; + + + // Socket Buffersize is stored in driver section + SQLGetPrivateProfileString(DBMS_NAME, INI_SOCKET, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] ) + globals.socket_buffersize = atoi(temp); + else + globals.socket_buffersize = SOCK_BUFFER_SIZE; + + + // Debug is stored in the driver section + SQLGetPrivateProfileString(DBMS_NAME, INI_DEBUG, "0", + temp, sizeof(temp), ODBCINST_INI); + globals.debug = atoi(temp); + + + // CommLog is stored in the driver section + SQLGetPrivateProfileString(DBMS_NAME, INI_COMMLOG, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.commlog = DEFAULT_COMMLOG; + else + globals.commlog = atoi(temp); + + + // Optimizer is stored in the driver section only + SQLGetPrivateProfileString(DBMS_NAME, INI_OPTIMIZER, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.disable_optimizer = DEFAULT_OPTIMIZER; + else + globals.disable_optimizer = atoi(temp); + + + // Recognize Unique Index is stored in the driver section only + SQLGetPrivateProfileString(DBMS_NAME, INI_UNIQUEINDEX, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.unique_index = DEFAULT_UNIQUEINDEX; + else + globals.unique_index = atoi(temp); + + + // Unknown Sizes is stored in the driver section AND per datasource + SQLGetPrivateProfileString(DBMS_NAME, INI_UNKNOWNSIZES, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.unknown_sizes = DEFAULT_UNKNOWNSIZES; + else + globals.unknown_sizes = atoi(temp); + + + // Readonly is stored in the driver section AND per datasource + SQLGetPrivateProfileString(DBMS_NAME, INI_READONLY, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.readonly = DEFAULT_READONLY; + else + globals.readonly = atoi(temp); + + + // UseDeclareFetch is stored in the driver section only + SQLGetPrivateProfileString(DBMS_NAME, INI_USEDECLAREFETCH, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.use_declarefetch = DEFAULT_USEDECLAREFETCH; + else + globals.use_declarefetch = atoi(temp); + + + // Max Varchar Size + SQLGetPrivateProfileString(DBMS_NAME, INI_MAXVARCHARSIZE, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.max_varchar_size = MAX_VARCHAR_SIZE; + else + globals.max_varchar_size = atoi(temp); + + // Max TextField Size + SQLGetPrivateProfileString(DBMS_NAME, INI_MAXLONGVARCHARSIZE, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.max_longvarchar_size = TEXT_FIELD_SIZE; + else + globals.max_longvarchar_size = atoi(temp); + + // Text As LongVarchar + SQLGetPrivateProfileString(DBMS_NAME, INI_TEXTASLONGVARCHAR, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.text_as_longvarchar = DEFAULT_TEXTASLONGVARCHAR; + else + globals.text_as_longvarchar = atoi(temp); + + // Unknowns As LongVarchar + SQLGetPrivateProfileString(DBMS_NAME, INI_UNKNOWNSASLONGVARCHAR, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.unknowns_as_longvarchar = DEFAULT_UNKNOWNSASLONGVARCHAR; + else + globals.unknowns_as_longvarchar = atoi(temp); + + // Bools As Char + SQLGetPrivateProfileString(DBMS_NAME, INI_BOOLSASCHAR, "", + temp, sizeof(temp), ODBCINST_INI); + if ( temp[0] == '\0') + globals.bools_as_char = DEFAULT_BOOLSASCHAR; + else + globals.bools_as_char = atoi(temp); + + + // Extra System Table prefixes + SQLGetPrivateProfileString(DBMS_NAME, INI_EXTRASYSTABLEPREFIXES, "@@@", + globals.extra_systable_prefixes, sizeof(globals.extra_systable_prefixes), ODBCINST_INI); + if ( ! strcmp(globals.extra_systable_prefixes, "@@@")) { + strcpy(globals.extra_systable_prefixes, DEFAULT_EXTRASYSTABLEPREFIXES); + } + mylog("globals.extra_systable_prefixes = '%s'\n", globals.extra_systable_prefixes); + + + // ConnSettings is stored in the driver section and per datasource for override + SQLGetPrivateProfileString(DBMS_NAME, INI_CONNSETTINGS, "", + globals.conn_settings, sizeof(globals.conn_settings), ODBCINST_INI); + + +} + + +/* This function writes any global parameters (that can be manipulated) + to the ODBCINST.INI portion of the registry +*/ +void updateGlobals(void) +{ +char tmp[128]; + + sprintf(tmp, "%d", globals.fetch_max); + SQLWritePrivateProfileString(DBMS_NAME, + INI_FETCH, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.commlog); + SQLWritePrivateProfileString(DBMS_NAME, + INI_COMMLOG, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.disable_optimizer); + SQLWritePrivateProfileString(DBMS_NAME, + INI_OPTIMIZER, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.unique_index); + SQLWritePrivateProfileString(DBMS_NAME, + INI_UNIQUEINDEX, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.readonly); + SQLWritePrivateProfileString(DBMS_NAME, + INI_READONLY, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.use_declarefetch); + SQLWritePrivateProfileString(DBMS_NAME, + INI_USEDECLAREFETCH, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.unknown_sizes); + SQLWritePrivateProfileString(DBMS_NAME, + INI_UNKNOWNSIZES, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.text_as_longvarchar); + SQLWritePrivateProfileString(DBMS_NAME, + INI_TEXTASLONGVARCHAR, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.unknowns_as_longvarchar); + SQLWritePrivateProfileString(DBMS_NAME, + INI_UNKNOWNSASLONGVARCHAR, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.bools_as_char); + SQLWritePrivateProfileString(DBMS_NAME, + INI_BOOLSASCHAR, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.max_varchar_size); + SQLWritePrivateProfileString(DBMS_NAME, + INI_MAXVARCHARSIZE, tmp, ODBCINST_INI); + + sprintf(tmp, "%d", globals.max_longvarchar_size); + SQLWritePrivateProfileString(DBMS_NAME, + INI_MAXLONGVARCHARSIZE, tmp, ODBCINST_INI); + + SQLWritePrivateProfileString(DBMS_NAME, + INI_EXTRASYSTABLEPREFIXES, globals.extra_systable_prefixes, ODBCINST_INI); + + SQLWritePrivateProfileString(DBMS_NAME, + INI_CONNSETTINGS, globals.conn_settings, ODBCINST_INI); +} diff --git a/src/interfaces/odbc/dlg_specific.h b/src/interfaces/odbc/dlg_specific.h new file mode 100644 index 0000000000..bedf222937 --- /dev/null +++ b/src/interfaces/odbc/dlg_specific.h @@ -0,0 +1,103 @@ + +/* File: dlg_specific.h + * + * Description: See "dlg_specific.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ + +#ifndef __DLG_SPECIFIC_H__ +#define __DLG_SPECIFIC_H__ + +#include "psqlodbc.h" +#include "connection.h" +#include +#include +#include +#include "resource.h" + +/* Unknown data type sizes */ +#define UNKNOWNS_AS_MAX 0 +#define UNKNOWNS_AS_DONTKNOW 1 +#define UNKNOWNS_AS_LONGEST 2 + +/* INI File Stuff */ +#define ODBC_INI "ODBC.INI" /* ODBC initialization file */ +#define ODBCINST_INI "ODBCINST.INI" /* ODBC Installation file */ + +#define INI_DSN DBMS_NAME /* Name of default Datasource in ini file (not used?) */ +#define INI_KDESC "Description" /* Data source description */ +#define INI_SERVER "Servername" /* Name of Server running the Postgres service */ +#define INI_PORT "Port" /* Port on which the Postmaster is listening */ +#define INI_DATABASE "Database" /* Database Name */ +#define INI_USER "Username" /* Default User Name */ +#define INI_PASSWORD "Password" /* Default Password */ +#define INI_DEBUG "Debug" /* Debug flag */ +#define INI_FETCH "Fetch" /* Fetch Max Count */ +#define INI_SOCKET "Socket" /* Socket buffer size */ +#define INI_READONLY "ReadOnly" /* Database is read only */ +#define INI_COMMLOG "CommLog" /* Communication to backend logging */ +#define INI_PROTOCOL "Protocol" /* What protocol (6.2) */ +#define INI_OPTIMIZER "Optimizer" /* Use backend genetic optimizer */ +#define INI_CONNSETTINGS "ConnSettings" /* Anything to send to backend on successful connection */ +#define INI_UNIQUEINDEX "UniqueIndex" /* Recognize unique indexes */ +#define INI_UNKNOWNSIZES "UnknownSizes" /* How to handle unknown result set sizes */ + +#define INI_USEDECLAREFETCH "UseDeclareFetch" /* Use Declare/Fetch cursors */ + +/* More ini stuff */ +#define INI_TEXTASLONGVARCHAR "TextAsLongVarchar" +#define INI_UNKNOWNSASLONGVARCHAR "UnknownsAsLongVarchar" +#define INI_BOOLSASCHAR "BoolsAsChar" +#define INI_MAXVARCHARSIZE "MaxVarcharSize" +#define INI_MAXLONGVARCHARSIZE "MaxLongVarcharSize" + +#define INI_FAKEOIDINDEX "FakeOidIndex" +#define INI_SHOWOIDCOLUMN "ShowOidColumn" +#define INI_SHOWSYSTEMTABLES "ShowSystemTables" +#define INI_EXTRASYSTABLEPREFIXES "ExtraSysTablePrefixes" + +/* Connection Defaults */ +#define DEFAULT_PORT "5432" +#define DEFAULT_READONLY 1 +#define DEFAULT_USEDECLAREFETCH 1 +#define DEFAULT_TEXTASLONGVARCHAR 1 +#define DEFAULT_UNKNOWNSASLONGVARCHAR 0 +#define DEFAULT_BOOLSASCHAR 1 +#define DEFAULT_OPTIMIZER 1 // disable +#define DEFAULT_UNIQUEINDEX 0 // dont recognize +#define DEFAULT_COMMLOG 0 // dont log +#define DEFAULT_UNKNOWNSIZES UNKNOWNS_AS_MAX + + +#define DEFAULT_FAKEOIDINDEX 0 +#define DEFAULT_SHOWOIDCOLUMN 0 +#define DEFAULT_SHOWSYSTEMTABLES 0 // dont show system tables + +#define DEFAULT_EXTRASYSTABLEPREFIXES "dd_;" + +/* prototypes */ +void updateGlobals(void); +void getGlobalDefaults(void); + +void SetDlgStuff(HWND hdlg, ConnInfo *ci); +void GetDlgStuff(HWND hdlg, ConnInfo *ci); + +int CALLBACK driver_optionsProc(HWND hdlg, + WORD wMsg, + WPARAM wParam, + LPARAM lParam); +int CALLBACK ds_optionsProc(HWND hdlg, + WORD wMsg, + WPARAM wParam, + LPARAM lParam); + +void makeConnectString(char *connect_string, ConnInfo *ci); +void copyAttributes(ConnInfo *ci, char *attribute, char *value); +void getDSNdefaults(ConnInfo *ci); + +void getDSNinfo(ConnInfo *ci, char overwrite); +void writeDSNinfo(ConnInfo *ci); + +#endif diff --git a/src/interfaces/odbc/drvconn.c b/src/interfaces/odbc/drvconn.c index ee0d8252d7..89cb23b189 100644 --- a/src/interfaces/odbc/drvconn.c +++ b/src/interfaces/odbc/drvconn.c @@ -1,16 +1,16 @@ - -/* Module: drvconn.c - * - * Description: This module contains only routines related to - * implementing SQLDriverConnect. - * - * Classes: n/a - * - * API functions: SQLDriverConnect - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: drvconn.c + * + * Description: This module contains only routines related to + * implementing SQLDriverConnect. + * + * Classes: n/a + * + * API functions: SQLDriverConnect + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include #include @@ -26,15 +26,17 @@ #include #include "resource.h" +#include "dlg_specific.h" + /* prototypes */ BOOL FAR PASCAL dconn_FDriverConnectProc(HWND hdlg, UINT wMsg, WPARAM wParam, LPARAM lParam); RETCODE dconn_DoDialog(HWND hwnd, ConnInfo *ci); void dconn_get_connect_attributes(UCHAR FAR *connect_string, ConnInfo *ci); - extern HINSTANCE NEAR s_hModule; /* Saved module handle. */ extern GLOBAL_VALUES globals; + RETCODE SQL_API SQLDriverConnect( HDBC hdbc, HWND hwnd, @@ -67,10 +69,10 @@ char password_required = FALSE; // If the ConnInfo in the hdbc is missing anything, // this function will fill them in from the registry (assuming // of course there is a DSN given -- if not, it does nothing!) - CC_DSN_info(conn, CONN_DONT_OVERWRITE); + getDSNinfo(ci, CONN_DONT_OVERWRITE); // Fill in any default parameters if they are not there. - CC_set_defaults(conn); + getDSNdefaults(ci); dialog: ci->focus_password = password_required; @@ -102,14 +104,14 @@ dialog: break; } - /* Password is not a required parameter unless authentication asks for it. - For now, I think its better to just let the application ask over and over until - a password is entered (the user can always hit Cancel to get out) - */ + /* Password is not a required parameter unless authentication asks for it. + For now, I think its better to just let the application ask over and over until + a password is entered (the user can always hit Cancel to get out) + */ if( ci->username[0] == '\0' || ci->server[0] == '\0' || ci->database[0] == '\0' || - ci->port[0] == '\0') { + ci->port[0] == '\0') { // (password_required && ci->password[0] == '\0')) return SQL_NO_DATA_FOUND; @@ -119,19 +121,7 @@ dialog: // return the completed string to the caller. - char got_dsn = (ci->dsn[0] != '\0'); - - sprintf(connect_string, "%s=%s;DATABASE=%s;SERVER=%s;PORT=%s;UID=%s;READONLY=%s;PWD=%s;PROTOCOL=%s;CONNSETTINGS=%s", - got_dsn ? "DSN" : "DRIVER", - got_dsn ? ci->dsn : ci->driver, - ci->database, - ci->server, - ci->port, - ci->username, - ci->readonly, - ci->password, - ci->protocol, - ci->conn_settings); + makeConnectString(connect_string, ci); if(pcbConnStrOut) { *pcbConnStrOut = strlen(connect_string); @@ -169,7 +159,7 @@ int dialog_result; mylog("dconn_DoDialog: ci = %u\n", ci); if(hwnd) { - dialog_result = DialogBoxParam(s_hModule, MAKEINTRESOURCE(DRIVERCONNDIALOG), + dialog_result = DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_CONFIG), hwnd, dconn_FDriverConnectProc, (LPARAM) ci); if(!dialog_result || (dialog_result == -1)) { return SQL_NO_DATA_FOUND; @@ -188,34 +178,37 @@ BOOL FAR PASCAL dconn_FDriverConnectProc( WPARAM wParam, LPARAM lParam) { -static ConnInfo *ci; +ConnInfo *ci; switch (wMsg) { case WM_INITDIALOG: - ci = (ConnInfo *) lParam; // Save the ConnInfo for the "OK" + ci = (ConnInfo *) lParam; - SetDlgItemText(hdlg, SERVER_EDIT, ci->server); - SetDlgItemText(hdlg, DATABASE_EDIT, ci->database); - SetDlgItemText(hdlg, USERNAME_EDIT, ci->username); - SetDlgItemText(hdlg, PASSWORD_EDIT, ci->password); - SetDlgItemText(hdlg, PORT_EDIT, ci->port); - CheckDlgButton(hdlg, READONLY_EDIT, atoi(ci->readonly)); - - CheckDlgButton(hdlg, PG62_EDIT, PROTOCOL_62(ci)); - - /* The driver connect dialog box allows manipulating this global variable */ - CheckDlgButton(hdlg, COMMLOG_EDIT, globals.commlog); + /* Change the caption for the setup dialog */ + SetWindowText(hdlg, "PostgreSQL Connection"); + + SetWindowText(GetDlgItem(hdlg, IDC_DATASOURCE), "Connection"); + + /* Hide the DSN and description fields */ + ShowWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), SW_HIDE); + ShowWindow(GetDlgItem(hdlg, IDC_DSNAME), SW_HIDE); + ShowWindow(GetDlgItem(hdlg, IDC_DESCTEXT), SW_HIDE); + ShowWindow(GetDlgItem(hdlg, IDC_DESC), SW_HIDE); + + SetWindowLong(hdlg, DWL_USER, lParam);// Save the ConnInfo for the "OK" + + SetDlgStuff(hdlg, ci); if (ci->database[0] == '\0') ; /* default focus */ else if (ci->server[0] == '\0') - SetFocus(GetDlgItem(hdlg, SERVER_EDIT)); + SetFocus(GetDlgItem(hdlg, IDC_SERVER)); else if (ci->port[0] == '\0') - SetFocus(GetDlgItem(hdlg, PORT_EDIT)); + SetFocus(GetDlgItem(hdlg, IDC_PORT)); else if (ci->username[0] == '\0') - SetFocus(GetDlgItem(hdlg, USERNAME_EDIT)); + SetFocus(GetDlgItem(hdlg, IDC_USER)); else if (ci->focus_password) - SetFocus(GetDlgItem(hdlg, PASSWORD_EDIT)); + SetFocus(GetDlgItem(hdlg, IDC_PASSWORD)); break; @@ -223,26 +216,30 @@ static ConnInfo *ci; switch (GET_WM_COMMAND_ID(wParam, lParam)) { case IDOK: - GetDlgItemText(hdlg, SERVER_EDIT, ci->server, sizeof(ci->server)); - GetDlgItemText(hdlg, DATABASE_EDIT, ci->database, sizeof(ci->database)); - GetDlgItemText(hdlg, USERNAME_EDIT, ci->username, sizeof(ci->username)); - GetDlgItemText(hdlg, PASSWORD_EDIT, ci->password, sizeof(ci->password)); - GetDlgItemText(hdlg, PORT_EDIT, ci->port, sizeof(ci->port)); + ci = (ConnInfo *) GetWindowLong(hdlg, DWL_USER); + + GetDlgStuff(hdlg, ci); - sprintf(ci->readonly, "%d", IsDlgButtonChecked(hdlg, READONLY_EDIT)); - - if (IsDlgButtonChecked(hdlg, PG62_EDIT)) - strcpy(ci->protocol, PG62); - else - ci->protocol[0] = '\0'; - - /* The driver connect dialog box allows manipulating this global variable */ - globals.commlog = IsDlgButtonChecked(hdlg, COMMLOG_EDIT); - updateGlobals(); case IDCANCEL: EndDialog(hdlg, GET_WM_COMMAND_ID(wParam, lParam) == IDOK); return TRUE; + + case IDC_DRIVER: + + DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV), + hdlg, driver_optionsProc, (LPARAM) NULL); + + + break; + + case IDC_DATASOURCE: + + ci = (ConnInfo *) GetWindowLong(hdlg, DWL_USER); + DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DS), + hdlg, ds_optionsProc, (LPARAM) ci); + + break; } } @@ -286,42 +283,12 @@ char *strtok_arg; if( !attribute || !value) continue; - /*********************************************************/ - /* PARSE ATTRIBUTES */ - /*********************************************************/ - if(stricmp(attribute, "DSN") == 0) - strcpy(ci->dsn, value); + // Copy the appropriate value to the conninfo + copyAttributes(ci, attribute, value); - else if(stricmp(attribute, "driver") == 0) - strcpy(ci->driver, value); - - else if(stricmp(attribute, "uid") == 0) - strcpy(ci->username, value); - - else if(stricmp(attribute, "pwd") == 0) - strcpy(ci->password, value); - - else if ((stricmp(attribute, "server") == 0) || - (stricmp(attribute, "servername") == 0)) - strcpy(ci->server, value); - - else if(stricmp(attribute, "port") == 0) - strcpy(ci->port, value); - - else if(stricmp(attribute, "database") == 0) - strcpy(ci->database, value); - - else if (stricmp(attribute, "readonly") == 0) - strcpy(ci->readonly, value); - - else if (stricmp(attribute, "protocol") == 0) - strcpy(ci->protocol, value); - - else if (stricmp(attribute, "connsettings") == 0) - strcpy(ci->conn_settings, value); - } free(our_connect_string); } + diff --git a/src/interfaces/odbc/environ.c b/src/interfaces/odbc/environ.c index b7d808476f..3ca80810be 100644 --- a/src/interfaces/odbc/environ.c +++ b/src/interfaces/odbc/environ.c @@ -1,17 +1,17 @@ - -/* Module: environ.c - * - * Description: This module contains routines related to - * the environment, such as storing connection handles, - * and returning errors. - * - * Classes: EnvironmentClass (Functions prefix: "EN_") - * - * API functions: SQLAllocEnv, SQLFreeEnv, SQLError - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: environ.c + * + * Description: This module contains routines related to + * the environment, such as storing connection handles, + * and returning errors. + * + * Classes: EnvironmentClass (Functions prefix: "EN_") + * + * API functions: SQLAllocEnv, SQLFreeEnv, SQLError + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "environ.h" #include "connection.h" @@ -113,6 +113,10 @@ int status; strcpy(szSqlState, "08S01"); // communication link failure break; + case STMT_CREATE_TABLE_ERROR: + strcpy(szSqlState, "S0001"); + // table already exists + break; case STMT_STATUS_ERROR: case STMT_SEQUENCE_ERROR: strcpy(szSqlState, "S1010"); @@ -158,6 +162,12 @@ int status; break; case STMT_OPTION_VALUE_CHANGED: strcpy(szSqlState, "01S02"); + break; + case STMT_INVALID_CURSOR_NAME: + strcpy(szSqlState, "34000"); + break; + case STMT_NO_CURSOR_NAME: + strcpy(szSqlState, "S1015"); break; default: strcpy(szSqlState, "S1000"); @@ -213,10 +223,10 @@ int status; case CONN_OPENDB_ERROR: strcpy(szSqlState, "08001"); // unable to connect to data source - break; - case CONN_INVALID_AUTHENTICATION: - case CONN_AUTH_TYPE_UNSUPPORTED: - strcpy(szSqlState, "28000"); + break; + case CONN_INVALID_AUTHENTICATION: + case CONN_AUTH_TYPE_UNSUPPORTED: + strcpy(szSqlState, "28000"); break; case CONN_STMT_ALLOC_ERROR: strcpy(szSqlState, "S1001"); @@ -339,7 +349,7 @@ EnvironmentClass *rv; rv->errormsg = 0; rv->errornumber = 0; } - + return rv; } @@ -356,7 +366,7 @@ char rv = 1; // the source--they should not be freed /* Free any connections belonging to this environment */ - for (lf = 0; lf < MAX_CONNECTIONS; lf++) { + for (lf = 0; lf < MAX_CONNECTIONS; lf++) { if (conns[lf] && conns[lf]->henv == self) rv = rv && CC_Destructor(conns[lf]); } @@ -383,16 +393,16 @@ char EN_add_connection(EnvironmentClass *self, ConnectionClass *conn) { int i; - -mylog("EN_add_connection: self = %u, conn = %u\n", self, conn); + +mylog("EN_add_connection: self = %u, conn = %u\n", self, conn); for (i = 0; i < MAX_CONNECTIONS; i++) { - if ( ! conns[i]) { + if ( ! conns[i]) { conn->henv = self; - conns[i] = conn; - - mylog(" added at i =%d, conn->henv = %u, conns[i]->henv = %u\n", - i, conn->henv, conns[i]->henv); + conns[i] = conn; + + mylog(" added at i =%d, conn->henv = %u, conns[i]->henv = %u\n", + i, conn->henv, conns[i]->henv); return TRUE; } diff --git a/src/interfaces/odbc/environ.h b/src/interfaces/odbc/environ.h index 0989163506..aa941cf0c2 100644 --- a/src/interfaces/odbc/environ.h +++ b/src/interfaces/odbc/environ.h @@ -1,11 +1,11 @@ - -/* File: environ.h - * - * Description: See "environ.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: environ.h + * + * Description: See "environ.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __ENVIRON_H__ #define __ENVIRON_H__ @@ -13,15 +13,16 @@ #include "psqlodbc.h" #include #include - +#include + #define ENV_ALLOC_ERROR 1 - + /********** Environment Handle *************/ struct EnvironmentClass_ { char *errormsg; int errornumber; }; - + /* Environment prototypes */ EnvironmentClass *EN_Constructor(void); char EN_Destructor(EnvironmentClass *self); diff --git a/src/interfaces/odbc/execute.c b/src/interfaces/odbc/execute.c index 6afbe308c9..960f16ca91 100644 --- a/src/interfaces/odbc/execute.c +++ b/src/interfaces/odbc/execute.c @@ -1,17 +1,17 @@ - -/* Module: execute.c - * - * Description: This module contains routines related to - * preparing and executing an SQL statement. - * - * Classes: n/a - * - * API functions: SQLPrepare, SQLExecute, SQLExecDirect, SQLTransact, - * SQLCancel, SQLNativeSql, SQLParamData, SQLPutData - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: execute.c + * + * Description: This module contains routines related to + * preparing and executing an SQL statement. + * + * Classes: n/a + * + * API functions: SQLPrepare, SQLExecute, SQLExecDirect, SQLTransact, + * SQLCancel, SQLNativeSql, SQLParamData, SQLPutData + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "psqlodbc.h" #include @@ -24,6 +24,7 @@ #include "qresult.h" #include "convert.h" #include "bind.h" +#include "lobj.h" // Perform a Prepare on the SQL statement @@ -36,72 +37,29 @@ StatementClass *self = (StatementClass *) hstmt; if ( ! self) return SQL_INVALID_HANDLE; - /* CC: According to the ODBC specs it is valid to call SQLPrepare mulitple times. In that case, - the bound SQL statement is replaced by the new one */ + /* According to the ODBC specs it is valid to call SQLPrepare mulitple times. + In that case, the bound SQL statement is replaced by the new one + */ - switch (self->status) { + switch(self->status) { case STMT_PREMATURE: mylog("**** SQLPrepare: STMT_PREMATURE, recycle\n"); - SC_recycle_statement(self); /* recycle the statement, but do not remove parameter bindings */ + break; - /* NO Break! -- Contiue the same way as with a newly allocated statement ! */ + case STMT_FINISHED: + mylog("**** SQLPrepare: STMT_FINISHED, recycle\n"); + SC_recycle_statement(self); /* recycle the statement, but do not remove parameter bindings */ + break; case STMT_ALLOCATED: - // it is not really necessary to do any conversion of the statement - // here--just copy it, and deal with it when it's ready to be - // executed. mylog("**** SQLPrepare: STMT_ALLOCATED, copy\n"); - - self->statement = make_string(szSqlStr, cbSqlStr, NULL); - if ( ! self->statement) { - self->errornumber = STMT_NO_MEMORY_ERROR; - self->errormsg = "No memory available to store statement"; - return SQL_ERROR; - } - - self->statement_type = statement_type(self->statement); - - // Check if connection is readonly (only selects are allowed) - if ( CC_is_readonly(self->hdbc) && self->statement_type != STMT_TYPE_SELECT ) { - self->errornumber = STMT_EXEC_ERROR; - self->errormsg = "Connection is readonly, only select statements are allowed."; - return SQL_ERROR; - } - - self->prepare = TRUE; self->status = STMT_READY; + break; - return SQL_SUCCESS; - - case STMT_READY: /* SQLPrepare has already been called -- Just changed the SQL statement that is assigned to the handle */ + case STMT_READY: mylog("**** SQLPrepare: STMT_READY, change SQL\n"); - - if (self->statement) - free(self->statement); - - self->statement = make_string(szSqlStr, cbSqlStr, NULL); - if ( ! self->statement) { - self->errornumber = STMT_NO_MEMORY_ERROR; - self->errormsg = "No memory available to store statement"; - return SQL_ERROR; - } - - self->prepare = TRUE; - self->statement_type = statement_type(self->statement); - - // Check if connection is readonly (only selects are allowed) - if ( CC_is_readonly(self->hdbc) && self->statement_type != STMT_TYPE_SELECT ) { - self->errornumber = STMT_EXEC_ERROR; - self->errormsg = "Connection is readonly, only select statements are allowed."; - return SQL_ERROR; - } - - return SQL_SUCCESS; - - case STMT_FINISHED: - mylog("**** SQLPrepare: STMT_FINISHED\n"); - /* No BREAK: continue as with STMT_EXECUTING */ + break; case STMT_EXECUTING: mylog("**** SQLPrepare: STMT_EXECUTING, error!\n"); @@ -116,6 +74,30 @@ StatementClass *self = (StatementClass *) hstmt; self->errormsg = "An Internal Error has occured -- Unknown statement status."; return SQL_ERROR; } + + if (self->statement) + free(self->statement); + + self->statement = make_string(szSqlStr, cbSqlStr, NULL); + if ( ! self->statement) { + self->errornumber = STMT_NO_MEMORY_ERROR; + self->errormsg = "No memory available to store statement"; + return SQL_ERROR; + } + + self->prepare = TRUE; + self->statement_type = statement_type(self->statement); + + // Check if connection is readonly (only selects are allowed) + if ( CC_is_readonly(self->hdbc) && STMT_UPDATE(self)) { + self->errornumber = STMT_EXEC_ERROR; + self->errormsg = "Connection is readonly, only select statements are allowed."; + return SQL_ERROR; + } + + return SQL_SUCCESS; + + } // - - - - - - - - - @@ -150,7 +132,7 @@ StatementClass *stmt = (StatementClass *) hstmt; stmt->statement_type = statement_type(stmt->statement); // Check if connection is readonly (only selects are allowed) - if ( CC_is_readonly(stmt->hdbc) && stmt->statement_type != STMT_TYPE_SELECT ) { + if ( CC_is_readonly(stmt->hdbc) && STMT_UPDATE(stmt)) { stmt->errornumber = STMT_EXEC_ERROR; stmt->errormsg = "Connection is readonly, only select statements are allowed."; return SQL_ERROR; @@ -165,10 +147,10 @@ StatementClass *stmt = (StatementClass *) hstmt; RETCODE SQL_API SQLExecute( HSTMT hstmt) { -StatementClass *stmt = (StatementClass *) hstmt; +StatementClass *stmt = (StatementClass *) hstmt; ConnectionClass *conn; -int i, retval; - +int i, retval; + if ( ! stmt) return SQL_INVALID_HANDLE; @@ -212,45 +194,45 @@ int i, retval; stmt->errornumber = STMT_STATUS_ERROR; stmt->errormsg = "The handle does not point to a statement that is ready to be executed"; return SQL_ERROR; - } - - - /* The bound parameters could have possibly changed since the last execute - of this statement? Therefore check for params and re-copy. - */ - stmt->data_at_exec = -1; - for (i = 0; i < stmt->parameters_allocated; i++) { - /* Check for data at execution parameters */ - if ( stmt->parameters[i].data_at_exec == TRUE) { - if (stmt->data_at_exec < 0) - stmt->data_at_exec = 1; - else - stmt->data_at_exec++; - } - } - // If there are some data at execution parameters, return need data - // SQLParamData and SQLPutData will be used to send params and execute the statement. - if (stmt->data_at_exec > 0) - return SQL_NEED_DATA; - - - mylog("SQLExecute: copying statement params: trans_status=%d, len=%d, stmt='%s'\n", conn->transact_status, strlen(stmt->statement), stmt->statement); - - // Create the statement with parameters substituted. - retval = copy_statement_with_parameters(stmt); - if( retval != SQL_SUCCESS) - /* error msg passed from above */ - return retval; - - mylog(" stmt_with_params = '%s'\n", stmt->stmt_with_params); - - - return SC_execute(stmt); + } + + + /* The bound parameters could have possibly changed since the last execute + of this statement? Therefore check for params and re-copy. + */ + stmt->data_at_exec = -1; + for (i = 0; i < stmt->parameters_allocated; i++) { + /* Check for data at execution parameters */ + if ( stmt->parameters[i].data_at_exec == TRUE) { + if (stmt->data_at_exec < 0) + stmt->data_at_exec = 1; + else + stmt->data_at_exec++; + } + } + // If there are some data at execution parameters, return need data + // SQLParamData and SQLPutData will be used to send params and execute the statement. + if (stmt->data_at_exec > 0) + return SQL_NEED_DATA; + + + mylog("SQLExecute: copying statement params: trans_status=%d, len=%d, stmt='%s'\n", conn->transact_status, strlen(stmt->statement), stmt->statement); + + // Create the statement with parameters substituted. + retval = copy_statement_with_parameters(stmt); + if( retval != SQL_SUCCESS) + /* error msg passed from above */ + return retval; + + mylog(" stmt_with_params = '%s'\n", stmt->stmt_with_params); + + + return SC_execute(stmt); } - - - + + + // - - - - - - - - - RETCODE SQL_API SQLTransact( @@ -325,24 +307,24 @@ mylog("**** SQLTransact: hdbc=%u, henv=%u\n", hdbc, henv); RETCODE SQL_API SQLCancel( HSTMT hstmt) // Statement to cancel. { -StatementClass *stmt = (StatementClass *) hstmt; - - // Check if this can handle canceling in the middle of a SQLPutData? - if ( ! stmt) - return SQL_INVALID_HANDLE; - - // Not in the middle of SQLParamData/SQLPutData so cancel like a close. - if (stmt->data_at_exec < 0) - return SQLFreeStmt(hstmt, SQL_CLOSE); - - // In the middle of SQLParamData/SQLPutData, so cancel that. - // Note, any previous data-at-exec buffers will be freed in the recycle - // if they call SQLExecDirect or SQLExecute again. - - stmt->data_at_exec = -1; - stmt->current_exec_param = -1; - stmt->put_data = FALSE; - +StatementClass *stmt = (StatementClass *) hstmt; + + // Check if this can handle canceling in the middle of a SQLPutData? + if ( ! stmt) + return SQL_INVALID_HANDLE; + + // Not in the middle of SQLParamData/SQLPutData so cancel like a close. + if (stmt->data_at_exec < 0) + return SQLFreeStmt(hstmt, SQL_CLOSE); + + // In the middle of SQLParamData/SQLPutData, so cancel that. + // Note, any previous data-at-exec buffers will be freed in the recycle + // if they call SQLExecDirect or SQLExecute again. + + stmt->data_at_exec = -1; + stmt->current_exec_param = -1; + stmt->put_data = FALSE; + } // - - - - - - - - - @@ -371,45 +353,63 @@ RETCODE SQL_API SQLNativeSql( RETCODE SQL_API SQLParamData( HSTMT hstmt, PTR FAR *prgbValue) -{ -StatementClass *stmt = (StatementClass *) hstmt; -int i, retval; - - if ( ! stmt) - return SQL_INVALID_HANDLE; - - if (stmt->data_at_exec < 0) { - stmt->errornumber = STMT_SEQUENCE_ERROR; - stmt->errormsg = "No execution-time parameters for this statement"; - return SQL_ERROR; - } - - if (stmt->data_at_exec > stmt->parameters_allocated) { - stmt->errornumber = STMT_SEQUENCE_ERROR; - stmt->errormsg = "Too many execution-time parameters were present"; - return SQL_ERROR; - } - - /* Done, now copy the params and then execute the statement */ - if (stmt->data_at_exec == 0) { - retval = copy_statement_with_parameters(stmt); - if (retval != SQL_SUCCESS) - return retval; - - return SC_execute(stmt); - } - - /* At least 1 data at execution parameter, so Fill in the token value */ - for (i = 0; i < stmt->parameters_allocated; i++) { - if (stmt->parameters[i].data_at_exec == TRUE) { - stmt->data_at_exec--; - stmt->current_exec_param = i; - stmt->put_data = FALSE; - *prgbValue = stmt->parameters[i].buffer; /* token */ - } - } - - return SQL_NEED_DATA; +{ +StatementClass *stmt = (StatementClass *) hstmt; +int i, retval; + + if ( ! stmt) + return SQL_INVALID_HANDLE; + + mylog("SQLParamData, enter: data_at_exec=%d, params_alloc=%d\n", + stmt->data_at_exec, stmt->parameters_allocated); + + if (stmt->data_at_exec < 0) { + stmt->errornumber = STMT_SEQUENCE_ERROR; + stmt->errormsg = "No execution-time parameters for this statement"; + return SQL_ERROR; + } + + if (stmt->data_at_exec > stmt->parameters_allocated) { + stmt->errornumber = STMT_SEQUENCE_ERROR; + stmt->errormsg = "Too many execution-time parameters were present"; + return SQL_ERROR; + } + + /* close the large object */ + if ( stmt->lobj_fd >= 0) { + lo_close(stmt->hdbc, stmt->lobj_fd); + stmt->lobj_fd = -1; + } + + + /* Done, now copy the params and then execute the statement */ + if (stmt->data_at_exec == 0) { + retval = copy_statement_with_parameters(stmt); + if (retval != SQL_SUCCESS) + return retval; + + stmt->current_exec_param = -1; + + return SC_execute(stmt); + } + + /* Set beginning param; if first time SQLParamData is called , start at 0. + Otherwise, start at the last parameter + 1. + */ + i = stmt->current_exec_param >= 0 ? stmt->current_exec_param+1 : 0; + + /* At least 1 data at execution parameter, so Fill in the token value */ + for ( ; i < stmt->parameters_allocated; i++) { + if (stmt->parameters[i].data_at_exec == TRUE) { + stmt->data_at_exec--; + stmt->current_exec_param = i; + stmt->put_data = FALSE; + *prgbValue = stmt->parameters[i].buffer; /* token */ + break; + } + } + + return SQL_NEED_DATA; } // - - - - - - - - - @@ -422,114 +422,157 @@ RETCODE SQL_API SQLPutData( PTR rgbValue, SDWORD cbValue) { -StatementClass *stmt = (StatementClass *) hstmt; -char *buffer; -SDWORD *used; -int old_pos; - - - if ( ! stmt) - return SQL_INVALID_HANDLE; - - - if (stmt->current_exec_param < 0) { - stmt->errornumber = STMT_SEQUENCE_ERROR; - stmt->errormsg = "Previous call was not SQLPutData or SQLParamData"; - return SQL_ERROR; - } - - if ( ! stmt->put_data) { /* first call */ - - mylog("SQLPutData: (1) cbValue = %d\n", cbValue); - - stmt->put_data = TRUE; - - used = (SDWORD *) malloc(sizeof(SDWORD)); - if ( ! used) { - stmt->errornumber = STMT_NO_MEMORY_ERROR; - stmt->errormsg = "Out of memory in SQLPutData (1)"; - return SQL_ERROR; - } - - *used = cbValue; - stmt->parameters[stmt->current_exec_param].EXEC_used = used; - - if (cbValue == SQL_NULL_DATA) - return SQL_SUCCESS; - - if (cbValue == SQL_NTS) { - buffer = strdup(rgbValue); - if ( ! buffer) { - stmt->errornumber = STMT_NO_MEMORY_ERROR; - stmt->errormsg = "Out of memory in SQLPutData (2)"; - return SQL_ERROR; - } - } - else { - buffer = malloc(cbValue + 1); - if ( ! buffer) { - stmt->errornumber = STMT_NO_MEMORY_ERROR; - stmt->errormsg = "Out of memory in SQLPutData (2)"; - return SQL_ERROR; - } - memcpy(buffer, rgbValue, cbValue); - buffer[cbValue] = '\0'; - } - - stmt->parameters[stmt->current_exec_param].EXEC_buffer = buffer; - } - - else { /* calling SQLPutData more than once */ - - mylog("SQLPutData: (>1) cbValue = %d\n", cbValue); - - used = stmt->parameters[stmt->current_exec_param].EXEC_used; - buffer = stmt->parameters[stmt->current_exec_param].EXEC_buffer; - - if (cbValue == SQL_NTS) { - buffer = realloc(buffer, strlen(buffer) + strlen(rgbValue) + 1); - if ( ! buffer) { - stmt->errornumber = STMT_NO_MEMORY_ERROR; - stmt->errormsg = "Out of memory in SQLPutData (3)"; - return SQL_ERROR; - } - strcat(buffer, rgbValue); - - mylog(" cbValue = SQL_NTS: strlen(buffer) = %d\n", strlen(buffer)); - - *used = cbValue; - - } - else if (cbValue > 0) { - - old_pos = *used; - - *used += cbValue; - - mylog(" cbValue = %d, old_pos = %d, *used = %d\n", cbValue, old_pos, *used); - - buffer = realloc(buffer, *used + 1); - if ( ! buffer) { - stmt->errornumber = STMT_NO_MEMORY_ERROR; - stmt->errormsg = "Out of memory in SQLPutData (3)"; - return SQL_ERROR; - } - - memcpy(&buffer[old_pos], rgbValue, cbValue); - buffer[*used] = '\0'; - - } - else - return SQL_ERROR; - - - /* reassign buffer incase realloc moved it */ - stmt->parameters[stmt->current_exec_param].EXEC_buffer = buffer; - - } - - - return SQL_SUCCESS; +StatementClass *stmt = (StatementClass *) hstmt; +int old_pos, retval; +ParameterInfoClass *current_param; +char *buffer; + + + if ( ! stmt) + return SQL_INVALID_HANDLE; + + + if (stmt->current_exec_param < 0) { + stmt->errornumber = STMT_SEQUENCE_ERROR; + stmt->errormsg = "Previous call was not SQLPutData or SQLParamData"; + return SQL_ERROR; + } + + current_param = &(stmt->parameters[stmt->current_exec_param]); + + if ( ! stmt->put_data) { /* first call */ + + mylog("SQLPutData: (1) cbValue = %d\n", cbValue); + + stmt->put_data = TRUE; + + current_param->EXEC_used = (SDWORD *) malloc(sizeof(SDWORD)); + if ( ! current_param->EXEC_used) { + stmt->errornumber = STMT_NO_MEMORY_ERROR; + stmt->errormsg = "Out of memory in SQLPutData (1)"; + return SQL_ERROR; + } + + *current_param->EXEC_used = cbValue; + + if (cbValue == SQL_NULL_DATA) + return SQL_SUCCESS; + + + /* Handle Long Var Binary with Large Objects */ + if ( current_param->SQLType == SQL_LONGVARBINARY) { + + /* store the oid */ + current_param->lobj_oid = lo_creat(stmt->hdbc, INV_READ | INV_WRITE); + if (current_param->lobj_oid == 0) { + stmt->errornumber = STMT_EXEC_ERROR; + stmt->errormsg = "Couldnt create large object."; + return SQL_ERROR; + } + + /* major hack -- to allow convert to see somethings there */ + /* have to modify convert to handle this better */ + current_param->EXEC_buffer = (char *) ¤t_param->lobj_oid; + + /* store the fd */ + stmt->lobj_fd = lo_open(stmt->hdbc, current_param->lobj_oid, INV_WRITE); + if ( stmt->lobj_fd < 0) { + stmt->errornumber = STMT_EXEC_ERROR; + stmt->errormsg = "Couldnt open large object for writing."; + return SQL_ERROR; + } + + retval = lo_write(stmt->hdbc, stmt->lobj_fd, rgbValue, cbValue); + mylog("lo_write: cbValue=%d, wrote %d bytes\n", cbValue, retval); + + } + else { /* for handling text fields and small binaries */ + + if (cbValue == SQL_NTS) { + current_param->EXEC_buffer = strdup(rgbValue); + if ( ! current_param->EXEC_buffer) { + stmt->errornumber = STMT_NO_MEMORY_ERROR; + stmt->errormsg = "Out of memory in SQLPutData (2)"; + return SQL_ERROR; + } + } + else { + current_param->EXEC_buffer = malloc(cbValue + 1); + if ( ! current_param->EXEC_buffer) { + stmt->errornumber = STMT_NO_MEMORY_ERROR; + stmt->errormsg = "Out of memory in SQLPutData (2)"; + return SQL_ERROR; + } + memcpy(current_param->EXEC_buffer, rgbValue, cbValue); + current_param->EXEC_buffer[cbValue] = '\0'; + } + } + } + + else { /* calling SQLPutData more than once */ + + mylog("SQLPutData: (>1) cbValue = %d\n", cbValue); + + if (current_param->SQLType == SQL_LONGVARBINARY) { + + /* the large object fd is in EXEC_buffer */ + retval = lo_write(stmt->hdbc, stmt->lobj_fd, rgbValue, cbValue); + mylog("lo_write(2): cbValue = %d, wrote %d bytes\n", cbValue, retval); + + *current_param->EXEC_used += cbValue; + + } else { + + buffer = current_param->EXEC_buffer; + + if (cbValue == SQL_NTS) { + buffer = realloc(buffer, strlen(buffer) + strlen(rgbValue) + 1); + if ( ! buffer) { + stmt->errornumber = STMT_NO_MEMORY_ERROR; + stmt->errormsg = "Out of memory in SQLPutData (3)"; + return SQL_ERROR; + } + strcat(buffer, rgbValue); + + mylog(" cbValue = SQL_NTS: strlen(buffer) = %d\n", strlen(buffer)); + + *current_param->EXEC_used = cbValue; + + /* reassign buffer incase realloc moved it */ + current_param->EXEC_buffer = buffer; + + } + else if (cbValue > 0) { + + old_pos = *current_param->EXEC_used; + + *current_param->EXEC_used += cbValue; + + mylog(" cbValue = %d, old_pos = %d, *used = %d\n", cbValue, old_pos, *current_param->EXEC_used); + + /* dont lose the old pointer in case out of memory */ + buffer = realloc(current_param->EXEC_buffer, *current_param->EXEC_used + 1); + if ( ! buffer) { + stmt->errornumber = STMT_NO_MEMORY_ERROR; + stmt->errormsg = "Out of memory in SQLPutData (3)"; + return SQL_ERROR; + } + + memcpy(&buffer[old_pos], rgbValue, cbValue); + buffer[*current_param->EXEC_used] = '\0'; + + /* reassign buffer incase realloc moved it */ + current_param->EXEC_buffer = buffer; + + } + else + return SQL_ERROR; + + } + } + + + return SQL_SUCCESS; } diff --git a/src/interfaces/odbc/info.c b/src/interfaces/odbc/info.c index b87efbd33d..ae6c6074f7 100644 --- a/src/interfaces/odbc/info.c +++ b/src/interfaces/odbc/info.c @@ -1,20 +1,20 @@ - -/* Module: info.c - * - * Description: This module contains routines related to - * ODBC informational functions. - * - * Classes: n/a - * - * API functions: SQLGetInfo, SQLGetTypeInfo, SQLGetFunctions, - * SQLTables, SQLColumns, SQLStatistics, SQLSpecialColumns, - * SQLPrimaryKeys, SQLForeignKeys, - * SQLProcedureColumns(NI), SQLProcedures(NI), - * SQLTablePrivileges(NI), SQLColumnPrivileges(NI) - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: info.c + * + * Description: This module contains routines related to + * ODBC informational functions. + * + * Classes: n/a + * + * API functions: SQLGetInfo, SQLGetTypeInfo, SQLGetFunctions, + * SQLTables, SQLColumns, SQLStatistics, SQLSpecialColumns, + * SQLPrimaryKeys, SQLForeignKeys, + * SQLProcedureColumns(NI), SQLProcedures(NI), + * SQLTablePrivileges(NI), SQLColumnPrivileges(NI) + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include #include @@ -33,6 +33,9 @@ #include "misc.h" #include "pgtypes.h" + +extern GLOBAL_VALUES globals; + // - - - - - - - - - RETCODE SQL_API SQLGetInfo( @@ -192,7 +195,10 @@ char *p; // this tag doesn't seem to be in ODBC 2.0, and it conflicts // with a valid tag (SQL_TIMEDATE_ADD_INTERVALS). - p = CC_get_database(conn); + /* Returning the database name causes problems in MS Query. + It generates query like: "SELECT DISTINCT a FROM byronncrap3 crap3" + */ + p = ""; // CC_get_database(conn); if (pcbInfoValue) *pcbInfoValue = strlen(p); strncpy_null((char *)rgbInfoValue, p, (size_t)cbInfoValueMax); break; @@ -224,8 +230,6 @@ char *p; break; case SQL_DRIVER_ODBC_VER: - /* I think we should return 02.00--at least, that is the version of the */ - /* spec I'm currently referring to. */ if (pcbInfoValue) *pcbInfoValue = 5; strncpy_null((char *)rgbInfoValue, "02.00", (size_t)cbInfoValueMax); break; @@ -245,12 +249,11 @@ char *p; case SQL_FETCH_DIRECTION: /* ODBC 1.0 */ // which fetch directions are supported? (bitmask) - // I guess these apply to SQLExtendedFetch? - *((DWORD *)rgbInfoValue) = SQL_FETCH_NEXT || - SQL_FETCH_FIRST || - SQL_FETCH_LAST || - SQL_FETCH_PRIOR || - SQL_FETCH_ABSOLUTE; + *((DWORD *)rgbInfoValue) = globals.use_declarefetch ? 0 : (SQL_FD_FETCH_NEXT | + SQL_FD_FETCH_FIRST | + SQL_FD_FETCH_LAST | + SQL_FD_FETCH_PRIOR | + SQL_FD_FETCH_ABSOLUTE); if(pcbInfoValue) { *pcbInfoValue = 4; } break; @@ -326,7 +329,7 @@ char *p; case SQL_MAX_COLUMN_NAME_LEN: /* ODBC 1.0 */ // maximum length of a column name - *((WORD *)rgbInfoValue) = 0; + *((WORD *)rgbInfoValue) = 32; if(pcbInfoValue) { *pcbInfoValue = 2; } break; @@ -349,7 +352,6 @@ char *p; break; case SQL_MAX_COLUMNS_IN_SELECT: /* ODBC 2.0 */ - // I think you get the idea by now *((WORD *)rgbInfoValue) = 0; if(pcbInfoValue) { *pcbInfoValue = 2; } break; @@ -360,7 +362,7 @@ char *p; break; case SQL_MAX_CURSOR_NAME_LEN: /* ODBC 1.0 */ - *((WORD *)rgbInfoValue) = 0; + *((WORD *)rgbInfoValue) = 32; if(pcbInfoValue) { *pcbInfoValue = 2; } break; @@ -395,7 +397,7 @@ char *p; case SQL_MAX_ROW_SIZE_INCLUDES_LONG: /* ODBC 2.0 */ // does the preceding value include LONGVARCHAR and LONGVARBINARY - // fields? + // fields? Well, it does include longvarchar, but not longvarbinary. *pcbInfoValue = 1; strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax); break; @@ -407,7 +409,7 @@ char *p; break; case SQL_MAX_TABLE_NAME_LEN: /* ODBC 1.0 */ - *((WORD *)rgbInfoValue) = 0; + *((WORD *)rgbInfoValue) = 32; if(pcbInfoValue) { *pcbInfoValue = 2; } break; @@ -424,7 +426,7 @@ char *p; case SQL_MULT_RESULT_SETS: /* ODBC 1.0 */ // do we support multiple result sets? if (pcbInfoValue) *pcbInfoValue = 1; - strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax); + strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax); break; case SQL_MULTIPLE_ACTIVE_TXN: /* ODBC 1.0 */ @@ -434,7 +436,7 @@ char *p; break; case SQL_NEED_LONG_DATA_LEN: /* ODBC 2.0 */ - if (pcbInfoValue) *pcbInfoValue = 1; + if (pcbInfoValue) *pcbInfoValue = 1; /* Dont need the length, SQLPutData can handle any size and multiple calls */ strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax); break; @@ -460,7 +462,7 @@ char *p; break; case SQL_ODBC_API_CONFORMANCE: /* ODBC 1.0 */ - *((WORD *)rgbInfoValue) = SQL_OAC_LEVEL1; /* well, almost */ + *((WORD *)rgbInfoValue) = SQL_OAC_LEVEL1; if(pcbInfoValue) { *pcbInfoValue = 2; } break; @@ -572,22 +574,23 @@ char *p; break; case SQL_ROW_UPDATES: /* ODBC 1.0 */ - // not quite sure what this means + // Driver doesn't support keyset-driven or mixed cursors, so + // not much point in saying row updates are supported if (pcbInfoValue) *pcbInfoValue = 1; - strncpy_null((char *)rgbInfoValue, "Y", (size_t)cbInfoValueMax); + strncpy_null((char *)rgbInfoValue, "N", (size_t)cbInfoValueMax); break; case SQL_SCROLL_CONCURRENCY: /* ODBC 1.0 */ - // what concurrency options are supported? (bitmask) - // taking a guess here - *((DWORD *)rgbInfoValue) = SQL_SCCO_OPT_ROWVER; + // what concurrency options are supported BY THE CURSOR? (bitmask) + *((DWORD *)rgbInfoValue) = (SQL_SCCO_READ_ONLY); if(pcbInfoValue) { *pcbInfoValue = 4; } break; case SQL_SCROLL_OPTIONS: /* ODBC 1.0 */ // what options are supported for scrollable cursors? (bitmask) - // not too sure about this one, either... - *((DWORD *)rgbInfoValue) = SQL_SO_KEYSET_DRIVEN; + // for declare/fetch, only FORWARD scrolling is allowed + // otherwise, the result set is STATIC (to SQLExtendedFetch for example) + *((DWORD *)rgbInfoValue) = globals.use_declarefetch ? SQL_SO_FORWARD_ONLY : (SQL_SO_FORWARD_ONLY | SQL_SO_STATIC); if(pcbInfoValue) { *pcbInfoValue = 4; } break; @@ -623,8 +626,14 @@ char *p; case SQL_STRING_FUNCTIONS: /* ODBC 1.0 */ // what string functions exist? (bitmask) - // not sure if any of these exist, either - *((DWORD *)rgbInfoValue) = 0; + *((DWORD *)rgbInfoValue) = (SQL_FN_STR_CONCAT | + SQL_FN_STR_LCASE | + SQL_FN_STR_LENGTH | + SQL_FN_STR_LOCATE | + SQL_FN_STR_LTRIM | + SQL_FN_STR_RTRIM | + SQL_FN_STR_SUBSTRING | + SQL_FN_STR_UCASE); if(pcbInfoValue) { *pcbInfoValue = 4; } break; @@ -666,7 +675,7 @@ char *p; case SQL_TIMEDATE_FUNCTIONS: /* ODBC 1.0 */ // what time and date functions are supported? (bitmask) - *((DWORD *)rgbInfoValue) = 0; + *((DWORD *)rgbInfoValue) = (SQL_FN_TD_NOW); if(pcbInfoValue) { *pcbInfoValue = 4; } break; @@ -751,30 +760,30 @@ Int4 type; // cycle through the types for(i=0, type = pgtypes_defined[0]; type; type = pgtypes_defined[++i]) { - if(fSqlType == SQL_ALL_TYPES || fSqlType == pgtype_to_sqltype(type)) { + if(fSqlType == SQL_ALL_TYPES || fSqlType == pgtype_to_sqltype(stmt, type)) { row = (TupleNode *)malloc(sizeof(TupleNode) + (15 - 1)*sizeof(TupleField)); /* These values can't be NULL */ - set_tuplefield_string(&row->tuple[0], pgtype_to_name(type)); - set_tuplefield_int2(&row->tuple[1], pgtype_to_sqltype(type)); - set_tuplefield_int2(&row->tuple[6], pgtype_nullable(type)); - set_tuplefield_int2(&row->tuple[7], pgtype_case_sensitive(type)); - set_tuplefield_int2(&row->tuple[8], pgtype_searchable(type)); - set_tuplefield_int2(&row->tuple[10], pgtype_money(type)); + set_tuplefield_string(&row->tuple[0], pgtype_to_name(stmt, type)); + set_tuplefield_int2(&row->tuple[1], pgtype_to_sqltype(stmt, type)); + set_tuplefield_int2(&row->tuple[6], pgtype_nullable(stmt, type)); + set_tuplefield_int2(&row->tuple[7], pgtype_case_sensitive(stmt, type)); + set_tuplefield_int2(&row->tuple[8], pgtype_searchable(stmt, type)); + set_tuplefield_int2(&row->tuple[10], pgtype_money(stmt, type)); /* Localized data-source dependent data type name (always NULL) */ set_tuplefield_null(&row->tuple[12]); /* These values can be NULL */ - set_nullfield_int4(&row->tuple[2], pgtype_precision(type)); - set_nullfield_string(&row->tuple[3], pgtype_literal_prefix(type)); - set_nullfield_string(&row->tuple[4], pgtype_literal_suffix(type)); - set_nullfield_string(&row->tuple[5], pgtype_create_params(type)); - set_nullfield_int2(&row->tuple[9], pgtype_unsigned(type)); - set_nullfield_int2(&row->tuple[11], pgtype_auto_increment(type)); - set_nullfield_int2(&row->tuple[13], pgtype_scale(type)); - set_nullfield_int2(&row->tuple[14], pgtype_scale(type)); + set_nullfield_int4(&row->tuple[2], pgtype_precision(stmt, type, PG_STATIC, PG_STATIC)); + set_nullfield_string(&row->tuple[3], pgtype_literal_prefix(stmt, type)); + set_nullfield_string(&row->tuple[4], pgtype_literal_suffix(stmt, type)); + set_nullfield_string(&row->tuple[5], pgtype_create_params(stmt, type)); + set_nullfield_int2(&row->tuple[9], pgtype_unsigned(stmt, type)); + set_nullfield_int2(&row->tuple[11], pgtype_auto_increment(stmt, type)); + set_nullfield_int2(&row->tuple[13], pgtype_scale(stmt, type)); + set_nullfield_int2(&row->tuple[14], pgtype_scale(stmt, type)); QR_add_tuple(stmt->result, row); } @@ -782,6 +791,7 @@ Int4 type; stmt->status = STMT_FINISHED; stmt->currTuple = -1; + stmt->current_col = -1; return SQL_SUCCESS; } @@ -826,12 +836,12 @@ RETCODE SQL_API SQLGetFunctions( pfExists[SQL_API_SQLFREECONNECT] = TRUE; pfExists[SQL_API_SQLFREEENV] = TRUE; pfExists[SQL_API_SQLFREESTMT] = TRUE; - pfExists[SQL_API_SQLGETCURSORNAME] = FALSE; + pfExists[SQL_API_SQLGETCURSORNAME] = TRUE; pfExists[SQL_API_SQLNUMRESULTCOLS] = TRUE; pfExists[SQL_API_SQLPREPARE] = TRUE; // complete? pfExists[SQL_API_SQLROWCOUNT] = TRUE; - pfExists[SQL_API_SQLSETCURSORNAME] = FALSE; - pfExists[SQL_API_SQLSETPARAM] = FALSE; + pfExists[SQL_API_SQLSETCURSORNAME] = TRUE; + pfExists[SQL_API_SQLSETPARAM] = FALSE; // odbc 1.0 pfExists[SQL_API_SQLTRANSACT] = TRUE; // ODBC level 1 functions @@ -840,11 +850,9 @@ RETCODE SQL_API SQLGetFunctions( pfExists[SQL_API_SQLDRIVERCONNECT] = TRUE; pfExists[SQL_API_SQLGETCONNECTOPTION] = TRUE; // partial pfExists[SQL_API_SQLGETDATA] = TRUE; - pfExists[SQL_API_SQLGETFUNCTIONS] = TRUE; // sadly, I still - // had to think about - // this one + pfExists[SQL_API_SQLGETFUNCTIONS] = TRUE; pfExists[SQL_API_SQLGETINFO] = TRUE; - pfExists[SQL_API_SQLGETSTMTOPTION] = TRUE; // very partial + pfExists[SQL_API_SQLGETSTMTOPTION] = TRUE; // partial pfExists[SQL_API_SQLGETTYPEINFO] = TRUE; pfExists[SQL_API_SQLPARAMDATA] = TRUE; pfExists[SQL_API_SQLPUTDATA] = TRUE; @@ -858,11 +866,11 @@ RETCODE SQL_API SQLGetFunctions( pfExists[SQL_API_SQLBROWSECONNECT] = FALSE; pfExists[SQL_API_SQLCOLUMNPRIVILEGES] = FALSE; pfExists[SQL_API_SQLDATASOURCES] = FALSE; // only implemented by DM - pfExists[SQL_API_SQLDESCRIBEPARAM] = FALSE; - pfExists[SQL_API_SQLDRIVERS] = FALSE; - pfExists[SQL_API_SQLEXTENDEDFETCH] = TRUE; // partial? + pfExists[SQL_API_SQLDESCRIBEPARAM] = TRUE; + pfExists[SQL_API_SQLDRIVERS] = FALSE; // only implemented by DM + pfExists[SQL_API_SQLEXTENDEDFETCH] = globals.use_declarefetch ? FALSE : TRUE; pfExists[SQL_API_SQLFOREIGNKEYS] = TRUE; - pfExists[SQL_API_SQLMORERESULTS] = TRUE; + pfExists[SQL_API_SQLMORERESULTS] = FALSE; pfExists[SQL_API_SQLNATIVESQL] = TRUE; pfExists[SQL_API_SQLNUMPARAMS] = TRUE; pfExists[SQL_API_SQLPARAMOPTIONS] = FALSE; @@ -870,7 +878,7 @@ RETCODE SQL_API SQLGetFunctions( pfExists[SQL_API_SQLPROCEDURECOLUMNS] = FALSE; pfExists[SQL_API_SQLPROCEDURES] = FALSE; pfExists[SQL_API_SQLSETPOS] = FALSE; - pfExists[SQL_API_SQLSETSCROLLOPTIONS] = FALSE; + pfExists[SQL_API_SQLSETSCROLLOPTIONS] = FALSE; // odbc 1.0 pfExists[SQL_API_SQLTABLEPRIVILEGES] = FALSE; #endif } else { @@ -894,12 +902,12 @@ RETCODE SQL_API SQLGetFunctions( case SQL_API_SQLFREECONNECT: *pfExists = TRUE; break; case SQL_API_SQLFREEENV: *pfExists = TRUE; break; case SQL_API_SQLFREESTMT: *pfExists = TRUE; break; - case SQL_API_SQLGETCURSORNAME: *pfExists = FALSE; break; + case SQL_API_SQLGETCURSORNAME: *pfExists = TRUE; break; case SQL_API_SQLNUMRESULTCOLS: *pfExists = TRUE; break; case SQL_API_SQLPREPARE: *pfExists = TRUE; break; case SQL_API_SQLROWCOUNT: *pfExists = TRUE; break; - case SQL_API_SQLSETCURSORNAME: *pfExists = FALSE; break; - case SQL_API_SQLSETPARAM: *pfExists = FALSE; break; + case SQL_API_SQLSETCURSORNAME: *pfExists = TRUE; break; + case SQL_API_SQLSETPARAM: *pfExists = FALSE; break; // odbc 1.0 case SQL_API_SQLTRANSACT: *pfExists = TRUE; break; // ODBC level 1 functions @@ -910,7 +918,7 @@ RETCODE SQL_API SQLGetFunctions( case SQL_API_SQLGETDATA: *pfExists = TRUE; break; case SQL_API_SQLGETFUNCTIONS: *pfExists = TRUE; break; case SQL_API_SQLGETINFO: *pfExists = TRUE; break; - case SQL_API_SQLGETSTMTOPTION: *pfExists = TRUE; break; // very partial + case SQL_API_SQLGETSTMTOPTION: *pfExists = TRUE; break; // partial case SQL_API_SQLGETTYPEINFO: *pfExists = TRUE; break; case SQL_API_SQLPARAMDATA: *pfExists = TRUE; break; case SQL_API_SQLPUTDATA: *pfExists = TRUE; break; @@ -924,11 +932,11 @@ RETCODE SQL_API SQLGetFunctions( case SQL_API_SQLBROWSECONNECT: *pfExists = FALSE; break; case SQL_API_SQLCOLUMNPRIVILEGES: *pfExists = FALSE; break; case SQL_API_SQLDATASOURCES: *pfExists = FALSE; break; // only implemented by DM - case SQL_API_SQLDESCRIBEPARAM: *pfExists = FALSE; break; - case SQL_API_SQLDRIVERS: *pfExists = FALSE; break; - case SQL_API_SQLEXTENDEDFETCH: *pfExists = TRUE; break; // partial? + case SQL_API_SQLDESCRIBEPARAM: *pfExists = TRUE; break; + case SQL_API_SQLDRIVERS: *pfExists = FALSE; break; // only implemented by DM + case SQL_API_SQLEXTENDEDFETCH: *pfExists = globals.use_declarefetch ? FALSE : TRUE; break; case SQL_API_SQLFOREIGNKEYS: *pfExists = TRUE; break; - case SQL_API_SQLMORERESULTS: *pfExists = TRUE; break; + case SQL_API_SQLMORERESULTS: *pfExists = FALSE; break; case SQL_API_SQLNATIVESQL: *pfExists = TRUE; break; case SQL_API_SQLNUMPARAMS: *pfExists = TRUE; break; case SQL_API_SQLPARAMOPTIONS: *pfExists = FALSE; break; @@ -936,7 +944,7 @@ RETCODE SQL_API SQLGetFunctions( case SQL_API_SQLPROCEDURECOLUMNS: *pfExists = FALSE; break; case SQL_API_SQLPROCEDURES: *pfExists = FALSE; break; case SQL_API_SQLSETPOS: *pfExists = FALSE; break; - case SQL_API_SQLSETSCROLLOPTIONS: *pfExists = FALSE; break; + case SQL_API_SQLSETSCROLLOPTIONS: *pfExists = FALSE; break; // odbc 1.0 case SQL_API_SQLTABLEPRIVILEGES: *pfExists = FALSE; break; } #endif @@ -965,8 +973,14 @@ HSTMT htbl_stmt; RETCODE result; char *tableType; char tables_query[MAX_STATEMENT_LEN]; -char table_name[MAX_INFO_STRING], table_owner[MAX_INFO_STRING]; -SDWORD table_name_len, table_owner_len; +char table_name[MAX_INFO_STRING], table_owner[MAX_INFO_STRING], relhasrules[MAX_INFO_STRING]; +SDWORD table_name_len, table_owner_len, relhasrules_len; +ConnInfo *ci; +char *prefix[32], prefixes[MEDIUM_REGISTRY_LEN]; +char *table_type[32], table_types[MAX_INFO_STRING]; +char show_system_tables, show_regular_tables, show_views; +char regular_table, view, systable; +int i; mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); @@ -976,6 +990,8 @@ mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); stmt->manual_result = TRUE; stmt->errormsg_created = TRUE; + ci = &stmt->hdbc->connInfo; + result = SQLAllocStmt( stmt->hdbc, &htbl_stmt); if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { stmt->errornumber = STMT_NO_MEMORY_ERROR; @@ -988,20 +1004,72 @@ mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); // Create the query to find out the tables // ********************************************************************** - strcpy(tables_query, "select relname, usename from pg_class, pg_user where relkind = 'r' "); + strcpy(tables_query, "select relname, usename, relhasrules from pg_class, pg_user where relkind = 'r' "); my_strcat(tables_query, " and usename like '%.*s'", szTableOwner, cbTableOwner); my_strcat(tables_query, " and relname like '%.*s'", szTableName, cbTableName); + + // Parse the extra systable prefix + strcpy(prefixes, globals.extra_systable_prefixes); + i = 0; + prefix[i] = strtok(prefixes, ";"); + while (prefix[i] && i<32) { + prefix[++i] = strtok(NULL, ";"); + } + + /* Parse the desired table types to return */ + show_system_tables = FALSE; + show_regular_tables = FALSE; + show_views = FALSE; + // make_string mallocs memory tableType = make_string(szTableType, cbTableType, NULL); - if (tableType && ! strstr(tableType, "SYSTEM TABLE")) // is SYSTEM TABLE not present? - strcat(tables_query, " and relname not like '" POSTGRES_SYS_PREFIX "%' and relname not like '" INSIGHT_SYS_PREFIX "%'"); - - if (tableType) + if (tableType) { + strcpy(table_types, tableType); free(tableType); + i = 0; + table_type[i] = strtok(table_types, ","); + while (table_type[i] && i<32) { + table_type[++i] = strtok(NULL, ","); + } - strcat(tables_query, " and relname !~ '^Inv[0-9]+' and int4out(usesysid) = int4out(relowner) order by relname"); + /* Check for desired table types to return */ + i = 0; + while (table_type[i]) { + if ( strstr(table_type[i], "SYSTEM TABLE")) + show_system_tables = TRUE; + else if ( strstr(table_type[i], "TABLE")) + show_regular_tables = TRUE; + else if ( strstr(table_type[i], "VIEW")) + show_views = TRUE; + + i++; + } + + } + + /* If not interested in SYSTEM TABLES then filter them out + to save some time on the query. If treating system tables + as regular tables, then dont filter either. + */ + if ( ! atoi(ci->show_system_tables) && ! show_system_tables) { + strcat(tables_query, " and relname !~ '^" POSTGRES_SYS_PREFIX); + + /* Also filter out user-defined system table types */ + i = 0; + while(prefix[i]) { + strcat(tables_query, "|^"); + strcat(tables_query, prefix[i]); + i++; + } + + strcat(tables_query, "'"); + } + + + /* filter out large objects unconditionally (they are not system tables) and match users */ + strcat(tables_query, " and relname !~ '^xinv[0-9]+' and int4out(usesysid) = int4out(relowner) order by relname"); // ********************************************************************** @@ -1030,6 +1098,14 @@ mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); SQLFreeStmt(htbl_stmt, SQL_DROP); return SQL_ERROR; } + result = SQLBindCol(htbl_stmt, 3, SQL_C_CHAR, + relhasrules, MAX_INFO_STRING, &relhasrules_len); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = tbl_stmt->errormsg; + stmt->errornumber = tbl_stmt->errornumber; + SQLFreeStmt(htbl_stmt, SQL_DROP); + return SQL_ERROR; + } stmt->result = QR_Constructor(); if(!stmt->result) { @@ -1054,33 +1130,63 @@ mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); // add the tuples result = SQLFetch(htbl_stmt); while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { - row = (TupleNode *)malloc(sizeof(TupleNode) + (5 - 1) * sizeof(TupleField)); - set_tuplefield_string(&row->tuple[0], ""); + /* Determine if this table name is a system table. + If treating system tables as regular tables, then + no need to do this test. + */ + systable = FALSE; + if( ! atoi(ci->show_system_tables)) { - // I have to hide the table owner from Access, otherwise it - // insists on referring to the table as 'owner.table'. - // (this is valid according to the ODBC SQL grammar, but - // Postgres won't support it.) - // set_tuplefield_string(&row->tuple[1], table_owner); + if ( strncmp(table_name, POSTGRES_SYS_PREFIX, strlen(POSTGRES_SYS_PREFIX)) == 0) + systable = TRUE; - set_tuplefield_string(&row->tuple[1], ""); - set_tuplefield_string(&row->tuple[2], table_name); - - mylog("SQLTables: table_name = '%s'\n", table_name); - - // careful: this is case-sensitive - if(strncmp(table_name, POSTGRES_SYS_PREFIX, strlen(POSTGRES_SYS_PREFIX)) == 0 || - strncmp(table_name, INSIGHT_SYS_PREFIX, strlen(INSIGHT_SYS_PREFIX)) == 0) { - set_tuplefield_string(&row->tuple[3], "SYSTEM TABLE"); - } else { - set_tuplefield_string(&row->tuple[3], "TABLE"); + else { /* Check extra system table prefixes */ + i = 0; + while (prefix[i]) { + mylog("table_name='%s', prefix[%d]='%s'\n", table_name, i, prefix[i]); + if (strncmp(table_name, prefix[i], strlen(prefix[i])) == 0) { + systable = TRUE; + break; + } + i++; + } + } } - set_tuplefield_string(&row->tuple[4], ""); + /* Determine if the table name is a view */ + view = (relhasrules[0] == '1'); - QR_add_tuple(stmt->result, row); + /* It must be a regular table */ + regular_table = ( ! systable && ! view); + + /* Include the row in the result set if meets all criteria */ + /* NOTE: Unsupported table types (i.e., LOCAL TEMPORARY, ALIAS, etc) + will return nothing */ + if ( (systable && show_system_tables) || + (view && show_views) || + (regular_table && show_regular_tables)) { + + row = (TupleNode *)malloc(sizeof(TupleNode) + (5 - 1) * sizeof(TupleField)); + + set_tuplefield_string(&row->tuple[0], ""); + + // I have to hide the table owner from Access, otherwise it + // insists on referring to the table as 'owner.table'. + // (this is valid according to the ODBC SQL grammar, but + // Postgres won't support it.) + // set_tuplefield_string(&row->tuple[1], table_owner); + + mylog("SQLTables: table_name = '%s'\n", table_name); + + set_tuplefield_string(&row->tuple[1], ""); + set_tuplefield_string(&row->tuple[2], table_name); + set_tuplefield_string(&row->tuple[3], systable ? "SYSTEM TABLE" : (view ? "VIEW" : "TABLE")); + set_tuplefield_string(&row->tuple[4], ""); + + QR_add_tuple(stmt->result, row); + } result = SQLFetch(htbl_stmt); } if(result != SQL_NO_DATA_FOUND) { @@ -1096,6 +1202,7 @@ mylog("**** SQLTables(): ENTER, stmt=%u\n", stmt); // set up the current tuple pointer for SQLFetch stmt->currTuple = -1; + stmt->current_col = -1; SQLFreeStmt(htbl_stmt, SQL_DROP); mylog("SQLTables(): EXIT, stmt=%u\n", stmt); @@ -1125,6 +1232,9 @@ Int4 field_type; SDWORD table_owner_len, table_name_len, field_name_len, field_type_len, field_type_name_len, field_number_len, field_length_len, mod_length_len; +ConnInfo *ci; + + mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt); @@ -1133,13 +1243,15 @@ mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt); stmt->manual_result = TRUE; stmt->errormsg_created = TRUE; - + + ci = &stmt->hdbc->connInfo; + // ********************************************************************** // Create the query to find out the columns (Note: pre 6.3 did not have the atttypmod field) // ********************************************************************** sprintf(columns_query, "select u.usename, c.relname, a.attname, a.atttypid,t.typname, a.attnum, a.attlen, %s from pg_user u, pg_class c, pg_attribute a, pg_type t where " - "int4out(u.usesysid) = int4out(c.relowner) and c.oid= a.attrelid and a.atttypid = t.oid and (a.attnum > 0)", - PROTOCOL_62(&(stmt->hdbc->connInfo)) ? "a.attlen" : "a.atttypmod"); + "int4out(u.usesysid) = int4out(c.relowner) and c.oid= a.attrelid and a.atttypid = t.oid and (a.attnum > 0)", + PROTOCOL_62(ci) ? "a.attlen" : "a.atttypmod"); my_strcat(columns_query, " and c.relname like '%.*s'", szTableName, cbTableName); my_strcat(columns_query, " and u.usename like '%.*s'", szTableOwner, cbTableOwner); @@ -1266,7 +1378,43 @@ mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt); QR_set_field_info(stmt->result, 10, "NULLABLE", PG_TYPE_INT2, 2); QR_set_field_info(stmt->result, 11, "REMARKS", PG_TYPE_TEXT, 254); - result = SQLFetch(hcol_stmt); + + result = SQLFetch(hcol_stmt); + + /* Only show oid if option AND there are other columns AND + its not being called by SQLStatistics . + Always show OID if its a system table + */ + + if (result != SQL_ERROR && ! stmt->internal && + (atoi(ci->show_oid_column) || strncmp(table_name, POSTGRES_SYS_PREFIX, strlen(POSTGRES_SYS_PREFIX)) == 0)) { + + /* For OID fields */ + row = (TupleNode *)malloc(sizeof(TupleNode) + + (12 - 1) * sizeof(TupleField)); + + set_tuplefield_string(&row->tuple[0], ""); + // see note in SQLTables() + // set_tuplefield_string(&row->tuple[1], table_owner); + set_tuplefield_string(&row->tuple[1], ""); + set_tuplefield_string(&row->tuple[2], table_name); + set_tuplefield_string(&row->tuple[3], "oid"); + set_tuplefield_int2(&row->tuple[4], pgtype_to_sqltype(stmt, PG_TYPE_OID)); + set_tuplefield_string(&row->tuple[5], "OID"); + + set_tuplefield_int4(&row->tuple[7], pgtype_length(stmt, PG_TYPE_OID, PG_STATIC, + PG_STATIC)); + set_tuplefield_int4(&row->tuple[6], pgtype_precision(stmt, PG_TYPE_OID, PG_STATIC, + PG_STATIC)); + + set_nullfield_int2(&row->tuple[8], pgtype_scale(stmt, PG_TYPE_OID)); + set_nullfield_int2(&row->tuple[9], pgtype_radix(stmt, PG_TYPE_OID)); + set_tuplefield_int2(&row->tuple[10], SQL_NO_NULLS); + set_tuplefield_string(&row->tuple[11], ""); + + QR_add_tuple(stmt->result, row); + } + while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { row = (TupleNode *)malloc(sizeof(TupleNode) + (12 - 1) * sizeof(TupleField)); @@ -1277,51 +1425,40 @@ mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt); set_tuplefield_string(&row->tuple[1], ""); set_tuplefield_string(&row->tuple[2], table_name); set_tuplefield_string(&row->tuple[3], field_name); - - /* Replace an unknown postgres type with SQL_CHAR type */ - /* Leave the field_type_name with "unknown" */ - if (pgtype_to_sqltype(field_type) == PG_UNKNOWN) - set_tuplefield_int2(&row->tuple[4], SQL_CHAR); - else - set_tuplefield_int2(&row->tuple[4], pgtype_to_sqltype(field_type)); - + set_tuplefield_int2(&row->tuple[4], pgtype_to_sqltype(stmt, field_type)); set_tuplefield_string(&row->tuple[5], field_type_name); - - - /* Some Notes about Postgres Data Types: - - VARCHAR - the length is stored in the pg_attribute.atttypmod field - BPCHAR - the length is also stored as varchar is - NAME - the length is fixed and stored in pg_attribute.attlen field (32 on my system) - + + + /* Some Notes about Postgres Data Types: + + VARCHAR - the length is stored in the pg_attribute.atttypmod field + BPCHAR - the length is also stored as varchar is + */ if((field_type == PG_TYPE_VARCHAR) || - (field_type == PG_TYPE_NAME) || (field_type == PG_TYPE_BPCHAR)) { - if (field_type == PG_TYPE_NAME) - mod_length = field_length; // the length is in attlen - else if (mod_length >= 4) + if (mod_length >= 4) mod_length -= 4; // the length is in atttypmod - 4 - if (mod_length > MAX_VARCHAR_SIZE || mod_length <= 0) - mod_length = MAX_VARCHAR_SIZE; + if (mod_length > globals.max_varchar_size || mod_length <= 0) + mod_length = globals.max_varchar_size; - mylog("SQLColumns: field type is VARCHAR,NAME: field_type = %d, mod_length = %d\n", field_type, mod_length); + mylog("SQLColumns: field type is VARCHAR,BPCHAR: field_type = %d, mod_length = %d\n", field_type, mod_length); set_tuplefield_int4(&row->tuple[7], mod_length); set_tuplefield_int4(&row->tuple[6], mod_length); } else { - mylog("SQLColumns: field type is OTHER: field_type = %d, pgtype_length = %d\n", field_type, pgtype_length(field_type)); + mylog("SQLColumns: field type is OTHER: field_type = %d, pgtype_length = %d\n", field_type, pgtype_length(stmt, field_type, PG_STATIC, PG_STATIC)); - set_tuplefield_int4(&row->tuple[7], pgtype_length(field_type)); - set_tuplefield_int4(&row->tuple[6], pgtype_precision(field_type)); + set_tuplefield_int4(&row->tuple[7], pgtype_length(stmt, field_type, PG_STATIC, PG_STATIC)); + set_tuplefield_int4(&row->tuple[6], pgtype_precision(stmt, field_type, PG_STATIC, PG_STATIC)); } - set_nullfield_int2(&row->tuple[8], pgtype_scale(field_type)); - set_nullfield_int2(&row->tuple[9], pgtype_radix(field_type)); - set_tuplefield_int2(&row->tuple[10], pgtype_nullable(field_type)); + set_nullfield_int2(&row->tuple[8], pgtype_scale(stmt, field_type)); + set_nullfield_int2(&row->tuple[9], pgtype_radix(stmt, field_type)); + set_tuplefield_int2(&row->tuple[10], pgtype_nullable(stmt, field_type)); set_tuplefield_string(&row->tuple[11], ""); QR_add_tuple(stmt->result, row); @@ -1341,6 +1478,7 @@ mylog("**** SQLColumns(): ENTER, stmt=%u\n", stmt); // set up the current tuple pointer for SQLFetch stmt->currTuple = -1; + stmt->current_col = -1; SQLFreeStmt(hcol_stmt, SQL_DROP); mylog("SQLColumns(): EXIT, stmt=%u\n", stmt); @@ -1383,16 +1521,15 @@ mylog("**** SQLSpecialColumns(): ENTER, stmt=%u\n", stmt); /* use the oid value for the rowid */ if(fColType == SQL_BEST_ROWID) { - row = (TupleNode *)malloc(sizeof(TupleNode) + (8 - 1) * sizeof(TupleField)); set_tuplefield_int2(&row->tuple[0], SQL_SCOPE_SESSION); set_tuplefield_string(&row->tuple[1], "oid"); - set_tuplefield_int2(&row->tuple[2], pgtype_to_sqltype(PG_TYPE_OID)); + set_tuplefield_int2(&row->tuple[2], pgtype_to_sqltype(stmt, PG_TYPE_OID)); set_tuplefield_string(&row->tuple[3], "OID"); - set_tuplefield_int4(&row->tuple[4], pgtype_precision(PG_TYPE_OID)); - set_tuplefield_int4(&row->tuple[5], pgtype_length(PG_TYPE_OID)); - set_tuplefield_int2(&row->tuple[6], pgtype_scale(PG_TYPE_OID)); + set_tuplefield_int4(&row->tuple[4], pgtype_precision(stmt, PG_TYPE_OID, PG_STATIC, PG_STATIC)); + set_tuplefield_int4(&row->tuple[5], pgtype_length(stmt, PG_TYPE_OID, PG_STATIC, PG_STATIC)); + set_tuplefield_int2(&row->tuple[6], pgtype_scale(stmt, PG_TYPE_OID)); set_tuplefield_int2(&row->tuple[7], SQL_PC_PSEUDO); QR_add_tuple(stmt->result, row); @@ -1405,6 +1542,7 @@ mylog("**** SQLSpecialColumns(): ENTER, stmt=%u\n", stmt); stmt->status = STMT_FINISHED; stmt->currTuple = -1; + stmt->current_col = -1; mylog("SQLSpecialColumns(): EXIT, stmt=%u\n", stmt); return SQL_SUCCESS; @@ -1428,6 +1566,7 @@ RETCODE result; char *table_name; char index_name[MAX_INFO_STRING]; short fields_vector[8]; +char isunique[10], isclustered[10]; SDWORD index_name_len, fields_vector_len; TupleNode *row; int i; @@ -1438,6 +1577,8 @@ char **column_names = 0; Int4 column_name_len; int total_columns = 0; char error = TRUE; +ConnInfo *ci; +char buf[256]; mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); @@ -1448,6 +1589,8 @@ mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); stmt->manual_result = TRUE; stmt->errormsg_created = TRUE; + ci = &stmt->hdbc->connInfo; + stmt->result = QR_Constructor(); if(!stmt->result) { stmt->errormsg = "Couldn't allocate memory for SQLStatistics result."; @@ -1475,114 +1618,178 @@ mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); QR_set_field_info(stmt->result, 11, "PAGES", PG_TYPE_INT4, 4); QR_set_field_info(stmt->result, 12, "FILTER_CONDITION", PG_TYPE_TEXT, MAX_INFO_STRING); - // there are no unique indexes in postgres, so return nothing - // if those are requested - if(fUnique != SQL_INDEX_UNIQUE) { - // only use the table name... the owner should be redundant, and - // we never use qualifiers. - table_name = make_string(szTableName, cbTableName, NULL); - if ( ! table_name) { - stmt->errormsg = "No table name passed to SQLStatistics."; - stmt->errornumber = STMT_INTERNAL_ERROR; - return SQL_ERROR; - } - // we need to get a list of the field names first, - // so we can return them later. - result = SQLAllocStmt( stmt->hdbc, &hcol_stmt); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for columns."; - stmt->errornumber = STMT_NO_MEMORY_ERROR; - goto SEEYA; - } + // only use the table name... the owner should be redundant, and + // we never use qualifiers. + table_name = make_string(szTableName, cbTableName, NULL); + if ( ! table_name) { + stmt->errormsg = "No table name passed to SQLStatistics."; + stmt->errornumber = STMT_INTERNAL_ERROR; + return SQL_ERROR; + } - col_stmt = (StatementClass *) hcol_stmt; + // we need to get a list of the field names first, + // so we can return them later. + result = SQLAllocStmt( stmt->hdbc, &hcol_stmt); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for columns."; + stmt->errornumber = STMT_NO_MEMORY_ERROR; + goto SEEYA; + } - result = SQLColumns(hcol_stmt, "", 0, "", 0, - table_name, (SWORD) strlen(table_name), "", 0); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = col_stmt->errormsg; // "SQLColumns failed in SQLStatistics."; - stmt->errornumber = col_stmt->errornumber; // STMT_EXEC_ERROR; - SQLFreeStmt(hcol_stmt, SQL_DROP); - goto SEEYA; - } - result = SQLBindCol(hcol_stmt, 4, SQL_C_CHAR, - column_name, MAX_INFO_STRING, &column_name_len); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = col_stmt->errormsg; - stmt->errornumber = col_stmt->errornumber; + col_stmt = (StatementClass *) hcol_stmt; + + /* "internal" prevents SQLColumns from returning the oid if it is being shown. + This would throw everything off. + */ + col_stmt->internal = TRUE; + result = SQLColumns(hcol_stmt, "", 0, "", 0, + table_name, (SWORD) strlen(table_name), "", 0); + col_stmt->internal = FALSE; + + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = col_stmt->errormsg; // "SQLColumns failed in SQLStatistics."; + stmt->errornumber = col_stmt->errornumber; // STMT_EXEC_ERROR; SQLFreeStmt(hcol_stmt, SQL_DROP); goto SEEYA; + } + result = SQLBindCol(hcol_stmt, 4, SQL_C_CHAR, + column_name, MAX_INFO_STRING, &column_name_len); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = col_stmt->errormsg; + stmt->errornumber = col_stmt->errornumber; + SQLFreeStmt(hcol_stmt, SQL_DROP); + goto SEEYA; - } + } + + result = SQLFetch(hcol_stmt); + while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { + total_columns++; + + column_names = + (char **)realloc(column_names, + total_columns * sizeof(char *)); + column_names[total_columns-1] = + (char *)malloc(strlen(column_name)+1); + strcpy(column_names[total_columns-1], column_name); + + mylog("SQLStatistics: column_name = '%s'\n", column_name); result = SQLFetch(hcol_stmt); - while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { - total_columns++; - - column_names = - (char **)realloc(column_names, - total_columns * sizeof(char *)); - column_names[total_columns-1] = - (char *)malloc(strlen(column_name)+1); - strcpy(column_names[total_columns-1], column_name); - - result = SQLFetch(hcol_stmt); - } - if(result != SQL_NO_DATA_FOUND || total_columns == 0) { - stmt->errormsg = SC_create_errormsg(hcol_stmt); // "Couldn't get column names in SQLStatistics."; - stmt->errornumber = col_stmt->errornumber; - SQLFreeStmt(hcol_stmt, SQL_DROP); - goto SEEYA; - - } - - SQLFreeStmt(hcol_stmt, SQL_DROP); - - // get a list of indexes on this table - result = SQLAllocStmt( stmt->hdbc, &hindx_stmt); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for indices."; - stmt->errornumber = STMT_NO_MEMORY_ERROR; - goto SEEYA; - - } - indx_stmt = (StatementClass *) hindx_stmt; - - sprintf(index_query, "select c.relname, i.indkey from pg_index i, pg_class c, pg_class d where c.oid = i.indexrelid and d.relname = '%s' and d.oid = i.indrelid", - table_name); - - result = SQLExecDirect(hindx_stmt, index_query, strlen(index_query)); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = SC_create_errormsg(hindx_stmt); // "Couldn't execute index query (w/SQLExecDirect) in SQLStatistics."; - stmt->errornumber = indx_stmt->errornumber; - SQLFreeStmt(hindx_stmt, SQL_DROP); - goto SEEYA; - - } - - result = SQLBindCol(hindx_stmt, 1, SQL_C_CHAR, - index_name, MAX_INFO_STRING, &index_name_len); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; - stmt->errornumber = indx_stmt->errornumber; - SQLFreeStmt(hindx_stmt, SQL_DROP); + } + if(result != SQL_NO_DATA_FOUND || total_columns == 0) { + stmt->errormsg = SC_create_errormsg(hcol_stmt); // "Couldn't get column names in SQLStatistics."; + stmt->errornumber = col_stmt->errornumber; + SQLFreeStmt(hcol_stmt, SQL_DROP); goto SEEYA; - } - // bind the vector column - result = SQLBindCol(hindx_stmt, 2, SQL_C_DEFAULT, - fields_vector, 16, &fields_vector_len); - if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { - stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; - stmt->errornumber = indx_stmt->errornumber; - SQLFreeStmt(hindx_stmt, SQL_DROP); - goto SEEYA; + } + + SQLFreeStmt(hcol_stmt, SQL_DROP); - } + // get a list of indexes on this table + result = SQLAllocStmt( stmt->hdbc, &hindx_stmt); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = "SQLAllocStmt failed in SQLStatistics for indices."; + stmt->errornumber = STMT_NO_MEMORY_ERROR; + goto SEEYA; - result = SQLFetch(hindx_stmt); - while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { + } + indx_stmt = (StatementClass *) hindx_stmt; + + sprintf(index_query, "select c.relname, i.indkey, i.indisunique, i.indisclustered from pg_index i, pg_class c, pg_class d where c.oid = i.indexrelid and d.relname = '%s' and d.oid = i.indrelid", + table_name); + + result = SQLExecDirect(hindx_stmt, index_query, strlen(index_query)); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = SC_create_errormsg(hindx_stmt); // "Couldn't execute index query (w/SQLExecDirect) in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + + } + + // bind the index name column + result = SQLBindCol(hindx_stmt, 1, SQL_C_CHAR, + index_name, MAX_INFO_STRING, &index_name_len); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + + } + // bind the vector column + result = SQLBindCol(hindx_stmt, 2, SQL_C_DEFAULT, + fields_vector, 16, &fields_vector_len); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + + } + // bind the "is unique" column + result = SQLBindCol(hindx_stmt, 3, SQL_C_CHAR, + isunique, sizeof(isunique), NULL); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + } + + // bind the "is clustered" column + result = SQLBindCol(hindx_stmt, 4, SQL_C_CHAR, + isclustered, sizeof(isclustered), NULL); + if((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO)) { + stmt->errormsg = indx_stmt->errormsg; // "Couldn't bind column in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + + } + + /* fake index of OID */ + if (atoi(ci->show_oid_column) && atoi(ci->fake_oid_index)) { + row = (TupleNode *)malloc(sizeof(TupleNode) + + (13 - 1) * sizeof(TupleField)); + + // no table qualifier + set_tuplefield_string(&row->tuple[0], ""); + // don't set the table owner, else Access tries to use it + set_tuplefield_string(&row->tuple[1], ""); + set_tuplefield_string(&row->tuple[2], table_name); + + // non-unique index? + set_tuplefield_int2(&row->tuple[3], (Int2) (globals.unique_index ? FALSE : TRUE)); + + // no index qualifier + set_tuplefield_string(&row->tuple[4], ""); + + sprintf(buf, "%s_idx_fake_oid", table_name); + set_tuplefield_string(&row->tuple[5], buf); + + // Clustered index? I think non-clustered should be type OTHER not HASHED + set_tuplefield_int2(&row->tuple[6], (Int2) SQL_INDEX_OTHER); + set_tuplefield_int2(&row->tuple[7], (Int2) 1); + + set_tuplefield_string(&row->tuple[8], "oid"); + set_tuplefield_string(&row->tuple[9], "A"); + set_tuplefield_null(&row->tuple[10]); + set_tuplefield_null(&row->tuple[11]); + set_tuplefield_null(&row->tuple[12]); + + QR_add_tuple(stmt->result, row); + } + + result = SQLFetch(hindx_stmt); + while((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO)) { + + // If only requesting unique indexs, then just return those. + if (fUnique == SQL_INDEX_ALL || + (fUnique == SQL_INDEX_UNIQUE && atoi(isunique))) { i = 0; // add a row in this table for each field in the index while(i < 8 && fields_vector[i] != 0) { @@ -1596,24 +1803,32 @@ mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); set_tuplefield_string(&row->tuple[1], ""); set_tuplefield_string(&row->tuple[2], table_name); - // Postgres95 indices always allow non-unique values. - set_tuplefield_int2(&row->tuple[3], TRUE); + // non-unique index? + if (globals.unique_index) + set_tuplefield_int2(&row->tuple[3], (Int2) (atoi(isunique) ? FALSE : TRUE)); + else + set_tuplefield_int2(&row->tuple[3], TRUE); // no index qualifier set_tuplefield_string(&row->tuple[4], ""); set_tuplefield_string(&row->tuple[5], index_name); - // check this--what does it mean for an index - // to be clustered? (none of mine seem to be-- - // we can and probably should find this out from - // the pg_index table) - set_tuplefield_int2(&row->tuple[6], SQL_INDEX_HASHED); + // Clustered index? I think non-clustered should be type OTHER not HASHED + set_tuplefield_int2(&row->tuple[6], (Int2) (atoi(isclustered) ? SQL_INDEX_CLUSTERED : SQL_INDEX_OTHER)); set_tuplefield_int2(&row->tuple[7], (Int2) (i+1)); - if(fields_vector[i] < 0 || fields_vector[i] > total_columns) + if(fields_vector[i] == OID_ATTNUM) { + set_tuplefield_string(&row->tuple[8], "oid"); + mylog("SQLStatistics: column name = oid\n"); + } + else if(fields_vector[i] < 0 || fields_vector[i] > total_columns) { set_tuplefield_string(&row->tuple[8], "UNKNOWN"); - else + mylog("SQLStatistics: column name = UNKNOWN\n"); + } + else { set_tuplefield_string(&row->tuple[8], column_names[fields_vector[i]-1]); + mylog("SQLStatistics: column name = '%s'\n", column_names[fields_vector[i]-1]); + } set_tuplefield_string(&row->tuple[9], "A"); set_tuplefield_null(&row->tuple[10]); @@ -1622,19 +1837,19 @@ mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); QR_add_tuple(stmt->result, row); i++; - } + } + } - result = SQLFetch(hindx_stmt); - } - if(result != SQL_NO_DATA_FOUND) { - stmt->errormsg = SC_create_errormsg(hindx_stmt); // "SQLFetch failed in SQLStatistics."; - stmt->errornumber = indx_stmt->errornumber; - SQLFreeStmt(hindx_stmt, SQL_DROP); - goto SEEYA; - } - - SQLFreeStmt(hindx_stmt, SQL_DROP); + result = SQLFetch(hindx_stmt); } + if(result != SQL_NO_DATA_FOUND) { + stmt->errormsg = SC_create_errormsg(hindx_stmt); // "SQLFetch failed in SQLStatistics."; + stmt->errornumber = indx_stmt->errornumber; + SQLFreeStmt(hindx_stmt, SQL_DROP); + goto SEEYA; + } + + SQLFreeStmt(hindx_stmt, SQL_DROP); // also, things need to think that this statement is finished so // the results can be retrieved. @@ -1642,6 +1857,7 @@ mylog("**** SQLStatistics(): ENTER, stmt=%u\n", stmt); // set up the current tuple pointer for SQLFetch stmt->currTuple = -1; + stmt->current_col = -1; error = FALSE; @@ -1672,6 +1888,7 @@ RETCODE SQL_API SQLColumnPrivileges( UCHAR FAR * szColumnName, SWORD cbColumnName) { +/* Neither Access or Borland care about this. */ return SQL_ERROR; } @@ -1702,7 +1919,7 @@ int nk = 0; tbl_stmt = (StatementClass *) htbl_stmt; tables_query[0] = '\0'; - if ( ! my_strcat(tables_query, "select distinct on attnum a2.attname, a2.attnum from pg_attribute a1, pg_attribute a2, pg_class c, pg_index i where c.relname = '%.*s_key' AND c.oid = i.indexrelid AND a1.attrelid = c.oid AND a2.attrelid = c.oid AND (i.indkey[0] = a1.attnum OR i.indkey[1] = a1.attnum OR i.indkey[2] = a1.attnum OR i.indkey[3] = a1.attnum OR i.indkey[4] = a1.attnum OR i.indkey[5] = a1.attnum OR i.indkey[6] = a1.attnum OR i.indkey[7] = a1.attnum) order by a2.attnum", + if ( ! my_strcat(tables_query, "select distinct on attnum a2.attname, a2.attnum from pg_attribute a1, pg_attribute a2, pg_class c, pg_index i where c.relname = '%.*s_pkey' AND c.oid = i.indexrelid AND a1.attrelid = c.oid AND a2.attrelid = c.oid AND (i.indkey[0] = a1.attnum OR i.indkey[1] = a1.attnum OR i.indkey[2] = a1.attnum OR i.indkey[3] = a1.attnum OR i.indkey[4] = a1.attnum OR i.indkey[5] = a1.attnum OR i.indkey[6] = a1.attnum OR i.indkey[7] = a1.attnum) order by a2.attnum", szTableName, cbTableName)) { stmt->errormsg = "No Table specified to getPrimaryKeyString."; @@ -1883,6 +2100,7 @@ mylog("**** SQLPrimaryKeys(): ENTER, stmt=%u\n", stmt); // set up the current tuple pointer for SQLFetch stmt->currTuple = -1; + stmt->current_col = -1; mylog("SQLPrimaryKeys(): EXIT, stmt=%u\n", stmt); return SQL_SUCCESS; @@ -2134,6 +2352,7 @@ mylog("**** SQLForeignKeys(): ENTER, stmt=%u\n", stmt); // set up the current tuple pointer for SQLFetch stmt->currTuple = -1; + stmt->current_col = -1; mylog("SQLForeignKeys(): EXIT, stmt=%u\n", stmt); return SQL_SUCCESS; diff --git a/src/interfaces/odbc/license.txt b/src/interfaces/odbc/license.txt index c5e8acfc57..bdf8ab0cb3 100644 --- a/src/interfaces/odbc/license.txt +++ b/src/interfaces/odbc/license.txt @@ -1,962 +1,962 @@ GNU LIBRARY GENERAL PUBLIC LICENSE - + Version 2, June 1991 - - + + Copyright (C) 1991 Free Software Foundation, Inc. - + 675 Mass Ave, Cambridge, MA 02139, USA - + Everyone is permitted to copy and distribute verbatim copies - + of this license document, but changing it is not allowed. - - + + [This is the first released version of the library GPL. It is - + numbered 2 because it goes with version 2 of the ordinary GPL.] - - + + Preamble - - + + The licenses for most software are designed to take away your - + freedom to share and change it. By contrast, the GNU General Public - + Licenses are intended to guarantee your freedom to share and change - + free software--to make sure the software is free for all its users. - - + + This license, the Library General Public License, applies to some - + specially designated Free Software Foundation software, and to any - + other libraries whose authors decide to use it. You can use it for - + your libraries, too. - - + + When we speak of free software, we are referring to freedom, not - + price. Our General Public Licenses are designed to make sure that you - + have the freedom to distribute copies of free software (and charge for - + this service if you wish), that you receive source code or can get it - + if you want it, that you can change the software or use pieces of it - + in new free programs; and that you know you can do these things. - - + + To protect your rights, we need to make restrictions that forbid - + anyone to deny you these rights or to ask you to surrender the rights. - + These restrictions translate to certain responsibilities for you if - + you distribute copies of the library, or if you modify it. - - + + For example, if you distribute copies of the library, whether gratis - + or for a fee, you must give the recipients all the rights that we gave - + you. You must make sure that they, too, receive or can get the source - + code. If you link a program with the library, you must provide - + complete object files to the recipients so that they can relink them - + with the library, after making changes to the library and recompiling - + it. And you must show them these terms so they know their rights. - - + + Our method of protecting your rights has two steps: (1) copyright - + the library, and (2) offer you this license which gives you legal - + permission to copy, distribute and/or modify the library. - - + + Also, for each distributor's protection, we want to make certain - + that everyone understands that there is no warranty for this free - + library. If the library is modified by someone else and passed on, we - + want its recipients to know that what they have is not the original - + version, so that any problems introduced by others will not reflect on - + the original authors' reputations. - + - + Finally, any free program is threatened constantly by software - + patents. We wish to avoid the danger that companies distributing free - + software will individually obtain patent licenses, thus in effect - + transforming the program into proprietary software. To prevent this, - + we have made it clear that any patent must be licensed for everyone's - + free use or not licensed at all. - - + + Most GNU software, including some libraries, is covered by the ordinary - + GNU General Public License, which was designed for utility programs. This - + license, the GNU Library General Public License, applies to certain - + designated libraries. This license is quite different from the ordinary - + one; be sure to read it in full, and don't assume that anything in it is - + the same as in the ordinary license. - - + + The reason we have a separate public license for some libraries is that - + they blur the distinction we usually make between modifying or adding to a - + program and simply using it. Linking a program with a library, without - + changing the library, is in some sense simply using the library, and is - + analogous to running a utility program or application program. However, in - + a textual and legal sense, the linked executable is a combined work, a - + derivative of the original library, and the ordinary General Public License - + treats it as such. - - + + Because of this blurred distinction, using the ordinary General - + Public License for libraries did not effectively promote software - + sharing, because most developers did not use the libraries. We - + concluded that weaker conditions might promote sharing better. - - + + However, unrestricted linking of non-free programs would deprive the - + users of those programs of all benefit from the free status of the - + libraries themselves. This Library General Public License is intended to - + permit developers of non-free programs to use free libraries, while - + preserving your freedom as a user of such programs to change the free - + libraries that are incorporated in them. (We have not seen how to achieve - + this as regards changes in header files, but we have achieved it as regards - + changes in the actual functions of the Library.) The hope is that this - + will lead to faster development of free libraries. - - + + The precise terms and conditions for copying, distribution and - + modification follow. Pay close attention to the difference between a - + "work based on the library" and a "work that uses the library". The - + former contains code derived from the library, while the latter only - + works together with the library. - - + + Note that it is possible for a library to be covered by the ordinary - + General Public License rather than by this special one. - + - + GNU LIBRARY GENERAL PUBLIC LICENSE - + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - + + 0. This License Agreement applies to any software library which - + contains a notice placed by the copyright holder or other authorized - + party saying it may be distributed under the terms of this Library - + General Public License (also called "this License"). Each licensee is - + addressed as "you". - - + + A "library" means a collection of software functions and/or data - + prepared so as to be conveniently linked with application programs - + (which use some of those functions and data) to form executables. - - + + The "Library", below, refers to any such software library or work - + which has been distributed under these terms. A "work based on the - + Library" means either the Library or any derivative work under - + copyright law: that is to say, a work containing the Library or a - + portion of it, either verbatim or with modifications and/or translated - + straightforwardly into another language. (Hereinafter, translation is - + included without limitation in the term "modification".) - - + + "Source code" for a work means the preferred form of the work for - + making modifications to it. For a library, complete source code means - + all the source code for all modules it contains, plus any associated - + interface definition files, plus the scripts used to control compilation - + and installation of the library. - - + + Activities other than copying, distribution and modification are not - + covered by this License; they are outside its scope. The act of - + running a program using the Library is not restricted, and output from - + such a program is covered only if its contents constitute a work based - + on the Library (independent of the use of the Library in a tool for - + writing it). Whether that is true depends on what the Library does - + and what the program that uses the Library does. - + - + 1. You may copy and distribute verbatim copies of the Library's - + complete source code as you receive it, in any medium, provided that - + you conspicuously and appropriately publish on each copy an - + appropriate copyright notice and disclaimer of warranty; keep intact - + all the notices that refer to this License and to the absence of any - + warranty; and distribute a copy of this License along with the - + Library. - - + + You may charge a fee for the physical act of transferring a copy, - + and you may at your option offer warranty protection in exchange for a - + fee. - + - + 2. You may modify your copy or copies of the Library or any portion - + of it, thus forming a work based on the Library, and copy and - + distribute such modifications or work under the terms of Section 1 - + above, provided that you also meet all of these conditions: - - + + a) The modified work must itself be a software library. - - + + b) You must cause the files modified to carry prominent notices - + stating that you changed the files and the date of any change. - - + + c) You must cause the whole of the work to be licensed at no - + charge to all third parties under the terms of this License. - - + + d) If a facility in the modified Library refers to a function or a - + table of data to be supplied by an application program that uses - + the facility, other than as an argument passed when the facility - + is invoked, then you must make a good faith effort to ensure that, - + in the event an application does not supply such function or - + table, the facility still operates, and performs whatever part of - + its purpose remains meaningful. - - + + (For example, a function in a library to compute square roots has - + a purpose that is entirely well-defined independent of the - + application. Therefore, Subsection 2d requires that any - + application-supplied function or table used by this function must - + be optional: if the application does not supply it, the square - + root function must still compute square roots.) - - + + These requirements apply to the modified work as a whole. If - + identifiable sections of that work are not derived from the Library, - + and can be reasonably considered independent and separate works in - + themselves, then this License, and its terms, do not apply to those - + sections when you distribute them as separate works. But when you - + distribute the same sections as part of a whole which is a work based - + on the Library, the distribution of the whole must be on the terms of - + this License, whose permissions for other licensees extend to the - + entire whole, and thus to each and every part regardless of who wrote - + it. - - + + Thus, it is not the intent of this section to claim rights or contest - + your rights to work written entirely by you; rather, the intent is to - + exercise the right to control the distribution of derivative or - + collective works based on the Library. - - + + In addition, mere aggregation of another work not based on the Library - + with the Library (or with a work based on the Library) on a volume of - + a storage or distribution medium does not bring the other work under - + the scope of this License. - - + + 3. You may opt to apply the terms of the ordinary GNU General Public - + License instead of this License to a given copy of the Library. To do - + this, you must alter all the notices that refer to this License, so - + that they refer to the ordinary GNU General Public License, version 2, - + instead of to this License. (If a newer version than version 2 of the - + ordinary GNU General Public License has appeared, then you can specify - + that version instead if you wish.) Do not make any other change in - + these notices. - + - + Once this change is made in a given copy, it is irreversible for - + that copy, so the ordinary GNU General Public License applies to all - + subsequent copies and derivative works made from that copy. - - + + This option is useful when you wish to copy part of the code of - + the Library into a program that is not a library. - - + + 4. You may copy and distribute the Library (or a portion or - + derivative of it, under Section 2) in object code or executable form - + under the terms of Sections 1 and 2 above provided that you accompany - + it with the complete corresponding machine-readable source code, which - + must be distributed under the terms of Sections 1 and 2 above on a - + medium customarily used for software interchange. - - + + If distribution of object code is made by offering access to copy - + from a designated place, then offering equivalent access to copy the - + source code from the same place satisfies the requirement to - + distribute the source code, even though third parties are not - + compelled to copy the source along with the object code. - - + + 5. A program that contains no derivative of any portion of the - + Library, but is designed to work with the Library by being compiled or - + linked with it, is called a "work that uses the Library". Such a - + work, in isolation, is not a derivative work of the Library, and - + therefore falls outside the scope of this License. - - + + However, linking a "work that uses the Library" with the Library - + creates an executable that is a derivative of the Library (because it - + contains portions of the Library), rather than a "work that uses the - + library". The executable is therefore covered by this License. - + Section 6 states terms for distribution of such executables. - - + + When a "work that uses the Library" uses material from a header file - + that is part of the Library, the object code for the work may be a - + derivative work of the Library even though the source code is not. - + Whether this is true is especially significant if the work can be - + linked without the Library, or if the work is itself a library. The - + threshold for this to be true is not precisely defined by law. - - + + If such an object file uses only numerical parameters, data - + structure layouts and accessors, and small macros and small inline - + functions (ten lines or less in length), then the use of the object - + file is unrestricted, regardless of whether it is legally a derivative - + work. (Executables containing this object code plus portions of the - + Library will still fall under Section 6.) - - + + Otherwise, if the work is a derivative of the Library, you may - + distribute the object code for the work under the terms of Section 6. - + Any executables containing that work also fall under Section 6, - + whether or not they are linked directly with the Library itself. - + - + 6. As an exception to the Sections above, you may also compile or - + link a "work that uses the Library" with the Library to produce a - + work containing portions of the Library, and distribute that work - + under terms of your choice, provided that the terms permit - + modification of the work for the customer's own use and reverse - + engineering for debugging such modifications. - - + + You must give prominent notice with each copy of the work that the - + Library is used in it and that the Library and its use are covered by - + this License. You must supply a copy of this License. If the work - + during execution displays copyright notices, you must include the - + copyright notice for the Library among them, as well as a reference - + directing the user to the copy of this License. Also, you must do one - + of these things: - - + + a) Accompany the work with the complete corresponding - + machine-readable source code for the Library including whatever - + changes were used in the work (which must be distributed under - + Sections 1 and 2 above); and, if the work is an executable linked - + with the Library, with the complete machine-readable "work that - + uses the Library", as object code and/or source code, so that the - + user can modify the Library and then relink to produce a modified - + executable containing the modified Library. (It is understood - + that the user who changes the contents of definitions files in the - + Library will not necessarily be able to recompile the application - + to use the modified definitions.) - - + + b) Accompany the work with a written offer, valid for at - + least three years, to give the same user the materials - + specified in Subsection 6a, above, for a charge no more - + than the cost of performing this distribution. - - + + c) If distribution of the work is made by offering access to copy - + from a designated place, offer equivalent access to copy the above - + specified materials from the same place. - - + + d) Verify that the user has already received a copy of these - + materials or that you have already sent this user a copy. - - + + For an executable, the required form of the "work that uses the - + Library" must include any data and utility programs needed for - + reproducing the executable from it. However, as a special exception, - + the source code distributed need not include anything that is normally - + distributed (in either source or binary form) with the major - + components (compiler, kernel, and so on) of the operating system on - + which the executable runs, unless that component itself accompanies - + the executable. - - + + It may happen that this requirement contradicts the license - + restrictions of other proprietary libraries that do not normally - + accompany the operating system. Such a contradiction means you cannot - + use both them and the Library together in an executable that you - + distribute. - + - + 7. You may place library facilities that are a work based on the - + Library side-by-side in a single library together with other library - + facilities not covered by this License, and distribute such a combined - + library, provided that the separate distribution of the work based on - + the Library and of the other library facilities is otherwise - + permitted, and provided that you do these two things: - - + + a) Accompany the combined library with a copy of the same work - + based on the Library, uncombined with any other library - + facilities. This must be distributed under the terms of the - + Sections above. - - + + b) Give prominent notice with the combined library of the fact - + that part of it is a work based on the Library, and explaining - + where to find the accompanying uncombined form of the same work. - - + + 8. You may not copy, modify, sublicense, link with, or distribute - + the Library except as expressly provided under this License. Any - + attempt otherwise to copy, modify, sublicense, link with, or - + distribute the Library is void, and will automatically terminate your - + rights under this License. However, parties who have received copies, - + or rights, from you under this License will not have their licenses - + terminated so long as such parties remain in full compliance. - - + + 9. You are not required to accept this License, since you have not - + signed it. However, nothing else grants you permission to modify or - + distribute the Library or its derivative works. These actions are - + prohibited by law if you do not accept this License. Therefore, by - + modifying or distributing the Library (or any work based on the - + Library), you indicate your acceptance of this License to do so, and - + all its terms and conditions for copying, distributing or modifying - + the Library or works based on it. - - + + 10. Each time you redistribute the Library (or any work based on the - + Library), the recipient automatically receives a license from the - + original licensor to copy, distribute, link with or modify the Library - + subject to these terms and conditions. You may not impose any further - + restrictions on the recipients' exercise of the rights granted herein. - + You are not responsible for enforcing compliance by third parties to - + this License. - + - + 11. If, as a consequence of a court judgment or allegation of patent - + infringement or for any other reason (not limited to patent issues), - + conditions are imposed on you (whether by court order, agreement or - + otherwise) that contradict the conditions of this License, they do not - + excuse you from the conditions of this License. If you cannot - + distribute so as to satisfy simultaneously your obligations under this - + License and any other pertinent obligations, then as a consequence you - + may not distribute the Library at all. For example, if a patent - + license would not permit royalty-free redistribution of the Library by - + all those who receive copies directly or indirectly through you, then - + the only way you could satisfy both it and this License would be to - + refrain entirely from distribution of the Library. - - + + If any portion of this section is held invalid or unenforceable under any - + particular circumstance, the balance of the section is intended to apply, - + and the section as a whole is intended to apply in other circumstances. - - + + It is not the purpose of this section to induce you to infringe any - + patents or other property right claims or to contest validity of any - + such claims; this section has the sole purpose of protecting the - + integrity of the free software distribution system which is - + implemented by public license practices. Many people have made - + generous contributions to the wide range of software distributed - + through that system in reliance on consistent application of that - + system; it is up to the author/donor to decide if he or she is willing - + to distribute software through any other system and a licensee cannot - + impose that choice. - - + + This section is intended to make thoroughly clear what is believed to - + be a consequence of the rest of this License. - - + + 12. If the distribution and/or use of the Library is restricted in - + certain countries either by patents or by copyrighted interfaces, the - + original copyright holder who places the Library under this License may add - + an explicit geographical distribution limitation excluding those countries, - + so that distribution is permitted only in or among countries not thus - + excluded. In such case, this License incorporates the limitation as if - + written in the body of this License. - - + + 13. The Free Software Foundation may publish revised and/or new - + versions of the Library General Public License from time to time. - + Such new versions will be similar in spirit to the present version, - + but may differ in detail to address new problems or concerns. - - + + Each version is given a distinguishing version number. If the Library - + specifies a version number of this License which applies to it and - + "any later version", you have the option of following the terms and - + conditions either of that version or of any later version published by - + the Free Software Foundation. If the Library does not specify a - + license version number, you may choose any version ever published by - + the Free Software Foundation. - + - + 14. If you wish to incorporate parts of the Library into other free - + programs whose distribution conditions are incompatible with these, - + write to the author to ask for permission. For software which is - + copyrighted by the Free Software Foundation, write to the Free - + Software Foundation; we sometimes make exceptions for this. Our - + decision will be guided by the two goals of preserving the free status - + of all derivatives of our free software and of promoting the sharing - + and reuse of software generally. - - + + NO WARRANTY - - + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO - + WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. - + EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR - + OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY - + KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE - + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - + PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE - + LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME - + THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN - + WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY - + AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU - + FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR - + CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE - + LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING - + RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A - + FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF - + SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH - + DAMAGES. - - + + END OF TERMS AND CONDITIONS - + - + Appendix: How to Apply These Terms to Your New Libraries - - + + If you develop a new library, and you want it to be of the greatest - + possible use to the public, we recommend making it free software that - + everyone can redistribute and change. You can do so by permitting - + redistribution under these terms (or, alternatively, under the terms of the - + ordinary General Public License). - - + + To apply these terms, attach the following notices to the library. It is - + safest to attach them to the start of each source file to most effectively - + convey the exclusion of warranty; and each file should have at least the - + "copyright" line and a pointer to where the full notice is found. - - + + - + Copyright (C) - - + + This library is free software; you can redistribute it and/or - + modify it under the terms of the GNU Library General Public - + License as published by the Free Software Foundation; either - + version 2 of the License, or (at your option) any later version. - - + + This library is distributed in the hope that it will be useful, - + but WITHOUT ANY WARRANTY; without even the implied warranty of - + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - + Library General Public License for more details. - - + + You should have received a copy of the GNU Library General Public - + License along with this library; if not, write to the Free - + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - + + Also add information on how to contact you by electronic and paper mail. - - + + You should also get your employer (if you work as a programmer) or your - + school, if any, to sign a "copyright disclaimer" for the library, if - + necessary. Here is a sample; alter the names: - - + + Yoyodyne, Inc., hereby disclaims all copyright interest in the - + library `Frob' (a library for tweaking knobs) written by James Random Hacker. - - + + , 1 April 1990 - - Ty Coon, President of Vice - - + Ty Coon, President of Vice + + + That's all there is to it! - + diff --git a/src/interfaces/odbc/misc.c b/src/interfaces/odbc/misc.c index 7ce83484d8..382508d673 100644 --- a/src/interfaces/odbc/misc.c +++ b/src/interfaces/odbc/misc.c @@ -1,28 +1,27 @@ - -/* Module: misc.c - * - * Description: This module contains miscellaneous routines - * such as for debugging/logging and string functions. - * - * Classes: n/a - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: misc.c + * + * Description: This module contains miscellaneous routines + * such as for debugging/logging and string functions. + * + * Classes: n/a + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include -#include -#include +#include #include "psqlodbc.h" - + + extern GLOBAL_VALUES globals; #ifdef MY_LOG -#include void mylog(va_alist) @@ -52,7 +51,6 @@ static FILE *LOGFP = 0; #ifdef Q_LOG -#include void qlog(va_alist) va_dcl @@ -78,10 +76,17 @@ static FILE *LOGFP = 0; } #endif +/* Undefine these because windows.h will redefine and cause a warning */ +#undef va_start +#undef va_end + +#include +#include + /* returns STRCPY_FAIL, STRCPY_TRUNCATED, or #bytes copied (not including null term) */ int -my_strcpy(char *dst, size_t dst_len, char *src, size_t src_len) +my_strcpy(char *dst, int dst_len, char *src, int src_len) { if (dst_len <= 0) return STRCPY_FAIL; @@ -90,31 +95,23 @@ my_strcpy(char *dst, size_t dst_len, char *src, size_t src_len) dst[0] = '\0'; return STRCPY_NULL; } + else if (src_len == SQL_NTS) + src_len = strlen(src); - else if (src_len == SQL_NTS) { - if (src_len < dst_len) - strcpy(dst, src); - else { - memcpy(dst, src, dst_len-1); - dst[dst_len-1] = '\0'; /* truncated */ - return STRCPY_TRUNCATED; - } - } - - else if (src_len <= 0) + if (src_len <= 0) return STRCPY_FAIL; else { if (src_len < dst_len) { memcpy(dst, src, src_len); - dst[src_len] = '\0'; + dst[src_len] = '\0'; } else { memcpy(dst, src, dst_len-1); dst[dst_len-1] = '\0'; /* truncated */ return STRCPY_TRUNCATED; } - } + } return strlen(dst); } @@ -123,9 +120,9 @@ my_strcpy(char *dst, size_t dst_len, char *src, size_t src_len) // the destination string if src has len characters or more. // instead, I want it to copy up to len-1 characters and always // terminate the destination string. -char *strncpy_null(char *dst, const char *src, size_t len) +char *strncpy_null(char *dst, const char *src, int len) { -unsigned int i; +int i; if (NULL != dst) { @@ -138,7 +135,7 @@ unsigned int i; else if (len == SQL_NTS) len = strlen(src) + 1; - for(i = 0; src[i] && i < len - 1; i++) { + for(i = 0; src[i] && i < len - 1; i++) { dst[i] = src[i]; } diff --git a/src/interfaces/odbc/misc.h b/src/interfaces/odbc/misc.h index b9047f16ff..b9e81d4464 100644 --- a/src/interfaces/odbc/misc.h +++ b/src/interfaces/odbc/misc.h @@ -1,11 +1,11 @@ - -/* File: misc.h - * - * Description: See "misc.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: misc.h + * + * Description: See "misc.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __MISC_H__ #define __MISC_H__ @@ -23,7 +23,7 @@ /* Uncomment Q_LOG to compile in the qlog() statements (Communications log, i.e. CommLog). This logfile contains serious log statements that are intended for an end user to be able to read and understand. It is controlled by the - 'CommLog' flag in the ODBCINST.INI portion of the registry (see above), + 'CommLog' flag in the ODBCINST.INI portion of the registry (see above), which is manipulated on the setup/connection dialog boxes. */ #define Q_LOG @@ -42,7 +42,7 @@ void qlog(); /* prototype */ #endif void remove_newlines(char *string); -char *strncpy_null(char *dst, const char *src, size_t len); +char *strncpy_null(char *dst, const char *src, int len); char *trim(char *string); char *make_string(char *s, int len, char *buf); char *my_strcat(char *buf, char *fmt, char *s, int len); @@ -53,6 +53,6 @@ char *my_strcat(char *buf, char *fmt, char *s, int len); #define STRCPY_TRUNCATED -1 #define STRCPY_NULL -2 -int my_strcpy(char *dst, size_t dst_len, char *src, size_t src_len); +int my_strcpy(char *dst, int dst_len, char *src, int src_len); #endif diff --git a/src/interfaces/odbc/notice.txt b/src/interfaces/odbc/notice.txt index 695ba6b483..70df4401cd 100644 --- a/src/interfaces/odbc/notice.txt +++ b/src/interfaces/odbc/notice.txt @@ -1,35 +1,35 @@ - -/******************************************************************** - - PSQLODBC.DLL - A library to talk to the PostgreSQL DBMS using ODBC. - - - Copyright (C) 1998; Insight Distribution Systems - - The code contained in this library is based on code written by - Christian Czezatke and Dan McGuirk, (C) 1996. - - - This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with this library (see "license.txt"); if not, write to - the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA - 02139, USA. - - - How to contact the author: - - email: byronn@insightdist.com (Byron Nikolaidis) - - -***********************************************************************/ - + +/******************************************************************** + + PSQLODBC.DLL - A library to talk to the PostgreSQL DBMS using ODBC. + + + Copyright (C) 1998; Insight Distribution Systems + + The code contained in this library is based on code written by + Christian Czezatke and Dan McGuirk, (C) 1996. + + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library (see "license.txt"); if not, write to + the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA + 02139, USA. + + + How to contact the author: + + email: byronn@insightdist.com (Byron Nikolaidis) + + +***********************************************************************/ + diff --git a/src/interfaces/odbc/options.c b/src/interfaces/odbc/options.c index 1649eedfa4..716cfcdfdf 100644 --- a/src/interfaces/odbc/options.c +++ b/src/interfaces/odbc/options.c @@ -1,25 +1,30 @@ - -/* Module: options.c - * - * Description: This module contains routines for getting/setting - * connection and statement options. - * - * Classes: n/a - * - * API functions: SQLSetConnectOption, SQLSetStmtOption, SQLGetConnectOption, - * SQLGetStmtOption - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: options.c + * + * Description: This module contains routines for getting/setting + * connection and statement options. + * + * Classes: n/a + * + * API functions: SQLSetConnectOption, SQLSetStmtOption, SQLGetConnectOption, + * SQLGetStmtOption + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "psqlodbc.h" #include #include +#include #include "environ.h" #include "connection.h" #include "statement.h" + +extern GLOBAL_VALUES globals; + + /* Implements only SQL_AUTOCOMMIT */ RETCODE SQL_API SQLSetConnectOption( HDBC hdbc, @@ -81,58 +86,6 @@ ConnectionClass *conn = (ConnectionClass *) hdbc; // - - - - - - - - - -RETCODE SQL_API SQLSetStmtOption( - HSTMT hstmt, - UWORD fOption, - UDWORD vParam) -{ -StatementClass *stmt = (StatementClass *) hstmt; - - // thought we could fake Access out by just returning SQL_SUCCESS - // all the time, but it tries to set a huge value for SQL_MAX_LENGTH - // and expects the driver to reduce it to the real value - - if( ! stmt) { - return SQL_INVALID_HANDLE; - } - - switch(fOption) { - case SQL_QUERY_TIMEOUT: - mylog("SetStmtOption: vParam = %d\n", vParam); - /* - stmt->errornumber = STMT_OPTION_VALUE_CHANGED; - stmt->errormsg = "Query Timeout: value changed to 0"; - return SQL_SUCCESS_WITH_INFO; - */ - return SQL_SUCCESS; - break; - case SQL_MAX_LENGTH: -/* CC: Some apps consider returning SQL_SUCCESS_WITH_INFO to be an error */ -/* so if we're going to return SQL_SUCCESS, we better not set an */ -/* error message. (otherwise, if a subsequent function call returns */ -/* SQL_ERROR without setting a message, things can get confused.) */ - - /* - stmt->errormsg = "Requested value changed."; - stmt->errornumber = STMT_OPTION_VALUE_CHANGED; - */ - - return SQL_SUCCESS; - break; - case SQL_MAX_ROWS: - mylog("SetStmtOption(): SQL_MAX_ROWS = %d, returning success\n", vParam); - stmt->maxRows = vParam; - return SQL_SUCCESS; - break; - default: - return SQL_ERROR; - } - - return SQL_SUCCESS; -} - -// - - - - - - - - - - /* This function just can tell you whether you are in Autcommit mode or not */ RETCODE SQL_API SQLGetConnectOption( HDBC hdbc, @@ -141,32 +94,127 @@ RETCODE SQL_API SQLGetConnectOption( { ConnectionClass *conn = (ConnectionClass *) hdbc; - if (! conn) - return SQL_INVALID_HANDLE; + if (! conn) + return SQL_INVALID_HANDLE; - switch (fOption) { - case SQL_AUTOCOMMIT: - /* CC 28.05.96: Do not set fOption, but pvParam */ - *((UDWORD *)pvParam) = (UDWORD)( CC_is_in_autocommit(conn) ? - SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF); - break; - /* we don't use qualifiers */ - case SQL_CURRENT_QUALIFIER: - if(pvParam) { - strcpy(pvParam, ""); - } - break; - default: - conn->errormsg = "This option is currently unsupported by the driver"; - conn->errornumber = CONN_UNSUPPORTED_OPTION; - return SQL_ERROR; - break; + switch (fOption) { + case SQL_AUTOCOMMIT: + *((UDWORD *)pvParam) = (UDWORD)( CC_is_in_autocommit(conn) ? + SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF); + break; - } + /* don't use qualifiers */ + case SQL_CURRENT_QUALIFIER: + if(pvParam) + strcpy(pvParam, ""); - return SQL_SUCCESS; + break; + + default: + conn->errormsg = "This option is currently unsupported by the driver"; + conn->errornumber = CONN_UNSUPPORTED_OPTION; + return SQL_ERROR; + break; + + } + + return SQL_SUCCESS; } +// - - - - - - - - - + +RETCODE SQL_API SQLSetStmtOption( + HSTMT hstmt, + UWORD fOption, + UDWORD vParam) +{ +StatementClass *stmt = (StatementClass *) hstmt; +char changed = FALSE; + + // thought we could fake Access out by just returning SQL_SUCCESS + // all the time, but it tries to set a huge value for SQL_MAX_LENGTH + // and expects the driver to reduce it to the real value + + if( ! stmt) + return SQL_INVALID_HANDLE; + + switch(fOption) { + case SQL_QUERY_TIMEOUT: + mylog("SetStmtOption: vParam = %d\n", vParam); + // "0" returned in SQLGetStmtOption + break; + + case SQL_MAX_LENGTH: + // "4096" returned in SQLGetStmtOption + break; + + case SQL_MAX_ROWS: + mylog("SetStmtOption(): SQL_MAX_ROWS = %d, returning success\n", vParam); + stmt->maxRows = vParam; + return SQL_SUCCESS; + break; + + case SQL_ROWSET_SIZE: + mylog("SetStmtOption(): SQL_ROWSET_SIZE = %d\n", vParam); + + stmt->rowset_size = 1; // only support 1 row at a time + if (vParam != 1) + changed = TRUE; + + break; + + case SQL_CONCURRENCY: + // positioned update isn't supported so cursor concurrency is read-only + mylog("SetStmtOption(): SQL_CONCURRENCY = %d\n", vParam); + + stmt->scroll_concurrency = SQL_CONCUR_READ_ONLY; + if (vParam != SQL_CONCUR_READ_ONLY) + changed = TRUE; + + break; + + case SQL_CURSOR_TYPE: + // if declare/fetch, then type can only be forward. + // otherwise, it can only be forward or static. + mylog("SetStmtOption(): SQL_CURSOR_TYPE = %d\n", vParam); + + if (globals.use_declarefetch) { + stmt->cursor_type = SQL_CURSOR_FORWARD_ONLY; + if (vParam != SQL_CURSOR_FORWARD_ONLY) + changed = TRUE; + } + else { + if (vParam == SQL_CURSOR_FORWARD_ONLY || vParam == SQL_CURSOR_STATIC) + stmt->cursor_type = vParam; // valid type + else { + stmt->cursor_type = SQL_CURSOR_STATIC; + changed = TRUE; + } + } + + break; + + case SQL_SIMULATE_CURSOR: + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + stmt->errormsg = "Simulated positioned update/delete not supported"; + return SQL_ERROR; + + default: + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + stmt->errormsg = "Driver does not support this statement option"; + return SQL_ERROR; + } + + if (changed) { + stmt->errormsg = "Requested value changed."; + stmt->errornumber = STMT_OPTION_VALUE_CHANGED; + return SQL_SUCCESS_WITH_INFO; + } + else + return SQL_SUCCESS; +} + + // - - - - - - - - - RETCODE SQL_API SQLGetStmtOption( @@ -180,31 +228,54 @@ StatementClass *stmt = (StatementClass *) hstmt; // all the time, but it tries to set a huge value for SQL_MAX_LENGTH // and expects the driver to reduce it to the real value - if( ! stmt) { - return SQL_INVALID_HANDLE; - } + if( ! stmt) + return SQL_INVALID_HANDLE; + + switch(fOption) { + case SQL_QUERY_TIMEOUT: + // how long we wait on a query before returning to the + // application (0 == forever) + *((SDWORD *)pvParam) = 0; + break; + + case SQL_MAX_LENGTH: + // what is the maximum length that will be returned in + // a single column + *((SDWORD *)pvParam) = 4096; + break; - switch(fOption) { - case SQL_QUERY_TIMEOUT: - // how long we wait on a query before returning to the - // application (0 == forever) - *((SDWORD *)pvParam) = 0; - break; - case SQL_MAX_LENGTH: - // what is the maximum length that will be returned in - // a single column - *((SDWORD *)pvParam) = 4096; - break; case SQL_MAX_ROWS: *((SDWORD *)pvParam) = stmt->maxRows; mylog("GetSmtOption: MAX_ROWS, returning %d\n", stmt->maxRows); - break; - default: - return SQL_ERROR; - } - return SQL_SUCCESS; + case SQL_ROWSET_SIZE: + mylog("GetStmtOption(): SQL_ROWSET_SIZE\n"); + *((SDWORD *)pvParam) = stmt->rowset_size; + break; + + case SQL_CONCURRENCY: + mylog("GetStmtOption(): SQL_CONCURRENCY\n"); + *((SDWORD *)pvParam) = stmt->scroll_concurrency; + break; + + case SQL_CURSOR_TYPE: + mylog("GetStmtOption(): SQL_CURSOR_TYPE\n"); + *((SDWORD *)pvParam) = stmt->cursor_type; + break; + + case SQL_SIMULATE_CURSOR: + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + stmt->errormsg = "Simulated positioned update/delete not supported"; + return SQL_ERROR; + + default: + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + stmt->errormsg = "Driver does not support this statement option"; + return SQL_ERROR; + } + + return SQL_SUCCESS; } // - - - - - - - - - diff --git a/src/interfaces/odbc/pgtypes.c b/src/interfaces/odbc/pgtypes.c index e1762b3fec..20d9b42adc 100644 --- a/src/interfaces/odbc/pgtypes.c +++ b/src/interfaces/odbc/pgtypes.c @@ -1,26 +1,34 @@ - -/* Module: pgtypes.c - * - * Description: This module contains routines for getting information - * about the supported Postgres data types. Only the function - * pgtype_to_sqltype() returns an unknown condition. All other - * functions return a suitable default so that even data types that - * are not directly supported can be used (it is handled as char data). - * - * Classes: n/a - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: pgtypes.c + * + * Description: This module contains routines for getting information + * about the supported Postgres data types. Only the function + * pgtype_to_sqltype() returns an unknown condition. All other + * functions return a suitable default so that even data types that + * are not directly supported can be used (it is handled as char data). + * + * Classes: n/a + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "psqlodbc.h" +#include "dlg_specific.h" #include "pgtypes.h" +#include "statement.h" +#include "connection.h" +#include "qresult.h" #include #include #include + +extern GLOBAL_VALUES globals; + + /* these are the types we support. all of the pgtype_ functions should */ /* return values for each one of these. */ @@ -32,13 +40,14 @@ Int4 pgtypes_defined[] = { PG_TYPE_CHAR2, PG_TYPE_CHAR4, PG_TYPE_CHAR8, - PG_TYPE_BPCHAR, + PG_TYPE_CHAR16, + PG_TYPE_NAME, PG_TYPE_VARCHAR, + PG_TYPE_BPCHAR, PG_TYPE_DATE, PG_TYPE_TIME, PG_TYPE_ABSTIME, /* a timestamp, sort of */ PG_TYPE_TEXT, - PG_TYPE_NAME, PG_TYPE_INT2, PG_TYPE_INT4, PG_TYPE_FLOAT4, @@ -46,9 +55,9 @@ Int4 pgtypes_defined[] = { PG_TYPE_OID, PG_TYPE_MONEY, PG_TYPE_BOOL, - PG_TYPE_CHAR16, - PG_TYPE_DATETIME, - PG_TYPE_BYTEA, + PG_TYPE_DATETIME, + PG_TYPE_BYTEA, + PG_TYPE_LO, 0 }; @@ -57,113 +66,197 @@ Int4 pgtypes_defined[] = { 2. When taking any type id (SQLColumns, SQLGetData) The first type will always work because all the types defined are returned here. - The second type will return PG_UNKNOWN when it does not know. The calling - routine checks for this and changes it to a char type. This allows for supporting + The second type will return a default based on global parameter when it does not + know. This allows for supporting types that are unknown. All other pg routines in here return a suitable default. */ -Int2 pgtype_to_sqltype(Int4 type) +Int2 pgtype_to_sqltype(StatementClass *stmt, Int4 type) { - switch(type) { - case PG_TYPE_CHAR: + switch(type) { + case PG_TYPE_CHAR: case PG_TYPE_CHAR2: case PG_TYPE_CHAR4: - case PG_TYPE_CHAR8: - case PG_TYPE_CHAR16: return SQL_CHAR; + case PG_TYPE_CHAR8: + case PG_TYPE_CHAR16: + case PG_TYPE_NAME: return SQL_CHAR; - case PG_TYPE_BPCHAR: - case PG_TYPE_NAME: - case PG_TYPE_VARCHAR: return SQL_VARCHAR; + case PG_TYPE_BPCHAR: return SQL_CHAR; // temporary? - case PG_TYPE_TEXT: return SQL_LONGVARCHAR; - case PG_TYPE_BYTEA: return SQL_LONGVARBINARY; + case PG_TYPE_VARCHAR: return SQL_VARCHAR; - case PG_TYPE_INT2: return SQL_SMALLINT; - case PG_TYPE_OID: - case PG_TYPE_INT4: return SQL_INTEGER; - case PG_TYPE_FLOAT4: return SQL_REAL; - case PG_TYPE_FLOAT8: return SQL_FLOAT; + case PG_TYPE_TEXT: return globals.text_as_longvarchar ? SQL_LONGVARCHAR : SQL_VARCHAR; + + case PG_TYPE_BYTEA: return SQL_VARBINARY; + case PG_TYPE_LO: return SQL_LONGVARBINARY; + + case PG_TYPE_INT2: return SQL_SMALLINT; + case PG_TYPE_OID: + case PG_TYPE_INT4: return SQL_INTEGER; + case PG_TYPE_FLOAT4: return SQL_REAL; + case PG_TYPE_FLOAT8: return SQL_FLOAT; case PG_TYPE_DATE: return SQL_DATE; case PG_TYPE_TIME: return SQL_TIME; case PG_TYPE_ABSTIME: case PG_TYPE_DATETIME: return SQL_TIMESTAMP; case PG_TYPE_MONEY: return SQL_FLOAT; - case PG_TYPE_BOOL: return SQL_CHAR; + case PG_TYPE_BOOL: return globals.bools_as_char ? SQL_CHAR : SQL_BIT; - default: return PG_UNKNOWN; /* check return for this */ - } + default: + + /* first, check to see if 'type' is in list. If not, look up with query. + Add oid, name to list. If its already in list, just return. + */ + if (type == stmt->hdbc->lobj_type) /* hack until permanent type is available */ + return SQL_LONGVARBINARY; + + return globals.unknowns_as_longvarchar ? SQL_LONGVARCHAR : SQL_VARCHAR; + } } -Int2 pgtype_to_ctype(Int4 type) +Int2 pgtype_to_ctype(StatementClass *stmt, Int4 type) { - switch(type) { - case PG_TYPE_INT2: return SQL_C_SSHORT; - case PG_TYPE_OID: - case PG_TYPE_INT4: return SQL_C_SLONG; - case PG_TYPE_FLOAT4: return SQL_C_FLOAT; - case PG_TYPE_FLOAT8: return SQL_C_DOUBLE; + switch(type) { + case PG_TYPE_INT2: return SQL_C_SSHORT; + case PG_TYPE_OID: + case PG_TYPE_INT4: return SQL_C_SLONG; + case PG_TYPE_FLOAT4: return SQL_C_FLOAT; + case PG_TYPE_FLOAT8: return SQL_C_DOUBLE; case PG_TYPE_DATE: return SQL_C_DATE; case PG_TYPE_TIME: return SQL_C_TIME; case PG_TYPE_ABSTIME: case PG_TYPE_DATETIME: return SQL_C_TIMESTAMP; case PG_TYPE_MONEY: return SQL_C_FLOAT; - case PG_TYPE_BOOL: return SQL_C_CHAR; - - case PG_TYPE_BYTEA: return SQL_C_BINARY; + case PG_TYPE_BOOL: return globals.bools_as_char ? SQL_C_CHAR : SQL_C_BIT; - default: return SQL_C_CHAR; - } + case PG_TYPE_BYTEA: return SQL_C_BINARY; + case PG_TYPE_LO: return SQL_C_BINARY; + + default: + + if (type == stmt->hdbc->lobj_type) /* hack until permanent type is available */ + return SQL_C_BINARY; + + return SQL_C_CHAR; + } } -char *pgtype_to_name(Int4 type) +char *pgtype_to_name(StatementClass *stmt, Int4 type) { - switch(type) { - case PG_TYPE_CHAR: return "char"; + switch(type) { + case PG_TYPE_CHAR: return "char"; case PG_TYPE_CHAR2: return "char2"; case PG_TYPE_CHAR4: return "char4"; - case PG_TYPE_CHAR8: return "char8"; + case PG_TYPE_CHAR8: return "char8"; case PG_TYPE_CHAR16: return "char16"; - case PG_TYPE_VARCHAR: return "varchar"; - case PG_TYPE_BPCHAR: return "bpchar"; - case PG_TYPE_TEXT: return "text"; - case PG_TYPE_NAME: return "name"; - case PG_TYPE_INT2: return "int2"; - case PG_TYPE_OID: return "oid"; - case PG_TYPE_INT4: return "int4"; - case PG_TYPE_FLOAT4: return "float4"; - case PG_TYPE_FLOAT8: return "float8"; + case PG_TYPE_VARCHAR: return "varchar"; + case PG_TYPE_BPCHAR: return "bpchar"; + case PG_TYPE_TEXT: return "text"; + case PG_TYPE_NAME: return "name"; + case PG_TYPE_INT2: return "int2"; + case PG_TYPE_OID: return "oid"; + case PG_TYPE_INT4: return "int4"; + case PG_TYPE_FLOAT4: return "float4"; + case PG_TYPE_FLOAT8: return "float8"; case PG_TYPE_DATE: return "date"; case PG_TYPE_TIME: return "time"; case PG_TYPE_ABSTIME: return "abstime"; case PG_TYPE_DATETIME: return "datetime"; case PG_TYPE_MONEY: return "money"; - case PG_TYPE_BOOL: return "bool"; + case PG_TYPE_BOOL: return "bool"; case PG_TYPE_BYTEA: return "bytea"; - /* "unknown" can actually be used in alter table because it is a real PG type! */ - default: return "unknown"; - } + case PG_TYPE_LO: return PG_TYPE_LO_NAME; + + default: + if (type == stmt->hdbc->lobj_type) /* hack until permanent type is available */ + return PG_TYPE_LO_NAME; + + /* "unknown" can actually be used in alter table because it is a real PG type! */ + return "unknown"; + } } -/* For PG_TYPE_VARCHAR, PG_TYPE_BPCHAR, SQLColumns will - override this length with the atttypmod length from pg_attribute -*/ -Int4 pgtype_precision(Int4 type) +Int4 +getCharPrecision(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as) { +int p = -1, maxsize; +QResultClass *result; +ColumnInfoClass *flds; + +mylog("getCharPrecision: type=%d, col=%d, unknown = %d\n", type,col,handle_unknown_size_as); + + /* Assign Maximum size based on parameters */ + switch(type) { + case PG_TYPE_TEXT: + if (globals.text_as_longvarchar) + maxsize = globals.max_longvarchar_size; + else + maxsize = globals.max_varchar_size; + break; + + case PG_TYPE_VARCHAR: + case PG_TYPE_BPCHAR: + maxsize = globals.max_varchar_size; + break; + + default: + if (globals.unknowns_as_longvarchar) + maxsize = globals.max_longvarchar_size; + else + maxsize = globals.max_varchar_size; + break; + } + + /* Static Precision (i.e., the Maximum Precision of the datatype) + This has nothing to do with a result set. + */ + if (col < 0) + return maxsize; + + result = SC_get_Result(stmt); + + /* Manual Result Sets -- use assigned column width (i.e., from set_tuplefield_string) */ + if (stmt->manual_result) { + flds = result->fields; + if (flds) + return flds->adtsize[col]; + else + return maxsize; + } + + /* Size is unknown -- handle according to parameter */ + if (type == PG_TYPE_BPCHAR || handle_unknown_size_as == UNKNOWNS_AS_LONGEST) { + p = QR_get_display_size(result, col); + mylog("getCharPrecision: LONGEST: p = %d\n", p); + } + + if (p < 0 && handle_unknown_size_as == UNKNOWNS_AS_MAX) + return maxsize; + else + return p; +} + +/* For PG_TYPE_VARCHAR, PG_TYPE_BPCHAR, SQLColumns will + override this length with the atttypmod length from pg_attribute . + + If col >= 0, then will attempt to get the info from the result set. + This is used for functions SQLDescribeCol and SQLColAttributes. +*/ +Int4 pgtype_precision(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as) +{ + switch(type) { case PG_TYPE_CHAR: return 1; case PG_TYPE_CHAR2: return 2; case PG_TYPE_CHAR4: return 4; case PG_TYPE_CHAR8: return 8; - case PG_TYPE_CHAR16: return 16; + case PG_TYPE_CHAR16: return 16; - case PG_TYPE_NAME: return 32; - - case PG_TYPE_VARCHAR: - case PG_TYPE_BPCHAR: return MAX_VARCHAR_SIZE; + case PG_TYPE_NAME: return NAME_FIELD_SIZE; + + case PG_TYPE_INT2: return 5; - case PG_TYPE_INT2: return 5; - case PG_TYPE_OID: case PG_TYPE_INT4: return 10; @@ -180,29 +273,46 @@ Int4 pgtype_precision(Int4 type) case PG_TYPE_BOOL: return 1; + case PG_TYPE_LO: return SQL_NO_TOTAL; + default: - return TEXT_FIELD_SIZE; /* text field types and unknown types */ + + if (type == stmt->hdbc->lobj_type) /* hack until permanent type is available */ + return SQL_NO_TOTAL; + + /* Handle Character types and unknown types */ + return getCharPrecision(stmt, type, col, handle_unknown_size_as); } } -/* For PG_TYPE_VARCHAR, PG_TYPE_BPCHAR, SQLColumns will - override this length with the atttypmod length from pg_attribute -*/ -Int4 pgtype_length(Int4 type) +Int4 pgtype_display_size(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as) +{ + switch(type) { + case PG_TYPE_INT2: return 6; + + case PG_TYPE_OID: return 10; + + case PG_TYPE_INT4: return 11; + + case PG_TYPE_MONEY: return 15; /* ($9,999,999.99) */ + + case PG_TYPE_FLOAT4: return 13; + + case PG_TYPE_FLOAT8: return 22; + + /* Character types use regular precision */ + default: + return pgtype_precision(stmt, type, col, handle_unknown_size_as); + } +} + +/* For PG_TYPE_VARCHAR, PG_TYPE_BPCHAR, SQLColumns will + override this length with the atttypmod length from pg_attribute +*/ +Int4 pgtype_length(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as) { switch(type) { - case PG_TYPE_CHAR: return 1; - case PG_TYPE_CHAR2: return 2; - case PG_TYPE_CHAR4: return 4; - case PG_TYPE_CHAR8: return 8; - case PG_TYPE_CHAR16: return 16; - - case PG_TYPE_NAME: return 32; - - case PG_TYPE_VARCHAR: - case PG_TYPE_BPCHAR: return MAX_VARCHAR_SIZE; - case PG_TYPE_INT2: return 2; case PG_TYPE_OID: @@ -219,14 +329,14 @@ Int4 pgtype_length(Int4 type) case PG_TYPE_ABSTIME: case PG_TYPE_DATETIME: return 16; - case PG_TYPE_BOOL: return 1; - default: - return TEXT_FIELD_SIZE; /* text field types and unknown types */ + /* Character types use the default precision */ + default: + return pgtype_precision(stmt, type, col, handle_unknown_size_as); } } -Int2 pgtype_scale(Int4 type) +Int2 pgtype_scale(StatementClass *stmt, Int4 type) { switch(type) { @@ -247,7 +357,7 @@ Int2 pgtype_scale(Int4 type) } -Int2 pgtype_radix(Int4 type) +Int2 pgtype_radix(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_INT2: @@ -261,12 +371,12 @@ Int2 pgtype_radix(Int4 type) } } -Int2 pgtype_nullable(Int4 type) +Int2 pgtype_nullable(StatementClass *stmt, Int4 type) { return SQL_NULLABLE; /* everything should be nullable */ } -Int2 pgtype_auto_increment(Int4 type) +Int2 pgtype_auto_increment(StatementClass *stmt, Int4 type) { switch(type) { @@ -287,7 +397,7 @@ Int2 pgtype_auto_increment(Int4 type) } } -Int2 pgtype_case_sensitive(Int4 type) +Int2 pgtype_case_sensitive(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_CHAR: @@ -306,7 +416,7 @@ Int2 pgtype_case_sensitive(Int4 type) } } -Int2 pgtype_money(Int4 type) +Int2 pgtype_money(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_MONEY: return TRUE; @@ -314,7 +424,7 @@ Int2 pgtype_money(Int4 type) } } -Int2 pgtype_searchable(Int4 type) +Int2 pgtype_searchable(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_CHAR: @@ -329,11 +439,10 @@ Int2 pgtype_searchable(Int4 type) case PG_TYPE_NAME: return SQL_SEARCHABLE; default: return SQL_ALL_EXCEPT_LIKE; - } } -Int2 pgtype_unsigned(Int4 type) +Int2 pgtype_unsigned(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_OID: return TRUE; @@ -348,7 +457,7 @@ Int2 pgtype_unsigned(Int4 type) } } -char *pgtype_literal_prefix(Int4 type) +char *pgtype_literal_prefix(StatementClass *stmt, Int4 type) { switch(type) { @@ -363,7 +472,7 @@ char *pgtype_literal_prefix(Int4 type) } } -char *pgtype_literal_suffix(Int4 type) +char *pgtype_literal_suffix(StatementClass *stmt, Int4 type) { switch(type) { @@ -378,7 +487,7 @@ char *pgtype_literal_suffix(Int4 type) } } -char *pgtype_create_params(Int4 type) +char *pgtype_create_params(StatementClass *stmt, Int4 type) { switch(type) { case PG_TYPE_CHAR: diff --git a/src/interfaces/odbc/pgtypes.h b/src/interfaces/odbc/pgtypes.h index 0b4be5fe06..dd7d94370a 100644 --- a/src/interfaces/odbc/pgtypes.h +++ b/src/interfaces/odbc/pgtypes.h @@ -1,19 +1,22 @@ - -/* File: pgtypes.h - * - * Description: See "pgtypes.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: pgtypes.h + * + * Description: See "pgtypes.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __PGTYPES_H__ #define __PGTYPES_H__ +#include "psqlodbc.h" + /* the type numbers are defined by the OID's of the types' rows */ /* in table pg_type */ -#define PG_UNKNOWN -666 /* returned only from pgtype_to_sqltype() */ + +// #define PG_TYPE_LO ???? /* waiting for permanent type */ #define PG_TYPE_BOOL 16 #define PG_TYPE_BYTEA 17 @@ -58,22 +61,30 @@ extern Int4 pgtypes_defined[]; -Int2 pgtype_to_sqltype(Int4 type); -Int2 pgtype_to_ctype(Int4 type); -char *pgtype_to_name(Int4 type); -Int4 pgtype_precision(Int4 type); -Int4 pgtype_length(Int4 type); -Int2 pgtype_scale(Int4 type); -Int2 pgtype_radix(Int4 type); -Int2 pgtype_nullable(Int4 type); -Int2 pgtype_auto_increment(Int4 type); -Int2 pgtype_case_sensitive(Int4 type); -Int2 pgtype_money(Int4 type); -Int2 pgtype_searchable(Int4 type); -Int2 pgtype_unsigned(Int4 type); -char *pgtype_literal_prefix(Int4 type); -char *pgtype_literal_suffix(Int4 type); -char *pgtype_create_params(Int4 type); +/* Defines for pgtype_precision */ +#define PG_STATIC -1 + + +Int2 pgtype_to_sqltype(StatementClass *stmt, Int4 type); +Int2 pgtype_to_ctype(StatementClass *stmt, Int4 type); +char *pgtype_to_name(StatementClass *stmt, Int4 type); + +/* These functions can use static numbers or result sets(col parameter) */ +Int4 pgtype_precision(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as); +Int4 pgtype_display_size(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as); +Int4 pgtype_length(StatementClass *stmt, Int4 type, int col, int handle_unknown_size_as); + +Int2 pgtype_scale(StatementClass *stmt, Int4 type); +Int2 pgtype_radix(StatementClass *stmt, Int4 type); +Int2 pgtype_nullable(StatementClass *stmt, Int4 type); +Int2 pgtype_auto_increment(StatementClass *stmt, Int4 type); +Int2 pgtype_case_sensitive(StatementClass *stmt, Int4 type); +Int2 pgtype_money(StatementClass *stmt, Int4 type); +Int2 pgtype_searchable(StatementClass *stmt, Int4 type); +Int2 pgtype_unsigned(StatementClass *stmt, Int4 type); +char *pgtype_literal_prefix(StatementClass *stmt, Int4 type); +char *pgtype_literal_suffix(StatementClass *stmt, Int4 type); +char *pgtype_create_params(StatementClass *stmt, Int4 type); Int2 sqltype_to_default_ctype(Int2 sqltype); diff --git a/src/interfaces/odbc/psqlodbc.c b/src/interfaces/odbc/psqlodbc.c index 08a22a249d..e5d0d88b8e 100644 --- a/src/interfaces/odbc/psqlodbc.c +++ b/src/interfaces/odbc/psqlodbc.c @@ -1,88 +1,28 @@ - -/* Module: psqlodbc.c - * - * Description: This module contains the main entry point (DllMain) for the library. - * It also contains functions to get and set global variables for the - * driver in the registry. - * - * Classes: n/a - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: psqlodbc.c + * + * Description: This module contains the main entry point (DllMain) for the library. + * It also contains functions to get and set global variables for the + * driver in the registry. + * + * Classes: n/a + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "psqlodbc.h" +#include "dlg_specific.h" #include #include #include #include HINSTANCE NEAR s_hModule; /* Saved module handle. */ -GLOBAL_VALUES globals; - +GLOBAL_VALUES globals; -/* This function reads the ODBCINST.INI portion of - the registry and gets any driver defaults. -*/ -void getGlobalDefaults(void) -{ -char temp[128]; - - - // Fetch Count is stored in driver section - SQLGetPrivateProfileString(DBMS_NAME, INI_FETCH, "", - temp, sizeof(temp), ODBCINST_INI); - if ( temp[0] ) - globals.fetch_max = atoi(temp); - else - globals.fetch_max = FETCH_MAX; - - - // Socket Buffersize is stored in driver section - SQLGetPrivateProfileString(DBMS_NAME, INI_SOCKET, "", - temp, sizeof(temp), ODBCINST_INI); - if ( temp[0] ) - globals.socket_buffersize = atoi(temp); - else - globals.socket_buffersize = SOCK_BUFFER_SIZE; - - - // Debug is stored in the driver section - SQLGetPrivateProfileString(DBMS_NAME, INI_DEBUG, "0", - temp, sizeof(temp), ODBCINST_INI); - globals.debug = atoi(temp); - - - // CommLog is stored in the driver section - SQLGetPrivateProfileString(DBMS_NAME, INI_COMMLOG, "0", - temp, sizeof(temp), ODBCINST_INI); - globals.commlog = atoi(temp); - - - // Optimizer is stored in the driver section only (OFF, ON, or ON=x) - SQLGetPrivateProfileString(DBMS_NAME, INI_OPTIMIZER, "", - globals.optimizer, sizeof(globals.optimizer), ODBCINST_INI); - - - // ConnSettings is stored in the driver section and per datasource for override - SQLGetPrivateProfileString(DBMS_NAME, INI_CONNSETTINGS, "", - globals.conn_settings, sizeof(globals.conn_settings), ODBCINST_INI); -} - - -/* This function writes any global parameters (that can be manipulated) - to the ODBCINST.INI portion of the registry -*/ -void updateGlobals(void) -{ -char tmp[128]; - - sprintf(tmp, "%d", globals.commlog); - SQLWritePrivateProfileString(DBMS_NAME, - INI_COMMLOG, tmp, ODBCINST_INI); -} /* This is where the Driver Manager attaches to this Driver */ BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) diff --git a/src/interfaces/odbc/psqlodbc.h b/src/interfaces/odbc/psqlodbc.h index a08acf6410..386e369600 100644 --- a/src/interfaces/odbc/psqlodbc.h +++ b/src/interfaces/odbc/psqlodbc.h @@ -1,12 +1,12 @@ - -/* File: psqlodbc.h - * - * Description: This file contains defines and declarations that are related to - * the entire driver. - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: psqlodbc.h + * + * Description: This file contains defines and declarations that are related to + * the entire driver. + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __PSQLODBC_H__ #define __PSQLODBC_H__ @@ -21,27 +21,23 @@ typedef UInt4 Oid; /* Limits */ #define MAX_MESSAGE_LEN 8192 -#define MAX_CONNECT_STRING 4096 +#define MAX_CONNECT_STRING 4096 #define ERROR_MSG_LENGTH 4096 #define FETCH_MAX 100 /* default number of rows to cache for declare/fetch */ -#define SOCK_BUFFER_SIZE 4096 /* default socket buffer size */ +#define FETCH_INCR 1000 +#define SOCK_BUFFER_SIZE 4096 /* default socket buffer size */ #define MAX_CONNECTIONS 128 /* conns per environment (arbitrary) */ #define MAX_FIELDS 512 #define BYTELEN 8 #define VARHDRSZ sizeof(Int4) - + /* Registry length limits */ -#define LARGE_REGISTRY_LEN 4096 /* used for special cases */ -#define MEDIUM_REGISTRY_LEN 128 /* normal size for user,database,etc. */ -#define SMALL_REGISTRY_LEN 10 /* for 1/0 settings */ +#define LARGE_REGISTRY_LEN 4096 /* used for special cases */ +#define MEDIUM_REGISTRY_LEN 256 /* normal size for user,database,etc. */ +#define SMALL_REGISTRY_LEN 10 /* for 1/0 settings */ -/* Connection Defaults */ -#define DEFAULT_PORT "5432" -#define DEFAULT_READONLY "1" - /* These prefixes denote system tables */ -#define INSIGHT_SYS_PREFIX "dd_" #define POSTGRES_SYS_PREFIX "pg_" #define KEYS_TABLE "dd_fkey" @@ -54,33 +50,13 @@ typedef UInt4 Oid; /* Driver stuff */ #define DRIVERNAME "PostgreSQL ODBC" #define DBMS_NAME "PostgreSQL" -#define DBMS_VERSION "06.30.0000 PostgreSQL 6.3" -#define POSTGRESDRIVERVERSION "06.30.0000" +#define DBMS_VERSION "06.30.0244 PostgreSQL 6.3" +#define POSTGRESDRIVERVERSION "06.30.0244" #define DRIVER_FILE_NAME "PSQLODBC.DLL" - - -#define PG62 "6.2" /* "Protocol" key setting to force Postgres 6.2 */ -/* INI File Stuff */ -#define ODBC_INI "ODBC.INI" /* ODBC initialization file */ -#define ODBCINST_INI "ODBCINST.INI" /* ODBC Installation file */ -#define INI_DSN DBMS_NAME /* Name of default Datasource in ini file (not used?) */ -#define INI_KDESC "Description" /* Data source description */ -#define INI_SERVER "Servername" /* Name of Server running the Postgres service */ -#define INI_PORT "Port" /* Port on which the Postmaster is listening */ -#define INI_DATABASE "Database" /* Database Name */ -#define INI_USER "Username" /* Default User Name */ -#define INI_PASSWORD "Password" /* Default Password */ -#define INI_DEBUG "Debug" /* Debug flag */ -#define INI_FETCH "Fetch" /* Fetch Max Count */ -#define INI_SOCKET "Socket" /* Socket buffer size */ -#define INI_READONLY "ReadOnly" /* Database is read only */ -#define INI_COMMLOG "CommLog" /* Communication to backend logging */ -#define INI_PROTOCOL "Protocol" /* What protocol (6.2) */ -#define INI_OPTIMIZER "Optimizer" /* Use backend genetic optimizer */ -#define INI_CONNSETTINGS "ConnSettings" /* Anything to send to backend on successful connection */ - +#define PG62 "6.2" /* "Protocol" key setting to force Postgres 6.2 */ + typedef struct ConnectionClass_ ConnectionClass; typedef struct StatementClass_ StatementClass; @@ -93,27 +69,40 @@ typedef struct TupleListClass_ TupleListClass; typedef struct EnvironmentClass_ EnvironmentClass; typedef struct TupleNode_ TupleNode; typedef struct TupleField_ TupleField; - - -typedef struct GlobalValues_ -{ - int fetch_max; - int socket_buffersize; - int debug; - int commlog; - char optimizer[MEDIUM_REGISTRY_LEN]; - char conn_settings[LARGE_REGISTRY_LEN]; -} GLOBAL_VALUES; - - -/* sizes */ -#define TEXT_FIELD_SIZE 4094 /* size of text fields (not including null term) */ -#define MAX_VARCHAR_SIZE 254 /* maximum size of a varchar (not including null term) */ - -/* global prototypes */ -void updateGlobals(void); - +typedef struct lo_arg LO_ARG; + +typedef struct GlobalValues_ +{ + int fetch_max; + int socket_buffersize; + int unknown_sizes; + int max_varchar_size; + int max_longvarchar_size; + char debug; + char commlog; + char disable_optimizer; + char unique_index; + char readonly; + char use_declarefetch; + char text_as_longvarchar; + char unknowns_as_longvarchar; + char bools_as_char; + char extra_systable_prefixes[MEDIUM_REGISTRY_LEN]; + char conn_settings[LARGE_REGISTRY_LEN]; +} GLOBAL_VALUES; + + +#define PG_TYPE_LO -999 /* hack until permanent type available */ +#define PG_TYPE_LO_NAME "lo" +#define OID_ATTNUM -2 /* the attnum in pg_index of the oid */ + +/* sizes */ +#define TEXT_FIELD_SIZE 4094 /* size of text fields (not including null term) */ +#define NAME_FIELD_SIZE 32 /* size of name fields */ +#define MAX_VARCHAR_SIZE 254 /* maximum size of a varchar (not including null term) */ + + #include "misc.h" diff --git a/src/interfaces/odbc/psqlodbc.rc b/src/interfaces/odbc/psqlodbc.rc index b84bb86462..6b251ded00 100644 --- a/src/interfaces/odbc/psqlodbc.rc +++ b/src/interfaces/odbc/psqlodbc.rc @@ -1,205 +1,262 @@ -//Microsoft Developer Studio generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "afxres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// English (U.S.) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -#ifdef _WIN32 -LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US -#pragma code_page(1252) -#endif //_WIN32 - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE DISCARDABLE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE DISCARDABLE -BEGIN - "#include ""afxres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE DISCARDABLE -BEGIN - "\r\n" - "\0" -END - -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// Dialog -// - -DRIVERCONNDIALOG DIALOG DISCARDABLE 0, 0, 269, 133 -STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU -CAPTION "PostgreSQL Connection" -FONT 8, "MS Sans Serif" -BEGIN - RTEXT "&Database:",IDC_STATIC,16,25,37,8 - EDITTEXT DATABASE_EDIT,55,25,72,12,ES_AUTOHSCROLL - RTEXT "&Server:",IDC_STATIC,26,40,27,8 - EDITTEXT SERVER_EDIT,55,40,72,12,ES_AUTOHSCROLL - RTEXT "&Port:",IDC_STATIC,150,40,20,8 - EDITTEXT PORT_EDIT,172,40,72,12,ES_AUTOHSCROLL - RTEXT "&User Name:",IDC_STATIC,16,56,37,8 - EDITTEXT USERNAME_EDIT,55,56,72,12,ES_AUTOHSCROLL - RTEXT "Pass&word:",IDC_STATIC,137,56,33,8 - EDITTEXT PASSWORD_EDIT,172,56,72,12,ES_PASSWORD | ES_AUTOHSCROLL - GROUPBOX "Options:",IDC_STATIC,25,71,200,25 - CONTROL "&ReadOnly:",READONLY_EDIT,"Button",BS_AUTOCHECKBOX | - BS_LEFTTEXT | BS_RIGHT | WS_GROUP | WS_TABSTOP,45,80,45, - 14 - CONTROL "&CommLog (Global):",COMMLOG_EDIT,"Button", - BS_AUTOCHECKBOX | BS_LEFTTEXT | BS_RIGHT | WS_TABSTOP, - 100,80,75,14 - CONTROL "6.2",PG62_EDIT,"Button",BS_AUTOCHECKBOX | BS_LEFTTEXT | - BS_RIGHT | WS_TABSTOP,185,80,25,14 - DEFPUSHBUTTON "OK",IDOK,84,108,40,14,WS_GROUP - PUSHBUTTON "Cancel",IDCANCEL,146,108,40,14 - CTEXT "Please supply any missing information needed to connect.", - IDC_STATIC,40,7,188,11 -END - -CONFIGDSN DIALOG DISCARDABLE 65, 43, 292, 151 -STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | - WS_SYSMENU -CAPTION "PostgreSQL Driver Setup" -FONT 8, "MS Sans Serif" -BEGIN - RTEXT "&Data Source:",IDC_DSNAMETEXT,5,30,50,12,NOT WS_GROUP - EDITTEXT IDC_DSNAME,57,30,72,12,ES_AUTOHSCROLL | WS_GROUP - RTEXT "Des&cription:",IDC_STATIC,135,30,39,12,NOT WS_GROUP - EDITTEXT IDC_DESC,175,30,108,12,ES_AUTOHSCROLL - RTEXT "Data&base:",IDC_STATIC,17,45,38,12,NOT WS_GROUP - EDITTEXT IDC_DATABASE,57,45,72,12,ES_AUTOHSCROLL - RTEXT "&Server:",IDC_STATIC,27,60,29,12,NOT WS_GROUP - EDITTEXT IDC_SERVER,57,60,72,12,ES_AUTOHSCROLL - RTEXT "&Port:",IDC_STATIC,153,60,22,12 - EDITTEXT IDC_PORT,175,60,37,12,ES_AUTOHSCROLL - RTEXT "&User Name:",IDC_STATIC,17,75,39,12 - EDITTEXT IDC_USER,57,75,72,12,ES_AUTOHSCROLL - RTEXT "Pass&word:",IDC_STATIC,141,75,34,12 - EDITTEXT IDC_PASSWORD,175,75,72,12,ES_PASSWORD | ES_AUTOHSCROLL - GROUPBOX "Options:",IDC_STATIC,35,92,205,25 - CONTROL "&ReadOnly:",IDC_READONLY,"Button",BS_AUTOCHECKBOX | - BS_LEFTTEXT | BS_RIGHT | WS_GROUP | WS_TABSTOP,50,100,45, - 14 - CONTROL "&CommLog (Global):",IDC_COMMLOG,"Button", - BS_AUTOCHECKBOX | BS_LEFTTEXT | BS_RIGHT | WS_TABSTOP, - 105,100,75,14 - CONTROL "6.2",IDC_PG62,"Button",BS_AUTOCHECKBOX | BS_LEFTTEXT | - BS_RIGHT | WS_TABSTOP,195,100,25,14 - DEFPUSHBUTTON "OK",IDOK,85,129,40,14,WS_GROUP - PUSHBUTTON "Cancel",IDCANCEL,145,129,40,14 - CTEXT "Change data source name, description, or options. Then choose OK.", - IDC_STATIC,44,5,180,17 -END - - -///////////////////////////////////////////////////////////////////////////// -// -// DESIGNINFO -// - -#ifdef APSTUDIO_INVOKED -GUIDELINES DESIGNINFO DISCARDABLE -BEGIN - DRIVERCONNDIALOG, DIALOG - BEGIN - RIGHTMARGIN, 268 - END -END -#endif // APSTUDIO_INVOKED - - -#ifndef _MAC -///////////////////////////////////////////////////////////////////////////// -// -// Version -// - -VS_VERSION_INFO VERSIONINFO - FILEVERSION 6,30,0,0 - PRODUCTVERSION 6,30,0,0 - FILEFLAGSMASK 0x3L -#ifdef _DEBUG - FILEFLAGS 0x1L -#else - FILEFLAGS 0x0L -#endif - FILEOS 0x4L - FILETYPE 0x2L - FILESUBTYPE 0x0L -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "040904e4" - BEGIN - VALUE "Comments", "PostgreSQL ODBC driver for Windows 95\0" - VALUE "CompanyName", "Insight Distribution Systems\0" - VALUE "FileDescription", "PostgreSQL Driver\0" - VALUE "FileVersion", " 6.30.0000\0" - VALUE "InternalName", "psqlodbc\0" - VALUE "LegalTrademarks", "ODBC(TM) is a trademark of Microsoft Corporation. Microsoft® is a registered trademark of Microsoft Corporation. Windows(TM) is a trademark of Microsoft Corporation.\0" - VALUE "OriginalFilename", "psqlodbc.dll\0" - VALUE "ProductName", "Microsoft Open Database Connectivity\0" - VALUE "ProductVersion", " 6.30.0000\0" - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x409, 1252 - END -END - -#endif // !_MAC - - -///////////////////////////////////////////////////////////////////////////// -// -// String Table -// - -STRINGTABLE DISCARDABLE -BEGIN - IDS_BADDSN "Invalid DSN entry, please recheck." - IDS_MSGTITLE "Invalid DSN" -END - -#endif // English (U.S.) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// - - -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED - +//Microsoft Developer Studio generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE DISCARDABLE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE DISCARDABLE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE DISCARDABLE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Dialog +// + +DLG_CONFIG DIALOG DISCARDABLE 65, 43, 292, 116 +STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | + WS_SYSMENU +CAPTION "PostgreSQL Driver Setup" +FONT 8, "MS Sans Serif" +BEGIN + RTEXT "&Data Source:",IDC_DSNAMETEXT,5,10,50,12,NOT WS_GROUP + EDITTEXT IDC_DSNAME,57,10,72,12,ES_AUTOHSCROLL | WS_GROUP + RTEXT "Des&cription:",IDC_DESCTEXT,135,10,39,12,NOT WS_GROUP + EDITTEXT IDC_DESC,175,10,108,12,ES_AUTOHSCROLL + RTEXT "Data&base:",IDC_STATIC,17,25,38,12,NOT WS_GROUP + EDITTEXT IDC_DATABASE,57,25,72,12,ES_AUTOHSCROLL + RTEXT "&Server:",IDC_STATIC,27,40,29,12,NOT WS_GROUP + EDITTEXT IDC_SERVER,57,40,72,12,ES_AUTOHSCROLL + RTEXT "&Port:",IDC_STATIC,153,40,22,12 + EDITTEXT IDC_PORT,175,40,37,12,ES_AUTOHSCROLL + RTEXT "&User Name:",IDC_STATIC,17,55,39,12 + EDITTEXT IDC_USER,57,55,72,12,ES_AUTOHSCROLL + RTEXT "Pass&word:",IDC_STATIC,141,55,34,12 + EDITTEXT IDC_PASSWORD,175,55,72,12,ES_PASSWORD | ES_AUTOHSCROLL + DEFPUSHBUTTON "OK",IDOK,25,90,40,14,WS_GROUP + PUSHBUTTON "Cancel",IDCANCEL,80,90,40,14 + GROUPBOX "Options (Advanced):",IDC_STATIC,140,74,140,35,BS_CENTER + PUSHBUTTON "Driver",IDC_DRIVER,160,90,50,14 + PUSHBUTTON "DataSource",IDC_DATASOURCE,220,90,50,14 + CTEXT "Please supply any missing information needed to connect.", + DRV_MSG_LABEL,36,5,220,15 +END + +DLG_OPTIONS_DRV DIALOG DISCARDABLE 0, 0, 287, 226 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Advanced Options (Driver)" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "Disable Genetic &Optimizer",DRV_OPTIMIZER,"Button", + BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,15,10,97,10 + CONTROL "Comm&Log (C:\\psqlodbc.log)",DRV_COMMLOG,"Button", + BS_AUTOCHECKBOX | WS_TABSTOP,140,10,113,10 + CONTROL "Recognize Unique &Indexes",DRV_UNIQUEINDEX,"Button", + BS_AUTOCHECKBOX | WS_TABSTOP,15,25,101,10 + CONTROL "&ReadOnly (Default)",DRV_READONLY,"Button", + BS_AUTOCHECKBOX | WS_TABSTOP,140,25,80,10 + CONTROL "&Use Declare/Fetch",DRV_USEDECLAREFETCH,"Button", + BS_AUTOCHECKBOX | WS_TABSTOP,15,40,80,10 + GROUPBOX "Unknown Sizes",IDC_STATIC,10,55,175,25 + CONTROL "Maximum",DRV_UNKNOWN_MAX,"Button",BS_AUTORADIOBUTTON | + WS_GROUP | WS_TABSTOP,15,65,45,10 + CONTROL "Don't Know",DRV_UNKNOWN_DONTKNOW,"Button", + BS_AUTORADIOBUTTON | WS_TABSTOP,70,65,53,10 + CONTROL "Longest",DRV_UNKNOWN_LONGEST,"Button", + BS_AUTORADIOBUTTON | WS_TABSTOP,130,65,50,10 + GROUPBOX "Data Type Options",IDC_STATIC,10,85,270,25 + CONTROL "Text as LongVarChar",DRV_TEXT_LONGVARCHAR,"Button", + BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,15,95,80,10 + CONTROL "Unknowns as LongVarChar",DRV_UNKNOWNS_LONGVARCHAR, + "Button",BS_AUTOCHECKBOX | WS_TABSTOP,105,95,100,10 + CONTROL "Bools as Char",DRV_BOOLS_CHAR,"Button",BS_AUTOCHECKBOX | + WS_TABSTOP,215,95,60,10 + LTEXT "&Cache Size:",IDC_STATIC,10,120,40,10 + EDITTEXT DRV_CACHE_SIZE,50,120,35,12,ES_AUTOHSCROLL + LTEXT "Max &Varchar:",IDC_STATIC,90,120,45,10 + EDITTEXT DRV_VARCHAR_SIZE,135,120,35,12,ES_AUTOHSCROLL + LTEXT "Max Lon&gVarChar:",IDC_STATIC,180,120,60,10 + EDITTEXT DRV_LONGVARCHAR_SIZE,240,120,35,12,ES_AUTOHSCROLL + LTEXT "SysTable &Prefixes:",IDC_STATIC,15,135,35,20 + EDITTEXT DRV_EXTRASYSTABLEPREFIXES,50,140,75,12,ES_AUTOHSCROLL + RTEXT "Connect &Settings:",IDC_STATIC,10,165,35,25 + EDITTEXT DRV_CONNSETTINGS,50,160,225,35,ES_MULTILINE | + ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",IDOK,45,205,50,14,WS_GROUP + PUSHBUTTON "Cancel",IDCANCEL,115,205,50,14 + PUSHBUTTON "Defaults",IDDEFAULTS,185,205,50,15 +END + +DLG_OPTIONS_DS DIALOG DISCARDABLE 0, 0, 267, 170 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU +CAPTION "Advanced Options (DataSource)" +FONT 8, "MS Sans Serif" +BEGIN + CONTROL "&ReadOnly",DS_READONLY,"Button",BS_AUTOCHECKBOX | + WS_GROUP | WS_TABSTOP,25,10,45,15 + CONTROL "&6.2 Protocol",DS_PG62,"Button",BS_AUTOCHECKBOX | + WS_TABSTOP,130,10,60,14 + CONTROL "Show System &Tables",DS_SHOWSYSTEMTABLES,"Button", + BS_AUTOCHECKBOX | WS_TABSTOP,25,30,85,10 + GROUPBOX "OID Options",IDC_STATIC,15,50,180,25 + CONTROL "Show &Column",DS_SHOWOIDCOLUMN,"Button",BS_AUTOCHECKBOX | + WS_GROUP | WS_TABSTOP,25,60,59,10 + CONTROL "Fake &Index",DS_FAKEOIDINDEX,"Button",BS_AUTOCHECKBOX | + WS_GROUP | WS_TABSTOP,115,60,51,10 + RTEXT "Connect &Settings:",IDC_STATIC,10,90,35,25 + EDITTEXT DS_CONNSETTINGS,50,85,200,35,ES_MULTILINE | + ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN + DEFPUSHBUTTON "OK",IDOK,65,130,50,14,WS_GROUP + PUSHBUTTON "Cancel",IDCANCEL,140,130,50,14 + GROUPBOX "Unknown Sizes",IDC_STATIC,10,145,175,25,NOT WS_VISIBLE + CONTROL "Maximum",DS_UNKNOWN_MAX,"Button",BS_AUTORADIOBUTTON | + NOT WS_VISIBLE | WS_GROUP | WS_TABSTOP,15,155,45,10 + CONTROL "Don't Know",DS_UNKNOWN_DONTKNOW,"Button", + BS_AUTORADIOBUTTON | NOT WS_VISIBLE | WS_TABSTOP,70,155, + 53,10 + CONTROL "Longest",DS_UNKNOWN_LONGEST,"Button",BS_AUTORADIOBUTTON | + NOT WS_VISIBLE | WS_TABSTOP,130,155,50,10 +END + + +///////////////////////////////////////////////////////////////////////////// +// +// DESIGNINFO +// + +#ifdef APSTUDIO_INVOKED +GUIDELINES DESIGNINFO DISCARDABLE +BEGIN + DLG_CONFIG, DIALOG + BEGIN + BOTTOMMARGIN, 115 + END + + DLG_OPTIONS_DRV, DIALOG + BEGIN + LEFTMARGIN, 7 + RIGHTMARGIN, 280 + TOPMARGIN, 7 + BOTTOMMARGIN, 219 + END + + DLG_OPTIONS_DS, DIALOG + BEGIN + LEFTMARGIN, 5 + RIGHTMARGIN, 260 + VERTGUIDE, 55 + TOPMARGIN, 7 + BOTTOMMARGIN, 163 + END +END +#endif // APSTUDIO_INVOKED + + +#ifndef _MAC +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 6,30,2,44 + PRODUCTVERSION 6,30,2,44 + FILEFLAGSMASK 0x3L +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904e4" + BEGIN + VALUE "Comments", "PostgreSQL ODBC driver for Windows 95\0" + VALUE "CompanyName", "Insight Distribution Systems\0" + VALUE "FileDescription", "PostgreSQL Driver\0" + VALUE "FileVersion", " 6.30.0244\0" + VALUE "InternalName", "psqlodbc\0" + VALUE "LegalTrademarks", "ODBC(TM) is a trademark of Microsoft Corporation. Microsoft® is a registered trademark of Microsoft Corporation. Windows(TM) is a trademark of Microsoft Corporation.\0" + VALUE "OriginalFilename", "psqlodbc.dll\0" + VALUE "ProductName", "Microsoft Open Database Connectivity\0" + VALUE "ProductVersion", " 6.30.0244\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1252 + END +END + +#endif // !_MAC + + +///////////////////////////////////////////////////////////////////////////// +// +// String Table +// + +STRINGTABLE DISCARDABLE +BEGIN + IDS_BADDSN "Invalid DSN entry, please recheck." + IDS_MSGTITLE "Invalid DSN" +END + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/src/interfaces/odbc/qresult.c b/src/interfaces/odbc/qresult.c index 148e469cef..eb06abcd93 100644 --- a/src/interfaces/odbc/qresult.c +++ b/src/interfaces/odbc/qresult.c @@ -1,22 +1,22 @@ - -/* Module: qresult.c - * - * Description: This module contains functions related to - * managing result information (i.e, fetching rows from the backend, - * managing the tuple cache, etc.) and retrieving it. - * Depending on the situation, a QResultClass will hold either data - * from the backend or a manually built result (see "qresult.h" to - * see which functions/macros are for manual or backend results. - * For manually built results, the QResultClass simply points to - * TupleList and ColumnInfo structures, which actually hold the data. - * - * Classes: QResultClass (Functions prefix: "QR_") - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: qresult.c + * + * Description: This module contains functions related to + * managing result information (i.e, fetching rows from the backend, + * managing the tuple cache, etc.) and retrieving it. + * Depending on the situation, a QResultClass will hold either data + * from the backend or a manually built result (see "qresult.h" to + * see which functions/macros are for manual or backend results. + * For manually built results, the QResultClass simply points to + * TupleList and ColumnInfo structures, which actually hold the data. + * + * Classes: QResultClass (Functions prefix: "QR_") + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "qresult.h" #include "misc.h" @@ -24,7 +24,7 @@ extern GLOBAL_VALUES globals; -/* Used for building a Manual Result only */ +/* Used for building a Manual Result only */ /* All info functions call this function to create the manual result set. */ void QR_set_num_fields(QResultClass *self, int new_num_fields) @@ -84,7 +84,7 @@ QR_Destructor(QResultClass *self) mylog("QResult: in DESTRUCTOR\n"); /* manual result set tuples */ - if (self->manual_tuples) + if (self->manual_tuples) TL_Destructor(self->manual_tuples); // If conn is defined, then we may have used "backend_tuples", @@ -183,12 +183,14 @@ QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, char *cursor) if (self->cursor) free(self->cursor); - if ( ! cursor || cursor[0] == '\0') { - self->status = PGRES_INTERNAL_ERROR; - QR_set_message(self, "Internal Error -- no cursor for fetch"); - return FALSE; + if ( globals.use_declarefetch) { + if (! cursor || cursor[0] == '\0') { + self->status = PGRES_INTERNAL_ERROR; + QR_set_message(self, "Internal Error -- no cursor for fetch"); + return FALSE; + } + self->cursor = strdup(cursor); } - self->cursor = strdup(cursor); // Read the field attributes. // $$$$ Should do some error control HERE! $$$$ @@ -205,6 +207,7 @@ QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, char *cursor) mylog("QR_fetch_tuples: past CI_read_fields: num_fields = %d\n", self->num_fields); /* allocate memory for the tuple cache */ + mylog("MALLOC: fetch_max = %d, size = %d\n", globals.fetch_max, self->num_fields * sizeof(TupleField) * globals.fetch_max); self->backend_tuples = (TupleField *) malloc(self->num_fields * sizeof(TupleField) * globals.fetch_max); if ( ! self->backend_tuples) { self->status = PGRES_FATAL_ERROR; @@ -232,24 +235,23 @@ QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, char *cursor) } } -// Close the cursor and end the transaction +// Close the cursor and end the transaction (if no cursors left) // We only close cursor/end the transaction if a cursor was used. int QR_close(QResultClass *self) { QResultClass *res; - if (self->conn && self->cursor) { + if (globals.use_declarefetch && self->conn && self->cursor) { char buf[64]; - sprintf(buf, "close %s; END", self->cursor); - + sprintf(buf, "close %s", self->cursor); mylog("QResult: closing cursor: '%s'\n", buf); res = CC_send_query(self->conn, buf, NULL, NULL); - CC_set_no_trans(self->conn); self->inTuples = FALSE; + free(self->cursor); self->cursor = NULL; @@ -259,6 +261,21 @@ QResultClass *res; return FALSE; } + /* End the transaction if there are no cursors left on this conn */ + if (CC_cursor_count(self->conn) == 0) { + mylog("QResult: END transaction on conn=%u\n", self->conn); + + res = CC_send_query(self->conn, "END", NULL, NULL); + + CC_set_no_trans(self->conn); + + if (res == NULL) { + self->status = PGRES_FATAL_ERROR; + QR_set_message(self, "Error ending transaction."); + return FALSE; + } + } + } return TRUE; @@ -301,6 +318,13 @@ char cmdbuffer[MAX_MESSAGE_LEN+1]; // QR_set_command() dups this string so dont if ( ! self->inTuples) { char fetch[128]; + if ( ! globals.use_declarefetch) { + mylog("next_tuple: ALL_ROWS: done, fcount = %d, fetch_count = %d\n", fcount, fetch_count); + self->tupleField = NULL; + self->status = PGRES_END_TUPLES; + return -1; /* end of tuples */ + } + sprintf(fetch, "fetch %d in %s", globals.fetch_max, self->cursor); mylog("next_tuple: sending actual fetch (%d) query '%s'\n", globals.fetch_max, fetch); @@ -336,65 +360,81 @@ char cmdbuffer[MAX_MESSAGE_LEN+1]; // QR_set_command() dups this string so dont id = SOCK_get_char(sock); switch (id) { - case 'T': /* Tuples within tuples cannot be handled */ - self->status = PGRES_BAD_RESPONSE; - QR_set_message(self, "Tuples within tuples cannot be handled"); - return FALSE; - case 'B': /* Tuples in binary format */ - case 'D': /* Tuples in ASCII format */ - if ( ! QR_read_tuple(self, (char) (id == 0))) { - self->status = PGRES_BAD_RESPONSE; - QR_set_message(self, "Error reading the tuple"); - return FALSE; - } + case 'T': /* Tuples within tuples cannot be handled */ + self->status = PGRES_BAD_RESPONSE; + QR_set_message(self, "Tuples within tuples cannot be handled"); + return FALSE; + case 'B': /* Tuples in binary format */ + case 'D': /* Tuples in ASCII format */ - self->fcount++; - break; // continue reading + if ( ! globals.use_declarefetch && self->fcount > 0 && ! (self->fcount % globals.fetch_max)) { + size_t old_size = self->fcount * self->num_fields * sizeof(TupleField); + mylog("REALLOC: old_size = %d\n", old_size); - - case 'C': /* End of tuple list */ - SOCK_get_string(sock, cmdbuffer, MAX_MESSAGE_LEN); - QR_set_command(self, cmdbuffer); - - mylog("end of tuple list -- setting inUse to false: this = %u\n", self); - - self->inTuples = FALSE; - if (self->fcount > 0) { - - qlog(" [ fetched %d rows ]\n", self->fcount); - mylog("_next_tuple: 'C' fetch_max && fcount = %d\n", self->fcount); - - /* set to first row */ - self->tupleField = the_tuples; - return TRUE; - } - else { // We are surely done here (we read 0 tuples) - qlog(" [ fetched 0 rows ]\n"); - mylog("_next_tuple: 'C': DONE (fcount == 0)\n"); - return -1; /* end of tuples */ + self->backend_tuples = (TupleField *) realloc(self->backend_tuples, old_size + (self->num_fields * sizeof(TupleField) * globals.fetch_max)); + if ( ! self->backend_tuples) { + self->status = PGRES_FATAL_ERROR; + QR_set_message(self, "Out of memory while reading tuples."); + return FALSE; } + } - case 'E': /* Error */ - SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); - QR_set_message(self, msgbuffer); - self->status = PGRES_FATAL_ERROR; - CC_set_no_trans(self->conn); - qlog("ERROR from backend in next_tuple: '%s'\n", msgbuffer); + if ( ! QR_read_tuple(self, (char) (id == 0))) { + self->status = PGRES_BAD_RESPONSE; + QR_set_message(self, "Error reading the tuple"); + return FALSE; + } + + self->fcount++; + break; // continue reading - return FALSE; - case 'N': /* Notice */ - SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); - QR_set_message(self, msgbuffer); - self->status = PGRES_NONFATAL_ERROR; - qlog("NOTICE from backend in next_tuple: '%s'\n", msgbuffer); - continue; + case 'C': /* End of tuple list */ + SOCK_get_string(sock, cmdbuffer, MAX_MESSAGE_LEN); + QR_set_command(self, cmdbuffer); - default: /* this should only happen if the backend dumped core */ - QR_set_message(self, "Unexpected result from backend. It probably crashed"); - self->status = PGRES_FATAL_ERROR; - CC_set_no_trans(self->conn); - return FALSE; + mylog("end of tuple list -- setting inUse to false: this = %u\n", self); + + self->inTuples = FALSE; + if (self->fcount > 0) { + + qlog(" [ fetched %d rows ]\n", self->fcount); + mylog("_next_tuple: 'C' fetch_max && fcount = %d\n", self->fcount); + + /* set to first row */ + self->tupleField = self->backend_tuples; // the_tuples; + return TRUE; + } + else { // We are surely done here (we read 0 tuples) + qlog(" [ fetched 0 rows ]\n"); + mylog("_next_tuple: 'C': DONE (fcount == 0)\n"); + return -1; /* end of tuples */ + } + + case 'E': /* Error */ + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + QR_set_message(self, msgbuffer); + self->status = PGRES_FATAL_ERROR; + + if ( ! strncmp(msgbuffer, "FATAL", 5)) + CC_set_no_trans(self->conn); + + qlog("ERROR from backend in next_tuple: '%s'\n", msgbuffer); + + return FALSE; + + case 'N': /* Notice */ + SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + QR_set_message(self, msgbuffer); + self->status = PGRES_NONFATAL_ERROR; + qlog("NOTICE from backend in next_tuple: '%s'\n", msgbuffer); + continue; + + default: /* this should only happen if the backend dumped core */ + QR_set_message(self, "Unexpected result from backend. It probably crashed"); + self->status = PGRES_FATAL_ERROR; + CC_set_no_trans(self->conn); + return FALSE; } } return TRUE; @@ -413,7 +453,8 @@ Int4 len; char *buffer; int num_fields = self->num_fields; // speed up access SocketClass *sock = CC_get_socket(self->conn); - +ColumnInfoClass *flds; + /* set the current row to read the fields into */ this_tuplefield = self->backend_tuples + (self->fcount * num_fields); @@ -455,6 +496,17 @@ SocketClass *sock = CC_get_socket(self->conn); this_tuplefield[field_lf].len = len; this_tuplefield[field_lf].value = buffer; + + /* This can be used to set the longest length of the column for any + row in the tuple cache. It would not be accurate for varchar and + text fields to use this since a tuple cache is only 100 rows. + Bpchar can be handled since the strlen of all rows is fixed, + assuming there are not 100 nulls in a row! + */ + + flds = self->fields; + if (flds->display_size[field_lf] < len) + flds->display_size[field_lf] = len; } /* Now adjust for the next bit to be scanned in the diff --git a/src/interfaces/odbc/qresult.h b/src/interfaces/odbc/qresult.h index b425a34215..132a398326 100644 --- a/src/interfaces/odbc/qresult.h +++ b/src/interfaces/odbc/qresult.h @@ -1,11 +1,11 @@ - -/* File: qresult.h - * - * Description: See "qresult.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: qresult.h + * + * Description: See "qresult.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __QRESULT_H__ #define __QRESULT_H__ @@ -43,7 +43,7 @@ struct QResultClass_ { // Stuff for declare/fetch tuples int fetch_count; // logical rows read so far - int fcount; // actual rows read in the fetch + int fcount; // actual rows read in the fetch int num_fields; // number of fields in the result QueryResultCode status; @@ -63,19 +63,22 @@ struct QResultClass_ { /* These functions are for retrieving data from the qresult */ -#define QR_get_value_manual(self, tupleno, fieldno) (TL_get_fieldval(self->manual_tuples, tupleno, fieldno)) -#define QR_get_value_backend(self, fieldno) (self->tupleField[fieldno].value) - +#define QR_get_value_manual(self, tupleno, fieldno) (TL_get_fieldval(self->manual_tuples, tupleno, fieldno)) +#define QR_get_value_backend(self, fieldno) (self->tupleField[fieldno].value) +#define QR_get_value_backend_row(self, tupleno, fieldno) \ + ((self->backend_tuples + (tupleno * self->num_fields))[fieldno].value) + /* These functions are used by both manual and backend results */ -#define QR_NumResultCols(self) (CI_get_num_fields(self->fields)) +#define QR_NumResultCols(self) (CI_get_num_fields(self->fields)) #define QR_get_fieldname(self, fieldno_) (CI_get_fieldname(self->fields, fieldno_)) #define QR_get_fieldsize(self, fieldno_) (CI_get_fieldsize(self->fields, fieldno_)) -#define QR_get_field_type(self, fieldno_) (CI_get_oid(self->fields, fieldno_)) - +#define QR_get_display_size(self, fieldno_) (CI_get_display_size(self->fields, fieldno_)) +#define QR_get_field_type(self, fieldno_) (CI_get_oid(self->fields, fieldno_)) + /* These functions are used only for manual result sets */ -#define QR_get_num_tuples(self) (self->manual_tuples ? TL_get_num_tuples(self->manual_tuples) : 0) +#define QR_get_num_tuples(self) (self->manual_tuples ? TL_get_num_tuples(self->manual_tuples) : self->fcount) #define QR_add_tuple(self, new_tuple) (TL_add_tuple(self->manual_tuples, new_tuple)) -#define QR_set_field_info(self, field_num, name, adtid, adtsize) (CI_set_field_info(self->fields, field_num, name, adtid, adtsize)) +#define QR_set_field_info(self, field_num, name, adtid, adtsize) (CI_set_field_info(self->fields, field_num, name, adtid, adtsize)) /* status macros */ #define QR_command_successful(self) ( !(self->status == PGRES_BAD_RESPONSE || self->status == PGRES_NONFATAL_ERROR || self->status == PGRES_FATAL_ERROR)) @@ -98,7 +101,7 @@ int QR_close(QResultClass *self); char QR_fetch_tuples(QResultClass *self, ConnectionClass *conn, char *cursor); void QR_free_memory(QResultClass *self); void QR_set_command(QResultClass *self, char *msg); -void QR_set_notice(QResultClass *self, char *msg); +void QR_set_notice(QResultClass *self, char *msg); void QR_set_num_fields(QResultClass *self, int new_num_fields); /* manual result only */ diff --git a/src/interfaces/odbc/readme.txt b/src/interfaces/odbc/readme.txt new file mode 100644 index 0000000000..377bb28bf0 --- /dev/null +++ b/src/interfaces/odbc/readme.txt @@ -0,0 +1,72 @@ + +Readme for psqlodbc.dll 4/15/98 +------------------------------------------------------------------------------- +Latest binary and source updates available at http://www.insightdist.com/psqlodbc + + +I. Building the Driver from the source code + +This section describes how to build the PostgreSQL ODBC Driver (psqlodbc.dll). +Microsoft Visual C++ version 4.0 or higher is required. There is no manually +constructed Makefile. The visual C++ environment automatically generates one +during the build process. Thus, the project binary files (".ncb", ".mdp", ".aps") +nor the makefile are really distributed as part of the source code release +(although they are probably in there anyway). + +1. Create a new project workspace with the type DLL. For the name, type in the + name "psqlodbc". + +2. The above step creates the directory "psqlodbc" under the + "\\projects" path to hold the source files. + (example, \msdev\projects\psqlodbc). Now, either unzip the source code release + into this directory or just copy all the files into this directory. + +3. Insert all of the source files (*.c, *.h, *.rc, *.def) into the Visual project + using the "Insert files into project" command. You may have to do 2 inserts -- + the first to get the 'c' and header files, and the second to get the def file. + Don't forget the .def file since it is an important part of the release. + You can even insert ".txt" files into the projects -- they will do nothing. + +4. Add the "wsock32.lib" library to the end of the list of libraries for linking + using the Build settings menu. + +5. Select the type of build on the toolbar (i.e., Release or Debug). This is + one of the useful features of the visual c++ environment in that you can + browse the entire project if you build the "Debug" release. For release + purposes however, select "Release" build. + +6. Build the dll by selecting Build from the build menu. + +7. When complete, the "psqlodbc.dll" file is under the "Release" subdirectory. + (i.e., "\msdev\projects\psqlodbc\release\psqlodbc.dll") + + + +II. Using Large Objects for handling LongVarBinary (OLE Objects in Access) + +Large objects are mapped to LONGVARBINARY in the driver to allow storing things like +OLE objects in Microsoft Access. Multiple SQLPutData and SQLGetData calls are usually +used to send and retrieve these objects. The driver creates a new large object and simply +inserts its 'identifier' into the respective table. However, since Postgres uses an 'Oid' +to identify a Large Object, it is necessary to create a new Postgres type to be able +to discriminate between an ordinary Oid and a Large Object Oid. Until this new type +becomes an official part of Postgres, it must be added into the desired database and +looked up for each connection. The type used in the driver is simply called "lo" and +here is the command used to create it: + +create type lo (internallength=4,externallength=10,input=int4in,output=int4out, + default='',passedbyvalue); + +Once this is done, simply use the new 'lo' type to define columns in that database. Note +that this must be done for each database you want to use large objects in with the driver. +When the driver sees an 'lo' type, it will handle it as LONGVARBINARY. + +Another important note is that this new type is lacking in functionality. It will not +cleanup after itself on updates and deletes, thus leaving orphans around and using up +extra disk space. And currently, Postgres does not support the vacuuming of large +objects. Hopefully in the future, a real large object data type will be available. + +But for now, it sure is fun to stick a Word document, Visio document, or avi of a dancing +baby into a database column, even if you will fill up your server's hard disk after a while! + + diff --git a/src/interfaces/odbc/resource.h b/src/interfaces/odbc/resource.h index c58a0b72a0..51fa7078f1 100644 --- a/src/interfaces/odbc/resource.h +++ b/src/interfaces/odbc/resource.h @@ -1,39 +1,58 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Developer Studio generated include file. -// Used by psqlodbc.rc -// -#define IDS_BADDSN 1 -#define IDS_MSGTITLE 2 -#define DRIVERCONNDIALOG 101 -#define IDC_DSNAME 400 -#define IDC_DSNAMETEXT 401 -#define IDC_DESC 404 -#define IDC_SERVER 407 -#define IDC_DATABASE 408 -#define CONFIGDSN 1001 -#define IDC_PORT 1002 -#define IDC_USER 1006 -#define IDC_PASSWORD 1009 -#define IDC_READONLY 1011 -#define READONLY_EDIT 1012 -#define SAVEPASSWORD_EDIT 1013 -#define IDC_COMMLOG 1014 -#define COMMLOG_EDIT 1015 -#define IDC_PG62 1016 -#define PG62_EDIT 1017 -#define SERVER_EDIT 1501 -#define PORT_EDIT 1502 -#define DATABASE_EDIT 1503 -#define USERNAME_EDIT 1504 -#define PASSWORD_EDIT 1505 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 102 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1018 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif +//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by psqlodbc.rc +// +#define IDS_BADDSN 1 +#define IDS_MSGTITLE 2 +#define DLG_OPTIONS_DRV 102 +#define DLG_OPTIONS_DS 103 +#define IDC_DSNAME 400 +#define IDC_DSNAMETEXT 401 +#define IDC_DESC 404 +#define IDC_SERVER 407 +#define IDC_DATABASE 408 +#define DLG_CONFIG 1001 +#define IDC_PORT 1002 +#define IDC_USER 1006 +#define IDC_PASSWORD 1009 +#define DS_READONLY 1011 +#define DS_SHOWOIDCOLUMN 1012 +#define DS_FAKEOIDINDEX 1013 +#define DRV_COMMLOG 1014 +#define DS_PG62 1016 +#define IDC_DATASOURCE 1018 +#define DRV_OPTIMIZER 1019 +#define DS_CONNSETTINGS 1020 +#define IDC_DRIVER 1021 +#define DS_UNKNOWN_MAX 1023 +#define DS_UNKNOWN_DONTKNOW 1024 +#define DRV_CONNSETTINGS 1031 +#define DRV_UNIQUEINDEX 1032 +#define DRV_UNKNOWN_MAX 1035 +#define DRV_UNKNOWN_DONTKNOW 1036 +#define DRV_READONLY 1037 +#define IDC_DESCTEXT 1039 +#define DRV_MSG_LABEL 1040 +#define DRV_UNKNOWN_LONGEST 1041 +#define DS_UNKNOWN_LONGEST 1042 +#define DRV_TEXT_LONGVARCHAR 1043 +#define DRV_UNKNOWNS_LONGVARCHAR 1044 +#define DRV_CACHE_SIZE 1045 +#define DRV_VARCHAR_SIZE 1046 +#define DRV_LONGVARCHAR_SIZE 1047 +#define IDDEFAULTS 1048 +#define DRV_USEDECLAREFETCH 1049 +#define DRV_BOOLS_CHAR 1050 +#define DS_SHOWSYSTEMTABLES 1051 +#define DRV_EXTRASYSTABLEPREFIXES 1051 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 104 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1054 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/src/interfaces/odbc/results.c b/src/interfaces/odbc/results.c index 1396083046..c06dc69170 100644 --- a/src/interfaces/odbc/results.c +++ b/src/interfaces/odbc/results.c @@ -1,22 +1,23 @@ - -/* Module: results.c - * - * Description: This module contains functions related to - * retrieving result information through the ODBC API. - * - * Classes: n/a - * - * API functions: SQLRowCount, SQLNumResultCols, SQLDescribeCol, SQLColAttributes, - * SQLGetData, SQLFetch, SQLExtendedFetch, - * SQLMoreResults(NI), SQLSetPos(NI), SQLSetScrollOptions(NI), - * SQLSetCursorName(NI), SQLGetCursorName(NI) - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: results.c + * + * Description: This module contains functions related to + * retrieving result information through the ODBC API. + * + * Classes: n/a + * + * API functions: SQLRowCount, SQLNumResultCols, SQLDescribeCol, SQLColAttributes, + * SQLGetData, SQLFetch, SQLExtendedFetch, + * SQLMoreResults(NI), SQLSetPos(NI), SQLSetScrollOptions(NI), + * SQLSetCursorName, SQLGetCursorName + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include #include "psqlodbc.h" +#include "dlg_specific.h" #include "environ.h" #include "connection.h" #include "statement.h" @@ -29,6 +30,7 @@ #include #include +extern GLOBAL_VALUES globals; RETCODE SQL_API SQLRowCount( HSTMT hstmt, @@ -46,7 +48,7 @@ char *msg, *ptr; res = SC_get_Result(stmt); if(res && pcrow) { - *pcrow = QR_get_num_tuples(res); + *pcrow = globals.use_declarefetch ? 0 : QR_get_num_tuples(res); return SQL_SUCCESS; } } @@ -115,11 +117,11 @@ QResultClass *result; return SQL_SUCCESS; } + // - - - - - - - - - // Return information about the database column the user wants // information about. -/* CC: preliminary implementation */ RETCODE SQL_API SQLDescribeCol( HSTMT hstmt, UWORD icol, @@ -136,10 +138,14 @@ StatementClass *stmt = (StatementClass *) hstmt; QResultClass *result; char *name; Int4 fieldtype; +int p; +ConnInfo *ci; if ( ! stmt) return SQL_INVALID_HANDLE; + ci = &(stmt->hdbc->connInfo); + SC_clear_error(stmt); /* CC: Now check for the "prepared, but not executed" situation, that enables us to @@ -149,7 +155,7 @@ Int4 fieldtype; SC_pre_execute(stmt); - + result = SC_get_Result(stmt); mylog("**** SQLDescribeCol: result = %u, stmt->status = %d, !finished=%d, !premature=%d\n", result, stmt->status, stmt->status != STMT_FINISHED, stmt->status != STMT_PREMATURE); if ( (NULL == result) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE))) { @@ -159,9 +165,18 @@ Int4 fieldtype; return SQL_ERROR; } + if(icol < 1) { + // we do not support bookmarks + stmt->errormsg = "Bookmarks are not currently supported."; + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + return SQL_ERROR; + } + + icol--; /* use zero based column numbers */ + if (cbColNameMax >= 1) { - name = QR_get_fieldname(result, (Int2) (icol-1)); - mylog("describeCol: col %d fieldname = '%s'\n", icol - 1, name); + name = QR_get_fieldname(result, icol); + mylog("describeCol: col %d fieldname = '%s'\n", icol, name); /* our indices start from 0 whereas ODBC defines indices starting from 1 */ if (NULL != pcbColName) { // we want to get the total number of bytes in the column name @@ -179,27 +194,51 @@ Int4 fieldtype; } } - fieldtype = QR_get_field_type(result, (Int2) (icol-1)); - mylog("describeCol: col %d fieldtype = %d\n", icol - 1, fieldtype); + fieldtype = QR_get_field_type(result, icol); + mylog("describeCol: col %d fieldtype = %d\n", icol, fieldtype); if (NULL != pfSqlType) { - *pfSqlType = pgtype_to_sqltype(fieldtype); - if (*pfSqlType == PG_UNKNOWN) - *pfSqlType = SQL_CHAR; + *pfSqlType = pgtype_to_sqltype(stmt, fieldtype); + + mylog("describeCol: col %d *pfSqlType = %d\n", icol, *pfSqlType); } - if (NULL != pcbColDef) - *pcbColDef = pgtype_precision(fieldtype); + if (NULL != pcbColDef) { + + /* If type is BPCHAR, then precision is length of column because all + columns in the result set will be blank padded to the column length. + + If type is VARCHAR or TEXT, then precision can not be accurately + determined. Possibilities are: + 1. return 0 (I dont know -- seems to work ok with Borland) + 2. return MAXIMUM PRECISION for that datatype (Borland bad!) + 3. return longest column thus far (that would be the longest + strlen of any row in the tuple cache, which may not be a + good representation if the result set is more than one + tuple cache long.) + */ + + p = pgtype_precision(stmt, fieldtype, icol, globals.unknown_sizes); // atoi(ci->unknown_sizes) + if ( p < 0) + p = 0; // "I dont know" + + *pcbColDef = p; + + mylog("describeCol: col %d *pcbColDef = %d\n", icol, *pcbColDef); + } if (NULL != pibScale) { Int2 scale; - scale = pgtype_scale(fieldtype); + scale = pgtype_scale(stmt, fieldtype); if(scale == -1) { scale = 0; } *pibScale = scale; + mylog("describeCol: col %d *pibScale = %d\n", icol, *pibScale); } + if (NULL != pfNullable) { - *pfNullable = pgtype_nullable(fieldtype); + *pfNullable = pgtype_nullable(stmt, fieldtype); + mylog("describeCol: col %d *pfNullable = %d\n", icol, *pfNullable); } return SQL_SUCCESS; @@ -219,132 +258,169 @@ RETCODE SQL_API SQLColAttributes( StatementClass *stmt = (StatementClass *) hstmt; char *value; Int4 field_type; +ConnInfo *ci; +int unknown_sizes; - if( ! stmt) { - return SQL_INVALID_HANDLE; - } + if( ! stmt) + return SQL_INVALID_HANDLE; + + ci = &(stmt->hdbc->connInfo); /* CC: Now check for the "prepared, but not executed" situation, that enables us to deal with "SQLPrepare -- SQLDescribeCol -- ... -- SQLExecute" situations. (AutoCAD 13 ASE/ASI just _loves_ that ;-) ) */ - SC_pre_execute(stmt); + SC_pre_execute(stmt); mylog("**** SQLColAtt: result = %u, status = %d, numcols = %d\n", stmt->result, stmt->status, stmt->result != NULL ? QR_NumResultCols(stmt->result) : -1); - if ( (NULL == stmt->result) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE)) ) { - stmt->errormsg = "Can't get column attributes: no result found."; - stmt->errornumber = STMT_SEQUENCE_ERROR; - return SQL_ERROR; - } + if ( (NULL == stmt->result) || ((stmt->status != STMT_FINISHED) && (stmt->status != STMT_PREMATURE)) ) { + stmt->errormsg = "Can't get column attributes: no result found."; + stmt->errornumber = STMT_SEQUENCE_ERROR; + return SQL_ERROR; + } - if(icol < 1) { - // we do not support bookmarks - stmt->errormsg = "Bookmarks are not currently supported."; - stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; - return SQL_ERROR; - } + if(icol < 1) { + // we do not support bookmarks + stmt->errormsg = "Bookmarks are not currently supported."; + stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR; + return SQL_ERROR; + } + + icol -= 1; + field_type = QR_get_field_type(stmt->result, icol); - icol -= 1; - field_type = QR_get_field_type(stmt->result, icol); mylog("colAttr: col %d field_type = %d\n", icol, field_type); - switch(fDescType) { - case SQL_COLUMN_AUTO_INCREMENT: - if (NULL != pfDesc) { - *pfDesc = pgtype_auto_increment(field_type); - if(*pfDesc == -1) { /* "not applicable" becomes false */ - *pfDesc = FALSE; - } - } - break; - case SQL_COLUMN_CASE_SENSITIVE: - if (NULL != pfDesc) - *pfDesc = pgtype_case_sensitive(field_type); - break; - case SQL_COLUMN_COUNT: - if (NULL != pfDesc) - *pfDesc = QR_NumResultCols(stmt->result); - break; - case SQL_COLUMN_DISPLAY_SIZE: - if (NULL != pfDesc) - *pfDesc = pgtype_precision(field_type); - - mylog("colAttr: col %d fieldsize = %d\n", icol, *pfDesc); + unknown_sizes = globals.unknown_sizes; // atoi(ci->unknown_sizes); + if (unknown_sizes == UNKNOWNS_AS_DONTKNOW) // not appropriate for SQLColAttributes() + unknown_sizes = UNKNOWNS_AS_MAX; - break; - case SQL_COLUMN_LABEL: - case SQL_COLUMN_NAME: - value = QR_get_fieldname(stmt->result, icol); - strncpy_null((char *)rgbDesc, value, cbDescMax); - /* CC: Check for Nullpointesr */ - if (NULL != pcbDesc) - *pcbDesc = strlen(value); - break; - case SQL_COLUMN_LENGTH: - if (NULL != pfDesc) - *pfDesc = pgtype_precision(field_type); - return SQL_SUCCESS; - break; - case SQL_COLUMN_MONEY: - if (NULL != pfDesc) - *pfDesc = pgtype_money(field_type); - break; - case SQL_COLUMN_NULLABLE: - if (NULL != pfDesc) - *pfDesc = pgtype_nullable(field_type); - break; - case SQL_COLUMN_OWNER_NAME: - return SQL_ERROR; - break; - case SQL_COLUMN_PRECISION: - if (NULL != pfDesc) - *pfDesc = pgtype_precision(field_type); - break; - case SQL_COLUMN_QUALIFIER_NAME: - strncpy_null((char *)rgbDesc, "", cbDescMax); - if (NULL != pfDesc) - *pcbDesc = 1; - break; - case SQL_COLUMN_SCALE: - if (NULL != pfDesc) - *pfDesc = pgtype_scale(field_type); - break; - case SQL_COLUMN_SEARCHABLE: - if (NULL != pfDesc) - *pfDesc = pgtype_searchable(field_type); - break; - case SQL_COLUMN_TABLE_NAME: - return SQL_ERROR; - break; - case SQL_COLUMN_TYPE: - if (NULL != pfDesc) { - *pfDesc = pgtype_to_sqltype(field_type); - if (*pfDesc == PG_UNKNOWN) - *pfDesc = SQL_CHAR; + switch(fDescType) { + case SQL_COLUMN_AUTO_INCREMENT: + if (NULL != pfDesc) { + *pfDesc = pgtype_auto_increment(stmt, field_type); + if (*pfDesc == -1) /* non-numeric becomes FALSE (ODBC Doc) */ + *pfDesc = FALSE; + } + break; + + case SQL_COLUMN_CASE_SENSITIVE: + if (NULL != pfDesc) + *pfDesc = pgtype_case_sensitive(stmt, field_type); + break; + + case SQL_COLUMN_COUNT: + if (NULL != pfDesc) + *pfDesc = QR_NumResultCols(stmt->result); + break; + + case SQL_COLUMN_DISPLAY_SIZE: + if (NULL != pfDesc) { + *pfDesc = pgtype_display_size(stmt, field_type, icol, unknown_sizes); + } + + mylog("SQLColAttributes: col %d, display_size= %d\n", icol, *pfDesc); + break; - case SQL_COLUMN_TYPE_NAME: - value = pgtype_to_name(field_type); - strncpy_null((char *)rgbDesc, value, cbDescMax); - if (NULL != pcbDesc) - *pcbDesc = strlen(value); + + case SQL_COLUMN_LABEL: + case SQL_COLUMN_NAME: + value = QR_get_fieldname(stmt->result, icol); + strncpy_null((char *)rgbDesc, value, cbDescMax); + + if (NULL != pcbDesc) + *pcbDesc = strlen(value); + break; + + case SQL_COLUMN_LENGTH: + if (NULL != pfDesc) { + *pfDesc = pgtype_length(stmt, field_type, icol, unknown_sizes); + } + mylog("SQLColAttributes: col %d, length = %d\n", icol, *pfDesc); break; - case SQL_COLUMN_UNSIGNED: - if (NULL != pfDesc) { - *pfDesc = pgtype_unsigned(field_type); - if(*pfDesc == -1) { - *pfDesc = FALSE; - } - } + + case SQL_COLUMN_MONEY: + if (NULL != pfDesc) + *pfDesc = pgtype_money(stmt, field_type); + break; + + case SQL_COLUMN_NULLABLE: + if (NULL != pfDesc) + *pfDesc = pgtype_nullable(stmt, field_type); + break; + + case SQL_COLUMN_OWNER_NAME: + strncpy_null((char *)rgbDesc, "", cbDescMax); + if (NULL != pcbDesc) + *pcbDesc = 0; + break; + + case SQL_COLUMN_PRECISION: + if (NULL != pfDesc) { + *pfDesc = pgtype_precision(stmt, field_type, icol, unknown_sizes); + } + mylog("SQLColAttributes: col %d, precision = %d\n", icol, *pfDesc); break; - case SQL_COLUMN_UPDATABLE: - // everything should be updatable, I guess, unless access permissions - // prevent it--are we supposed to check for that here? seems kind - // of complicated. hmm... - if (NULL != pfDesc) - *pfDesc = SQL_ATTR_WRITE; + + case SQL_COLUMN_QUALIFIER_NAME: + strncpy_null((char *)rgbDesc, "", cbDescMax); + if (NULL != pcbDesc) + *pcbDesc = 0; + break; + + case SQL_COLUMN_SCALE: + if (NULL != pfDesc) + *pfDesc = pgtype_scale(stmt, field_type); + break; + + case SQL_COLUMN_SEARCHABLE: + if (NULL != pfDesc) + *pfDesc = pgtype_searchable(stmt, field_type); + break; + + case SQL_COLUMN_TABLE_NAME: + strncpy_null((char *)rgbDesc, "", cbDescMax); + if (NULL != pcbDesc) + *pcbDesc = 0; break; + + case SQL_COLUMN_TYPE: + if (NULL != pfDesc) { + *pfDesc = pgtype_to_sqltype(stmt, field_type); + } + break; + + case SQL_COLUMN_TYPE_NAME: + value = pgtype_to_name(stmt, field_type); + strncpy_null((char *)rgbDesc, value, cbDescMax); + if (NULL != pcbDesc) + *pcbDesc = strlen(value); + break; + + case SQL_COLUMN_UNSIGNED: + if (NULL != pfDesc) { + *pfDesc = pgtype_unsigned(stmt, field_type); + if(*pfDesc == -1) /* non-numeric becomes TRUE (ODBC Doc) */ + *pfDesc = TRUE; + } + break; + + case SQL_COLUMN_UPDATABLE: + // everything should be updatable, I guess, unless access permissions + // prevent it--are we supposed to check for that here? seems kind + // of complicated. hmm... + if (NULL != pfDesc) { + /* Neither Access or Borland care about this. + + if (field_type == PG_TYPE_OID) + *pfDesc = SQL_ATTR_READONLY; + else + */ + *pfDesc = SQL_ATTR_WRITE; + } + break; } return SQL_SUCCESS; @@ -366,6 +442,10 @@ int num_cols, num_rows; Int4 field_type; void *value; int result; +char multiple; + + +mylog("SQLGetData: enter, stmt=%u\n", stmt); if( ! stmt) { return SQL_INVALID_HANDLE; @@ -401,7 +481,7 @@ int result; return SQL_ERROR; } - if ( stmt->manual_result) { + if ( stmt->manual_result || ! globals.use_declarefetch) { // make sure we're positioned on a valid row num_rows = QR_get_num_tuples(res); if((stmt->currTuple < 0) || @@ -410,7 +490,14 @@ int result; stmt->errornumber = STMT_INVALID_CURSOR_STATE_ERROR; return SQL_ERROR; } - value = QR_get_value_manual(res, stmt->currTuple, icol); + mylog(" num_rows = %d\n", num_rows); + if ( stmt->manual_result) { + value = QR_get_value_manual(res, stmt->currTuple, icol); + } + else { + value = QR_get_value_backend_row(res, stmt->currTuple, icol); + } + mylog(" value = '%s'\n", value); } else { /* its a SOCKET result (backend data) */ if (stmt->currTuple == -1 || ! res || QR_end_tuples(res)) { @@ -421,35 +508,52 @@ int result; value = QR_get_value_backend(res, icol); + mylog(" socket: value = '%s'\n", value); } field_type = QR_get_field_type(res, icol); mylog("**** SQLGetData: icol = %d, fCType = %d, field_type = %d, value = '%s'\n", icol, fCType, field_type, value); - result = copy_and_convert_field(field_type, value, - fCType, rgbValue, cbValueMax, pcbValue); + /* Is this another call for the same column to retrieve more data? */ + multiple = (icol == stmt->current_col) ? TRUE : FALSE; + + result = copy_and_convert_field(stmt, field_type, value, + fCType, rgbValue, cbValueMax, pcbValue, multiple); - if(result == COPY_UNSUPPORTED_TYPE) { - stmt->errormsg = "Received an unsupported type from Postgres."; - stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; - return SQL_ERROR; - } else if(result == COPY_UNSUPPORTED_CONVERSION) { - stmt->errormsg = "Couldn't handle the necessary data type conversion."; - stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; - return SQL_ERROR; - } else if(result == COPY_RESULT_TRUNCATED) { - stmt->errornumber = STMT_TRUNCATED; - stmt->errormsg = "The buffer was too small for the result."; - return SQL_SUCCESS_WITH_INFO; - } else if(result != COPY_OK) { + stmt->current_col = icol; + + switch(result) { + case COPY_OK: + return SQL_SUCCESS; + + case COPY_UNSUPPORTED_TYPE: + stmt->errormsg = "Received an unsupported type from Postgres."; + stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; + return SQL_ERROR; + + case COPY_UNSUPPORTED_CONVERSION: + stmt->errormsg = "Couldn't handle the necessary data type conversion."; + stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; + return SQL_ERROR; + + case COPY_RESULT_TRUNCATED: + stmt->errornumber = STMT_TRUNCATED; + stmt->errormsg = "The buffer was too small for the result."; + return SQL_SUCCESS_WITH_INFO; + + case COPY_GENERAL_ERROR: /* error msg already filled in */ + return SQL_ERROR; + + case COPY_NO_DATA_FOUND: + return SQL_NO_DATA_FOUND; + + default: stmt->errormsg = "Unrecognized return value from copy_and_convert_field."; stmt->errornumber = STMT_INTERNAL_ERROR; return SQL_ERROR; } - - return SQL_SUCCESS; } // Returns data for bound columns in the current row ("hstmt->iCursor"), @@ -465,120 +569,136 @@ Int2 num_cols, lf; Oid type; char *value; ColumnInfoClass *ci; +// TupleField *tupleField; + + if ( ! stmt) + return SQL_INVALID_HANDLE; + + SC_clear_error(stmt); + + if ( ! (res = stmt->result)) { + stmt->errormsg = "Null statement result in SQLFetch."; + stmt->errornumber = STMT_SEQUENCE_ERROR; + return SQL_ERROR; + } + + ci = QR_get_fields(res); /* the column info */ + + if (stmt->status == STMT_EXECUTING) { + stmt->errormsg = "Can't fetch while statement is still executing."; + stmt->errornumber = STMT_SEQUENCE_ERROR; + return SQL_ERROR; + } - if ( ! stmt) - return SQL_INVALID_HANDLE; + if (stmt->status != STMT_FINISHED) { + stmt->errornumber = STMT_STATUS_ERROR; + stmt->errormsg = "Fetch can only be called after the successful execution on a SQL statement"; + return SQL_ERROR; + } - SC_clear_error(stmt); - - if ( ! (res = stmt->result)) { - stmt->errormsg = "Null statement result in SQLFetch."; - stmt->errornumber = STMT_SEQUENCE_ERROR; - return SQL_ERROR; - } - - ci = QR_get_fields(res); /* the column info */ - - if (stmt->status == STMT_EXECUTING) { - stmt->errormsg = "Can't fetch while statement is still executing."; - stmt->errornumber = STMT_SEQUENCE_ERROR; - return SQL_ERROR; - } - - - if (stmt->status != STMT_FINISHED) { - stmt->errornumber = STMT_STATUS_ERROR; - stmt->errormsg = "Fetch can only be called after the successful execution on a SQL statement"; - return SQL_ERROR; - } - - if (stmt->bindings == NULL) { - // just to avoid a crash if the user insists on calling this - // function even if SQL_ExecDirect has reported an Error - stmt->errormsg = "Bindings were not allocated properly."; - stmt->errornumber = STMT_SEQUENCE_ERROR; - return SQL_ERROR; - } + if (stmt->bindings == NULL) { + // just to avoid a crash if the user insists on calling this + // function even if SQL_ExecDirect has reported an Error + stmt->errormsg = "Bindings were not allocated properly."; + stmt->errornumber = STMT_SEQUENCE_ERROR; + return SQL_ERROR; + } + mylog("manual_result = %d, use_declarefetch = %d\n", + stmt->manual_result, globals.use_declarefetch); - if ( stmt->manual_result) { - if (QR_get_num_tuples(res) -1 == stmt->currTuple || - (stmt->maxRows > 0 && stmt->currTuple == stmt->maxRows - 1)) - /* if we are at the end of a tuple list, we return a "no data found" */ - return SQL_NO_DATA_FOUND; + if ( stmt->manual_result || ! globals.use_declarefetch) { + + if (stmt->currTuple >= QR_get_num_tuples(res) -1 || + (stmt->maxRows > 0 && stmt->currTuple == stmt->maxRows - 1)) { + + /* if at the end of the tuples, return "no data found" + and set the cursor past the end of the result set + */ + stmt->currTuple = QR_get_num_tuples(res); + return SQL_NO_DATA_FOUND; + } - mylog("**** SQLFetch: manual_result\n"); - (stmt->currTuple)++; - } - else { + mylog("**** SQLFetch: manual_result\n"); + (stmt->currTuple)++; + } + else { - // read from the cache or the physical next tuple - retval = QR_next_tuple(res); - if (retval < 0) { - mylog("**** SQLFetch: end_tuples\n"); - return SQL_NO_DATA_FOUND; - } - else if (retval > 0) - (stmt->currTuple)++; // all is well + // read from the cache or the physical next tuple + retval = QR_next_tuple(res); + if (retval < 0) { + mylog("**** SQLFetch: end_tuples\n"); + return SQL_NO_DATA_FOUND; + } + else if (retval > 0) + (stmt->currTuple)++; // all is well - else { - mylog("SQLFetch: error\n"); - stmt->errornumber = STMT_EXEC_ERROR; - stmt->errormsg = "Error fetching next row"; - return SQL_ERROR; - } + else { + mylog("SQLFetch: error\n"); + stmt->errornumber = STMT_EXEC_ERROR; + stmt->errormsg = "Error fetching next row"; + return SQL_ERROR; + } + } - } + num_cols = QR_NumResultCols(res); - num_cols = QR_NumResultCols(res); + for (lf=0; lf < num_cols; lf++) { - for (lf=0; lf < num_cols; lf++) { + mylog("fetch: cols=%d, lf=%d, stmt = %u, stmt->bindings = %u, buffer[] = %u\n", + num_cols, lf, stmt, stmt->bindings, stmt->bindings[lf].buffer); - mylog("fetch: cols=%d, lf=%d, buffer[] = %u\n", - num_cols, lf, stmt->bindings[lf].buffer); - - if (stmt->bindings[lf].buffer != NULL) { + if (stmt->bindings[lf].buffer != NULL) { // this column has a binding // type = QR_get_field_type(res, lf); type = CI_get_oid(ci, lf); /* speed things up */ + mylog("type = %d\n", type); + if (stmt->manual_result) value = QR_get_value_manual(res, stmt->currTuple, lf); - else + else if (globals.use_declarefetch) value = QR_get_value_backend(res, lf); + else { + value = QR_get_value_backend_row(res, stmt->currTuple, lf); + } - retval = copy_and_convert_field_bindinfo(type, value, &(stmt->bindings[lf])); + mylog("value = '%s'\n", value); - // check whether the complete result was copied - if(retval == COPY_UNSUPPORTED_TYPE) { - stmt->errormsg = "Received an unsupported type from Postgres."; - stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; - return SQL_ERROR; + retval = copy_and_convert_field_bindinfo(stmt, type, value, lf); - } else if(retval == COPY_UNSUPPORTED_CONVERSION) { - stmt->errormsg = "Couldn't handle the necessary data type conversion."; - stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; - return SQL_ERROR; + mylog("copy_and_convert: retval = %d\n", retval); - } else if(retval == COPY_RESULT_TRUNCATED) { - /* The result has been truncated during the copy */ - /* this will generate a SQL_SUCCESS_WITH_INFO result */ - stmt->errornumber = STMT_TRUNCATED; - stmt->errormsg = "A buffer was too small for the return value to fit in"; + // check whether the complete result was copied + if(retval == COPY_UNSUPPORTED_TYPE) { + stmt->errormsg = "Received an unsupported type from Postgres."; + stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; + return SQL_ERROR; + + } else if(retval == COPY_UNSUPPORTED_CONVERSION) { + stmt->errormsg = "Couldn't handle the necessary data type conversion."; + stmt->errornumber = STMT_RESTRICTED_DATA_TYPE_ERROR; + return SQL_ERROR; + + } else if(retval == COPY_RESULT_TRUNCATED) { + /* The result has been truncated during the copy */ + /* this will generate a SQL_SUCCESS_WITH_INFO result */ + stmt->errornumber = STMT_TRUNCATED; + stmt->errormsg = "A buffer was too small for the return value to fit in"; return SQL_SUCCESS_WITH_INFO; - } else if(retval != COPY_OK) { - stmt->errormsg = "Unrecognized return value from copy_and_convert_field."; - stmt->errornumber = STMT_INTERNAL_ERROR; - return SQL_ERROR; + } else if(retval != COPY_OK) { + stmt->errormsg = "Unrecognized return value from copy_and_convert_field."; + stmt->errornumber = STMT_INTERNAL_ERROR; + return SQL_ERROR; - } - } - } + } + } + } - return SQL_SUCCESS; + return SQL_SUCCESS; } // This fetchs a block of data (rowset). @@ -591,52 +711,91 @@ RETCODE SQL_API SQLExtendedFetch( UWORD FAR *rgfRowStatus) { StatementClass *stmt = (StatementClass *) hstmt; +int num_tuples; +RETCODE result; - if ( ! stmt) - return SQL_INVALID_HANDLE; - /* Currently, only for manual results can this be done - because not all the tuples are read in ahead of time. - */ - if ( ! stmt->manual_result) - return SQL_ERROR; +mylog("SQLExtendedFetch: stmt=%u\n", stmt); - // CC: we currently only support fetches in one row bits - if (NULL != pcrow) - *pcrow = 1; - if (NULL != rgfRowStatus) - *rgfRowStatus = SQL_ROW_SUCCESS; + if ( ! stmt) + return SQL_INVALID_HANDLE; + + if ( globals.use_declarefetch) + return SQL_ERROR; + + /* Initialize to no rows fetched */ + if (rgfRowStatus) + *rgfRowStatus = SQL_ROW_NOROW; + if (pcrow) + *pcrow = 0; + + num_tuples = QR_get_num_tuples(stmt->result); + + switch (fFetchType) { + case SQL_FETCH_NEXT: + mylog("SQL_FETCH_NEXT: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple); + break; + + case SQL_FETCH_PRIOR: + mylog("SQL_FETCH_PRIOR: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple); + + /* If already before result set, return no data found */ + if (stmt->currTuple <= 0) + return SQL_NO_DATA_FOUND; + + stmt->currTuple -= 2; + break; + + case SQL_FETCH_FIRST: + mylog("SQL_FETCH_FIRST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple); + + stmt->currTuple = -1; + break; + + case SQL_FETCH_LAST: + mylog("SQL_FETCH_LAST: num_tuples=%d, currtuple=%d\n", num_tuples, stmt->currTuple); + stmt->currTuple = num_tuples <= 0 ? -1 : (num_tuples - 2); + break; + + case SQL_FETCH_ABSOLUTE: + mylog("SQL_FETCH_ABSOLUTE: num_tuples=%d, currtuple=%d, irow=%d\n", num_tuples, stmt->currTuple, irow); + + /* Position before result set, but dont fetch anything */ + if (irow == 0) { + stmt->currTuple = -1; + return SQL_NO_DATA_FOUND; + } + /* Position before the desired row */ + else if (irow > 0) { + stmt->currTuple = irow-2; + } + /* Position with respect to the end of the result set */ + else { + stmt->currTuple = num_tuples + irow - 1; + } + break; + + default: + return SQL_ERROR; + + } + + mylog("SQLExtendedFetch: new currTuple = %d\n", stmt->currTuple); + + result = SQLFetch(hstmt); + + if (result == SQL_SUCCESS) { + if (rgfRowStatus) + *rgfRowStatus = SQL_ROW_SUCCESS; + if (pcrow) + *pcrow = 1; + } + + return result; - switch (fFetchType) { - case SQL_FETCH_NEXT: - return SQLFetch(hstmt); - case SQL_FETCH_PRIOR: - if (stmt->currTuple <= 0) - return SQL_ERROR; - stmt->currTuple--; - return SQLFetch(hstmt); - case SQL_FETCH_FIRST: - stmt->currTuple = -1; - return SQLFetch(hstmt); - case SQL_FETCH_LAST: - stmt->currTuple = QR_get_num_tuples(stmt->result)-1; - return SQLFetch(hstmt); - case SQL_FETCH_ABSOLUTE: - if (irow == 0) { - stmt->currTuple = stmt->currTuple > 0 ? stmt->currTuple-2 : -1; - } else if (irow > 0) { - stmt->currTuple = irow-2; - return SQLFetch(hstmt); - } else { - // CC: ??? not sure about the specification in that case - return SQL_ERROR; - } - default: - return SQL_ERROR; - } - return SQL_SUCCESS; } + // This determines whether there are more results sets available for // the "hstmt". @@ -677,7 +836,24 @@ RETCODE SQL_API SQLSetCursorName( UCHAR FAR *szCursor, SWORD cbCursor) { - return SQL_SUCCESS; +StatementClass *stmt = (StatementClass *) hstmt; +int len; + +mylog("SQLSetCursorName: hstmt=%u, szCursor=%u, cbCursorMax=%d\n", + hstmt, szCursor, cbCursor); + + if ( ! stmt) + return SQL_INVALID_HANDLE; + + len = (cbCursor == SQL_NTS) ? strlen(szCursor) : cbCursor; + mylog("cursor len = %d\n", len); + if (len <= 0 || len > sizeof(stmt->cursor_name) - 1) { + stmt->errornumber = STMT_INVALID_CURSOR_NAME; + stmt->errormsg = "Invalid Cursor Name"; + return SQL_ERROR; + } + strncpy_null(stmt->cursor_name, szCursor, cbCursor); + return SQL_SUCCESS; } // Return the cursor name for a statement handle @@ -688,7 +864,27 @@ RETCODE SQL_API SQLGetCursorName( SWORD cbCursorMax, SWORD FAR *pcbCursor) { - return SQL_ERROR; +StatementClass *stmt = (StatementClass *) hstmt; + +mylog("SQLGetCursorName: hstmt=%u, szCursor=%u, cbCursorMax=%d, pcbCursor=%u\n", + hstmt, szCursor, cbCursorMax, pcbCursor); + + if ( ! stmt) + return SQL_INVALID_HANDLE; + + + if ( stmt->cursor_name[0] == '\0') { + stmt->errornumber = STMT_NO_CURSOR_NAME; + stmt->errormsg = "No Cursor name available"; + return SQL_ERROR; + } + + strncpy_null(szCursor, stmt->cursor_name, cbCursorMax); + + if (pcbCursor) + *pcbCursor = strlen(szCursor); + + return SQL_SUCCESS; } diff --git a/src/interfaces/odbc/setup.c b/src/interfaces/odbc/setup.c index 05f076a1e5..42f538dc7d 100644 --- a/src/interfaces/odbc/setup.c +++ b/src/interfaces/odbc/setup.c @@ -1,95 +1,28 @@ - -/* Module: setup.c - * - * Description: This module contains the setup functions for - * adding/modifying a Data Source in the ODBC.INI portion - * of the registry. - * - * Classes: n/a - * - * API functions: ConfigDSN - * - * Comments: See "notice.txt" for copyright and license information. - * - *************************************************************************************/ -/* -** SETUP.C - This is the ODBC sample driver code for -** setup. -** -** This code is furnished on an as-is basis as part of the ODBC SDK and is -** intended for example purposes only. -** -*/ -/*-------------------------------------------------------------------------- - setup.c -- Sample ODBC setup +/* Module: setup.c + * + * Description: This module contains the setup functions for + * adding/modifying a Data Source in the ODBC.INI portion + * of the registry. + * + * Classes: n/a + * + * API functions: ConfigDSN + * + * Comments: See "notice.txt" for copyright and license information. + * + *************************************************************************************/ - This code demonstrates how to interact with the ODBC Installer. These - functions may be part of your ODBC driver or in a separate DLL. - - The ODBC Installer allows a driver to control the management of - data sources by calling the ConfigDSN entry point in the appropriate - DLL. When called, ConfigDSN receives four parameters: - - hwndParent ---- Handle of the parent window for any dialogs which - may need to be created. If this handle is NULL, - then no dialogs should be displayed (that is, the - request should be processed silently). - - fRequest ------ Flag indicating the type of request (add, configure - (edit), or remove). - - lpszDriver ---- Far pointer to a null-terminated string containing - the name of your driver. This is the same string you - supply in the ODBC.INF file as your section header - and which ODBC Setup displays to the user in lieu - of the actual driver filename. This string needs to - be passed back to the ODBC Installer when adding a - new data source name. - - lpszAttributes- Far pointer to a list of null-terminated attribute - keywords. This list is similar to the list passed - to SQLDriverConnect, except that each key-value - pair is separated by a null-byte rather than a - semicolon. The entire list is then terminated with - a null-byte (that is, two consecutive null-bytes - mark the end of the list). The keywords accepted - should be those for SQLDriverConnect which are - applicable, any new keywords you define for ODBC.INI, - and any additional keywords you decide to document. - - ConfigDSN should return TRUE if the requested operation succeeds and - FALSE otherwise. The complete prototype for ConfigDSN is: - - BOOL FAR PASCAL ConfigDSN(HWND hwndParent, - WORD fRequest, - LPSTR lpszDriver, - LPCSTR lpszAttributes) - - Your setup code should not write to ODBC.INI directly to add or remove - data source names. Instead, link with ODBCINST.LIB (the ODBC Installer - library) and call SQLWriteDSNToIni and SQLRemoveDSNFromIni. - Use SQLWriteDSNToIni to add data source names. If the data source name - already exists, SQLWriteDSNToIni will delete it (removing all of its - associated keys) and rewrite it. SQLRemoveDSNToIni removes a data - source name and all of its associated keys. - - For NT compatibility, the driver code should not use the - Get/WritePrivateProfileString windows functions for ODBC.INI, but instead, - use SQLGet/SQLWritePrivateProfileString functions that are macros (16 bit) or - calls to the odbcinst.dll (32 bit). - ---------------------------------------------------------------------------*/ - - -// Includes ---------------------------------------------------------------- -#include "psqlodbc.h" // Local include files +#include "psqlodbc.h" +#include "connection.h" #include #include -#include // ODBC installer prototypes -#include // C include files +#include +#include #include #include "resource.h" +#include "dlg_specific.h" + #define INTFUNC __stdcall @@ -104,81 +37,27 @@ extern GLOBAL_VALUES globals; #define MAXDESC (255+1) // Max description length #define MAXDSNAME (32+1) // Max data source name length -static char far EMPTYSTR []= ""; -static char far OPTIONON []= "Yes"; -static char far OPTIONOFF []= "No"; - -// Attribute key indexes (into an array of Attr structs, see below) -#define KEY_DSN 0 -#define KEY_DESC 1 -#define KEY_PORT 2 -#define KEY_SERVER 3 -#define KEY_DATABASE 4 -#define KEY_USER 5 -#define KEY_PASSWORD 6 -#define KEY_DEBUG 7 -#define KEY_FETCH 8 -#define KEY_READONLY 9 -#define KEY_PROTOCOL 10 -#define NUMOFKEYS 11 // Number of keys supported - -// Attribute string look-up table (maps keys to associated indexes) -static struct { - char szKey[MAXKEYLEN]; - int iKey; -} s_aLookup[] = { "DSN", KEY_DSN, - INI_KDESC, KEY_DESC, - INI_PORT, KEY_PORT, - INI_SERVER, KEY_SERVER, - INI_DATABASE, KEY_DATABASE, - INI_USER, KEY_USER, - INI_PASSWORD, KEY_PASSWORD, - INI_DEBUG, KEY_DEBUG, - INI_FETCH, KEY_FETCH, - INI_READONLY, KEY_READONLY, - INI_PROTOCOL, KEY_PROTOCOL, - "", 0 - }; - - - -// Types ------------------------------------------------------------------- -typedef struct tagAttr { - BOOL fSupplied; - char szAttr[MAXPATHLEN]; -} Attr, FAR * LPAttr; - // Globals ----------------------------------------------------------------- // NOTE: All these are used by the dialog procedures typedef struct tagSETUPDLG { - HWND hwndParent; // Parent window handle - LPCSTR lpszDrvr; // Driver description - Attr aAttr[NUMOFKEYS]; // Attribute array - char szDSN[MAXDSNAME]; // Original data source name - BOOL fNewDSN; // New data source flag - BOOL fDefault; // Default data source flag + HWND hwndParent; // Parent window handle + LPCSTR lpszDrvr; // Driver description + ConnInfo ci; + char szDSN[MAXDSNAME]; // Original data source name + BOOL fNewDSN; // New data source flag + BOOL fDefault; // Default data source flag } SETUPDLG, FAR *LPSETUPDLG; // Prototypes -------------------------------------------------------------- -void INTFUNC CenterDialog (HWND hdlg); +void INTFUNC CenterDialog(HWND hdlg); +int CALLBACK ConfigDlgProc(HWND hdlg, WORD wMsg, WPARAM wParam, LPARAM lParam); +void INTFUNC ParseAttributes (LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg); +BOOL INTFUNC SetDSNAttributes(HWND hwnd, LPSETUPDLG lpsetupdlg); -int CALLBACK ConfigDlgProc (HWND hdlg, - WORD wMsg, - WPARAM wParam, - LPARAM lParam); -void INTFUNC ParseAttributes (LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg); - -/* CC: SetDSNAttributes is declared as "INTFUNC" below, but here it is declared as - "CALLBACK" -- Watcom complained about disagreeing modifiers. Changed - "CALLBACK" to "INTFUNC" here. - BOOL CALLBACK SetDSNAttributes(HWND hwnd, LPSETUPDLG lpsetupdlg); -*/ - -BOOL INTFUNC SetDSNAttributes(HWND hwnd, LPSETUPDLG lpsetupdlg); /* ConfigDSN --------------------------------------------------------------- Description: ODBC Setup entry point @@ -196,9 +75,9 @@ BOOL CALLBACK ConfigDSN (HWND hwnd, LPCSTR lpszDriver, LPCSTR lpszAttributes) { - BOOL fSuccess; // Success/fail flag - GLOBALHANDLE hglbAttr; - LPSETUPDLG lpsetupdlg; +BOOL fSuccess; // Success/fail flag +GLOBALHANDLE hglbAttr; +LPSETUPDLG lpsetupdlg; // Allocate attribute array @@ -212,20 +91,20 @@ BOOL CALLBACK ConfigDSN (HWND hwnd, ParseAttributes(lpszAttributes, lpsetupdlg); // Save original data source name - if (lpsetupdlg->aAttr[KEY_DSN].fSupplied) - lstrcpy(lpsetupdlg->szDSN, lpsetupdlg->aAttr[KEY_DSN].szAttr); + if (lpsetupdlg->ci.dsn[0]) + lstrcpy(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn); else lpsetupdlg->szDSN[0] = '\0'; // Remove data source if (ODBC_REMOVE_DSN == fRequest) { // Fail if no data source name was supplied - if (!lpsetupdlg->aAttr[KEY_DSN].fSupplied) + if (!lpsetupdlg->ci.dsn[0]) fSuccess = FALSE; // Otherwise remove data source from ODBC.INI else - fSuccess = SQLRemoveDSNFromIni(lpsetupdlg->aAttr[KEY_DSN].szAttr); + fSuccess = SQLRemoveDSNFromIni(lpsetupdlg->ci.dsn); } // Add or Configure data source @@ -235,19 +114,19 @@ BOOL CALLBACK ConfigDSN (HWND hwnd, lpsetupdlg->lpszDrvr = lpszDriver; lpsetupdlg->fNewDSN = (ODBC_ADD_DSN == fRequest); lpsetupdlg->fDefault = - !lstrcmpi(lpsetupdlg->aAttr[KEY_DSN].szAttr, INI_DSN); + !lstrcmpi(lpsetupdlg->ci.dsn, INI_DSN); // Display the appropriate dialog (if parent window handle supplied) if (hwnd) { // Display dialog(s) fSuccess = (IDOK == DialogBoxParam(s_hModule, - MAKEINTRESOURCE(CONFIGDSN), - hwnd, - ConfigDlgProc, - (LONG)(LPSTR)lpsetupdlg)); + MAKEINTRESOURCE(DLG_CONFIG), + hwnd, + ConfigDlgProc, + (LONG)(LPSTR)lpsetupdlg)); } - else if (lpsetupdlg->aAttr[KEY_DSN].fSupplied) + else if (lpsetupdlg->ci.dsn[0]) fSuccess = SetDSNAttributes(hwnd, lpsetupdlg); else fSuccess = FALSE; @@ -314,208 +193,123 @@ void INTFUNC CenterDialog(HWND hdlg) --------------------------------------------------------------------------*/ - -int CALLBACK ConfigDlgProc - (HWND hdlg, - WORD wMsg, - WPARAM wParam, - LPARAM lParam) +int CALLBACK ConfigDlgProc(HWND hdlg, + WORD wMsg, + WPARAM wParam, + LPARAM lParam) { - switch (wMsg) { - // Initialize the dialog - case WM_INITDIALOG: - { - LPSETUPDLG lpsetupdlg; - LPCSTR lpszDSN; + switch (wMsg) { + // Initialize the dialog + case WM_INITDIALOG: + { + LPSETUPDLG lpsetupdlg = (LPSETUPDLG) lParam; + ConnInfo *ci = &lpsetupdlg->ci; - SetWindowLong(hdlg, DWL_USER, lParam); - CenterDialog(hdlg); // Center dialog + /* Hide the driver connect message */ + ShowWindow(GetDlgItem(hdlg, DRV_MSG_LABEL), SW_HIDE); - lpsetupdlg = (LPSETUPDLG) lParam; - lpszDSN = lpsetupdlg->aAttr[KEY_DSN].szAttr; - // Initialize dialog fields - // NOTE: Values supplied in the attribute string will always - // override settings in ODBC.INI - SetDlgItemText(hdlg, IDC_DSNAME, lpszDSN); + SetWindowLong(hdlg, DWL_USER, lParam); + CenterDialog(hdlg); // Center dialog - // Description - if (!lpsetupdlg->aAttr[KEY_DESC].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_KDESC, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_DESC].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_DESC].szAttr), - ODBC_INI); - SetDlgItemText(hdlg, IDC_DESC, lpsetupdlg->aAttr[KEY_DESC].szAttr); + // NOTE: Values supplied in the attribute string will always + // override settings in ODBC.INI - // Database - if (!lpsetupdlg->aAttr[KEY_DATABASE].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_DATABASE, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_DATABASE].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_DATABASE].szAttr), - ODBC_INI); - SetDlgItemText(hdlg, IDC_DATABASE, lpsetupdlg->aAttr[KEY_DATABASE].szAttr); + // Get the rest of the common attributes + getDSNinfo(ci, CONN_DONT_OVERWRITE); - // Server - if (!lpsetupdlg->aAttr[KEY_SERVER].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_SERVER, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_SERVER].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_SERVER].szAttr), - ODBC_INI); - SetDlgItemText(hdlg, IDC_SERVER, lpsetupdlg->aAttr[KEY_SERVER].szAttr); - - // Port - if (!lpsetupdlg->aAttr[KEY_PORT].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_PORT, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_PORT].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_PORT].szAttr), - ODBC_INI); - if (lpsetupdlg->aAttr[KEY_PORT].szAttr[0] == '\0') - strcpy(lpsetupdlg->aAttr[KEY_PORT].szAttr, DEFAULT_PORT); - SetDlgItemText(hdlg, IDC_PORT, lpsetupdlg->aAttr[KEY_PORT].szAttr); - - /* Username */ - if (!lpsetupdlg->aAttr[KEY_USER].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_USER, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_USER].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_USER].szAttr), - ODBC_INI); - SetDlgItemText(hdlg, IDC_USER, lpsetupdlg->aAttr[KEY_USER].szAttr); - - // Password - if (!lpsetupdlg->aAttr[KEY_PASSWORD].fSupplied) - SQLGetPrivateProfileString(lpszDSN, INI_PASSWORD, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_PASSWORD].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_PASSWORD].szAttr), - ODBC_INI); - SetDlgItemText(hdlg, IDC_PASSWORD, lpsetupdlg->aAttr[KEY_PASSWORD].szAttr); - - // ReadOnly Parameter - if (!lpsetupdlg->aAttr[KEY_READONLY].fSupplied) { - SQLGetPrivateProfileString(lpszDSN, INI_READONLY, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_READONLY].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_READONLY].szAttr), - ODBC_INI); - } - if (lpsetupdlg->aAttr[KEY_READONLY].szAttr[0] == '\0') - strcpy(lpsetupdlg->aAttr[KEY_READONLY].szAttr, DEFAULT_READONLY); - CheckDlgButton(hdlg, IDC_READONLY, atoi(lpsetupdlg->aAttr[KEY_READONLY].szAttr)); - - // Protocol Parameter - if (!lpsetupdlg->aAttr[KEY_PROTOCOL].fSupplied) { - SQLGetPrivateProfileString(lpszDSN, INI_PROTOCOL, - EMPTYSTR, - lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr), - ODBC_INI); - } - if (strncmp(lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr, PG62, strlen(PG62)) == 0) - CheckDlgButton(hdlg, IDC_PG62, 1); - else - CheckDlgButton(hdlg, IDC_PG62, 0); - - - // CommLog Parameter (this is global) - CheckDlgButton(hdlg, IDC_COMMLOG, globals.commlog); + // Fill in any defaults + getDSNdefaults(ci); - if (lpsetupdlg->fDefault) - { - EnableWindow(GetDlgItem(hdlg, IDC_DSNAME), FALSE); - EnableWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), FALSE); - } - else - SendDlgItemMessage(hdlg, IDC_DSNAME, - EM_LIMITTEXT, (WPARAM)(MAXDSNAME-1), 0L); - SendDlgItemMessage(hdlg, IDC_DESC, - EM_LIMITTEXT, (WPARAM)(MAXDESC-1), 0L); - return TRUE; // Focus was not set + // Initialize dialog fields + SetDlgStuff(hdlg, ci); + + + if (lpsetupdlg->fDefault) { + EnableWindow(GetDlgItem(hdlg, IDC_DSNAME), FALSE); + EnableWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), FALSE); + } + else + SendDlgItemMessage(hdlg, IDC_DSNAME, + EM_LIMITTEXT, (WPARAM)(MAXDSNAME-1), 0L); + + SendDlgItemMessage(hdlg, IDC_DESC, + EM_LIMITTEXT, (WPARAM)(MAXDESC-1), 0L); + return TRUE; // Focus was not set } - // Process buttons - case WM_COMMAND: - switch (GET_WM_COMMAND_ID(wParam, lParam)) { - // Ensure the OK button is enabled only when a data source name - // is entered - case IDC_DSNAME: - if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE) - { - char szItem[MAXDSNAME]; // Edit control text + // Process buttons + case WM_COMMAND: - // Enable/disable the OK button - EnableWindow(GetDlgItem(hdlg, IDOK), - GetDlgItemText(hdlg, IDC_DSNAME, - szItem, sizeof(szItem))); - return TRUE; - } - break; + switch (GET_WM_COMMAND_ID(wParam, lParam)) { + // Ensure the OK button is enabled only when a data source name + // is entered + case IDC_DSNAME: + if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE) + { + char szItem[MAXDSNAME]; // Edit control text - // Accept results - case IDOK: - { - LPSETUPDLG lpsetupdlg; + // Enable/disable the OK button + EnableWindow(GetDlgItem(hdlg, IDOK), + GetDlgItemText(hdlg, IDC_DSNAME, + szItem, sizeof(szItem))); - lpsetupdlg = (LPSETUPDLG)GetWindowLong(hdlg, DWL_USER); - // Retrieve dialog values - if (!lpsetupdlg->fDefault) - GetDlgItemText(hdlg, IDC_DSNAME, - lpsetupdlg->aAttr[KEY_DSN].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_DSN].szAttr)); - GetDlgItemText(hdlg, IDC_DESC, - lpsetupdlg->aAttr[KEY_DESC].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_DESC].szAttr)); - - GetDlgItemText(hdlg, IDC_DATABASE, - lpsetupdlg->aAttr[KEY_DATABASE].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_DATABASE].szAttr)); - - GetDlgItemText(hdlg, IDC_PORT, - lpsetupdlg->aAttr[KEY_PORT].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_PORT].szAttr)); - - GetDlgItemText(hdlg, IDC_SERVER, - lpsetupdlg->aAttr[KEY_SERVER].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_SERVER].szAttr)); - - GetDlgItemText(hdlg, IDC_USER, - lpsetupdlg->aAttr[KEY_USER].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_USER].szAttr)); + return TRUE; + } + break; - GetDlgItemText(hdlg, IDC_PASSWORD, - lpsetupdlg->aAttr[KEY_PASSWORD].szAttr, - sizeof(lpsetupdlg->aAttr[KEY_PASSWORD].szAttr)); - - if ( IsDlgButtonChecked(hdlg, IDC_PG62)) - strcpy(lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr, PG62); - else - lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr[0] = '\0'; + // Accept results + case IDOK: + { + LPSETUPDLG lpsetupdlg; - sprintf(lpsetupdlg->aAttr[KEY_READONLY].szAttr, "%d", IsDlgButtonChecked(hdlg, IDC_READONLY)); - - globals.commlog = IsDlgButtonChecked(hdlg, IDC_COMMLOG); + lpsetupdlg = (LPSETUPDLG)GetWindowLong(hdlg, DWL_USER); + // Retrieve dialog values + if (!lpsetupdlg->fDefault) + GetDlgItemText(hdlg, IDC_DSNAME, + lpsetupdlg->ci.dsn, + sizeof(lpsetupdlg->ci.dsn)); - // Update ODBC.INI - SetDSNAttributes(hdlg, lpsetupdlg); + // Get Dialog Values + GetDlgStuff(hdlg, &lpsetupdlg->ci); + + // Update ODBC.INI + SetDSNAttributes(hdlg, lpsetupdlg); } - // Return to caller - case IDCANCEL: - EndDialog(hdlg, wParam); - return TRUE; - } - break; - } + // Return to caller + case IDCANCEL: + EndDialog(hdlg, wParam); + return TRUE; - // Message not processed - return FALSE; + case IDC_DRIVER: + + DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV), + hdlg, driver_optionsProc, (LPARAM) NULL); + + return TRUE; + + case IDC_DATASOURCE: + { + LPSETUPDLG lpsetupdlg; + + lpsetupdlg = (LPSETUPDLG)GetWindowLong(hdlg, DWL_USER); + + DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DS), + hdlg, ds_optionsProc, (LPARAM) &lpsetupdlg->ci); + + return TRUE; + } + } + + break; + } + + // Message not processed + return FALSE; } @@ -526,11 +320,13 @@ int CALLBACK ConfigDlgProc --------------------------------------------------------------------------*/ void INTFUNC ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg) { - LPCSTR lpsz; - LPCSTR lpszStart; - char aszKey[MAXKEYLEN]; - int iElement; - int cbKey; +LPCSTR lpsz; +LPCSTR lpszStart; +char aszKey[MAXKEYLEN]; +int cbKey; +char value[MAXPATHLEN]; + + memset(&lpsetupdlg->ci, 0, sizeof(ConnInfo)); for (lpsz=lpszAttributes; *lpsz; lpsz++) { // Extract key name (e.g., DSN), it must be terminated by an equals @@ -543,38 +339,26 @@ void INTFUNC ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg) break; // Valid key found } // Determine the key's index in the key table (-1 if not found) - iElement = -1; cbKey = lpsz - lpszStart; if (cbKey < sizeof(aszKey)) { - register int j; _fmemcpy(aszKey, lpszStart, cbKey); aszKey[cbKey] = '\0'; - for (j = 0; *s_aLookup[j].szKey; j++) - { - if (!lstrcmpi(s_aLookup[j].szKey, aszKey)) - { - iElement = s_aLookup[j].iKey; - break; - } - } } // Locate end of key value lpszStart = ++lpsz; for (; *lpsz; lpsz++); - // Save value if key is known - // NOTE: This code assumes the szAttr buffers in aAttr have been - // zero initialized - if (iElement >= 0) - { - lpsetupdlg->aAttr[iElement].fSupplied = TRUE; - _fmemcpy(lpsetupdlg->aAttr[iElement].szAttr, - lpszStart, - MIN(lpsz-lpszStart+1, sizeof(lpsetupdlg->aAttr[0].szAttr)-1)); - } + + // lpsetupdlg->aAttr[iElement].fSupplied = TRUE; + _fmemcpy(value, lpszStart, MIN(lpsz-lpszStart+1, MAXPATHLEN)); + + mylog("aszKey='%s', value='%s'\n", aszKey, value); + + // Copy the appropriate value to the conninfo + copyAttributes(&lpsetupdlg->ci, aszKey, value); } return; } @@ -588,12 +372,12 @@ void INTFUNC ParseAttributes(LPCSTR lpszAttributes, LPSETUPDLG lpsetupdlg) BOOL INTFUNC SetDSNAttributes(HWND hwndParent, LPSETUPDLG lpsetupdlg) { - LPCSTR lpszDSN; // Pointer to data source name +LPCSTR lpszDSN; // Pointer to data source name - lpszDSN = lpsetupdlg->aAttr[KEY_DSN].szAttr; + lpszDSN = lpsetupdlg->ci.dsn; // Validate arguments - if (lpsetupdlg->fNewDSN && !*lpsetupdlg->aAttr[KEY_DSN].szAttr) + if (lpsetupdlg->fNewDSN && !*lpsetupdlg->ci.dsn) return FALSE; // Write the data source name @@ -614,64 +398,11 @@ BOOL INTFUNC SetDSNAttributes(HWND hwndParent, LPSETUPDLG lpsetupdlg) // Update ODBC.INI - // Save the value if the data source is new, if it was edited, or if - // it was explicitly supplied - if (hwndParent || lpsetupdlg->aAttr[KEY_DESC].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_KDESC, - lpsetupdlg->aAttr[KEY_DESC].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_DATABASE].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_DATABASE, - lpsetupdlg->aAttr[KEY_DATABASE].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_PORT].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_PORT, - lpsetupdlg->aAttr[KEY_PORT].szAttr, - ODBC_INI); + writeDSNinfo(&lpsetupdlg->ci); - if (hwndParent || lpsetupdlg->aAttr[KEY_SERVER].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_SERVER, - lpsetupdlg->aAttr[KEY_SERVER].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_USER].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_USER, - lpsetupdlg->aAttr[KEY_USER].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_PASSWORD].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_PASSWORD, - lpsetupdlg->aAttr[KEY_PASSWORD].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_READONLY].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_READONLY, - lpsetupdlg->aAttr[KEY_READONLY].szAttr, - ODBC_INI); - - if (hwndParent || lpsetupdlg->aAttr[KEY_PROTOCOL].fSupplied ) - SQLWritePrivateProfileString(lpszDSN, - INI_PROTOCOL, - lpsetupdlg->aAttr[KEY_PROTOCOL].szAttr, - ODBC_INI); - - // CommLog Parameter -- write to ODBCINST_INI (for the whole driver) - if (hwndParent ) { - updateGlobals(); - } // If the data source name has changed, remove the old name - if (lpsetupdlg->aAttr[KEY_DSN].fSupplied && - lstrcmpi(lpsetupdlg->szDSN, lpsetupdlg->aAttr[KEY_DSN].szAttr)) + if (lstrcmpi(lpsetupdlg->szDSN, lpsetupdlg->ci.dsn)) { SQLRemoveDSNFromIni(lpsetupdlg->szDSN); } diff --git a/src/interfaces/odbc/socket.c b/src/interfaces/odbc/socket.c index 40318ea502..4d28f04b15 100644 --- a/src/interfaces/odbc/socket.c +++ b/src/interfaces/odbc/socket.c @@ -1,16 +1,16 @@ - -/* 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. - * - */ + +/* 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. + * + */ #include "socket.h" @@ -45,8 +45,8 @@ SocketClass *rv; return NULL; rv->errormsg = NULL; - rv->errornumber = 0; - + rv->errornumber = 0; + rv->reverse = FALSE; } return rv; @@ -77,6 +77,7 @@ SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname) { struct hostent *host; struct sockaddr_in sadr; +unsigned long iaddr; if (self->socket != -1) { self->errornumber = SOCKET_ALREADY_CONNECTED; @@ -84,15 +85,24 @@ struct sockaddr_in sadr; return 0; } - host = gethostbyname(hostname); - if (host == NULL) { - self->errornumber = SOCKET_HOST_NOT_FOUND; - self->errormsg = "Could not resolve hostname."; - return 0; - } - memset((char *)&sadr, 0, sizeof(sadr)); - memcpy(&(sadr.sin_addr), host->h_addr, host->h_length); + + /* If it is a valid IP address, use it. + Otherwise use hostname lookup. + */ + iaddr = inet_addr(hostname); + if (iaddr == INADDR_NONE) { + host = gethostbyname(hostname); + if (host == NULL) { + self->errornumber = SOCKET_HOST_NOT_FOUND; + self->errormsg = "Could not resolve hostname."; + return 0; + } + memcpy(&(sadr.sin_addr), host->h_addr, host->h_length); + } + else + memcpy(&(sadr.sin_addr), (struct in_addr *) &iaddr, sizeof(iaddr)); + sadr.sin_family = AF_INET; sadr.sin_port = htons(port); @@ -185,15 +195,15 @@ char buf[4]; switch (len) { case 2: SOCK_get_n_char(self, buf, len); - if (self->reverse) - return *((unsigned short *) buf); - else + if (self->reverse) + return *((unsigned short *) buf); + else return ntohs( *((unsigned short *) buf) ); case 4: - SOCK_get_n_char(self, buf, len); - if (self->reverse) - return *((unsigned int *) buf); + SOCK_get_n_char(self, buf, len); + if (self->reverse) + return *((unsigned int *) buf); else return ntohl( *((unsigned int *) buf) ); @@ -202,7 +212,7 @@ char buf[4]; self->errormsg = "Cannot read ints of that length"; return 0; } -} +} void @@ -211,12 +221,12 @@ SOCK_put_int(SocketClass *self, int value, short len) unsigned int rv; switch (len) { - case 2: + case 2: rv = self->reverse ? value : htons( (unsigned short) value); SOCK_put_n_char(self, (char *) &rv, 2); return; - case 4: + case 4: rv = self->reverse ? value : htonl( (unsigned int) value); SOCK_put_n_char(self, (char *) &rv, 4); return; @@ -251,7 +261,7 @@ SOCK_get_next_byte(SocketClass *self) // reload the buffer self->buffer_read_in = 0; - self->buffer_filled_in = recv(self->socket, (char *)self->buffer_in, globals.socket_buffersize, 0); + self->buffer_filled_in = recv(self->socket, (char *)self->buffer_in, globals.socket_buffersize, 0); mylog("read %d, global_socket_buffersize=%d\n", self->buffer_filled_in, globals.socket_buffersize); diff --git a/src/interfaces/odbc/socket.h b/src/interfaces/odbc/socket.h index 458adc716c..457ab258da 100644 --- a/src/interfaces/odbc/socket.h +++ b/src/interfaces/odbc/socket.h @@ -1,11 +1,11 @@ - -/* File: socket.h - * - * Description: See "socket.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: socket.h + * + * Description: See "socket.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __SOCKET_H__ #define __SOCKET_H__ @@ -36,8 +36,8 @@ struct SocketClass_ { SOCKET socket; char *errormsg; - int errornumber; - + int errornumber; + char reverse; /* used to handle Postgres 6.2 protocol (reverse byte order) */ }; @@ -49,9 +49,9 @@ struct SocketClass_ { /* error functions */ #define SOCK_get_errcode(self) (self->errornumber) #define SOCK_get_errmsg(self) (self->errormsg) - -/* Socket prototypes */ + +/* Socket prototypes */ SocketClass *SOCK_Constructor(); void SOCK_Destructor(SocketClass *self); char SOCK_connect_to(SocketClass *self, unsigned short port, char *hostname); diff --git a/src/interfaces/odbc/statement.c b/src/interfaces/odbc/statement.c index bea5968dc2..7a5b0d886b 100644 --- a/src/interfaces/odbc/statement.c +++ b/src/interfaces/odbc/statement.c @@ -1,16 +1,16 @@ - -/* Module: statement.c - * - * Description: This module contains functions related to creating - * and manipulating a statement. - * - * Classes: StatementClass (Functions prefix: "SC_") - * - * API functions: SQLAllocStmt, SQLFreeStmt - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: statement.c + * + * Description: This module contains functions related to creating + * and manipulating a statement. + * + * Classes: StatementClass (Functions prefix: "SC_") + * + * API functions: SQLAllocStmt, SQLFreeStmt + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "statement.h" #include "bind.h" @@ -23,8 +23,25 @@ #include #include -extern GLOBAL_VALUES globals; - +extern GLOBAL_VALUES globals; + +/* Map sql commands to statement types */ +static struct { + int type; + char *s; +} Statement_Type[] = { + { STMT_TYPE_SELECT, "SELECT" }, + { STMT_TYPE_INSERT, "INSERT" }, + { STMT_TYPE_UPDATE, "UPDATE" }, + { STMT_TYPE_DELETE, "DELETE" }, + { STMT_TYPE_CREATE, "CREATE" }, + { STMT_TYPE_ALTER, "ALTER" }, + { STMT_TYPE_DROP, "DROP" }, + { STMT_TYPE_GRANT, "GRANT" }, + { STMT_TYPE_REVOKE, "REVOKE" }, + { 0, NULL } +}; + RETCODE SQL_API SQLAllocStmt(HDBC hdbc, HSTMT FAR *phstmt) @@ -103,7 +120,7 @@ StatementClass *stmt = (StatementClass *) hstmt; // errormsg passed in above return SQL_ERROR; - } else if(fOption == SQL_RESET_PARAMS) { + } else if(fOption == SQL_RESET_PARAMS) { SC_free_params(stmt, STMT_FREE_PARAMS_ALL); } else { @@ -133,21 +150,28 @@ StatementClass *rv; rv->prepare = FALSE; rv->status = STMT_ALLOCATED; rv->maxRows = 0; // driver returns all rows + rv->rowset_size = 1; + rv->scroll_concurrency = SQL_CONCUR_READ_ONLY; + rv->cursor_type = SQL_CURSOR_FORWARD_ONLY; rv->errormsg = NULL; rv->errornumber = 0; rv->errormsg_created = FALSE; - rv->statement = NULL; - rv->stmt_with_params[0] = '\0'; + rv->statement = NULL; + rv->stmt_with_params[0] = '\0'; rv->statement_type = STMT_TYPE_UNKNOWN; rv->bindings = NULL; rv->bindings_allocated = 0; rv->parameters_allocated = 0; rv->parameters = 0; rv->currTuple = -1; + rv->current_col = -1; rv->result = 0; - rv->data_at_exec = -1; - rv->current_exec_param = -1; - rv->put_data = FALSE; + rv->data_at_exec = -1; + rv->current_exec_param = -1; + rv->put_data = FALSE; + rv->lobj_fd = -1; + rv->internal = FALSE; + rv->cursor_name[0] = '\0'; } return rv; } @@ -172,9 +196,9 @@ SC_Destructor(StatementClass *self) if (self->statement) free(self->statement); - - SC_free_params(self, STMT_FREE_PARAMS_ALL); - + + SC_free_params(self, STMT_FREE_PARAMS_ALL); + /* the memory pointed to by the bindings is not deallocated by the driver */ /* by by the application that uses that driver, so we don't have to care */ /* about that here. */ @@ -183,62 +207,63 @@ SC_Destructor(StatementClass *self) free(self); + mylog("SC_Destructor: EXIT\n"); + return TRUE; } - -/* Free parameters and free the memory from the - data-at-execution parameters that was allocated in SQLPutData. -*/ -void -SC_free_params(StatementClass *self, char option) -{ -int i; - - if( ! self->parameters) - return; - - for (i = 0; i < self->parameters_allocated; i++) { - if (self->parameters[i].data_at_exec == TRUE) { - - if (self->parameters[i].EXEC_used) { - free(self->parameters[i].EXEC_used); - self->parameters[i].EXEC_used = NULL; - } - - if (self->parameters[i].EXEC_buffer) { - free(self->parameters[i].EXEC_buffer); - self->parameters[i].EXEC_buffer = NULL; - } - } - } - self->data_at_exec = -1; - self->current_exec_param = -1; - self->put_data = FALSE; - - if (option == STMT_FREE_PARAMS_ALL) { - free(self->parameters); - self->parameters = NULL; - self->parameters_allocated = 0; - } -} + +/* Free parameters and free the memory from the + data-at-execution parameters that was allocated in SQLPutData. +*/ +void +SC_free_params(StatementClass *self, char option) +{ +int i; + + mylog("SC_free_params: ENTER, self=%d\n", self); + + if( ! self->parameters) + return; + + for (i = 0; i < self->parameters_allocated; i++) { + if (self->parameters[i].data_at_exec == TRUE) { + + if (self->parameters[i].EXEC_used) { + free(self->parameters[i].EXEC_used); + self->parameters[i].EXEC_used = NULL; + } + + if (self->parameters[i].EXEC_buffer) { + free(self->parameters[i].EXEC_buffer); + self->parameters[i].EXEC_buffer = NULL; + } + } + } + self->data_at_exec = -1; + self->current_exec_param = -1; + self->put_data = FALSE; + + if (option == STMT_FREE_PARAMS_ALL) { + free(self->parameters); + self->parameters = NULL; + self->parameters_allocated = 0; + } + + mylog("SC_free_params: EXIT\n"); +} + + int statement_type(char *statement) { - if(strnicmp(statement, "SELECT", 6) == 0) - return STMT_TYPE_SELECT; +int i; - else if(strnicmp(statement, "INSERT", 6) == 0) - return STMT_TYPE_INSERT; + for (i = 0; Statement_Type[i].s; i++) + if ( ! strnicmp(statement, Statement_Type[i].s, strlen(Statement_Type[i].s))) + return Statement_Type[i].type; - else if(strnicmp(statement, "UPDATE", 6) == 0) - return STMT_TYPE_UPDATE; - - else if(strnicmp(statement, "DELETE", 6) == 0) - return STMT_TYPE_DELETE; - - else - return STMT_TYPE_OTHER; + return STMT_TYPE_OTHER; } /* Called from SQLPrepare if STMT_PREMATURE, or @@ -298,16 +323,21 @@ ConnectionClass *conn; } self->status = STMT_READY; + self->manual_result = FALSE; // very important + self->currTuple = -1; + self->current_col = -1; self->errormsg = NULL; self->errornumber = 0; self->errormsg_created = FALSE; - - // Free any data at exec params before the statement is executed - // again. If not, then there will be a memory leak when - // the next SQLParamData/SQLPutData is called. - SC_free_params(self, STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY); + + self->lobj_fd = -1; + + // Free any data at exec params before the statement is executed + // again. If not, then there will be a memory leak when + // the next SQLParamData/SQLPutData is called. + SC_free_params(self, STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY); return TRUE; } @@ -414,132 +444,143 @@ char rv; return rv; } -RETCODE SC_execute(StatementClass *self) -{ -ConnectionClass *conn; -QResultClass *res; -char ok, was_ok, was_nonfatal; -Int2 oldstatus, numcols; - - - conn = SC_get_conn(self); - - /* Begin a transaction if one is not already in progress */ - /* The reason is because we can't use declare/fetch cursors without - starting a transaction first. - */ - - if ( ! CC_is_in_trans(conn)) { - mylog(" about to begin a transaction on statement = %u\n", self); - res = CC_send_query(conn, "BEGIN", NULL, NULL); - if ( ! res) { - self->errormsg = "Could not begin a transaction"; - self->errornumber = STMT_EXEC_ERROR; - return SQL_ERROR; - } - - ok = QR_command_successful(res); - - mylog("SQLExecute: ok = %d, status = %d\n", ok, QR_get_status(res)); - - QR_Destructor(res); - - if (!ok) { - self->errormsg = "Could not begin a transaction"; - self->errornumber = STMT_EXEC_ERROR; - return SQL_ERROR; - } - else - CC_set_in_trans(conn); - } - - - - oldstatus = conn->status; - conn->status = CONN_EXECUTING; - self->status = STMT_EXECUTING; - - - // If its a SELECT statement, use a cursor. - // Note that the declare cursor has already been prepended to the statement - // in copy_statement... - if (self->statement_type == STMT_TYPE_SELECT) { - - char cursor[32]; - char fetch[64]; - - sprintf(cursor, "C%u", self); - - mylog(" Sending SELECT statement on stmt=%u\n", self); - - /* send the declare/select */ - self->result = CC_send_query(conn, self->stmt_with_params, NULL, NULL); - if (self->result != NULL) { - /* That worked, so now send the fetch to start getting data back */ - sprintf(fetch, "fetch %d in %s", globals.fetch_max, cursor); - - // Save the cursor in the result for later use - self->result = CC_send_query( conn, fetch, NULL, cursor); - } - - mylog(" done sending the query:\n"); - - } - else { // not a SELECT statement so don't use a cursor - mylog(" its NOT a select statement: stmt=%u\n", self); - self->result = CC_send_query(conn, self->stmt_with_params, NULL, NULL); - - // If we are in autocommit, we must send the commit. - if (CC_is_in_autocommit(conn)) { - CC_send_query(conn, "COMMIT", NULL, NULL); - CC_set_no_trans(conn); - } - - } - - conn->status = oldstatus; - self->status = STMT_FINISHED; - - /* Check the status of the result */ - if (self->result) { - - was_ok = QR_command_successful(self->result); - was_nonfatal = QR_command_nonfatal(self->result); - - if ( was_ok) - self->errornumber = STMT_OK; - else - self->errornumber = was_nonfatal ? STMT_INFO_ONLY : STMT_ERROR_TAKEN_FROM_BACKEND; - - self->currTuple = -1; /* set cursor before the first tuple in the list */ - - /* see if the query did return any result columns */ - numcols = QR_NumResultCols(self->result); - - /* now allocate the array to hold the binding info */ - if (numcols > 0) { - extend_bindings(self, numcols); - if (self->bindings == NULL) { - self->errornumber = STMT_NO_MEMORY_ERROR; - self->errormsg = "Could not get enough free memory to store the binding information"; - return SQL_ERROR; - } - } - - } else { /* Bad Error -- The error message will be in the Connection */ - - self->errornumber = STMT_EXEC_ERROR; - self->errormsg = "Error while executing the query"; - - CC_abort(conn); - } - - if (self->errornumber == STMT_OK) - return SQL_SUCCESS; - - else if (self->errornumber == STMT_INFO_ONLY) - return SQL_SUCCESS_WITH_INFO; - - else - return SQL_ERROR; -} +RETCODE SC_execute(StatementClass *self) +{ +ConnectionClass *conn; +QResultClass *res; +char ok, was_ok, was_nonfatal; +Int2 oldstatus, numcols; + + + conn = SC_get_conn(self); + + /* Begin a transaction if one is not already in progress */ + /* The reason is because we can't use declare/fetch cursors without + starting a transaction first. + */ + if ( ! CC_is_in_trans(conn) && (globals.use_declarefetch || STMT_UPDATE(self))) { + + mylog(" about to begin a transaction on statement = %u\n", self); + res = CC_send_query(conn, "BEGIN", NULL, NULL); + if ( ! res) { + self->errormsg = "Could not begin a transaction"; + self->errornumber = STMT_EXEC_ERROR; + return SQL_ERROR; + } + + ok = QR_command_successful(res); + + mylog("SQLExecute: ok = %d, status = %d\n", ok, QR_get_status(res)); + + QR_Destructor(res); + + if (!ok) { + self->errormsg = "Could not begin a transaction"; + self->errornumber = STMT_EXEC_ERROR; + return SQL_ERROR; + } + else + CC_set_in_trans(conn); + } + + + + oldstatus = conn->status; + conn->status = CONN_EXECUTING; + self->status = STMT_EXECUTING; + + + // If its a SELECT statement, use a cursor. + // Note that the declare cursor has already been prepended to the statement + // in copy_statement... + if (self->statement_type == STMT_TYPE_SELECT) { + + char fetch[128]; + + mylog(" Sending SELECT statement on stmt=%u, cursor_name='%s'\n", self, self->cursor_name); + + /* send the declare/select */ + self->result = CC_send_query(conn, self->stmt_with_params, NULL, NULL); + + if (globals.use_declarefetch && self->result != NULL) { + /* That worked, so now send the fetch to start getting data back */ + sprintf(fetch, "fetch %d in %s", globals.fetch_max, self->cursor_name); + + // Save the cursor in the result for later use + self->result = CC_send_query( conn, fetch, NULL, self->cursor_name); + } + + mylog(" done sending the query:\n"); + + + + } + else { // not a SELECT statement so don't use a cursor + mylog(" its NOT a select statement: stmt=%u\n", self); + self->result = CC_send_query(conn, self->stmt_with_params, NULL, NULL); + + // If we are in autocommit, we must send the commit. + if (CC_is_in_autocommit(conn) && STMT_UPDATE(self)) { + CC_send_query(conn, "COMMIT", NULL, NULL); + CC_set_no_trans(conn); + } + + } + + conn->status = oldstatus; + self->status = STMT_FINISHED; + + /* Check the status of the result */ + if (self->result) { + + was_ok = QR_command_successful(self->result); + was_nonfatal = QR_command_nonfatal(self->result); + + if ( was_ok) + self->errornumber = STMT_OK; + else + self->errornumber = was_nonfatal ? STMT_INFO_ONLY : STMT_ERROR_TAKEN_FROM_BACKEND; + + self->currTuple = -1; /* set cursor before the first tuple in the list */ + self->current_col = -1; + + /* see if the query did return any result columns */ + numcols = QR_NumResultCols(self->result); + + /* now allocate the array to hold the binding info */ + if (numcols > 0) { + extend_bindings(self, numcols); + if (self->bindings == NULL) { + self->errornumber = STMT_NO_MEMORY_ERROR; + self->errormsg = "Could not get enough free memory to store the binding information"; + return SQL_ERROR; + } + } + + } else { /* Bad Error -- The error message will be in the Connection */ + + if (self->statement_type == STMT_TYPE_CREATE) { + self->errornumber = STMT_CREATE_TABLE_ERROR; + self->errormsg = "Error creating the table"; + /* This would allow the table to already exists, thus appending + rows to it. BUT, if the table didn't have the same attributes, + it would fail. + return SQL_SUCCESS_WITH_INFO; + */ + } + else { + self->errornumber = STMT_EXEC_ERROR; + self->errormsg = "Error while executing the query"; + } + CC_abort(conn); + } + + if (self->errornumber == STMT_OK) + return SQL_SUCCESS; + + else if (self->errornumber == STMT_INFO_ONLY) + return SQL_SUCCESS_WITH_INFO; + + else + return SQL_ERROR; +} diff --git a/src/interfaces/odbc/statement.h b/src/interfaces/odbc/statement.h index cc8e103d84..24f58bf050 100644 --- a/src/interfaces/odbc/statement.h +++ b/src/interfaces/odbc/statement.h @@ -1,11 +1,11 @@ - -/* File: statement.h - * - * Description: See "statement.c" - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: statement.h + * + * Description: See "statement.c" + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __STATEMENT_H__ #define __STATEMENT_H__ @@ -45,18 +45,26 @@ typedef enum { #define STMT_RESTRICTED_DATA_TYPE_ERROR 14 #define STMT_INVALID_CURSOR_STATE_ERROR 15 #define STMT_OPTION_VALUE_CHANGED 16 - +#define STMT_CREATE_TABLE_ERROR 17 +#define STMT_NO_CURSOR_NAME 18 +#define STMT_INVALID_CURSOR_NAME 19 /* statement types */ -#define STMT_TYPE_SELECT 0 -#define STMT_TYPE_INSERT 1 -#define STMT_TYPE_UPDATE 2 -#define STMT_TYPE_DELETE 3 -#define STMT_TYPE_OTHER 4 -#define STMT_TYPE_UNKNOWN 666 // 'unknown' means we don't have the statement yet, - // or haven't looked at it to see what type it is. - // 'other' means we looked, but couldn't tell. +enum { + STMT_TYPE_UNKNOWN = -2, + STMT_TYPE_OTHER = -1, + STMT_TYPE_SELECT = 0, + STMT_TYPE_INSERT, + STMT_TYPE_UPDATE, + STMT_TYPE_DELETE, + STMT_TYPE_CREATE, + STMT_TYPE_ALTER, + STMT_TYPE_DROP, + STMT_TYPE_GRANT, + STMT_TYPE_REVOKE, +}; +#define STMT_UPDATE(stmt) (stmt->statement_type > STMT_TYPE_SELECT) /******** Statement Handle ***********/ struct StatementClass_ { @@ -68,6 +76,9 @@ struct StatementClass_ { char *errormsg; int errornumber; int maxRows; + int rowset_size; + int cursor_type; + int scroll_concurrency; /* information on bindings */ BindInfoClass *bindings; /* array to store the binding information */ @@ -78,30 +89,35 @@ struct StatementClass_ { ParameterInfoClass *parameters; Int4 currTuple; + int current_col; /* current column for GetData -- used to handle multiple calls */ + int lobj_fd; /* fd of the current large object */ char *statement; /* if non--null pointer to the SQL statement that has been executed */ - + int statement_type; /* According to the defines above */ - int data_at_exec; /* Number of params needing SQLPutData */ - int current_exec_param; /* The current parameter for SQLPutData */ - - char put_data; /* Has SQLPutData been called yet? */ + int data_at_exec; /* Number of params needing SQLPutData */ + int current_exec_param; /* The current parameter for SQLPutData */ + + char put_data; /* Has SQLPutData been called yet? */ char errormsg_created; /* has an informative error msg been created? */ char manual_result; /* Is the statement result manually built? */ - char prepare; /* is this statement a prepared statement or direct */ - - char stmt_with_params[65536 /* MAX_STATEMENT_LEN */]; /* statement after parameter substitution */ + char prepare; /* is this statement a prepared statement or direct */ + + char internal; /* Is this statement being called internally? */ + + char cursor_name[32]; + char stmt_with_params[65536 /* MAX_STATEMENT_LEN */]; /* statement after parameter substitution */ }; #define SC_get_conn(a) (a->hdbc) #define SC_get_Result(a) (a->result); - -/* options for SC_free_params() */ -#define STMT_FREE_PARAMS_ALL 0 -#define STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY 1 - + +/* options for SC_free_params() */ +#define STMT_FREE_PARAMS_ALL 0 +#define STMT_FREE_PARAMS_DATA_AT_EXEC_ONLY 1 + /* Statement prototypes */ StatementClass *SC_Constructor(); char SC_Destructor(StatementClass *self); @@ -113,7 +129,7 @@ char SC_recycle_statement(StatementClass *self); void SC_clear_error(StatementClass *self); char SC_get_error(StatementClass *self, int *number, char **message); char *SC_create_errormsg(StatementClass *self); -RETCODE SC_execute(StatementClass *stmt); -void SC_free_params(StatementClass *self, char option); +RETCODE SC_execute(StatementClass *stmt); +void SC_free_params(StatementClass *self, char option); #endif diff --git a/src/interfaces/odbc/tuple.c b/src/interfaces/odbc/tuple.c index 9b50ba5f3d..81e9cfe33b 100644 --- a/src/interfaces/odbc/tuple.c +++ b/src/interfaces/odbc/tuple.c @@ -1,19 +1,19 @@ - -/* Module: tuple.c - * - * Description: This module contains functions for setting the data for individual - * fields (TupleField structure) of a manual result set. - * - * Important Note: These functions are ONLY used in building manual result sets for - * info functions (SQLTables, SQLColumns, etc.) - * - * Classes: n/a - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: tuple.c + * + * Description: This module contains functions for setting the data for individual + * fields (TupleField structure) of a manual result set. + * + * Important Note: These functions are ONLY used in building manual result sets for + * info functions (SQLTables, SQLColumns, etc.) + * + * Classes: n/a + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include "tuple.h" #include diff --git a/src/interfaces/odbc/tuple.h b/src/interfaces/odbc/tuple.h index 3d581079df..585254edc8 100644 --- a/src/interfaces/odbc/tuple.h +++ b/src/interfaces/odbc/tuple.h @@ -1,27 +1,27 @@ - -/* File: tuple.h - * - * Description: See "tuple.c" - * - * Important NOTE: The TupleField structure is used both to hold backend data and - * manual result set data. The "set_" functions and the TupleNode - * structure are only used for manual result sets by info routines. - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: tuple.h + * + * Description: See "tuple.c" + * + * Important NOTE: The TupleField structure is used both to hold backend data and + * manual result set data. The "set_" functions and the TupleNode + * structure are only used for manual result sets by info routines. + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __TUPLE_H__ #define __TUPLE_H__ #include "psqlodbc.h" - + /* Used by backend data AND manual result sets */ struct TupleField_ { Int4 len; /* length of the current Tuple */ void *value; /* an array representing the value */ }; - + /* Used ONLY for manual result sets */ struct TupleNode_ { struct TupleNode_ *prev, *next; diff --git a/src/interfaces/odbc/tuplelist.c b/src/interfaces/odbc/tuplelist.c index 80b962482f..7b2747450e 100644 --- a/src/interfaces/odbc/tuplelist.c +++ b/src/interfaces/odbc/tuplelist.c @@ -1,16 +1,16 @@ - -/* Module: tuplelist.c - * - * Description: This module contains functions for creating a manual result set - * (the TupleList) and retrieving data from it for a specific row/column. - * - * Classes: TupleListClass (Functions prefix: "TL_") - * - * API functions: none - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* Module: tuplelist.c + * + * Description: This module contains functions for creating a manual result set + * (the TupleList) and retrieving data from it for a specific row/column. + * + * Classes: TupleListClass (Functions prefix: "TL_") + * + * API functions: none + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #include #include @@ -21,8 +21,8 @@ TupleListClass * TL_Constructor(UInt4 fieldcnt) { TupleListClass *rv; - - mylog("in TL_Constructor\n"); + + mylog("in TL_Constructor\n"); rv = (TupleListClass *) malloc(sizeof(TupleListClass)); if (rv) { @@ -35,8 +35,8 @@ TupleListClass *rv; rv->last_indexed = -1; } - mylog("exit TL_Constructor\n"); - + mylog("exit TL_Constructor\n"); + return rv; } @@ -46,8 +46,8 @@ TL_Destructor(TupleListClass *self) int lf; TupleNode *node, *tp; - mylog("TupleList: in DESTRUCTOR\n"); - + mylog("TupleList: in DESTRUCTOR\n"); + node = self->list_start; while(node != NULL) { for (lf=0; lf < self->num_fields; lf++) @@ -57,11 +57,11 @@ TupleNode *node, *tp; tp = node->next; free(node); node = tp; - } - - free(self); + } - mylog("TupleList: exit DESTRUCTOR\n"); + free(self); + + mylog("TupleList: exit DESTRUCTOR\n"); } diff --git a/src/interfaces/odbc/tuplelist.h b/src/interfaces/odbc/tuplelist.h index 5097cc6fdc..0f0bdb42ff 100644 --- a/src/interfaces/odbc/tuplelist.h +++ b/src/interfaces/odbc/tuplelist.h @@ -1,14 +1,14 @@ - -/* File: tuplelist.h - * - * Description: See "tuplelist.c" - * - * Important Note: This structure and its functions are ONLY used in building manual result - * sets for info functions (SQLTables, SQLColumns, etc.) - * - * Comments: See "notice.txt" for copyright and license information. - * - */ + +/* File: tuplelist.h + * + * Description: See "tuplelist.c" + * + * Important Note: This structure and its functions are ONLY used in building manual result + * sets for info functions (SQLTables, SQLColumns, etc.) + * + * Comments: See "notice.txt" for copyright and license information. + * + */ #ifndef __TUPLELIST_H__ #define __TUPLELIST_H__