LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLPrimaryKeys.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 71 0.0 %
Date: 2021-10-13 02:24:04 Functions: 0 1 0.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * This code was created by Peter Harvey (mostly during Christmas 98/99).
      11             :  * This code is LGPL. Please ensure that this message remains in future
      12             :  * distributions and uses of this code (thats about all I get out of it).
      13             :  * - Peter Harvey pharvey@codebydesign.com
      14             :  *
      15             :  * This file has been modified for the MonetDB project.  See the file
      16             :  * Copyright in this directory for more information.
      17             :  */
      18             : 
      19             : /**********************************************************************
      20             :  * SQLPrimaryKeys()
      21             :  * CLI Compliance: ODBC (Microsoft)
      22             :  *
      23             :  * Author: Martin van Dinther, Sjoerd Mullender
      24             :  * Date  : 30 aug 2002
      25             :  *
      26             :  **********************************************************************/
      27             : 
      28             : #include "ODBCGlobal.h"
      29             : #include "ODBCStmt.h"
      30             : #include "ODBCUtil.h"
      31             : 
      32             : static SQLRETURN
      33           0 : MNDBPrimaryKeys(ODBCStmt *stmt,
      34             :                 const SQLCHAR *CatalogName,
      35             :                 SQLSMALLINT NameLength1,
      36             :                 const SQLCHAR *SchemaName,
      37             :                 SQLSMALLINT NameLength2,
      38             :                 const SQLCHAR *TableName,
      39             :                 SQLSMALLINT NameLength3)
      40             : {
      41             :         RETCODE rc;
      42             : 
      43             :         /* buffer for the constructed query to do meta data retrieval */
      44             :         char *query = NULL;
      45             :         size_t querylen;
      46             :         size_t pos = 0;
      47             :         char *sch = NULL, *tab = NULL;
      48             : 
      49             :         /* deal with SQL_NTS and SQL_NULL_DATA */
      50           0 :         fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
      51             :                       addStmtError, stmt, return SQL_ERROR);
      52           0 :         fixODBCstring(SchemaName, NameLength2, SQLSMALLINT,
      53             :                       addStmtError, stmt, return SQL_ERROR);
      54           0 :         fixODBCstring(TableName, NameLength3, SQLSMALLINT,
      55             :                       addStmtError, stmt, return SQL_ERROR);
      56             : 
      57             :         /* check if a valid (non null, not empty) table name is supplied */
      58           0 :         if (TableName == NULL) {
      59             :                 /* Invalid use of null pointer */
      60           0 :                 addStmtError(stmt, "HY009", NULL, 0);
      61           0 :                 return SQL_ERROR;
      62             :         }
      63             : #ifdef ODBCDEBUG
      64           0 :         ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
      65             :                 (int) NameLength1, CatalogName ? (char *) CatalogName : "",
      66             :                 (int) NameLength2, SchemaName ? (char *) SchemaName : "",
      67             :                 (int) NameLength3, TableName ? (char *) TableName : "");
      68             : #endif
      69             : 
      70           0 :         if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
      71           0 :                 if (NameLength2 > 0) {
      72           0 :                         sch = ODBCParseOA("s", "name",
      73             :                                           (const char *) SchemaName,
      74             :                                           (size_t) NameLength2);
      75           0 :                         if (sch == NULL)
      76           0 :                                 goto nomem;
      77             :                 }
      78           0 :                 if (NameLength3 > 0) {
      79           0 :                         tab = ODBCParseOA("t", "name",
      80             :                                           (const char *) TableName,
      81             :                                           (size_t) NameLength3);
      82           0 :                         if (tab == NULL)
      83           0 :                                 goto nomem;
      84             :                 }
      85             :         } else {
      86           0 :                 if (NameLength2 > 0) {
      87           0 :                         sch = ODBCParseID("s", "name",
      88             :                                           (const char *) SchemaName,
      89             :                                           (size_t) NameLength2);
      90           0 :                         if (sch == NULL)
      91           0 :                                 goto nomem;
      92             :                 }
      93           0 :                 if (NameLength3 > 0) {
      94           0 :                         tab = ODBCParseID("t", "name",
      95             :                                           (const char *) TableName,
      96             :                                           (size_t) NameLength3);
      97           0 :                         if (tab == NULL)
      98           0 :                                 goto nomem;
      99             :                 }
     100             :         }
     101             : 
     102             :         /* construct the query */
     103           0 :         querylen = 1000 + strlen(stmt->Dbc->dbname) + (sch ? strlen(sch) : 0) +
     104           0 :                 (tab ? strlen(tab) : 0);
     105           0 :         query = malloc(querylen);
     106           0 :         if (query == NULL)
     107           0 :                 goto nomem;
     108             : 
     109             :         /* SQLPrimaryKeys returns a table with the following columns:
     110             :            VARCHAR      table_cat
     111             :            VARCHAR      table_schem
     112             :            VARCHAR      table_name NOT NULL
     113             :            VARCHAR      column_name NOT NULL
     114             :            SMALLINT     key_seq NOT NULL
     115             :            VARCHAR      pk_name
     116             :          */
     117           0 :         pos += snprintf(query + pos, querylen - pos,
     118             :                "select '%s' as table_cat, "
     119             :                       "s.name as table_schem, "
     120             :                       "t.name as table_name, "
     121             :                       "kc.name as column_name, "
     122             :                       "cast(kc.nr + 1 as smallint) as key_seq, "
     123             :                       "k.name as pk_name "
     124             :                "from sys.schemas s, sys.tables t, "
     125             :                     "sys.keys k, sys.objects kc "
     126             :                "where k.id = kc.id and "
     127             :                      "k.table_id = t.id and "
     128             :                      "t.schema_id = s.id and "
     129             :                      "k.type = 0",
     130             :                 stmt->Dbc->dbname);
     131           0 :         assert(pos < 800);
     132             : 
     133             :         /* Construct the selection condition query part */
     134           0 :         if (NameLength1 > 0 && CatalogName != NULL) {
     135             :                 /* filtering requested on catalog name */
     136           0 :                 if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
     137             :                         /* catalog name does not match the database name, so return no rows */
     138           0 :                         pos += snprintf(query + pos, querylen - pos, " and 1=2");
     139             :                 }
     140             :         }
     141           0 :         if (sch) {
     142             :                 /* filtering requested on schema name */
     143           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", sch);
     144           0 :                 free(sch);
     145             :         }
     146           0 :         if (tab) {
     147             :                 /* filtering requested on table name */
     148           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", tab);
     149           0 :                 free(tab);
     150             :         }
     151             : 
     152             :         /* add the ordering */
     153           0 :         pos += strcpy_len(query + pos, " order by table_schem, table_name, key_seq", querylen - pos);
     154             : 
     155             :         /* query the MonetDB data dictionary tables */
     156           0 :         rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) pos);
     157             : 
     158           0 :         free(query);
     159             : 
     160           0 :         return rc;
     161             : 
     162           0 :   nomem:
     163             :         /* note that query must be NULL when we get here */
     164           0 :         if (sch)
     165           0 :                 free(sch);
     166           0 :         if (tab)
     167           0 :                 free(tab);
     168             :         /* Memory allocation error */
     169           0 :         addStmtError(stmt, "HY001", NULL, 0);
     170           0 :         return SQL_ERROR;
     171             : }
     172             : 
     173             : SQLRETURN SQL_API
     174             : SQLPrimaryKeys(SQLHSTMT StatementHandle,
     175             :                SQLCHAR *CatalogName,
     176             :                SQLSMALLINT NameLength1,
     177             :                SQLCHAR *SchemaName,
     178             :                SQLSMALLINT NameLength2,
     179             :                SQLCHAR *TableName,
     180             :                SQLSMALLINT NameLength3)
     181             : {
     182             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     183             : 
     184             : #ifdef ODBCDEBUG
     185           0 :         ODBCLOG("SQLPrimaryKeys %p ", StatementHandle);
     186             : #endif
     187             : 
     188           0 :         if (!isValidStmt(stmt))
     189             :                  return SQL_INVALID_HANDLE;
     190             : 
     191           0 :         clearStmtErrors(stmt);
     192             : 
     193           0 :         return MNDBPrimaryKeys(stmt,
     194             :                                CatalogName, NameLength1,
     195             :                                SchemaName, NameLength2,
     196             :                                TableName, NameLength3);
     197             : }
     198             : 
     199             : SQLRETURN SQL_API
     200             : SQLPrimaryKeysA(SQLHSTMT StatementHandle,
     201             :                 SQLCHAR *CatalogName,
     202             :                 SQLSMALLINT NameLength1,
     203             :                 SQLCHAR *SchemaName,
     204             :                 SQLSMALLINT NameLength2,
     205             :                 SQLCHAR *TableName,
     206             :                 SQLSMALLINT NameLength3)
     207             : {
     208           0 :         return SQLPrimaryKeys(StatementHandle,
     209             :                               CatalogName, NameLength1,
     210             :                               SchemaName, NameLength2,
     211             :                               TableName, NameLength3);
     212             : }
     213             : 
     214             : SQLRETURN SQL_API
     215             : SQLPrimaryKeysW(SQLHSTMT StatementHandle,
     216             :                 SQLWCHAR *CatalogName,
     217             :                 SQLSMALLINT NameLength1,
     218             :                 SQLWCHAR *SchemaName,
     219             :                 SQLSMALLINT NameLength2,
     220             :                 SQLWCHAR *TableName,
     221             :                 SQLSMALLINT NameLength3)
     222             : {
     223             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     224             :         SQLRETURN rc = SQL_ERROR;
     225             :         SQLCHAR *catalog = NULL, *schema = NULL, *table = NULL;
     226             : 
     227             : #ifdef ODBCDEBUG
     228           0 :         ODBCLOG("SQLPrimaryKeysW %p ", StatementHandle);
     229             : #endif
     230             : 
     231           0 :         if (!isValidStmt(stmt))
     232             :                  return SQL_INVALID_HANDLE;
     233             : 
     234           0 :         clearStmtErrors(stmt);
     235             : 
     236           0 :         fixWcharIn(CatalogName, NameLength1, SQLCHAR, catalog,
     237             :                    addStmtError, stmt, goto bailout);
     238           0 :         fixWcharIn(SchemaName, NameLength2, SQLCHAR, schema,
     239             :                    addStmtError, stmt, goto bailout);
     240           0 :         fixWcharIn(TableName, NameLength3, SQLCHAR, table,
     241             :                    addStmtError, stmt, goto bailout);
     242             : 
     243           0 :         rc = MNDBPrimaryKeys(stmt,
     244             :                              catalog, SQL_NTS,
     245             :                              schema, SQL_NTS,
     246             :                              table, SQL_NTS);
     247             : 
     248           0 :       bailout:
     249           0 :         if (catalog)
     250           0 :                 free(catalog);
     251           0 :         if (schema)
     252           0 :                 free(schema);
     253           0 :         if (table)
     254           0 :                 free(table);
     255             : 
     256             :         return rc;
     257             : }

Generated by: LCOV version 1.14