Programming with SQL Relay using the C API
- Compiling an SQL Relay Client Program
- Establishing a Sessions
- Executing Queries
- Commits and Rollbacks
- Temporary Tables
- Catching Errors
- Substitution and Bind Variables
- Re-Binding and Re-Executing
- Accessing Fields in the Result Set
- Dealing With Large Result Sets
- Cursors
- Getting Column Information
- Stored Procedures
- Caching The Result Set
- Suspending and Resuming Sessions
Compiling an SQL Relay Client Program
When writing an SQL Relay client program using the C API, you need to include the sqlrclientwrapper.h file.
> > #include
1<sqlrelay sqlrclientwrapper.h="">
2>
3
4You'll also need to link against the sqlrclientwrapper, sqlrclient and rudiments libraries. The include file is usually found in /usr/local/firstworks/include and the libraries are usually found in /usr/local/firstworks/lib.
5
6The command to compile your .c file to object code will look something like this (assuming you're using the GNU C compiler):
7
8> gcc -I/usr/local/firstworks/include -c myprogram.c
9
10The command to compile your .o file to an executable will look something like this (assuming you're using the GNU C++ compiler):
11
12> g++ -o myprogram myprogram.o -L/usr/local/firstworks/lib -lsqlrclientwrapper -lsqlrclient -lrudiments
13
14Note that g++ was used to link, not gcc. You could alternatively link using gcc like this:
15
16> gcc -o myprogram myprogram.o -L/usr/local/firstworks/lib -lsqlrclientwrapper -lsqlrclient -lrudiments -lstdc++
17
18When using the C API, it is important to compile the .c files to object code using the C compiler before linking them using the C++ compiler. Compiling/Linking in one step using the C++ compiler the will most likely fail as it will generate C++ style symbols for function calls which will not be resolved in the sqlrclientwrapper library since it contains C style function symbols. Compiling to object code using the C compiler as a seperate step ensures that C style symbols will be generated for function calls.
19
20Establishing a Session
21
22
23
24To use SQL Relay, you have to identify the connection that you intend to use.
25
26>
27> #include <sqlrelay sqlrclientwrapper.h="">
28> #include <stdio.h>
29>
30> main() {
31>
32> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
33>
34> ... execute some queries ...
35>
36> sqlrcon_free(con);
37> }
38>
39
40After calling the constructor, a session is established when the first query, sqlrcur_ping() or sqlrcur_identify() is run.
41
42For the duration of the session, the client stays connected to a database connection daemon. While one client is connected, no other client can connect. Care should be taken to minimize the length of a session.
43
44If you're using a transactional database, ending a session has a catch. Database connection daemons can be configured to send either a commit or rollback at the end of a session if DML queries were executed during the session with no commit or rollback. Program accordingly.
45
46Executing Queries
47
48
49
50Call sqlrcur_sendQuery() or sqlrcur_sendFileQuery() to run a query.
51
52>
53> #include <sqlrelay sqlrclientwrapper.h="">
54> #include <stdio.h>
55>
56> main() {
57>
58> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
59> sqlrcur cur=sqlrcur_alloc(con);
60>
61> sqlrcur_sendQuery(cur,"select * from my_table");
62>
63> ... do some stuff that takes a **short** time ...
64>
65> sqlrcur_sendFileQuery(cur,"/usr/local/myprogram/sql","myquery.sql");
66> sqlrcon_endSession(con);
67>
68> ... do some stuff that takes a **long** time ...
69>
70> sqlrcur_sendQuery(cur,"select * from my_other_table");
71> sqlrcon_endSession(con);
72>
73> ... process the result set ...
74>
75> sqlrcur_free(cur);
76> sqlrcon_free(con);
77> }
78>
79
80Note the call to sqlrcon_endSession() after the call to sqlrcur_sendFileQuery(). Since the program does some stuff that takes a long time between that query and the next, ending the session there allows another client an opportunity to use that database connection while your client is busy. The next call to sqlrcur_sendQuery() establishes another session. Since the program does some stuff that takes a short time between the first two queries, it's OK to leave the session open between them.
81
82Commits and Rollbacks
83
84
85
86If you need to execute a commit or rollback, you should use the sqlrcon_commit() and sqlrcon_rollback() functions rather than sending a "commit" or "rollback" query. There are two reasons for this. First, it's much more efficient to call the methods. Second, if you're writing code that can run on transactional or non-transactional databases, some non-transactional databases will throw errors if they receive a "commit" or "rollback" query, but by calling the sqlrcon_commit() and sqlrcon_rollback() functions you instruct the database connection daemon to call the commit and rollback API methods for that database rather than issuing them as queries. If the API's have no commit or rollback methods, the calls do nothing and the database throws no error. This is especially important when using SQL Relay with ODBC.
87
88You can also turn Autocommit on or off with the sqlrcon_autoCommitOn() and sqlrcon_autoCommitOff() functions. When Autocommit is on, the database performs a commit after each successful DML or DDL query. When Autocommit is off, the database commits when the client instructs it to, or (by default) when a client disconnects. For databases that don't support Autocommit, sqlrcon_autoCommitOn() and sqlrcon_autoCommitOff() have no effect.
89
90Temporary Tables
91
92
93
94Some databases support temporary tables. That is, tables which are automatically dropped or truncated when an application closes it's connection to the database or when a transaction is committed or rolled back.
95
96For databases which drop or truncate tables when a transaction is committed or rolled back, temporary tables work naturally.
97
98However, for databases which drop or truncate tables when an application closes it's connection to the database, there is an issue. Since SQL Relay maintains persistent database connections, when an application disconnects from SQL Relay, the connection between SQL Relay and the database remains, so the database does not know to drop or truncate the table. To remedy this situation, SQL Relay parses each query to see if it created a temporary table, keeps a list of temporary tables and drops (or truncates them) when the application disconnects from SQL Relay. Since each database has slightly different syntax for creating a temporary table, SQL Relay parses each query according to the rules for that database.
99
100In effect, temporary tables should work when an application connects to SQL Relay in the same manner that they would work if the application connected directly to the database.
101
102Catching Errors
103
104
105
106If your call to sqlrcur_sendQuery() or sqlrcur_sendFileQuery() returns a 0, the query failed. You can find out why by calling sqlrcur_errorMessage().
107
108>
109> #include <sqlrelay sqlrclientwrapper.h="">
110> #include <stdio.h>
111>
112> main() {
113>
114> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
115> sqlrcur cur=sqlrcur_alloc(con);
116>
117> **if** (!sqlrcur_sendQuery(cur,"select * from my_nonexistant_table")) {
118> printf("%s\n",sqlrcur_errorMessage(cur));
119> }
120>
121> sqlrcur_free(cur);
122> sqlrcon_free(con);
123> }
124>
125
126Substitution and Bind Variables
127
128
129
130Programs rarely execute fixed queries. More often than not, some part of the query is dynamically generated. It's convenient to store queries in files so they can be changed by a non-C programmer. The SQL Relay API provides functions for making substitutions and binds in those queries.
131
132For a detailed discussion of substitutions and binds, see this document .
133
134Rather than just calling sqlrcur_sendFileQuery() you call sqlrcur_prepareFileQuery(), one or more of sqlrcur_subString(), sqlrcur_subLong() or sqlrcur_subDouble, one or more of sqlrcur_inputBindString(), sqlrcur_inputBindLong() or sqlr_inputBindDouble() and sqlrcur_executeQuery().
135
136>
137> /usr/local/myprogram/sql/myquery.sql:
138>
139> select * from mytable $(whereclause)
140>
141> Program code:
142> >
143> #include <sqlrelay sqlrclientwrapper.h="">
144> #include <stdio.h>
145>
146> main() {
147>
148> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
149> sqlrcur cur=sqlrcur_alloc(con);
150>
151> sqlrcur_prepareFileQuery(cur,"/usr/local/myprogram/sql","myquery.sql");
152> sqlrcur_subString(cur,"whereclause","where col1=:value1");
153> sqlrcur_inputBindString(cur,"value1","true");
154> sqlrcur_executeQuery(cur);
155>
156> ... process the result set ...
157>
158> sqlrcur_free(cur);
159> sqlrcon_free(con);
160> }
161>
162
163If you're using a database with an embedded procedural language, you may want to retrieve data from a call to one of it's functions. To facilitate this, SQL Relay provides the sqlrcur_defineOutputBind() and sqlrcur_getOutputBind() methods.
164
165> PL/SQL Procedure:
166>
167>
168>
169> FUNCTION sp_mytable RETURN types.cursorType
170> l_cursor types.cursorType;
171> BEGIN
172> OPEN l_cursor FOR **SELECT** * FROM mytable;
173> RETURN l_cursor;
174> END;
175>
176>
177>
178> Program code:
179>
180>
181> #include <sqlrelay sqlrclientwrapper.h="">
182> #include <stdio.h>
183>
184> main() {
185>
186> **int** result;
187>
188> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
189> sqlrcur cur=sqlrcur_alloc(con);
190>
191> sqlrcur_prepareQuery(cur,"begin :result:=addTwoNumbers(:num1,:num2); end;");
192> sqlrcur_inputBindLong(cur,"num1",10);
193> sqlrcur_inputBindLong(cur,"num2",20);
194> sqlrcur_defineOutputBind(cur,"result",100);
195> sqlrcur_executeQuery(cur);
196> result=atoi(sqlrcur_getOutputBind(cur,"result"));
197> sqlrcon_endSession(con);
198>
199> ... do something with the result ...
200>
201> sqlrcur_free(cur);
202> sqlrcon_free(con);
203> }
204>
205
206The sqlrcur_getOutputBind() function returns a NULL value as an empty string. If you would it to come back as a NULL instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().
207
208The sqlrcur_getOutputBind() function returns a string, if you would like to get the value as a long or double, you can use sqlrcur_getOutputBindAsLong() or sqlrcur_getOutputBindAsDouble().
209
210If you are using Oracle 8i or higher, you can insert data into BLOB and CLOB columns using the inputBindBlob(), inputBindClob() methods.
211
212If you are curious how many bind variables have been declared in a query, you can call sqlrcur_countBindVariables() after preparing the query.
213
214>
215> #include <sqlrelay sqlrclientwrapper.h="">
216>
217> main() {
218>
219> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
220> sqlrcur cur=sqlrcur_alloc(con);
221>
222> sqlrcur_executeQuery(cur,"create table images (image blob, description clob)");
223>
224> **unsigned** **char** imagedata[40000];
225> **unsigned** **long** imagelength;
226>
227> ... read an image from a file into imagedata and the length of the
228> file into imagelength ...
229>
230> **unsigned** **char** description[40000];
231> **unsigned** **long** desclength;
232>
233> ... read a description from a file into description and the length of
234> the file into desclength ...
235>
236> sqlrcur_prepareQuery(cur,"insert into images values (:image,:desc)");
237> sqlrcur_inputBindBlob(cur,"image",imagedata,imagelength);
238> sqlrcur_inputBindClob(cur,"desc",description,desclength);
239> sqlrcur_executeQuery(cur);
240>
241> sqlrcur_free(cur);
242> sqlrcur_free(con);
243> }
244>
245
246Likewise, with Oracle 8i, you can retreive BLOB or CLOB data using defineOutputBindBlob(), defineOutputBindClob(), getOutputBind() and getOutputBindLength().
247
248>
249> #include <sqlrelay sqlrclientwrapper.h="">
250>
251> main() {
252>
253> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
254> sqlrcur cur=sqlrcur_alloc(con);
255>
256> sqlrcur_prepareQuery(cur,"begin select image into :image from images; select description into :desc from images; end;");
257> sqlrcur_defineOutputBindBlob(cur,"image");
258> sqlrcur_defineOutputBindClob(cur,"desc");
259> sqlrcur_executeQuery(cur);
260>
261> **char** *image=sqlrcur_getOutputBind(cur,"image");
262> **long** imagelength=sqlrcur_getOutputBindLength(cur,"image");
263>
264> **char** *desc=sqlrcur_getOutputBind(cur,"desc");
265> **char** *desclength=sqlrcur_getOutputBindLength(cur,"desc");
266>
267> sqlrcon_endSession(con);
268>
269> ... do something with image and desc ...
270>
271> sqlrcur_free(cur);
272> sqlrcon_free(con);
273> }
274>
275
276Sometimes it's convenient to bind a bunch of variables that may or may not actually be in the query. For example, if you are building a web based application, it may be easy to just bind all the form variables/values from the previous page, even though some of them don't appear in the query. Databases usually generate errors in this case. Calling validateBinds() just prior to calling executeQuery() causes the API to check the query for each bind variable before actually binding it, preventing those kinds of errors. There is a performance cost associated with calling validateBinds().
277
278Re-Binding and Re-Execution
279
280
281
282Another feature of the prepare/bind/execute paradigm is the ability to prepare, bind and execute a query once, then re-bind and re-execute the query over and over without re-preparing it. If your backend database natively supports this paradigm, you can reap a substantial performance improvement.
283
284>
285> #include <sqlrelay sqlrclientwrapper.h="">
286> #include <stdio.h>
287>
288> main() {
289>
290> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
291> sqlrcur cur=sqlrcur_alloc(con);
292>
293> sqlrcur_prepareQuery(cur,"select * from mytable where mycolumn>:value");
294> sqlrcur_inputBindLong(cur,"value",1);
295> sqlrcur_executeQuery(cur);
296>
297> ... process the result set ...
298>
299> sqlrcur_clearBinds(cur);
300> sqlrcur_inputBindLong(cur,"value",5);
301> sqlrcur_executeQuery(cur);
302>
303> ... process the result set ...
304>
305> sqlrcur_clearBinds(cur);
306> sqlrcur_inputBindLong(cur,"value",10);
307> sqlrcur_executeQuery(cur);
308>
309> ... process the result set ...
310>
311> sqlrcur_free(cur);
312> sqlrcon_free(con);
313> }
314>
315
316Accessing Fields in the Result Set
317
318
319
320The sqlrcur_rowCount(), sqlrcur_colCount(), sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions are useful for processing result sets.
321
322>
323> #include <sqlrelay sqlrclientwrapper.h="">
324> #include <stdio.h>
325>
326> main() {
327>
328> **int** row,col;
329>
330> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
331> sqlrcur cur=sqlrcur_alloc(con);
332>
333> sqlrcur_sendQuery(cur,"select * from my_table");
334> sqlrcon_endSession(con);
335>
336> **for** (row=0; row<sqlrcur_rowcount(cur); row++)="" {=""> **for** (col=0; col<sqlrcur_colcount(cur); col++)="" {=""> printf("%s,",sqlrcur_getFieldByIndex(cur,row,col));
337> }
338> printf("\n");
339> }
340>
341> sqlrcur_free(cur);
342> sqlrcon_free(con);
343> }
344>
345
346The sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions return strings. If you would like to get a field as a long or double, you can use sqlrcur_getFieldAsLongByName()/sqlrcur_getFieldAsLongByIndex() and sqlrcur_getFieldAsDoubleByName()/sqlrcur_getFieldAsDoubleByIndex().
347
348You can also use sqlrcur_getRow() which returns a NULL-terminated array of the fields in the row.
349
350>
351> #include <sqlrelay sqlrclientwrapper.h="">
352> #include <stdio.h>
353>
354> main() {
355>
356> **int** row,col;
357>
358> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
359> sqlrcur cur=sqlrcur_alloc(con);
360>
361> sqlrcur_sendQuery(cur,"select * from my_table");
362> sqlrcon_endSession(con);
363>
364> **for** (row=0; row<sqlrcur_rowcount(cur); row++)="" {=""> **char** **rowarray=sqlrcur_getRow(cur,row);
365> **for** (col=0; col<sqlrcur_colcount(cur); col++)="" {=""> printf("%s,",rowarray[col]);
366> }
367> printf("\n");
368> }
369>
370> sqlrcur_free(cur);
371> sqlrcon_free(con);
372> }
373>
374
375The sqlrcur_getFieldByIndex(), sqlrcur_getFieldByName() and sqlrcur_getRow() functions return NULL fields as empty strings. If you would like them to come back as NULL's instead, you can call the sqlrcur_getNullsAsNulls() method. To revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().
376
377If you want to access the result set, but don't care about the column information (column names, types or sizes) and don't mind getting fields by their numeric index instead of by name, you can call the sqlrcur_dontGetColumnInfo() method prior to executing your query. This can result in a performance improvement, especially when many queries with small result sets are executed in rapid succession. You can call sqlrcur_getColumnInfo() again later to turn off this feature.
378
379Dealing With Large Result Sets
380
381
382
383SQL Relay normally buffers the entire result set. This can speed things up at the cost of memory. With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.
384
385Use sqlrcur_setResultSetBufferSize() to set the number of rows to buffer at a time. Calls to sqlrcur_getRow(), sqlrcur_getFieldByIndex() and sqlrcur_getFieldByName() cause the chunk containing the requested field to be fetched. Rows in that chunk are accessible but rows before it are not.
386
387For example, if you setResultSetBufferSize(5) and execute a query that returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, then 15-19. When rows 5-9 are available, getFieldByIndex(0,0) will return NULL and getFieldByIndex(11,0) will cause rows 10-14 to be fetched and return the requested value.
388
389When buffering the result set in chunks, don't end the session until after you're done with the result set.
390
391If you call sqlrcur_setResultSetBufferSize() and forget what you set it to, you can always call sqlrcur_getResultSetBufferSize().
392
393When buffering a result set in chunks, the sqlrcur_rowCount() method returns the number of rows returned so far. The sqlrcur_firstRowIndex() method returns the index of the first row of the currently buffered chunk.
394
395>
396> #include <sqlrelay sqlrclientwrapper.h="">
397> #include <stdio.h>
398>
399> main() {
400>
401> **int** done=0;
402> **int** row=0;
403> **int** col;
404> **char** *field;
405>
406> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
407> sqlrcur cur=sqlrcur_alloc(con);
408>
409> sqlrcur_setResultSetBufferSize(cur,5);
410>
411> sqlrcur_sendQuery(cur,"select * from my_table");
412>
413> **while** (!done) {
414> **for** (col=0; col<sqlrcur_colcount(cur); col++)="" {=""> **if** (field=sqlrcur_getFieldByIndex(cur,row,col)) {
415> printf("%s,",field);
416> } **else** {
417> done=1;
418> }
419> }
420> printf("\n");
421> row++;
422> }
423>
424> sqlrcur_sendQuery(cur,"select * from my_other_table");
425>
426> ... process this query's result set in chunks also ...
427>
428> sqlrcur_setResultSetBufferSize(cur,0);
429>
430> sqlrcur_sendQuery(cur,"select * from my_third_table");
431>
432> ... process this query's result set all at once ...
433>
434> sqlrcon_endSession(con);
435>
436> sqlrcur_free(cur);
437> sqlrcon_free(con);
438> }
439>
440
441Cursors
442
443
444
445Cursors make it possible to execute queries while processing the result set of another query. You can select rows from a table in one query, then iterate through it's result set, inserting rows into another table, using only 1 database connection for both operations.
446
447For example:
448
449
450 #include <sqlrelay sqlrclientwrapper.h="">
451
452 main() {
453
454 sqlrcon con;
455 sqlrcur cursor1;
456 sqlrcur cursor2;
457 **int** index;
458
459 con=new sqlrcon_alloc("host",9000,"","user","password",0,1);
460 cursor1=new sqlrcur_alloc(con);
461 cursor2=new sqlrcur_alloc(con);
462
463 sqlrcur_setResultSetBufferSize(cursor1,10);
464 sqlrcur_sendQuery(cursor1,"select * from my_huge_table");
465
466 index=0;
467 **while** (!sqlrcur_endOfResultSet(cursor1)) {
468 sqlrcur_prepareQuery(cursor2,"insert into my_other_table values (:1,:2,:3)");
469 sqlrcur_inputBindString(cursor2,"1",sqlrcur_getFieldByIndex(cursor1,index,1));
470 sqlrcur_inputBindString(cursor2,"2",sqlrcur_getFieldByIndex(cursor1,index,2));
471 sqlrcur_inputBindString(cursor2,"3",sqlrcur_getFieldByIndex(cursor1,index,3));
472 sqlrcur_executeQuery(cursor2);
473 }
474
475 sqlrcur_free(cursor2);
476 sqlrcur_free(cursor1);
477 sqlrcon_free(con);
478 }
479
480
481Prior to SQL Relay version 0.25, you would have had to buffer the first result set or use 2 database connections instead of just 1.
482
483If you are using stored procedures with Oracle 8i or higher, a stored procedure can execute a query and return a cursor. A cursor bind variable can then retrieve that cursor. Your program can retrieve the result set from the cursor. All of this can be accomplished using defineOutputBindCursor(), getOutputBindCursor() and fetchFromOutputBindCursor().
484
485>
486> #include <sqlrelay sqlrclientwrapper.h="">
487> #include <stdio.h>
488>
489> main() {
490>
491> **int** i,j;
492>
493> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
494> sqlrcur cur=sqlrcur_alloc(con);
495>
496> sqlrcur_prepareQuery(cur,"begin :curs:=sp_mytable; end;");
497> sqlrcur_defineOutputBindCursor(cur,"curs");
498> sqlrcur_executeQuery(cur);
499>
500> sqlrcur bindcur=sqlrcur_getOutputBindCursor(cur,"curs");
501> sqlrcur_fetchFromBindCursor(bindcur);
502>
503> // print fields from table
504> **for** (i=0; i<sqlrcur_rowcount(bindcur); i++)="" {=""> **for** (j=0; j<sqlrcur_colcount(bindcur); j++)="" {=""> printf("%s,",sqlrcur_getFieldByIndex(bindcur,i,j));
505> }
506> printf("\n");
507> }
508>
509> sqlrcur_free(bindcur);
510>
511> sqlrcur_free(cur);
512> sqlrcon_free(con);
513> }
514>
515
516The number of cursors simultaneously available per-connection is set at compile time and defaults to 5.
517
518Getting Column Information
519
520
521
522For each column, the API supports getting the name, type and length of each field. All databases support these attributes. The API also supports getting the precision, scale, length of the longest field, and whether the column is nullable, the primary key, unique, part of a key, unsigned, zero-filled, binary, or an auto-incrementing field. However, not all databases support these attributes. If a database doesn't support an attribute, it is always returned as false.
523
524>
525> #include <sqlrelay sqlrclientwrapper.h="">
526> #include <stdio.h>
527>
528> main() {
529>
530> **int** i;
531>
532> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
533> sqlrcur cur=sqlrcur_alloc(con);
534>
535> sqlrcur_sendQuery(cur,"select * from my_table");
536> sqlrcon_endSession(con);
537>
538> **for** (i=0; i<sqlrcur_colcount(cur); i++)="" {=""> printf("Name: %s\n",sqlrcur_getColumnName(cur,i));
539> printf("Type: %s\n",sqlrcur_getColumnType(cur,i));
540> printf("Length: %d\n",sqlrcur_getColumnLength(cur,i));
541> printf("Precision: %d\n",sqlrcur_getColumnPrecision(cur,i));
542> printf("Scale: %d\n",sqlrcur_getColumnScale(cur,i));
543> printf("Longest Field: %d\n",sqlrcur_getLongest(cur,i));
544> printf("Nullable: %d\n",sqlrcur_getColumnIsNullable(cur,i));
545> printf("Primary Key: %d\n",sqlrcur_getColumnIsPrimaryKey(cur,i));
546> printf("Unique: %d\n",sqlrcur_getColumnIsUnique(cur,i));
547> printf("Part of Key: %d\n",sqlrcur_getColumnIsPartOfKey(cur,i));
548> printf("Unsigned: %d\n",sqlrcur_getColumnIsUnsigned(cur,i));
549> printf("Zero Filled: %d\n",sqlrcur_getColumnIsZeroFilled(cur,i));
550> printf("Binary: %d\n",sqlrcur_getColumnIsBinary(cur,i));
551> printf("Auth Increment:%d\n",sqlrcur_getColumnIsAutoIncrement(cur,i));
552> printf("\n");
553> }
554>
555> sqlrcur_free(cur);
556> sqlrcon_free(con);
557> }
558>
559
560Some databases force column names to upper case, others force column names to lower case, and others still support mixed-case column names. Sometimes, when migrating between databases, you can run into trouble. You can use upperCaseColumnNames() and lowerCaseColumnNames() to cause column names to be converted to upper or lower case, or you can use mixedCaseColumnNames() to cause column names to be returned in the same case as they are defined in the database.
561
562>
563> #include <sqlrelay sqlrclientwrapper.h="">
564> #include <stdio.h>
565>
566> main() {
567>
568> **int** i;
569>
570> sqlrcon con=sqlrcon_alloc("host",9000,"","user","password",0,1);
571> sqlrcur cur=sqlrcur_alloc(con);
572>
573> // column names will be forced to upper case
574> sqlrcur_upperCaseColumnNames(cur);
575> sqlrcur_endQuery(cur,"select * from my_table");
576> sqlrcon_endSession(con);
577>
578> **for** (i=0; i<sqlrcur_colcount(cur); i++)="" {=""> printf("Name: %s\n",sqlrcur_getColumnName(cur,i));
579> }
580>
581> // column names will be forced to lower case
582> sqlrcur_lowerCaseColumnNames(cur);
583> sqlrcur_endQuery(cur,"select * from my_table");
584> sqlrcon_endSession(con);
585>
586> **for** (i=0; i<sqlrcur_colcount(cur); i++)="" {=""> printf("Name: %s\n",sqlrcur_getColumnName(cur,i));
587> }
588>
589> // column names will be the same as they are in the database
590> sqlrcur_mixedCaseColumnNames(cur);
591> sqlrcur_endQuery(cur,"select * from my_table");
592> sqlrcon_endSession(con);
593>
594> **for** (i=0; i<sqlrcur_colcount(cur); i++)="" {=""> printf("Name: %s\n",sqlrcur_getColumnName(cur,i));
595> }
596>
597> sqlrcur_free(cur);
598> sqlrcon_free(con);
599> }
600>
601
602Stored Procedures
603
604
605
606Many databases support stored procedures. Stored procedures are sets of queries and procedural code that are executed inside of the database itself. For example, a stored procedure may select rows from one table, iterate through the result set and, based on the values in each row, insert, update or delete rows in other tables. A client program could do this as well, but a stored procedure is generally more efficient because queries and result sets don't have to be sent back and forth between the client and database. Also, stored procedures are generally stored in the database in a compiled state, while queries may have to be re-parsed and re-compiled each time they are sent.
607
608While many databases support stored procedures. The syntax for creating and executing stored procedures varies greatly between databases.
609
610SQL Relay supports stored procedures for most databases, but there are some caveats. Stored procedures are not currently supported when using FreeTDS against Sybase or Microsoft SQL Server. Blob/Clob bind variables are only supported in Oracle 8i or higher. Sybase stored procedures must use varchar output parameters.
611
612Stored procedures typically take input paramters from client programs through input bind variables and return values back to client programs either through bind variables or result sets. Stored procedures can be broken down into several categories, based on the values that they return. Some stored procedures don't return any values, some return a single value, some return multiple values and some return entire result sets.
613
614No Values
615
616
617Some stored procedures don't return any values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.
618
619Oracle
620
621
622To create the stored procedure, run a query like the following.
623
624>
625> **create** procedure testproc(in1 **in** **number** , in2 **in** **number** , in3 **in** **varchar2** ) is
626> begin
627> **insert** into mytable values (in1,in2,in3);
628> end;
629>
630
631To execute the stored procedure from an SQL Relay program, use code like the following.
632
633>
634> sqlrcur_prepareQuery(cur,"begin testproc(:in1,:in2,:in3); end;");
635> sqlrcur_inputBindLong(cur,"in1",1);
636> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
637> sqlrcur_inputBindString(cur,"in3","hello");
638> sqlrcur_executeQuery(cur);
639>
640
641To drop the stored procedure, run a query like the following.
642
643>
644> **drop** procedure testproc
645>
646
647Sybase and Microsoft SQL Server
648
649
650To create the stored procedure, run a query like the following.
651
652>
653> **create** procedure testproc @in1 int, @in2 **float** , @in3 **varchar** (20) as
654> **insert** into mytable values (@in1,@in2,@in3)
655>
656
657To execute the stored procedure from an SQL Relay program, use code like the following.
658
659>
660> sqlrcur_prepareQuery(cur,"exec testproc");
661> sqlrcur_inputBindLong(cur,"in1",1);
662> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
663> sqlrcur_inputBindString(cur,"in3","hello");
664> sqlrcur_executeQuery(cur);
665>
666
667To drop the stored procedure, run a query like the following.
668
669>
670> **drop** procedure testproc
671>
672
673Interbase and Firebird
674
675
676To create the stored procedure, run a query like the following.
677
678>
679> **create** procedure testproc(in1 **integer** , in2 **float** , in3 **varchar** (20)) as
680> begin
681> **insert** into mytable values (in1,in2,in3);
682> suspend;
683> end;
684>
685
686To execute the stored procedure from an SQL Relay program, use code like the following.
687
688>
689> sqlrcur_prepareQuery(cur,"execute procedure testproc ?, ?, ?");
690> sqlrcur_inputBindLong(cur,"1",1);
691> sqlrcur_inputBindDouble(cur,"2",1.1,2,1);
692> sqlrcur_inputBindString(cur,"3","hello");
693> sqlrcur_executeQuery(cur);
694>
695
696To drop the stored procedure, run a query like the following.
697
698>
699> **drop** procedure testproc
700>
701
702DB2
703
704
705To create the stored procedure, run a query like the following.
706
707>
708> **create** procedure testproc( **in** in1 int, **in** in2 double, **in** in3 **varchar** (20)) language sql
709> begin
710> **insert** into mytable values (in1,in2,in3);
711> end;
712>
713
714To execute the stored procedure from an SQL Relay program, use code like the following.
715
716>
717> sqlrcur_prepareQuery(cur,"call testproc(?,?,?)");
718> sqlrcur_inputBindLong(cur,"1",1);
719> sqlrcur_inputBindDouble(cur,"2",1.1,2,1);
720> sqlrcur_inputBindString(cur,"3","hello");
721> sqlrcur_executeQuery(cur);
722>
723
724To drop the stored procedure, run a query like the following.
725
726>
727> **drop** procedure testproc
728>
729
730Postgresql
731
732
733To create the stored procedure, run a query like the following.
734
735>
736> **create** function testproc(int, **float** , **varchar** (20)) returns void as '
737> begin
738> **insert** into mytable values ($1,$2,$3);
739> return;
740> end;' language plpgsql
741>
742
743To execute the stored procedure from an SQL Relay program, use code like the following.
744
745>
746> sqlrcur_prepareQuery(cur,"select testproc(:in1,:in2,:in3)");
747> sqlrcur_inputBindLong(cur,"in1",1);
748> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
749> sqlrcur_inputBindString(cur,"in3","hello");
750> sqlrcur_executeQuery(cur);
751>
752
753To drop the stored procedure, run a query like the following.
754
755>
756> **drop** procedure testproc
757>
758
759
760Single Values
761
762
763Some stored procedures return single values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.
764
765Oracle
766
767
768In Oracle, stored procedures can return values through output parameters or as return values of the procedure itself.
769
770Here is an example where the procedure itself returns a value. Note that Oracle calls these functions.
771
772To create the stored procedure, run a query like the following.
773
774>
775> **create** function testproc(in1 **in** **number** , in2 **in** **number** , in3 **in** **varchar2** ) returns **number** is
776> begin
777> return in1;
778> end;
779>
780
781To execute the stored procedure from an SQL Relay program, use code like the following.
782
783>
784> sqlrcur_prepareQuery(cur,"select testproc(:in1,:in2,:in3) from dual");
785> sqlrcur_inputBindLong(cur,"in1",1);
786> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
787> sqlrcur_inputBindString(cur,"in3","hello");
788> sqlrcur_executeQuery(cur);
789> **char** *result=sqlrcur_getFieldByIndex(cur,0,0);
790>
791
792To drop the stored procedure, run a query like the following.
793
794>
795> **drop** function testproc
796>
797
798Here is an example where the value is returned through an output parameter.
799
800To create the stored procedure, run a query like the following.
801
802>
803> **create** procedure testproc(in1 **in** **number** , in2 **in** **number** , in3 **in** **varchar2** , out1 **out** **number** ) as
804> begin
805> out1:=in1;
806> end;
807>
808
809To execute the stored procedure from an SQL Relay program, use code like the following.
810
811>
812> sqlrcur_prepareQuery(cur,"begin testproc(:in1,:in2,:in3,:out1); end;");
813> sqlrcur_inputBindLong(cur,"in1",1);
814> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
815> sqlrcur_inputBindString(cur,"in3","hello");
816> sqlrcur_defineOutputBind(cur,"out1",20);
817> sqlrcur_executeQuery(cur);
818> **char** *result=sqlrcur_getOutputBind(cur,"out1");
819>
820
821To drop the stored procedure, run a query like the following.
822
823>
824> **drop** procedure testproc
825>
826
827Sybase and Microsoft SQL Server
828
829
830In Sybase and Microsoft SQL Server, stored procedures return values through output parameters rather than as return values of the procedure itself.
831
832To create the stored procedure, run a query like the following.
833
834>
835> **create** procedure testproc @in1 int, @in2 **float** , @in3 **varchar** (20), @out1 int output as
836> **select** @out1=convert( **varchar** (20),@in1)
837>
838
839To execute the stored procedure from an SQL Relay program, use code like the following.
840
841>
842> sqlrcur_prepareQuery(cur,"exec testproc");
843> sqlrcur_inputBindLong(cur,"in1",1);
844> sqlrcur_inputBindDouble(cur,"in2",1.1,2,1);
845> sqlrcur_inputBindString(cur,"in3","hello");
846> sqlrcur_defineOutputBind(cur,"out1",20);
847> sqlrcur_executeQuery(cur);
848> **char** *result=sqlrcur_getOutputBind(cur,"out1");
849>
850
851To drop the stored procedure, run a query like the following.
852
853>
854> **drop** procedure testproc
855>
856
857Interbase and Firebird
858
859
860To create the stored procedure, run a query like the following.
861
862>
863> **create** procedure testproc(in1 **integer** , in2 **float** , in3 **varchar** (20)) returns (out1 **integer** ) as
864> begin
865> out1=in1;
866> suspend;
867> end;
868>
869
870To execute the stored procedure from an SQL Relay program, use code like the following.
871
872>
873> sqlrcur_prepareQuery(cur,"select * from testproc(?,?,?)");
874> sqlrcur_inputBindLong(cur,"1",1);
875> sqlrcur_inputBindDouble(cur,"2",1.1,2,1);
876> sqlrcur_inputBindString(cur,"3","hello");
877> sqlrcur_executeQuery(cur);
878> **char** *result=sqlrcur_getFieldByIndex(cur,0,0);
879>
880
881Alternatively, you can run a query like the following and receive the result using an output bind variable. Note that in Interbase/Firebird, input and output bind variable indices are distict from one another. The index of the output bind variable is 1 rather than 4, even though there were 3 input bind variables.
882
883>
884> sqlrcur_prepareQuery(cur,"execute procedure testproc ?, ?, ?");
885> sqlrcur_inputBindLong(cur,"1",1);
886> sqlrcur_inputBindDouble(cur,"2",1.1,2,1);
887> sqlrcur_inputBindString(cur,"3","hello");
888> sqlrcur_defineOutputBind(cur,"1",20);
889> sqlrcur_executeQuery(cur);
890> **char** *result=sqlrcur_getOutputBind(cur,"1");
891>
892
893To drop the stored procedure, run a query like the following.
894
895>
896> **drop** procedure testproc
897>
898
899DB2
900
901
902In DB2, stored procedures return values through output parameters rather than as return values of the procedure itself.
903
904To create the stored procedure, run a query like the following.
905
906>
907> **create** procedure testproc( **in** in1 int, **in** in2 double, **in** in3 **varchar** (20), **out** out1 int) language sql
908> begin
909> **set** out1 = in1;
910> end
911>
912
913To execute the stored procedure from an SQL Relay program, use code like the following.
914
915>
916> sqlrcur_prepareQuery(cur,"call testproc(?,?,?,?)");
917> sqlrcur_inputBindLong(cur,"1",1);
918> sqlrcur_inputBindDouble(cur,"2",1.1,2,1);
919> sqlrcur_inputBindString(cur,"3","hello");
920> sqlrcur_defineOutputBind(cur,"4",25);
921> sqlrcur_executeQuery(cur);
922> **char** *result=sqlrcur_getOutputBind(cur,"4");
923>
924
925To drop the stored procedure, run a query like the following.
926
927>
928> **drop** procedure testproc
929>
930
931Postgresql
932
933
934To create the stored procedure, run a query like the following.
935
936>
937> **create** function testfunc(int, **float** , **char** (20)) returns int as '
938> declare
939> in1 int;
940> in2 **float** ;
941> in3 **char** (20);
942> begin
943> in1:=$1;
944> return;
945> end;
946> ' language plpgsql
947>
948
949To execute the stored procedure from an SQL Relay program, use code like the following.
950
951>
952> sqlrcur_prepareQuery(cur,"select * from testfunc(:in1,:in2,:in3)");
953> sqlrcur_inputBindLong(cur,"in1",1);
954> sqlrcur_inputBindDouble(cur,"in2",1.1,4,2);
955> sqlrcur_inputBindString(cur,"in3","hello");
956> sqlrcur_executeQuery(cur);
957> **char** *result=sqlrcur_getFieldByIndex(cur,0,0);
958>
959
960To drop the stored procedure, run a query like the following.
961
962>
963> **drop** function testfunc(int, **float** , **char** (20))
964>
965
966
967Multiple Values
968
969
970Some stored procedures return multiple values. Below are examples, illustrating how to create, execute and drop this kind of stored procedure for each database that SQL Relay supports.
971
972Oracle
973
974
975In Oracle, stored procedures can return values through output parameters or as return values of the procedure itself. If a procedure needs to return multiple values, it can return one of them as the return value of the procedure itself, but the rest must be returned through output parameters.
976
977To create the stored procedure, run a query like the following.
978
979>
980> **create** procedure testproc(in1 <fo</sqlrcur_colcount(cur);></sqlrcur_colcount(cur);></sqlrcur_colcount(cur);></stdio.h></sqlrelay></sqlrcur_colcount(cur);></stdio.h></sqlrelay></sqlrcur_colcount(bindcur);></sqlrcur_rowcount(bindcur);></stdio.h></sqlrelay></sqlrelay></sqlrcur_colcount(cur);></stdio.h></sqlrelay></sqlrcur_colcount(cur);></sqlrcur_rowcount(cur);></stdio.h></sqlrelay></sqlrcur_colcount(cur);></sqlrcur_rowcount(cur);></stdio.h></sqlrelay></stdio.h></sqlrelay></sqlrelay></sqlrelay></stdio.h></sqlrelay></stdio.h></sqlrelay></stdio.h></sqlrelay></stdio.h></sqlrelay></stdio.h></sqlrelay></sqlrelay>