LCOV - code coverage report
Current view: top level - clients/odbc/driver - SQLProcedures.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 69 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             :  * SQLProcedures()
      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             : 
      33             : static SQLRETURN
      34           0 : MNDBProcedures(ODBCStmt *stmt,
      35             :                const SQLCHAR *CatalogName,
      36             :                SQLSMALLINT NameLength1,
      37             :                const SQLCHAR *SchemaName,
      38             :                SQLSMALLINT NameLength2,
      39             :                const SQLCHAR *ProcName,
      40             :                SQLSMALLINT NameLength3)
      41             : {
      42             :         RETCODE rc;
      43             : 
      44             :         /* buffer for the constructed query to do meta data retrieval */
      45             :         char *query = NULL;
      46             :         size_t querylen;
      47             :         size_t pos = 0;
      48             :         char *sch = NULL, *pro = NULL;
      49             : 
      50             :         /* convert input string parameters to normal null terminated C strings */
      51           0 :         fixODBCstring(CatalogName, NameLength1, SQLSMALLINT,
      52             :                       addStmtError, stmt, return SQL_ERROR);
      53           0 :         fixODBCstring(SchemaName, NameLength2, SQLSMALLINT,
      54             :                       addStmtError, stmt, return SQL_ERROR);
      55           0 :         fixODBCstring(ProcName, NameLength3, SQLSMALLINT,
      56             :                       addStmtError, stmt, return SQL_ERROR);
      57             : 
      58             : #ifdef ODBCDEBUG
      59           0 :         ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
      60             :                 (int) NameLength1, CatalogName ? (char *) CatalogName : "",
      61             :                 (int) NameLength2, SchemaName ? (char *) SchemaName : "",
      62             :                 (int) NameLength3, ProcName ? (char *) ProcName : "");
      63             : #endif
      64             : 
      65             :         /* SQLProcedures returns a table with the following columns:
      66             :            VARCHAR      procedure_cat
      67             :            VARCHAR      procedure_schem
      68             :            VARCHAR      procedure_name NOT NULL
      69             :            n/a          num_input_params (reserved for future use)
      70             :            n/a          num_output_params (reserved for future use)
      71             :            n/a          num_result_sets (reserved for future use)
      72             :            VARCHAR      remarks
      73             :            SMALLINT     procedure_type
      74             :          */
      75             : 
      76           0 :         if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) {
      77           0 :                 if (NameLength2 > 0) {
      78           0 :                         sch = ODBCParsePV("s", "name",
      79             :                                           (const char *) SchemaName,
      80             :                                           (size_t) NameLength2);
      81           0 :                         if (sch == NULL)
      82           0 :                                 goto nomem;
      83             :                 }
      84           0 :                 if (NameLength3 > 0) {
      85           0 :                         pro = ODBCParsePV("p", "name",
      86             :                                           (const char *) ProcName,
      87             :                                           (size_t) NameLength3);
      88           0 :                         if (pro == NULL)
      89           0 :                                 goto nomem;
      90             :                 }
      91             :         } else {
      92           0 :                 if (NameLength2 > 0) {
      93           0 :                         sch = ODBCParseID("s", "name",
      94             :                                           (const char *) SchemaName,
      95             :                                           (size_t) NameLength2);
      96           0 :                         if (sch == NULL)
      97           0 :                                 goto nomem;
      98             :                 }
      99           0 :                 if (NameLength3 > 0) {
     100           0 :                         pro = ODBCParseID("p", "name",
     101             :                                           (const char *) ProcName,
     102             :                                           (size_t) NameLength3);
     103           0 :                         if (pro == NULL)
     104           0 :                                 goto nomem;
     105             :                 }
     106             :         }
     107             : 
     108           0 :         querylen = 1000 + strlen(stmt->Dbc->dbname) +
     109           0 :                 (sch ? strlen(sch) : 0) + (pro ? strlen(pro) : 0);
     110           0 :         query = malloc(querylen);
     111           0 :         if (query == NULL)
     112           0 :                 goto nomem;
     113             : 
     114             : /* see sql_catalog.h */
     115             : #define F_FUNC 1
     116             : #define F_PROC 2
     117             : #define F_UNION 5
     118             : #define FUNC_LANG_SQL 2
     119           0 :         pos += snprintf(query + pos, querylen - pos,
     120             :                  "select '%s' as procedure_cat, "
     121             :                         "s.name as procedure_schem, "
     122             :                         "p.name as procedure_name, "
     123             :                         "0 as num_input_params, "
     124             :                         "0 as num_output_params, "
     125             :                         "0 as num_result_sets, "
     126             :                         "%s as remarks, "
     127             :                         "cast(case when p.type = %d then %d else %d end as smallint) as procedure_type "
     128             :                  "from sys.schemas as s, "
     129             :                       "sys.functions as p%s "
     130             :                  "where p.schema_id = s.id and "
     131             :                        "p.language >= %d and "
     132             :                        "p.type in (%d, %d, %d)",
     133             :                  stmt->Dbc->dbname,
     134             :                  stmt->Dbc->has_comment ? "c.remark" : "cast(null as varchar(1))",
     135             :                  F_PROC, SQL_PT_PROCEDURE, SQL_PT_FUNCTION,
     136           0 :                  stmt->Dbc->has_comment ? " left outer join sys.comments c on p.id = c.id" : "",
     137             :                  FUNC_LANG_SQL, F_FUNC, F_PROC, F_UNION);
     138           0 :         assert(pos < 800);
     139             : 
     140             :         /* Construct the selection condition query part */
     141           0 :         if (NameLength1 > 0 && CatalogName != NULL) {
     142             :                 /* filtering requested on catalog name */
     143           0 :                 if (strcmp((char *) CatalogName, stmt->Dbc->dbname) != 0) {
     144             :                         /* catalog name does not match the database name, so return no rows */
     145           0 :                         pos += snprintf(query + pos, querylen - pos, " and 1=2");
     146             :                 }
     147             :         }
     148           0 :         if (sch) {
     149             :                 /* filtering requested on schema name */
     150           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", sch);
     151           0 :                 free(sch);
     152             :         }
     153           0 :         if (pro) {
     154             :                 /* filtering requested on procedure name */
     155           0 :                 pos += snprintf(query + pos, querylen - pos, " and %s", pro);
     156           0 :                 free(pro);
     157             :         }
     158             : 
     159             :         /* add the ordering (exclude procedure_cat as it is the same for all rows) */
     160           0 :         pos += strcpy_len(query + pos, " order by procedure_schem, procedure_name", querylen - pos);
     161             : 
     162             :         /* query the MonetDB data dictionary tables */
     163           0 :         rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) pos);
     164             : 
     165           0 :         free(query);
     166             : 
     167           0 :         return rc;
     168             : 
     169           0 :   nomem:
     170             :         /* note that query must be NULL when we get here */
     171           0 :         if (sch)
     172           0 :                 free(sch);
     173           0 :         if (pro)
     174           0 :                 free(pro);
     175             :         /* Memory allocation error */
     176           0 :         addStmtError(stmt, "HY001", NULL, 0);
     177           0 :         return SQL_ERROR;
     178             : }
     179             : 
     180             : SQLRETURN SQL_API
     181             : SQLProcedures(SQLHSTMT StatementHandle,
     182             :               SQLCHAR *CatalogName,
     183             :               SQLSMALLINT NameLength1,
     184             :               SQLCHAR *SchemaName,
     185             :               SQLSMALLINT NameLength2,
     186             :               SQLCHAR *ProcName,
     187             :               SQLSMALLINT NameLength3)
     188             : {
     189             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     190             : 
     191             : #ifdef ODBCDEBUG
     192           0 :         ODBCLOG("SQLProcedures %p ", StatementHandle);
     193             : #endif
     194             : 
     195           0 :         if (!isValidStmt(stmt))
     196             :                  return SQL_INVALID_HANDLE;
     197             : 
     198           0 :         clearStmtErrors(stmt);
     199             : 
     200           0 :         return MNDBProcedures(stmt,
     201             :                               CatalogName, NameLength1,
     202             :                               SchemaName, NameLength2,
     203             :                               ProcName, NameLength3);
     204             : }
     205             : 
     206             : SQLRETURN SQL_API
     207             : SQLProceduresA(SQLHSTMT StatementHandle,
     208             :                SQLCHAR *CatalogName,
     209             :                SQLSMALLINT NameLength1,
     210             :                SQLCHAR *SchemaName,
     211             :                SQLSMALLINT NameLength2,
     212             :                SQLCHAR *ProcName,
     213             :                SQLSMALLINT NameLength3)
     214             : {
     215           0 :         return SQLProcedures(StatementHandle,
     216             :                              CatalogName, NameLength1,
     217             :                              SchemaName, NameLength2,
     218             :                              ProcName, NameLength3);
     219             : }
     220             : 
     221             : SQLRETURN SQL_API
     222             : SQLProceduresW(SQLHSTMT StatementHandle,
     223             :                SQLWCHAR *CatalogName, SQLSMALLINT NameLength1,
     224             :                SQLWCHAR *SchemaName, SQLSMALLINT NameLength2,
     225             :                SQLWCHAR *ProcName, SQLSMALLINT NameLength3)
     226             : {
     227             :         ODBCStmt *stmt = (ODBCStmt *) StatementHandle;
     228             :         SQLRETURN rc = SQL_ERROR;
     229             :         SQLCHAR *catalog = NULL, *schema = NULL, *proc = NULL;
     230             : 
     231             : #ifdef ODBCDEBUG
     232           0 :         ODBCLOG("SQLProceduresW %p ", StatementHandle);
     233             : #endif
     234             : 
     235           0 :         if (!isValidStmt(stmt))
     236             :                  return SQL_INVALID_HANDLE;
     237             : 
     238           0 :         clearStmtErrors(stmt);
     239             : 
     240           0 :         fixWcharIn(CatalogName, NameLength1, SQLCHAR, catalog,
     241             :                    addStmtError, stmt, goto bailout);
     242           0 :         fixWcharIn(SchemaName, NameLength2, SQLCHAR, schema,
     243             :                    addStmtError, stmt, goto bailout);
     244           0 :         fixWcharIn(ProcName, NameLength3, SQLCHAR, proc,
     245             :                    addStmtError, stmt, goto bailout);
     246             : 
     247           0 :         rc = MNDBProcedures(stmt,
     248             :                             catalog, SQL_NTS,
     249             :                             schema, SQL_NTS,
     250             :                             proc, SQL_NTS);
     251             : 
     252           0 :       bailout:
     253           0 :         if (catalog)
     254           0 :                 free(catalog);
     255           0 :         if (schema)
     256           0 :                 free(schema);
     257           0 :         if (proc)
     258           0 :                 free(proc);
     259             : 
     260             :         return rc;
     261             : }

Generated by: LCOV version 1.14