OSDN Git Service

Change SQLPrimaryKeys() so that it detects the primary key
authorHiroshi Inoue <inoue@tpf.co.jp>
Fri, 22 Jun 2001 05:59:43 +0000 (05:59 +0000)
committerHiroshi Inoue <inoue@tpf.co.jp>
Fri, 22 Jun 2001 05:59:43 +0000 (05:59 +0000)
other than tablename_pkey.

src/interfaces/odbc/bind.c
src/interfaces/odbc/drvconn.c
src/interfaces/odbc/execute.c
src/interfaces/odbc/info.c
src/interfaces/odbc/results.c
src/interfaces/odbc/statement.c

index fc1f4b5..906dfdc 100644 (file)
@@ -59,6 +59,7 @@ SQLBindParameter(
                SC_log_error(func, "", NULL);
                return SQL_INVALID_HANDLE;
        }
+       SC_clear_error(stmt);
 
        if (stmt->parameters_allocated < ipar)
        {
@@ -292,6 +293,7 @@ SQLDescribeParam(
                SC_log_error(func, "", NULL);
                return SQL_INVALID_HANDLE;
        }
+       SC_clear_error(stmt);
 
        if ((ipar < 1) || (ipar > stmt->parameters_allocated))
        {
@@ -366,6 +368,7 @@ SQLNumParams(
                SC_log_error(func, "", NULL);
                return SQL_INVALID_HANDLE;
        }
+       SC_clear_error(stmt);
 
        if (pcpar)
                *pcpar = 0;
index ea4c624..764424a 100644 (file)
@@ -233,7 +233,7 @@ dialog:
                {
                        result = SQL_SUCCESS_WITH_INFO;
                        conn->errornumber = CONN_TRUNCATED;
-                       conn->errormsg = "The buffer was too small for the result.";
+                       conn->errormsg = "The buffer was too small for the ConnStrOut.";
                }
        }
 
index ca9b211..fb64e8f 100644 (file)
@@ -545,7 +545,7 @@ SQLNativeSql(
                {
                        result = SQL_SUCCESS_WITH_INFO;
                        conn->errornumber = STMT_TRUNCATED;
-                       conn->errormsg = "The buffer was too small for the result.";
+                       conn->errormsg = "The buffer was too small for the NativeSQL.";
                }
        }
 
index 173a159..39a0a49 100644 (file)
@@ -677,7 +677,7 @@ SQLGetInfo(
                        {
                                result = SQL_SUCCESS_WITH_INFO;
                                conn->errornumber = STMT_TRUNCATED;
-                               conn->errormsg = "The buffer was too small for the result.";
+                               conn->errormsg = "The buffer was too small for tthe InfoValue.";
                        }
                }
        }
@@ -2441,6 +2441,7 @@ SQLPrimaryKeys(
 {
        static char *func = "SQLPrimaryKeys";
        StatementClass *stmt = (StatementClass *) hstmt;
+       ConnectionClass *conn;
        TupleNode  *row;
        RETCODE         result;
        int                     seq = 0;
@@ -2451,6 +2452,7 @@ SQLPrimaryKeys(
        SDWORD          attname_len;
        char            pktab[MAX_TABLE_LEN + 1];
        Int2            result_cols;
+       int             qno, qstart, qend;
 
        mylog("%s: entering...stmt=%u\n", func, stmt);
 
@@ -2511,37 +2513,6 @@ SQLPrimaryKeys(
                return SQL_ERROR;
        }
 
-#if 0
-       sprintf(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", pktab);
-#else
-
-       /*
-        * Simplified query to remove assumptions about number of possible
-        * index columns. Courtesy of Tom Lane - thomas 2000-03-21
-        */
-       sprintf(tables_query, "select ta.attname, ia.attnum"
-                " from pg_attribute ta, pg_attribute ia, pg_class c, pg_index i"
-                       " where c.relname = '%s_pkey'"
-                       " AND c.oid = i.indexrelid"
-                       " AND ia.attrelid = i.indexrelid"
-                       " AND ta.attrelid = i.indrelid"
-                       " AND ta.attnum = i.indkey[ia.attnum-1]"
-                       " order by ia.attnum", pktab);
-#endif
-
-
-       mylog("SQLPrimaryKeys: tables_query='%s'\n", tables_query);
-
-       result = SQLExecDirect(htbl_stmt, tables_query, strlen(tables_query));
-       if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
-       {
-               stmt->errormsg = SC_create_errormsg(htbl_stmt);
-               stmt->errornumber = tbl_stmt->errornumber;
-               SC_log_error(func, "", stmt);
-               SQLFreeStmt(htbl_stmt, SQL_DROP);
-               return SQL_ERROR;
-       }
-
        result = SQLBindCol(htbl_stmt, 1, SQL_C_CHAR,
                                                attname, MAX_INFO_STRING, &attname_len);
        if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
@@ -2553,7 +2524,61 @@ SQLPrimaryKeys(
                return SQL_ERROR;
        }
 
-       result = SQLFetch(htbl_stmt);
+       conn = (ConnectionClass *) (stmt->hdbc);
+       if (PG_VERSION_LE(conn, 6.4))
+               qstart = 2;
+       else
+               qstart = 1;
+       qend = 2;
+       for (qno = qstart; qno <= qend; qno++)
+       {
+               switch (qno)
+               {
+                       case 1:
+                               /*
+                                * Simplified query to remove assumptions about number of possible
+                                * index columns. Courtesy of Tom Lane - thomas 2000-03-21
+                                */
+                               sprintf(tables_query, "select ta.attname, ia.attnum"
+                               " from pg_attribute ta, pg_attribute ia, pg_class c, pg_index i"
+                               " where c.relname = '%s'"
+                               " AND c.oid = i.indrelid"
+                               " AND i.indisprimary = 't'"
+                               " AND ia.attrelid = i.indexrelid"
+                               " AND ta.attrelid = i.indrelid"
+                               " AND ta.attnum = i.indkey[ia.attnum-1]"
+                               " order by ia.attnum", pktab);
+                               break;
+                       case 2:
+                               /*
+                                * Simplified query to search old fashoned primary key
+                                */
+                               sprintf(tables_query, "select ta.attname, ia.attnum"
+                               " from pg_attribute ta, pg_attribute ia, pg_class c, pg_index i"
+                               " where c.relname = '%s_pkey'"
+                               " AND c.oid = i.indexrelid"
+                               " AND ia.attrelid = i.indexrelid"
+                               " AND ta.attrelid = i.indrelid"
+                               " AND ta.attnum = i.indkey[ia.attnum-1]"
+                               " order by ia.attnum", pktab);
+                               break;
+               }
+               mylog("SQLPrimaryKeys: tables_query='%s'\n", tables_query);
+
+               result = SQLExecDirect(htbl_stmt, tables_query, strlen(tables_query));
+               if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
+               {
+                       stmt->errormsg = SC_create_errormsg(htbl_stmt);
+                       stmt->errornumber = tbl_stmt->errornumber;
+                       SC_log_error(func, "", stmt);
+                       SQLFreeStmt(htbl_stmt, SQL_DROP);
+                       return SQL_ERROR;
+               }
+
+               result = SQLFetch(htbl_stmt);
+               if (result != SQL_NO_DATA_FOUND)
+                       break;
+       }
 
        while ((result == SQL_SUCCESS) || (result == SQL_SUCCESS_WITH_INFO))
        {
index 56bca06..d861a33 100644 (file)
@@ -309,7 +309,7 @@ SQLDescribeCol(
                {
                        result = SQL_SUCCESS_WITH_INFO;
                        stmt->errornumber = STMT_TRUNCATED;
-                       stmt->errormsg = "The buffer was too small for the result.";
+                       stmt->errormsg = "The buffer was too small for the colName.";
                }
        }
 
@@ -619,7 +619,7 @@ SQLColAttributes(
                        {
                                result = SQL_SUCCESS_WITH_INFO;
                                stmt->errornumber = STMT_TRUNCATED;
-                               stmt->errormsg = "The buffer was too small for the result.";
+                               stmt->errormsg = "The buffer was too small for the rgbDesc.";
                        }
                }
 
@@ -799,7 +799,7 @@ SQLGetData(
 
                case COPY_RESULT_TRUNCATED:
                        stmt->errornumber = STMT_TRUNCATED;
-                       stmt->errormsg = "The buffer was too small for the result.";
+                       stmt->errormsg = "The buffer was too small for the GetData.";
                        return SQL_SUCCESS_WITH_INFO;
 
                case COPY_GENERAL_ERROR:                /* error msg already filled in */
@@ -1352,7 +1352,7 @@ SQLGetCursorName(
                {
                        result = SQL_SUCCESS_WITH_INFO;
                        stmt->errornumber = STMT_TRUNCATED;
-                       stmt->errormsg = "The buffer was too small for the result.";
+                       stmt->errormsg = "The buffer was too small for the GetCursorName.";
                }
        }
 
index fd77eee..473d570 100644 (file)
@@ -153,6 +153,7 @@ SQLFreeStmt(HSTMT hstmt,
                SC_log_error(func, "", NULL);
                return SQL_INVALID_HANDLE;
        }
+       SC_clear_error(stmt);
 
        if (fOption == SQL_DROP)
        {
@@ -299,6 +300,7 @@ char
 SC_Destructor(StatementClass *self)
 {
        mylog("SC_Destructor: self=%u, self->result=%u, self->hdbc=%u\n", self, self->result, self->hdbc);
+       SC_clear_error(self);
        if (STMT_EXECUTING == self->status)
        {
                self->errornumber = STMT_SEQUENCE_ERROR;
@@ -438,6 +440,7 @@ SC_recycle_statement(StatementClass *self)
 
        mylog("recycle statement: self= %u\n", self);
 
+       SC_clear_error(self);
        /* This would not happen */
        if (self->status == STMT_EXECUTING)
        {
@@ -446,10 +449,6 @@ SC_recycle_statement(StatementClass *self)
                return FALSE;
        }
 
-       self->errormsg = NULL;
-       self->errornumber = 0;
-       self->errormsg_created = FALSE;
-
        switch (self->status)
        {
                case STMT_ALLOCATED:
@@ -836,7 +835,10 @@ SC_fetch(StatementClass *self)
 
                                case COPY_RESULT_TRUNCATED:
                                        self->errornumber = STMT_TRUNCATED;
-                                       self->errormsg = "The buffer was too small for the result.";
+                                       self->errormsg = "Fetched item was truncated.";
+                                       qlog("The %dth item was truncated\n", lf + 1);   
+                                       qlog("The buffer size = %d", self->bindings[lf].buflen);
+                                       qlog(" and the value is '%s'\n", value);
                                        result = SQL_SUCCESS_WITH_INFO;
                                        break;