00001 #include <stdio.h>
00002 #ifndef __USE_ISOC99
00003 #define __USE_ISOC99
00004 #include <stdlib.h>
00005 #undef __USE_ISOC99
00006 #else
00007 #include <stdlib.h>
00008 #endif
00009 #include <string.h>
00010 #include <alloca.h>
00011 #include <stdarg.h>
00012 #include "db.h"
00013 #include "xassert.h"
00014 #include "util.h"
00015 #include "xmem.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 int db_dmsv(DB_Handle_t *dbin, int *row_count, char *query,
00028 int n_rows, ...)
00029 {
00030 int status=1;
00031 DB_Type_t intype[MAXARG];
00032 void *argin[MAXARG];
00033 int n,i;
00034 char *q;
00035 db_char_t tchar;
00036 db_int1_t tint1;
00037 db_int2_t tint2;
00038 db_int4_t tint4;
00039 db_int8_t tint8;
00040 db_float_t tfloat;
00041 db_double_t tdouble;
00042
00043
00044 va_list ap;
00045 va_start(ap, n_rows);
00046
00047
00048 q = (char *)query;
00049 n = 0;
00050 while (*q)
00051 {
00052 if (*q == '?')
00053 {
00054 if (n>=MAXARG)
00055 {
00056 fprintf(stderr,"Maximum number of arguments exceeded.\n");
00057 goto failure;
00058 }
00059 n++;
00060 }
00061 q++;
00062 }
00063
00064 for (i=0; i<n; i++)
00065 {
00066 intype[i] = va_arg(ap, DB_Type_t);
00067 if (n_rows == -1)
00068 {
00069 switch(intype[i])
00070 {
00071 case DB_CHAR:
00072 tchar = (db_char_t) va_arg(ap, int);
00073 argin[i] = alloca(sizeof(db_char_t));
00074 memcpy(argin[i], &tchar, sizeof(db_char_t));
00075 break;
00076 case DB_INT1:
00077 tint1 = (db_int1_t) va_arg(ap, int);
00078 argin[i] = alloca(sizeof(db_int1_t));
00079 memcpy(argin[i], &tint1, sizeof(db_int1_t));
00080 break;
00081 case DB_INT2:
00082 tint2 = (db_int2_t) va_arg(ap, int);
00083 argin[i] = alloca(sizeof(db_int2_t));
00084 memcpy(argin[i], &tint2, sizeof(db_int2_t));
00085 break;
00086 case DB_INT4:
00087 tint4 = va_arg(ap, db_int4_t);
00088 argin[i] = alloca(sizeof(db_int4_t));
00089 memcpy(argin[i], &tint4, sizeof(db_int4_t));
00090 break;
00091 case DB_INT8:
00092 tint8 = va_arg(ap, db_int8_t);
00093 argin[i] = alloca(sizeof(db_int8_t));
00094 memcpy(argin[i], &tint8, sizeof(db_int8_t));
00095 break;
00096 case DB_FLOAT:
00097 tfloat = (db_float_t) va_arg(ap,double);
00098 argin[i] = alloca(sizeof(db_float_t));
00099 memcpy(argin[i], &tfloat, sizeof(db_float_t));
00100 break;
00101 case DB_DOUBLE:
00102 tdouble = va_arg(ap, db_double_t);
00103 argin[i] = alloca(sizeof(db_double_t));
00104 memcpy(argin[i], &tdouble, sizeof(db_double_t));
00105 break;
00106 case DB_STRING:
00107 case DB_VARCHAR:
00108 argin[i] = alloca(sizeof(char **));
00109 *((char **)argin[i]) = va_arg(ap, db_string_t);
00110 break;
00111 }
00112 }
00113 else
00114 {
00115 argin[i] = va_arg(ap, void *);
00116 }
00117 }
00118
00119 if (n_rows == -1)
00120 n_rows = 1;
00121
00122 status = db_dms_array(dbin, row_count, query, n_rows, n,
00123 intype, argin );
00124 failure:
00125 va_end(ap);
00126 return status;
00127 }
00128
00129
00130
00131 int db_bulk_insertv(DB_Handle_t *dbin, char *table,
00132 int n_rows, int n_cols, ...)
00133 {
00134 int status=1;
00135 DB_Type_t intype[MAXARG];
00136 void *argin[MAXARG];
00137 int i;
00138 db_char_t tchar;
00139 db_int1_t tint1;
00140 db_int2_t tint2;
00141 db_int4_t tint4;
00142 db_int8_t tint8;
00143 db_float_t tfloat;
00144 db_double_t tdouble;
00145
00146 va_list ap;
00147 va_start(ap, n_cols);
00148
00149 for (i=0; i<n_cols; i++)
00150 {
00151 intype[i] = va_arg(ap, DB_Type_t);
00152 if (n_rows == -1)
00153 {
00154 switch(intype[i])
00155 {
00156 case DB_CHAR:
00157 tchar = (db_char_t) va_arg(ap, int);
00158 argin[i] = alloca(sizeof(db_char_t));
00159 memcpy(argin[i], &tchar, sizeof(db_char_t));
00160 break;
00161 case DB_INT1:
00162 tint1 = (db_int1_t) va_arg(ap, int);
00163 argin[i] = alloca(sizeof(db_int1_t));
00164 memcpy(argin[i], &tint1, sizeof(db_int1_t));
00165 break;
00166 case DB_INT2:
00167 tint2 = (db_int2_t) va_arg(ap, int);
00168 argin[i] = alloca(sizeof(db_int2_t));
00169 memcpy(argin[i], &tint2, sizeof(db_int2_t));
00170 break;
00171 case DB_INT4:
00172 tint4 = va_arg(ap, db_int4_t);
00173 argin[i] = alloca(sizeof(db_int4_t));
00174 memcpy(argin[i], &tint4, sizeof(db_int4_t));
00175 break;
00176 case DB_INT8:
00177 tint8 = va_arg(ap, db_int8_t);
00178 argin[i] = alloca(sizeof(db_int8_t));
00179 memcpy(argin[i], &tint8, sizeof(db_int8_t));
00180 break;
00181 case DB_FLOAT:
00182 tfloat = (db_float_t) va_arg(ap,double);
00183 argin[i] = alloca(sizeof(db_float_t));
00184 memcpy(argin[i], &tfloat, sizeof(db_float_t));
00185 break;
00186 case DB_DOUBLE:
00187 tdouble = va_arg(ap, db_double_t);
00188 argin[i] = alloca(sizeof(db_double_t));
00189 memcpy(argin[i], &tdouble, sizeof(db_double_t));
00190 break;
00191 case DB_STRING:
00192 case DB_VARCHAR:
00193 argin[i] = alloca(sizeof(char **));
00194 *((char **)argin[i]) = va_arg(ap, db_string_t);
00195 break;
00196 }
00197 }
00198 else
00199 {
00200 argin[i] = va_arg(ap, void *);
00201 }
00202 }
00203
00204 if (n_rows == -1)
00205 n_rows = 1;
00206
00207 status = db_bulk_insert_array(dbin, table, n_rows, n_cols, intype, argin );
00208 va_end(ap);
00209 return status;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219 DB_Binary_Result_t *db_query_binv(DB_Handle_t *dbin, char *query, ...)
00220 {
00221 DB_Type_t intype[MAXARG];
00222 void *argin[MAXARG];
00223 int n,i;
00224 char *q;
00225 DB_Binary_Result_t *result;
00226 db_char_t tchar;
00227 db_int1_t tint1;
00228 db_int2_t tint2;
00229 db_int4_t tint4;
00230 db_int8_t tint8;
00231 db_float_t tfloat;
00232 db_double_t tdouble;
00233
00234 va_list ap;
00235 va_start(ap, query);
00236
00237
00238 q = (char *)query;
00239 n = 0;
00240 while (*q)
00241 {
00242 if (*q == '?')
00243 {
00244 if (n>=MAXARG)
00245 {
00246 fprintf(stderr,"Maximum number of arguments exceeded.\n");
00247 result = NULL;
00248 goto failure;
00249 }
00250 n++;
00251 }
00252 q++;
00253 }
00254
00255 for (i=0; i<n; i++)
00256 {
00257 intype[i] = va_arg(ap, DB_Type_t);
00258 switch(intype[i])
00259 {
00260 case DB_CHAR:
00261 tchar = (db_char_t) va_arg(ap, int);
00262 argin[i] = alloca(sizeof(db_char_t));
00263 memcpy(argin[i], &tchar, sizeof(db_char_t));
00264 break;
00265 case DB_INT1:
00266 tint1 = (db_int1_t) va_arg(ap, int);
00267 argin[i] = alloca(sizeof(db_int1_t));
00268 memcpy(argin[i], &tint1, sizeof(db_int1_t));
00269 break;
00270 case DB_INT2:
00271 tint2 = (db_int2_t) va_arg(ap, int);
00272 argin[i] = alloca(sizeof(db_int2_t));
00273 memcpy(argin[i], &tint2, sizeof(db_int2_t));
00274 break;
00275 case DB_INT4:
00276 tint4 = va_arg(ap, db_int4_t);
00277 argin[i] = alloca(sizeof(db_int4_t));
00278 memcpy(argin[i], &tint4, sizeof(db_int4_t));
00279 break;
00280 case DB_INT8:
00281 tint8 = va_arg(ap, db_int8_t);
00282 argin[i] = alloca(sizeof(db_int8_t));
00283 memcpy(argin[i], &tint8, sizeof(db_int8_t));
00284 break;
00285 case DB_FLOAT:
00286 tfloat = (db_float_t) va_arg(ap,double);
00287 printf("tfloat = %f\n",tfloat);
00288 argin[i] = alloca(sizeof(db_float_t));
00289 memcpy(argin[i], &tfloat, sizeof(db_float_t));
00290 break;
00291 case DB_DOUBLE:
00292 tdouble = va_arg(ap, db_double_t);
00293 argin[i] = alloca(sizeof(db_double_t));
00294 memcpy(argin[i], &tdouble, sizeof(db_double_t));
00295 break;
00296 case DB_STRING:
00297 case DB_VARCHAR:
00298 argin[i] = va_arg(ap, db_string_t);
00299 break;
00300 }
00301 }
00302
00303
00304 result = db_query_bin_array(dbin, query, n, intype, argin );
00305
00306 failure:
00307 va_end(ap);
00308 return result;
00309 }