Line data Source code
1 : /* 2 : * SPDX-License-Identifier: MPL-2.0 3 : * 4 : * This Source Code Form is subject to the terms of the Mozilla Public 5 : * License, v. 2.0. If a copy of the MPL was not distributed with this 6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 : * 8 : * Copyright 2024 MonetDB Foundation; 9 : * Copyright August 2008 - 2023 MonetDB B.V.; 10 : * Copyright 1997 - July 2008 CWI. 11 : */ 12 : 13 : /* 14 : * This code was created by Peter Harvey (mostly during Christmas 98/99). 15 : * This code is LGPL. Please ensure that this message remains in future 16 : * distributions and uses of this code (thats about all I get out of it). 17 : * - Peter Harvey pharvey@codebydesign.com 18 : * 19 : * This file has been modified for the MonetDB project. See the file 20 : * Copyright in this directory for more information. 21 : */ 22 : 23 : /********************************************************************** 24 : * SQLAllocHandle 25 : * CLI compliance: ISO 92 26 : * 27 : * Note: This function also implements the deprecated ODBC functions 28 : * SQLAllocEnv(), SQLAllocConnect() and SQLAllocStmt() 29 : * Those functions are simply mapped to this function. 30 : * All checks and allocation is done in this function. 31 : * 32 : * Author: Martin van Dinther, Sjoerd Mullender 33 : * Date : 30 Aug 2002 34 : * 35 : **********************************************************************/ 36 : 37 : #include "ODBCGlobal.h" 38 : #include "ODBCEnv.h" 39 : #include "ODBCDbc.h" 40 : #include "ODBCStmt.h" 41 : #include "ODBCError.h" 42 : 43 : static SQLRETURN 44 6 : MNDBAllocEnv(SQLHANDLE *OutputHandlePtr) 45 : { 46 6 : if (OutputHandlePtr == NULL) { 47 : return SQL_INVALID_HANDLE; 48 : } 49 6 : *OutputHandlePtr = (SQLHANDLE *) newODBCEnv(); 50 : #ifdef ODBCDEBUG 51 6 : ODBCLOG("new env %p\n", *OutputHandlePtr); 52 : #endif 53 6 : return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS; 54 : } 55 : 56 : static SQLRETURN 57 6 : MNDBAllocDbc(ODBCEnv *env, SQLHANDLE *OutputHandlePtr) 58 : { 59 6 : if (env->sql_attr_odbc_version == 0) { 60 : /* Function sequence error */ 61 0 : addEnvError(env, "HY010", NULL, 0); 62 0 : return SQL_ERROR; 63 : } 64 6 : if (OutputHandlePtr == NULL) { 65 : /* Invalid use of null pointer */ 66 0 : addEnvError(env, "HY009", NULL, 0); 67 0 : return SQL_ERROR; 68 : } 69 6 : *OutputHandlePtr = (SQLHANDLE *) newODBCDbc(env); 70 : #ifdef ODBCDEBUG 71 6 : ODBCLOG("new dbc %p\n", *OutputHandlePtr); 72 : #endif 73 6 : return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS; 74 : } 75 : 76 : SQLRETURN 77 13 : MNDBAllocStmt(ODBCDbc *dbc, SQLHANDLE *OutputHandlePtr) 78 : { 79 13 : if (!dbc->Connected) { 80 : /* Connection does not exist */ 81 0 : addDbcError(dbc, "08003", NULL, 0); 82 0 : return SQL_ERROR; 83 : } 84 13 : if (OutputHandlePtr == NULL) { 85 : /* Invalid use of null pointer */ 86 0 : addDbcError(dbc, "HY009", NULL, 0); 87 0 : return SQL_ERROR; 88 : } 89 13 : *OutputHandlePtr = (SQLHANDLE *) newODBCStmt(dbc); 90 : #ifdef ODBCDEBUG 91 13 : ODBCLOG("new stmt %p\n", *OutputHandlePtr); 92 : #endif 93 13 : return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS; 94 : } 95 : 96 : static SQLRETURN 97 0 : MNDBAllocDesc(ODBCDbc *dbc, SQLHANDLE *OutputHandlePtr) 98 : { 99 0 : if (!dbc->Connected) { 100 : /* Connection does not exist */ 101 0 : addDbcError(dbc, "08003", NULL, 0); 102 0 : return SQL_ERROR; 103 : } 104 0 : if (OutputHandlePtr == NULL) { 105 : /* Invalid use of null pointer */ 106 0 : addDbcError(dbc, "HY009", NULL, 0); 107 0 : return SQL_ERROR; 108 : } 109 0 : *OutputHandlePtr = (SQLHANDLE *) newODBCDesc(dbc); 110 : #ifdef ODBCDEBUG 111 0 : ODBCLOG("new desc %p\n", *OutputHandlePtr); 112 : #endif 113 0 : return *OutputHandlePtr == NULL ? SQL_ERROR : SQL_SUCCESS; 114 : } 115 : 116 : SQLRETURN 117 23 : MNDBAllocHandle(SQLSMALLINT HandleType, 118 : SQLHANDLE InputHandle, 119 : SQLHANDLE *OutputHandlePtr) 120 : { 121 23 : switch (HandleType) { 122 6 : case SQL_HANDLE_ENV: 123 6 : if (InputHandle != NULL) 124 : return SQL_INVALID_HANDLE; 125 6 : return MNDBAllocEnv(OutputHandlePtr); 126 6 : case SQL_HANDLE_DBC: 127 6 : if (!isValidEnv((ODBCEnv *) InputHandle)) 128 : return SQL_INVALID_HANDLE; 129 6 : clearEnvErrors((ODBCEnv *) InputHandle); 130 6 : return MNDBAllocDbc((ODBCEnv *) InputHandle, OutputHandlePtr); 131 11 : case SQL_HANDLE_STMT: 132 11 : if (!isValidDbc((ODBCDbc *) InputHandle)) 133 : return SQL_INVALID_HANDLE; 134 11 : clearDbcErrors((ODBCDbc *) InputHandle); 135 11 : return MNDBAllocStmt((ODBCDbc *) InputHandle, OutputHandlePtr); 136 0 : case SQL_HANDLE_DESC: 137 0 : if (!isValidDbc((ODBCDbc *) InputHandle)) 138 : return SQL_INVALID_HANDLE; 139 0 : clearDbcErrors((ODBCDbc *) InputHandle); 140 0 : return MNDBAllocDesc((ODBCDbc *) InputHandle, OutputHandlePtr); 141 : default: 142 : /* we cannot set an error because we do not know 143 : the handle type of the possibly non-null handle */ 144 : return SQL_INVALID_HANDLE; 145 : } 146 : } 147 : 148 : SQLRETURN SQL_API 149 : SQLAllocHandle(SQLSMALLINT HandleType, /* type to be allocated */ 150 : SQLHANDLE InputHandle, /* context for new handle */ 151 : SQLHANDLE *OutputHandlePtr) /* ptr for allocated handle struct */ 152 : { 153 : #ifdef ODBCDEBUG 154 12 : ODBCLOG("SQLAllocHandle %s %p\n", 155 : HandleType == SQL_HANDLE_ENV ? "Env" : 156 : HandleType == SQL_HANDLE_DBC ? "Dbc" : 157 : HandleType == SQL_HANDLE_STMT ? "Stmt" : "Desc", 158 : InputHandle); 159 : #endif 160 : 161 12 : return MNDBAllocHandle(HandleType, InputHandle, OutputHandlePtr); 162 : }