Changeset: f13de6e6d100 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=f13de6e6d100
Modified Files:
        clients/odbc/driver/SQLColumnPrivileges.c
        clients/odbc/driver/SQLColumns.c
        clients/odbc/driver/SQLExecDirect.c
        clients/odbc/driver/SQLForeignKeys.c
        clients/odbc/driver/SQLPrimaryKeys.c
        clients/odbc/driver/SQLProcedureColumns.c
        clients/odbc/driver/SQLProcedures.c
        clients/odbc/driver/SQLSpecialColumns.c
        clients/odbc/driver/SQLStatistics.c
        clients/odbc/driver/SQLTablePrivileges.c
        clients/odbc/driver/SQLTables.c
Branch: default
Log Message:

Simplify and optimize the ODBC data dictionary queries by removing the 
selection "from sys.env() where e.name = 'gdk_dbname'" parts.
The gdk_dbname is static once connected to a MonetDB server. It is also 
retrieved and stored in the Dbc object at connection time.
So we can use the stored Dbc->dbname instead for all places where a catalog 
name needs to be returned.
Actually we do not support catalog qualifiers (only schema and table 
qualifiers) at all, but for now we keep returning the dbname.


diffs (truncated from 1374 to 300 lines):

diff --git a/clients/odbc/driver/SQLColumnPrivileges.c 
b/clients/odbc/driver/SQLColumnPrivileges.c
--- a/clients/odbc/driver/SQLColumnPrivileges.c
+++ b/clients/odbc/driver/SQLColumnPrivileges.c
@@ -43,10 +43,10 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
        RETCODE rc;
        char *query = NULL;
        char *query_end = NULL;
-       char *cat = NULL, *sch = NULL, *tab = NULL, *col = NULL;
+       char *sch = NULL, *tab = NULL, *col = NULL;
 
-       fixODBCstring(CatalogName, NameLength1, SQLSMALLINT
-                     , addStmtError, stmt, return SQL_ERROR);
+       fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
+                     addStmtError, stmt, return SQL_ERROR);
        fixODBCstring(SchemaName, NameLength2, SQLSMALLINT,
                      addStmtError, stmt, return SQL_ERROR);
        fixODBCstring(TableName, NameLength3, SQLSMALLINT,
@@ -56,20 +56,13 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
 
 #ifdef ODBCDEBUG
        ODBCLOG(" \"%.*s\" \"%.*s\" \"%.*s\" \"%.*s\"\n",
-               (int) NameLength1, (char *) CatalogName,
-               (int) NameLength2, (char *) SchemaName,
-               (int) NameLength3, (char *) TableName,
-               (int) NameLength4, (char *) ColumnName);
+               (int) NameLength1, CatalogName ? (char *) CatalogName : "",
+               (int) NameLength2, SchemaName ? (char *) SchemaName : "",
+               (int) NameLength3, TableName ? (char *) TableName : "",
+               (int) NameLength4, ColumnName ? (char *) ColumnName : "");
 #endif
 
        if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
-               if (NameLength1 > 0) {
-                       cat = ODBCParseOA("e", "value",
-                                         (const char *) CatalogName,
-                                         (size_t) NameLength1);
-                       if (cat == NULL)
-                               goto nomem;
-               }
                if (NameLength2 > 0) {
                        sch = ODBCParseOA("s", "name",
                                          (const char *) SchemaName,
@@ -92,13 +85,6 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
                                goto nomem;
                }
        } else {
-               if (NameLength1 > 0) {
-                       cat = ODBCParseID("e", "value",
-                                         (const char *) CatalogName,
-                                         (size_t) NameLength1);
-                       if (cat == NULL)
-                               goto nomem;
-               }
                if (NameLength2 > 0) {
                        sch = ODBCParseID("s", "name",
                                          (const char *) SchemaName,
@@ -123,9 +109,8 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
        }
 
        /* construct the query now */
-       query = malloc(1200 + (cat ? strlen(cat) : 0) +
-                      (sch ? strlen(sch) : 0) + (tab ? strlen(tab) : 0) +
-                      (col ? strlen(col) : 0));
+       query = malloc(1200 + strlen(stmt->Dbc->dbname) + (sch ? strlen(sch) : 
0) +
+                       (tab ? strlen(tab) : 0) + (col ? strlen(col) : 0));
        if (query == NULL)
                goto nomem;
        query_end = query;
@@ -142,7 +127,7 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
         */
 
        sprintf(query_end,
-               "select e.value as table_cat, "
+               "select '%s' as table_cat, "
                       "s.name as table_schem, "
                       "t.name as table_name, "
                       "c.name as column_name, "
@@ -166,7 +151,6 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
                     "sys.auths as a, "
                     "sys.privileges as p, "
                     "sys.auths as g, "
-                    "sys.env() as e, "
                     "%s "
                "where p.obj_id = c.id and "
                      "c.table_id = t.id and "
@@ -174,8 +158,8 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
                      "t.schema_id = s.id and "
                      "not t.system and "
                      "p.grantor = g.id and "
-                     "e.name = 'gdk_dbname' and "
                      "p.privileges = pc.privilege_code_id",
+               stmt->Dbc->dbname,
                /* a server that supports sys.comments also supports
                 * sys.privilege_codes */
                stmt->Dbc->has_comment ? "sys.privilege_codes as pc" :
@@ -189,11 +173,13 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
        query_end += strlen(query_end);
 
        /* Construct the selection condition query part */
-       if (cat) {
+       if (NameLength1 > 0 && CatalogName != NULL) {
                /* filtering requested on catalog name */
-               sprintf(query_end, " and %s", cat);
-               query_end += strlen(query_end);
-               free(cat);
+               if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
+                       /* catalog name does not match the database name, so 
return no rows */
+                       sprintf(query_end, " and 1=2");
+                       query_end += strlen(query_end);
+               }
        }
        if (sch) {
                /* filtering requested on schema name */
@@ -214,15 +200,12 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
                free(col);
        }
 
-       /* add the ordering */
-       strcpy(query_end,
-              " order by table_cat, table_schem, table_name, "
-              "column_name, privilege");
+       /* add the ordering (exclude table_cat as it is the same for all rows) 
*/
+       strcpy(query_end, " order by table_schem, table_name, column_name, 
privilege");
        query_end += strlen(query_end);
 
        /* query the MonetDB data dictionary tables */
-       rc = MNDBExecDirect(stmt, (SQLCHAR *) query,
-                           (SQLINTEGER) (query_end - query));
+       rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) (query_end - 
query));
 
        free(query);
 
@@ -230,8 +213,6 @@ MNDBColumnPrivileges(ODBCStmt *stmt,
 
   nomem:
        /* note that query must be NULL when we get here */
-       if (cat)
-               free(cat);
        if (sch)
                free(sch);
        if (tab)
diff --git a/clients/odbc/driver/SQLColumns.c b/clients/odbc/driver/SQLColumns.c
--- a/clients/odbc/driver/SQLColumns.c
+++ b/clients/odbc/driver/SQLColumns.c
@@ -48,7 +48,7 @@ MNDBColumns(ODBCStmt *stmt,
        /* buffer for the constructed query to do meta data retrieval */
        char *query = NULL;
        char *query_end = NULL;
-       char *cat = NULL, *sch = NULL, *tab = NULL, *col = NULL;
+       char *sch = NULL, *tab = NULL, *col = NULL;
 
        /* null pointers not allowed if arguments are identifiers */
        if (stmt->Dbc->sql_attr_metadata_id == SQL_TRUE &&
@@ -68,20 +68,13 @@ MNDBColumns(ODBCStmt *stmt,
 
 #ifdef ODBCDEBUG
        ODBCLOG(" \"%.*s\" \"%.*s\" \"%.*s\" \"%.*s\"\n",
-               (int) NameLength1, (char *) CatalogName,
-               (int) NameLength2, (char *) SchemaName,
-               (int) NameLength3, (char *) TableName,
-               (int) NameLength4, (char *) ColumnName);
+               (int) NameLength1, CatalogName ? (char *) CatalogName : "",
+               (int) NameLength2, SchemaName ? (char *) SchemaName : "",
+               (int) NameLength3, TableName ? (char *) TableName : "",
+               (int) NameLength4, ColumnName ? (char *) ColumnName : "");
 #endif
 
        if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
-               if (NameLength1 > 0) {
-                       cat = ODBCParseOA("e", "value",
-                                         (const char *) CatalogName,
-                                         (size_t) NameLength1);
-                       if (cat == NULL)
-                               goto nomem;
-               }
                if (NameLength2 > 0) {
                        sch = ODBCParsePV("s", "name",
                                          (const char *) SchemaName,
@@ -104,13 +97,6 @@ MNDBColumns(ODBCStmt *stmt,
                                goto nomem;
                }
        } else {
-               if (NameLength1 > 0) {
-                       cat = ODBCParseID("e", "value",
-                                         (const char *) CatalogName,
-                                         (size_t) NameLength1);
-                       if (cat == NULL)
-                               goto nomem;
-               }
                if (NameLength2 > 0) {
                        sch = ODBCParseID("s", "name",
                                          (const char *) SchemaName,
@@ -135,9 +121,8 @@ MNDBColumns(ODBCStmt *stmt,
        }
 
        /* construct the query now */
-       query = malloc(6500 + (cat ? strlen(cat) : 0) +
-                      (sch ? strlen(sch) : 0) + (tab ? strlen(tab) : 0) +
-                      (col ? strlen(col) : 0));
+       query = malloc(6500 + strlen(stmt->Dbc->dbname) + (sch ? strlen(sch) : 
0) +
+                       (tab ? strlen(tab) : 0) + (col ? strlen(col) : 0));
        if (query == NULL)
                goto nomem;
        query_end = query;
@@ -164,7 +149,7 @@ MNDBColumns(ODBCStmt *stmt,
         */
 
        sprintf(query_end,
-               "select e.value as table_cat, "
+               "select '%s' as table_cat, "
                       "s.name as table_schem, "
                       "t.name as table_name, "
                       "c.name as column_name, "
@@ -190,11 +175,10 @@ MNDBColumns(ODBCStmt *stmt,
                       "end as is_nullable "
                 "from sys.schemas s, "
                      "sys.tables t, "
-                     "sys.columns c%s, "
-                     "sys.env() e "
+                     "sys.columns c%s "
                 "where s.id = t.schema_id and "
-                      "t.id = c.table_id and "
-                      "e.name = 'gdk_dbname'",
+                      "t.id = c.table_id",
+               stmt->Dbc->dbname,
 #ifdef DATA_TYPE_ARGS
                DATA_TYPE_ARGS,
 #endif
@@ -235,11 +219,13 @@ MNDBColumns(ODBCStmt *stmt,
           variable selection condition dynamically */
 
        /* Construct the selection condition query part */
-       if (cat) {
+       if (NameLength1 > 0 && CatalogName != NULL) {
                /* filtering requested on catalog name */
-               sprintf(query_end, " and %s", cat);
-               query_end += strlen(query_end);
-               free(cat);
+               if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
+                       /* catalog name does not match the database name, so 
return no rows */
+                       sprintf(query_end, " and 1=2");
+                       query_end += strlen(query_end);
+               }
        }
        if (sch) {
                /* filtering requested on schema name */
@@ -260,15 +246,12 @@ MNDBColumns(ODBCStmt *stmt,
                free(col);
        }
 
-       /* add the ordering */
-       strcpy(query_end,
-              " order by table_cat, table_schem, "
-              "table_name, ordinal_position");
+       /* add the ordering (exclude table_cat as it is the same for all rows) 
*/
+       strcpy(query_end, " order by table_schem, table_name, 
ordinal_position");
        query_end += strlen(query_end);
 
        /* query the MonetDB data dictionary tables */
-       rc = MNDBExecDirect(stmt, (SQLCHAR *) query,
-                           (SQLINTEGER) (query_end - query));
+       rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) (query_end - 
query));
 
        free(query);
 
@@ -276,8 +259,6 @@ MNDBColumns(ODBCStmt *stmt,
 
   nomem:
        /* note that query must be NULL when we get here */
-       if (cat)
-               free(cat);
        if (sch)
                free(sch);
        if (tab)
diff --git a/clients/odbc/driver/SQLExecDirect.c 
b/clients/odbc/driver/SQLExecDirect.c
--- a/clients/odbc/driver/SQLExecDirect.c
+++ b/clients/odbc/driver/SQLExecDirect.c
@@ -130,6 +130,8 @@ MNDBExecDirect(ODBCStmt *stmt,
        fixODBCstring(StatementText, TextLength, SQLINTEGER,
                      addStmtError, stmt, return SQL_ERROR);
        for (i = 0; i < TextLength; i++)
+               /* TODO FIX: only when the statement starts with PREPARE the
+                  questions marks have a special meaning */
                if (StatementText[i] == '?') {
                        /* query may have parameters, take the long route */
                        ret = MNDBPrepare(stmt, StatementText, TextLength);
diff --git a/clients/odbc/driver/SQLForeignKeys.c 
b/clients/odbc/driver/SQLForeignKeys.c
--- a/clients/odbc/driver/SQLForeignKeys.c
+++ b/clients/odbc/driver/SQLForeignKeys.c
@@ -49,8 +49,8 @@ MNDBForeignKeys(ODBCStmt *stmt,
        /* buffer for the constructed query to do meta data retrieval */
        char *query = NULL;
        char *query_end = NULL; /* pointer to end of built-up query */
-       char *pcat = NULL, *psch = NULL, *ptab = NULL;
-       char *fcat = NULL, *fsch = NULL, *ftab = NULL;
+       char *psch = NULL, *ptab = NULL;
+       char *fsch = NULL, *ftab = NULL;
 
        /* deal with SQL_NTS and SQL_NULL_DATA */
        fixODBCstring(PKCatalogName, NameLength1, SQLSMALLINT,
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to