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 : * ODBCEnv.c 25 : * 26 : * Description: 27 : * This file contains the functions which operate on 28 : * ODBC environment structures/objects (see ODBCEnv.h). 29 : * 30 : * Author: Martin van Dinther, Sjoerd Mullender 31 : * Date : 30 aug 2002 32 : * 33 : **********************************************/ 34 : 35 : #include "ODBCGlobal.h" 36 : #include "ODBCEnv.h" 37 : 38 : #define ODBC_ENV_MAGIC_NR 341 /* for internal sanity check only */ 39 : 40 : 41 : /* 42 : * Creates a new allocated ODBCEnv object and initializes it. 43 : * 44 : * Precondition: none 45 : * Postcondition: returns a new ODBCEnv object 46 : */ 47 : ODBCEnv * 48 6 : newODBCEnv(void) 49 : { 50 6 : ODBCEnv *env = (ODBCEnv *) malloc(sizeof(ODBCEnv)); 51 : 52 6 : if (env == NULL) 53 : return NULL; 54 : 55 6 : *env = (ODBCEnv) { 56 : .Type = ODBC_ENV_MAGIC_NR, 57 : }; 58 : 59 6 : return env; 60 : } 61 : 62 : 63 : /* 64 : * Check if the enviroment handle is valid. 65 : * Note: this function is used internally by the driver to assert legal 66 : * and save usage of the handle and prevent crashes as much as possible. 67 : * 68 : * Precondition: none 69 : * Postcondition: returns 1 if it is a valid environment handle, 70 : * returns 0 if is invalid and thus an unusable handle. 71 : */ 72 : int 73 30 : isValidEnv(ODBCEnv *env) 74 : { 75 : #ifdef ODBCDEBUG 76 30 : if (!(env && env->Type == ODBC_ENV_MAGIC_NR)) 77 0 : ODBCLOG("env %p not a valid environment handle\n", env); 78 : #endif 79 30 : return env && env->Type == ODBC_ENV_MAGIC_NR; 80 : } 81 : 82 : 83 : /* 84 : * Creates and adds an error msg object to the end of the error list of 85 : * this ODBCEnv struct. 86 : * When the errMsg is NULL and the SQLState is an ISO SQLState the 87 : * standard ISO message text for the SQLState is used as message. 88 : * 89 : * Precondition: env must be valid. SQLState and errMsg may be NULL. 90 : */ 91 : void 92 0 : addEnvError(ODBCEnv *env, const char *SQLState, const char *errMsg, int nativeErrCode) 93 : { 94 0 : ODBCError *error = NULL; 95 : 96 : #ifdef ODBCDEBUG 97 0 : ODBCLOG("addEnvError %p %s %s %d\n", env, SQLState, errMsg ? errMsg : getStandardSQLStateMsg(SQLState), nativeErrCode); 98 : #endif 99 0 : assert(isValidEnv(env)); 100 : 101 0 : error = newODBCError(SQLState, errMsg, nativeErrCode); 102 0 : appendODBCError(&env->Error, error); 103 0 : } 104 : 105 : 106 : /* 107 : * Extracts an error object from the error list of this ODBCEnv struct. 108 : * The error object itself is removed from the error list. 109 : * The caller is now responsible for freeing the error object memory. 110 : * 111 : * Precondition: env and error must be valid 112 : * Postcondition: returns a ODBCError object or null when no error is available. 113 : */ 114 : ODBCError * 115 0 : getEnvError(ODBCEnv *env) 116 : { 117 0 : assert(isValidEnv(env)); 118 0 : return env->Error; 119 : } 120 : 121 : 122 : /* 123 : * Destroys the ODBCEnv object including its own managed data. 124 : * 125 : * Precondition: env must be valid and no ODBCDbc objects may refer to this env. 126 : * Postcondition: env is completely destroyed, env handle is become invalid. 127 : */ 128 : void 129 6 : destroyODBCEnv(ODBCEnv *env) 130 : { 131 6 : assert(isValidEnv(env)); 132 6 : assert(env->FirstDbc == NULL); 133 : 134 : /* first set this object to invalid */ 135 6 : env->Type = 0; 136 : 137 6 : deleteODBCErrorList(&env->Error); 138 6 : free((void *) env); 139 6 : }