There are no available options for this view.

Parent Directory Parent Directory | Revision <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Log">Log</a> Revision <a href="/cvs/aolserver/aolserver/nsd/log.c#A_Log">Log</a>

Revision 1.1 - (show annotations) (download) (as text)
Wed May 15 20:17:49 2002 UTC (10 years ago) by jgdavidson
Branch: MAIN
CVS Tags: aolserver_v4_r0_beta_16, aolserver_v4_r0_beta_20, aolserver_v4_r0_beta_21, aolserver_v45_r0, aolserver_v4_r0_beta_2, aolserver_v4_r0_beta_3, aolserver_v4_r0_beta_13, aolserver_v4_r0_beta_1, aolserver_v4_r0_beta_7, aolserver_v4_r0_beta_6, aolserver_v4_r0_beta_5, aolserver_v4_r0_beta_12, aolserver_v4_r0_beta_9, aolserver_v40_r10, aolserver_v4_r0_beta_11, aolserver_v4_r0_beta_19, aolserver_v4_r0_beta_18, aolserver_v40_r9, aolserver_v40_r8, aolserver_v40_r7, aolserver_v40_r6, aolserver_v40_r5, aolserver_v4_r0_beta_10, aolserver_v40_r3, aolserver_v40_r2, aolserver_v40_r1, aolserver_v40_r0, aolserver_v4_r0_beta_15, aolserver_v4_r0_beta_14, aolserver_v4_r0_beta_8, aolserver_v4_r0_beta_4, aolserver_v4_r0_beta_17, aolserver_v40_r9_b2, HEAD
Branch point for: aolserver_v45_r1, aolserver_v45_r2, aolserver_v45_bp, aolserver_v40_bp
File MIME type: text/x-chdr
Initial import of Ns_Db API removed from core.
1 /*
2 * The contents of this file are subject to the AOLserver Public License
3 * Version 1.1 (the "License"); you may not use this file except in
4 * compliance with the License. You may obtain a copy of the License at
5 * http://aolserver.com/.
6 *
7 * Software distributed under the License is distributed on an "AS IS"
8 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 * the License for the specific language governing rights and limitations
10 * under the License.
11 *
12 * The Original Code is AOLserver Code and related documentation
13 * distributed by AOL.
14 *
15 * The Initial Developer of the Original Code is America Online,
16 * Inc. Portions created by AOL are Copyright (C) 1999 America Online,
17 * Inc. All Rights Reserved.
18 *
19 * Alternatively, the contents of this file may be used under the terms
20 * of the GNU General Public License (the "GPL"), in which case the
21 * provisions of GPL are applicable instead of those above. If you wish
22 * to allow use of your version of this file only under the terms of the
23 * GPL and not to allow others to use your version of this file under the
24 * License, indicate your decision by deleting the provisions above and
25 * replace them with the notice and other provisions required by the GPL.
26 * If you do not delete the provisions above, a recipient may use your
27 * version of this file under either the License or the GPL.
28 */
29
30
31 /*
32 * dbutil.c --
33 *
34 * Utility db routines.
35 */
36
37 static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsdb/dbutil.c,v 1.1 2002/05/15 20:17:49 jgdavidson Exp $, compiled: " __DATE__ " " __TIME__;
38
39 #include "db.h"
40
41 /*
42 * The following constants are defined for this file.
43 */
44
45 #define NS_SQLERRORCODE "NSINT" /* SQL error code for AOLserver exceptions. */
46
47
48 /*
49 *----------------------------------------------------------------------
50 *
51 * <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbQuoteValue">Ns_DbQuoteValue</a> --
52 *
53 * Add single quotes around an SQL string value if necessary.
54 *
55 * Results:
56 * None.
57 *
58 * Side effects:
59 * Copy of the string, modified if needed, is placed in the
60 * given Ns_DString.
61 *
62 *----------------------------------------------------------------------
63 */
64
65 void
66 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbQuoteValue">Ns_DbQuoteValue</a>(Ns_DString *pds, char *string)
67 {
68 while (*string != '\0') {
69 if (*string == '\'') {
70 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(pds, "'", 1);
71 }
72 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(pds, string, 1);
73 ++string;
74 }
75 }
76
77
78 /*
79 *----------------------------------------------------------------------
80 *
81 * <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_Db0or1Row">Ns_Db0or1Row</a> --
82 *
83 * Send an SQL statement which should return either no rows or
84 * exactly one row.
85 *
86 * Results:
87 * Pointer to new Ns_Set which must be eventually freed. The
88 * set includes the names of the columns and, if a row was
89 * fetched, the values for the row. On error, returns NULL.
90 *
91 * Side effects:
92 * Given nrows pointer is set to 0 or 1 to indicate if a row
93 * was actually returned.
94 *
95 *----------------------------------------------------------------------
96 */
97
98 Ns_Set *
99 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_Db0or1Row">Ns_Db0or1Row</a>(Ns_DbHandle *handle, char *sql, int *nrows)
100 {
101 Ns_Set *row;
102
103 row = <a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbSelect">Ns_DbSelect</a>(handle, sql);
104 if (row != NULL) {
105 if (<a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbGetRow">Ns_DbGetRow</a>(handle, row) == NS_END_DATA) {
106 *nrows = 0;
107 } else {
108 switch (<a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbGetRow">Ns_DbGetRow</a>(handle, row)) {
109 case NS_END_DATA:
110 *nrows = 1;
111 break;
112
113 case NS_OK:
114 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a>(handle, NS_SQLERRORCODE,
115 "Query returned more than one row.");
116 <a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbFlush">Ns_DbFlush</a>(handle);
117 /* FALLTHROUGH */
118
119 case NS_ERROR:
120 /* FALLTHROUGH */
121
122 default:
123 return NULL;
124 break;
125 }
126 }
127 row = <a href="/cvs/aolserver/aolserver/nsd/set.c#A_Ns_SetCopy">Ns_SetCopy</a>(row);
128 }
129
130 return row;
131 }
132
133
134 /*
135 *----------------------------------------------------------------------
136 *
137 * <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_Db1Row">Ns_Db1Row</a> --
138 *
139 * Send a SQL statement which is expected to return exactly 1 row.
140 *
141 * Results:
142 * Pointer to Ns_Set with row data or NULL on error. Set must
143 * eventually be freed.
144 *
145 * Side effects:
146 * None.
147 *
148 *----------------------------------------------------------------------
149 */
150
151 Ns_Set *
152 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_Db1Row">Ns_Db1Row</a>(Ns_DbHandle *handle, char *sql)
153 {
154 Ns_Set *row;
155 int nrows;
156
157 row = <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_Db0or1Row">Ns_Db0or1Row</a>(handle, sql, &nrows);
158 if (row != NULL) {
159 if (nrows != 1) {
160 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a>(handle, NS_SQLERRORCODE,
161 "Query did not return a row.");
162 row = NULL;
163 }
164 }
165
166 return row;
167 }
168
169
170 /*
171 *----------------------------------------------------------------------
172 *
173 * <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbInterpretSqlFile">Ns_DbInterpretSqlFile</a> --
174 *
175 * <a href="/cvs/aolserver/aolserver/nsd/adpparse.c#A_Parse">Parse</a> DML statements from an SQL file and send them to the
176 * database for execution.
177 *
178 * Results:
179 * NS_OK or NS_ERROR.
180 *
181 * Side effects:
182 * Stops on first error. Transaction protection is provided for
183 * Illustra and "\n-- comments are handled correctly.
184 *
185 *----------------------------------------------------------------------
186 */
187
188 int
189 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbInterpretSqlFile">Ns_DbInterpretSqlFile</a>(Ns_DbHandle *handle, char *filename)
190 {
191 FILE *fp;
192 Ns_DString dsSql;
193 int i, status, inquote;
194 char c, lastc;
195 char *p;
196
197 fp = fopen(filename, "rt");
198 if (fp == NULL) {
199 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a>(handle, NS_SQLERRORCODE,
200 "Could not read file");
201 return NS_ERROR;
202 }
203
204 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringInit">Ns_DStringInit</a>(&dsSql);
205 status = NS_OK;
206 inquote = 0;
207 c = '\n';
208 while ((i = getc(fp)) != EOF) {
209 lastc = c;
210 c = (char) i;
211 loopstart:
212 if (inquote) {
213 if (c != '\'') {
214 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(&dsSql, &c, 1);
215 } else {
216 if ((i = getc(fp)) == EOF) {
217 break;
218 }
219 lastc = c;
220 c = (char) i;
221 if (c == '\'') {
222 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(&dsSql, "''", 2);
223 continue;
224 } else {
225 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(&dsSql, "'", 1);
226 inquote = 0;
227 goto loopstart;
228 }
229 }
230 } else {
231 /* Check to see if it is a comment */
232 if ((c == '-') && (lastc == '\n')) {
233 if ((i = getc(fp)) == EOF) {
234 break;
235 }
236 lastc = c;
237 c = (char) i;
238 if (c != '-') {
239 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(&dsSql, "-", 1);
240 goto loopstart;
241 }
242 while ((i = getc(fp)) != EOF) {
243 lastc = c;
244 c = (char) i;
245 if (c == '\n') {
246 break;
247 }
248 }
249 } else if (c == ';') {
250 if (<a href="/cvs/aolserver/aolserver/nsdb/dbdrv.c#A_Ns_DbExec">Ns_DbExec</a>(handle, dsSql.string) == NS_ERROR) {
251 status = NS_ERROR;
252 break;
253 }
254 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringTrunc">Ns_DStringTrunc</a>(&dsSql, 0);
255 } else {
256 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringNAppend">Ns_DStringNAppend</a>(&dsSql, &c, 1);
257 if (c == '\'') {
258 inquote = 1;
259 }
260 }
261 }
262 }
263 fclose(fp);
264
265 /*
266 * If dstring contains anything but whitespace, return error
267 */
268 if (status != NS_ERROR) {
269 for (p = dsSql.string; *p != '\0'; p++) {
270 if (isspace(UCHAR(*p)) == 0) {
271 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a>(handle, NS_SQLERRORCODE,
272 "File ends with unterminated SQL");
273 status = NS_ERROR;
274 }
275 }
276 }
277 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringFree">Ns_DStringFree</a>(&dsSql);
278
279 return status;
280 }
281
282
283 /*
284 *----------------------------------------------------------------------
285 *
286 * <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a> --
287 *
288 * Set the stored SQL exception code and message in the handle.
289 *
290 * Results:
291 * None.
292 *
293 * Side effects:
294 * Code and message are updated.
295 *
296 *----------------------------------------------------------------------
297 */
298
299 void
300 <a href="/cvs/aolserver/aolserver/nsdb/dbutil.c#A_Ns_DbSetException">Ns_DbSetException</a>(Ns_DbHandle *handle, char *code, char *msg)
301 {
302 strcpy(handle->cExceptionCode, code);
303 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringFree">Ns_DStringFree</a>(&(handle->dsExceptionMsg));
304 <a href="/cvs/aolserver/aolserver/nsd/dstring.c#A_Ns_DStringAppend">Ns_DStringAppend</a>(&(handle->dsExceptionMsg), msg);
305 }
306