Changeset: d5a59b501e7e for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d5a59b501e7e
Modified Files:
        clients/odbc/ChangeLog
        clients/odbc/driver/SQLPrimaryKeys.c
        clients/odbc/driver/SQLSpecialColumns.c
        clients/odbc/driver/SQLStatistics.c
Branch: default
Log Message:

Corrected ODBC API functions SQLPrimaryKeys(), SQLSpecialColumns() and 
SQLStatistics() for local temporary tables located in schema tmp. They did not 
return any rows when the temp table had a primary or unique key or index.


diffs (191 lines):

diff --git a/clients/odbc/ChangeLog b/clients/odbc/ChangeLog
--- a/clients/odbc/ChangeLog
+++ b/clients/odbc/ChangeLog
@@ -1,3 +1,9 @@
 # ChangeLog file for odbc
 # This file is updated with Maddlog
 
+* Thu Apr 21 2022 Martin van Dinther <[email protected]>
+- Corrected ODBC API functions SQLPrimaryKeys(), SQLSpecialColumns() and
+  SQLStatistics() for local temporary tables located in schema tmp. They did
+  not return any rows when the temp table had a primary or unique key or
+  index. Now they do return rows as expected.
+
diff --git a/clients/odbc/driver/SQLPrimaryKeys.c 
b/clients/odbc/driver/SQLPrimaryKeys.c
--- a/clients/odbc/driver/SQLPrimaryKeys.c
+++ b/clients/odbc/driver/SQLPrimaryKeys.c
@@ -45,6 +45,7 @@ MNDBPrimaryKeys(ODBCStmt *stmt,
        size_t querylen;
        size_t pos = 0;
        char *sch = NULL, *tab = NULL;
+       char *sysORtmp = "sys";
 
        /* deal with SQL_NTS and SQL_NULL_DATA */
        fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
@@ -60,6 +61,12 @@ MNDBPrimaryKeys(ODBCStmt *stmt,
                addStmtError(stmt, "HY009", NULL, 0);
                return SQL_ERROR;
        }
+       if (NameLength3 == 0) {
+               /* Invalid string or buffer length */
+               addStmtError(stmt, "HY090", NULL, 0);
+               return SQL_ERROR;
+       }
+
 #ifdef ODBCDEBUG
        ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
                (int) NameLength1, CatalogName ? (char *) CatalogName : "",
@@ -106,6 +113,9 @@ MNDBPrimaryKeys(ODBCStmt *stmt,
        if (query == NULL)
                goto nomem;
 
+       if (SchemaName != NULL && strcmp((const char *) SchemaName, "tmp") == 0)
+               sysORtmp = "tmp";
+
        /* SQLPrimaryKeys returns a table with the following columns:
           VARCHAR      table_cat
           VARCHAR      table_schem
@@ -121,13 +131,14 @@ MNDBPrimaryKeys(ODBCStmt *stmt,
                      "kc.name as column_name, "
                      "cast(kc.nr + 1 as smallint) as key_seq, "
                      "k.name as pk_name "
-              "from sys.schemas s, sys.tables t, "
-                   "sys.keys k, sys.objects kc "
+              "from sys.schemas s, %s._tables t, "
+                   "%s.keys k, %s.objects kc "
               "where k.id = kc.id and "
                     "k.table_id = t.id and "
                     "t.schema_id = s.id and "
                     "k.type = 0",
-               stmt->Dbc->dbname);
+               stmt->Dbc->dbname,
+               sysORtmp, sysORtmp, sysORtmp);
        assert(pos < 800);
 
        /* Construct the selection condition query part */
diff --git a/clients/odbc/driver/SQLSpecialColumns.c 
b/clients/odbc/driver/SQLSpecialColumns.c
--- a/clients/odbc/driver/SQLSpecialColumns.c
+++ b/clients/odbc/driver/SQLSpecialColumns.c
@@ -95,6 +95,7 @@ MNDBSpecialColumns(ODBCStmt *stmt,
        size_t querylen;
        size_t pos = 0;
        char *sch = NULL, *tab = NULL;
+       char *sysORtmp = "sys";
 
        fixODBCstring(CatalogName, NameLength1, SQLSMALLINT, addStmtError, 
stmt, return SQL_ERROR);
        fixODBCstring(SchemaName, NameLength2, SQLSMALLINT, addStmtError, stmt, 
return SQL_ERROR);
@@ -204,6 +205,9 @@ MNDBSpecialColumns(ODBCStmt *stmt,
                if (query == NULL)
                        goto nomem;
 
+               if (SchemaName != NULL && strcmp((const char *) SchemaName, 
"tmp") == 0)
+                       sysORtmp = "tmp";
+
                /* Note: SCOPE is SQL_SCOPE_TRANSACTION */
                /* Note: PSEUDO_COLUMN is SQL_PC_NOT_PSEUDO */
                pos += snprintf(query + pos, querylen - pos,
@@ -218,10 +222,10 @@ MNDBSpecialColumns(ODBCStmt *stmt,
                        DECIMAL_DIGITS(c) ", "
                               "cast(%d as smallint) as pseudo_column "
                         "from sys.schemas s, "
-                             "sys.tables t, "
-                             "sys.columns c, "
-                             "sys.keys k, "
-                             "sys.objects kc "
+                             "%s._tables t, "
+                             "%s._columns c, "
+                             "%s.keys k, "
+                             "%s.objects kc "
                         "where s.id = t.schema_id and "
                               "t.id = c.table_id and "
                               "t.id = k.table_id and "
@@ -246,7 +250,8 @@ MNDBSpecialColumns(ODBCStmt *stmt,
                        DECIMAL_DIGITS_ARGS,
 #endif
                        /* pseudo_column: */
-                       SQL_PC_NOT_PSEUDO);
+                       SQL_PC_NOT_PSEUDO,
+                       sysORtmp, sysORtmp, sysORtmp, sysORtmp);
                assert(pos < 4300);
                /* TODO: improve the SQL to get the correct result:
                   - only one set of columns should be returned, also
@@ -285,11 +290,11 @@ MNDBSpecialColumns(ODBCStmt *stmt,
                        pos += strcpy_len(query + pos, " and c.\"null\" = 
false", querylen - pos);
                }
 
-               pos += strcpy_len(query + pos,
+               pos += snprintf(query + pos, querylen - pos,
                       "), "
                        "tid as ("
                           "select t.id as tid "
-                           "from sys._tables t, sys.keys k "
+                           "from %s._tables t, %s.keys k "
                            "where t.id = k.table_id and k.type = 0"
                       ") "
                        "select sc.scope, sc.column_name, sc.data_type, "
@@ -301,7 +306,7 @@ MNDBSpecialColumns(ODBCStmt *stmt,
                               "sc.table_id in (select tid from tid)) or "
                              "(sc.type = 1 and "
                               "sc.table_id not in (select tid from tid))",
-                       querylen - pos);
+                       sysORtmp, sysORtmp);
 
                /* ordering on SCOPE not needed (since it is constant) */
        } else {
diff --git a/clients/odbc/driver/SQLStatistics.c 
b/clients/odbc/driver/SQLStatistics.c
--- a/clients/odbc/driver/SQLStatistics.c
+++ b/clients/odbc/driver/SQLStatistics.c
@@ -76,6 +76,7 @@ MNDBStatistics(ODBCStmt *stmt,
        size_t querylen;
        size_t pos = 0;
        char *sch = NULL, *tab = NULL;
+       char *sysORtmp = "sys";
 
        fixODBCstring(TableName, NameLength3, SQLSMALLINT,
                      addStmtError, stmt, return SQL_ERROR);
@@ -114,7 +115,6 @@ MNDBStatistics(ODBCStmt *stmt,
                return SQL_ERROR;
        }
 
-
        /* check if a valid (non null, not empty) table name is supplied */
        if (TableName == NULL) {
                /* Invalid use of null pointer */
@@ -166,6 +166,9 @@ MNDBStatistics(ODBCStmt *stmt,
        if (query == NULL)
                goto nomem;
 
+       if (SchemaName != NULL && strcmp((const char *) SchemaName, "tmp") == 0)
+               sysORtmp = "tmp";
+
        /* SQLStatistics returns a table with the following columns:
           VARCHAR      table_cat
           VARCHAR      table_schem
@@ -198,12 +201,12 @@ MNDBStatistics(ODBCStmt *stmt,
                       "cast(null as integer) as cardinality, "
                       "cast(null as integer) as pages, "
                       "cast(null as varchar(1)) as filter_condition "
-               "from sys.idxs i, "
+               "from %s.idxs i, "
                     "sys.schemas s, "
-                    "sys.tables t, "
-                    "sys.columns c, "
-                    "sys.objects kc, "
-                    "sys.keys k "
+                    "%s._tables t, "
+                    "%s._columns c, "
+                    "%s.objects kc, "
+                    "%s.keys k "
                "where i.table_id = t.id and "
                      "t.schema_id = s.id and "
                      "i.id = kc.id and "
@@ -212,7 +215,8 @@ MNDBStatistics(ODBCStmt *stmt,
                      "k.name = i.name and "
                      "k.type in (0, 1)",
                stmt->Dbc->dbname,
-               SQL_INDEX_HASHED, SQL_INDEX_OTHER);
+               SQL_INDEX_HASHED, SQL_INDEX_OTHER,
+               sysORtmp, sysORtmp, sysORtmp, sysORtmp, sysORtmp);
        assert(pos < 1000);
 
        /* Construct the selection condition query part */
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to