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 : * SQLNativeSql()
25 : * CLI Compliance: ODBC (Microsoft)
26 : *
27 : * Author: Martin van Dinther, Sjoerd Mullender
28 : * Date : 30 aug 2002
29 : *
30 : **********************************************************************/
31 :
32 : #include "ODBCGlobal.h"
33 : #include "ODBCStmt.h"
34 : #include "ODBCUtil.h"
35 :
36 : static SQLRETURN
37 0 : MNDBNativeSql(ODBCDbc *dbc,
38 : const SQLCHAR *InStatementText,
39 : SQLINTEGER TextLength1,
40 : SQLCHAR *OutStatementText,
41 : SQLINTEGER BufferLength,
42 : SQLINTEGER *TextLength2Ptr)
43 : {
44 0 : char *query;
45 :
46 0 : fixODBCstring(InStatementText, TextLength1, SQLINTEGER,
47 : addDbcError, dbc, return SQL_ERROR);
48 :
49 0 : if (InStatementText == NULL) {
50 : /* Invalid use of null pointer */
51 0 : addDbcError(dbc, "HY009", NULL, 0);
52 0 : return SQL_ERROR;
53 : }
54 : #ifdef ODBCDEBUG
55 0 : ODBCLOG("\"%.*s\"\n", (int) TextLength1, (char *) InStatementText);
56 : #endif
57 :
58 0 : query = ODBCTranslateSQL(dbc, InStatementText, (size_t) TextLength1,
59 : SQL_NOSCAN_OFF);
60 0 : if (query == NULL) {
61 : /* Memory allocation error */
62 0 : addDbcError(dbc, "HY001", NULL, 0);
63 0 : return SQL_ERROR;
64 : }
65 0 : copyString(query, strlen(query), OutStatementText, BufferLength,
66 : TextLength2Ptr, SQLINTEGER, addDbcError, dbc,
67 : free(query); return SQL_ERROR);
68 0 : free(query);
69 :
70 0 : return dbc->Error ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;
71 : }
72 :
73 : SQLRETURN SQL_API
74 : SQLNativeSql(SQLHDBC ConnectionHandle,
75 : SQLCHAR *InStatementText,
76 : SQLINTEGER TextLength1,
77 : SQLCHAR *OutStatementText,
78 : SQLINTEGER BufferLength,
79 : SQLINTEGER *TextLength2Ptr)
80 : {
81 0 : ODBCDbc *dbc = (ODBCDbc *) ConnectionHandle;
82 :
83 : #ifdef ODBCDEBUG
84 0 : ODBCLOG("SQLNativeSql %p ", ConnectionHandle);
85 : #endif
86 :
87 0 : if (!isValidDbc(dbc))
88 : return SQL_INVALID_HANDLE;
89 :
90 0 : clearDbcErrors(dbc);
91 :
92 0 : return MNDBNativeSql(dbc,
93 : InStatementText,
94 : TextLength1,
95 : OutStatementText,
96 : BufferLength,
97 : TextLength2Ptr);
98 : }
99 :
100 : SQLRETURN SQL_API
101 : SQLNativeSqlA(SQLHDBC ConnectionHandle,
102 : SQLCHAR *InStatementText,
103 : SQLINTEGER TextLength1,
104 : SQLCHAR *OutStatementText,
105 : SQLINTEGER BufferLength,
106 : SQLINTEGER *TextLength2Ptr)
107 : {
108 0 : return SQLNativeSql(ConnectionHandle,
109 : InStatementText,
110 : TextLength1,
111 : OutStatementText,
112 : BufferLength,
113 : TextLength2Ptr);
114 : }
115 :
116 : SQLRETURN SQL_API
117 : SQLNativeSqlW(SQLHDBC ConnectionHandle,
118 : SQLWCHAR *InStatementText,
119 : SQLINTEGER TextLength1,
120 : SQLWCHAR *OutStatementText,
121 : SQLINTEGER BufferLength,
122 : SQLINTEGER *TextLength2Ptr)
123 : {
124 0 : ODBCDbc *dbc = (ODBCDbc *) ConnectionHandle;
125 0 : SQLRETURN rc;
126 0 : SQLINTEGER n;
127 0 : SQLSMALLINT nn;
128 0 : SQLCHAR *sqlin, *sqlout;
129 :
130 : #ifdef ODBCDEBUG
131 0 : ODBCLOG("SQLNativeSqlW %p ", ConnectionHandle);
132 : #endif
133 :
134 0 : if (!isValidDbc(dbc))
135 : return SQL_INVALID_HANDLE;
136 :
137 0 : clearDbcErrors(dbc);
138 :
139 0 : fixWcharIn(InStatementText, TextLength1, SQLCHAR, sqlin,
140 : addDbcError, dbc, return SQL_ERROR);
141 :
142 0 : rc = MNDBNativeSql(dbc, sqlin, SQL_NTS, NULL, 0, &n);
143 0 : if (!SQL_SUCCEEDED(rc))
144 : return rc;
145 0 : clearDbcErrors(dbc);
146 0 : n++; /* account for NUL byte */
147 0 : sqlout = malloc(n);
148 0 : if (sqlout == NULL) {
149 : /* Memory allocation error */
150 0 : addDbcError(dbc, "HY001", NULL, 0);
151 0 : return SQL_ERROR;
152 : }
153 0 : rc = MNDBNativeSql(dbc, sqlin, SQL_NTS, sqlout, n, &n);
154 0 : nn = (SQLSMALLINT) n;
155 0 : if (SQL_SUCCEEDED(rc)) {
156 0 : fixWcharOut(rc, sqlout, nn, OutStatementText, BufferLength,
157 : TextLength2Ptr, 1, addDbcError, dbc);
158 : }
159 0 : free(sqlout);
160 :
161 0 : return rc;
162 : }
|