PostgreSQL Source Code  git master
sqlda.c File Reference
#include "postgres_fe.h"
#include "catalog/pg_type_d.h"
#include "decimal.h"
#include "ecpg-pthread-win32.h"
#include "ecpgerrno.h"
#include "ecpglib.h"
#include "ecpglib_extern.h"
#include "ecpgtype.h"
#include "sqlca.h"
#include "sqlda-compat.h"
#include "sqlda-native.h"
Include dependency graph for sqlda.c:

Go to the source code of this file.

Macros

#define POSTGRES_ECPG_INTERNAL
 

Functions

static void ecpg_sqlda_align_add_size (long offset, int alignment, int size, long *current, long *next)
 
static long sqlda_compat_empty_size (const PGresult *res)
 
static long sqlda_common_total_size (const PGresult *res, int row, enum COMPAT_MODE compat, long offset)
 
static long sqlda_compat_total_size (const PGresult *res, int row, enum COMPAT_MODE compat)
 
static long sqlda_native_empty_size (const PGresult *res)
 
static long sqlda_native_total_size (const PGresult *res, int row, enum COMPAT_MODE compat)
 
struct sqlda_compatecpg_build_compat_sqlda (int line, PGresult *res, int row, enum COMPAT_MODE compat)
 
void ecpg_set_compat_sqlda (int lineno, struct sqlda_compat **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
 
struct sqlda_structecpg_build_native_sqlda (int line, PGresult *res, int row, enum COMPAT_MODE compat)
 
void ecpg_set_native_sqlda (int lineno, struct sqlda_struct **_sqlda, const PGresult *res, int row, enum COMPAT_MODE compat)
 

Variables

static int16 value_is_null = -1
 
static int16 value_is_not_null = 0
 

Macro Definition Documentation

◆ POSTGRES_ECPG_INTERNAL

#define POSTGRES_ECPG_INTERNAL

Definition at line 9 of file sqlda.c.

Function Documentation

◆ ecpg_build_compat_sqlda()

struct sqlda_compat* ecpg_build_compat_sqlda ( int  line,
PGresult res,
int  row,
enum COMPAT_MODE  compat 
)

Definition at line 205 of file sqlda.c.

References sqlda_compat::desc_occ, ecpg_alloc(), ecpg_log(), i, PQfname(), PQfsize(), PQftype(), PQnfields(), sqlda_compat::sqld, sqlda_compat_total_size(), sqlda_dynamic_type(), sqlvar_compat::sqlname, sqlvar_compat::sqltype, sqlvar_compat::sqltypelen, sqlda_compat::sqlvar, and sqlvar_compat::sqlxid.

Referenced by ecpg_process_output(), and ECPGdescribe().

206 {
207  struct sqlda_compat *sqlda;
208  struct sqlvar_compat *sqlvar;
209  char *fname;
210  long size;
211  int sqld;
212  int i;
213 
214  size = sqlda_compat_total_size(res, row, compat);
215  sqlda = (struct sqlda_compat *) ecpg_alloc(size, line);
216  if (!sqlda)
217  return NULL;
218 
219  memset(sqlda, 0, size);
220  sqlvar = (struct sqlvar_compat *) (sqlda + 1);
221  sqld = PQnfields(res);
222  fname = (char *) (sqlvar + sqld);
223 
224  sqlda->sqld = sqld;
225  ecpg_log("ecpg_build_compat_sqlda on line %d sqld = %d\n", line, sqld);
226  sqlda->desc_occ = size; /* cheat here, keep the full allocated size */
227  sqlda->sqlvar = sqlvar;
228 
229  for (i = 0; i < sqlda->sqld; i++)
230  {
231  sqlda->sqlvar[i].sqltype = sqlda_dynamic_type(PQftype(res, i), compat);
232  strcpy(fname, PQfname(res, i));
233  sqlda->sqlvar[i].sqlname = fname;
234  fname += strlen(sqlda->sqlvar[i].sqlname) + 1;
235 
236  /*
237  * this is reserved for future use, so we leave it empty for the time
238  * being
239  */
240  /* sqlda->sqlvar[i].sqlformat = (char *) (long) PQfformat(res, i); */
241  sqlda->sqlvar[i].sqlxid = PQftype(res, i);
242  sqlda->sqlvar[i].sqltypelen = PQfsize(res, i);
243  }
244 
245  return sqlda;
246 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
int PQfsize(const PGresult *res, int field_num)
Definition: fe-exec.c:3018
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2855
short sqltypelen
Definition: sqlda-compat.h:21
char * sqlname
Definition: sqlda-compat.h:14
short desc_occ
Definition: sqlda-compat.h:42
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3007
void ecpg_log(const char *format,...) pg_attribute_printf(1
enum COMPAT_MODE compat
Definition: ecpg.c:25
char * ecpg_alloc(long, int)
Definition: memory.c:19
int sqlda_dynamic_type(Oid, enum COMPAT_MODE)
Definition: typename.c:107
int i
static long sqlda_compat_total_size(const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:157
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40

◆ ecpg_build_native_sqlda()

struct sqlda_struct* ecpg_build_native_sqlda ( int  line,
PGresult res,
int  row,
enum COMPAT_MODE  compat 
)

Definition at line 412 of file sqlda.c.

References sqlname::data, ecpg_alloc(), ecpg_log(), i, sqlname::length, PQfname(), PQftype(), PQnfields(), sprintf, sqlda_struct::sqld, sqlda_dynamic_type(), sqlda_native_total_size(), sqlda_struct::sqldabc, sqlda_struct::sqldaid, sqlda_struct::sqln, sqlvar_struct::sqlname, sqlvar_struct::sqltype, and sqlda_struct::sqlvar.

Referenced by ecpg_process_output(), and ECPGdescribe().

413 {
414  struct sqlda_struct *sqlda;
415  long size;
416  int i;
417 
418  size = sqlda_native_total_size(res, row, compat);
419  sqlda = (struct sqlda_struct *) ecpg_alloc(size, line);
420  if (!sqlda)
421  return NULL;
422 
423  memset(sqlda, 0, size);
424 
425  sprintf(sqlda->sqldaid, "SQLDA ");
426  sqlda->sqld = sqlda->sqln = PQnfields(res);
427  ecpg_log("ecpg_build_native_sqlda on line %d sqld = %d\n", line, sqlda->sqld);
428  sqlda->sqldabc = sizeof(struct sqlda_struct) + (sqlda->sqld - 1) * sizeof(struct sqlvar_struct);
429 
430  for (i = 0; i < sqlda->sqld; i++)
431  {
432  char *fname;
433 
434  sqlda->sqlvar[i].sqltype = sqlda_dynamic_type(PQftype(res, i), compat);
435  fname = PQfname(res, i);
436  sqlda->sqlvar[i].sqlname.length = strlen(fname);
437  strcpy(sqlda->sqlvar[i].sqlname.data, fname);
438  }
439 
440  return sqlda;
441 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
static long sqlda_native_total_size(const PGresult *res, int row, enum COMPAT_MODE compat)
Definition: sqlda.c:186
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2855
short length
Definition: sqlda-native.h:20
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
#define sprintf
Definition: port.h:194
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3007
char sqldaid[8]
Definition: sqlda-native.h:35
void ecpg_log(const char *format,...) pg_attribute_printf(1
enum COMPAT_MODE compat
Definition: ecpg.c:25
char * ecpg_alloc(long, int)
Definition: memory.c:19
int sqlda_dynamic_type(Oid, enum COMPAT_MODE)
Definition: typename.c:107
char data[NAMEDATALEN]
Definition: sqlda-native.h:21
int i
struct sqlname sqlname
Definition: sqlda-native.h:30

◆ ecpg_set_compat_sqlda()

void ecpg_set_compat_sqlda ( int  lineno,
struct sqlda_compat **  _sqlda,
const PGresult res,
int  row,
enum COMPAT_MODE  compat 
)

Definition at line 255 of file sqlda.c.

References numeric::buf, numeric::digits, ECPG_ARRAY_NONE, ecpg_get_data(), ecpg_log(), ecpg_sqlda_align_add_size(), ECPGset_noind_null(), ECPGt_bool, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, i, numeric::ndigits, numeric(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PQgetisnull(), PQgetvalue(), sqlda_compat::sqld, sqlda_compat_empty_size(), sqlvar_compat::sqldata, sqlvar_compat::sqlilen, sqlvar_compat::sqlilongdata, sqlvar_compat::sqlind, sqlvar_compat::sqlitype, sqlvar_compat::sqllen, sqlvar_compat::sqltype, sqlda_compat::sqlvar, val, value_is_not_null, and value_is_null.

Referenced by ecpg_process_output().

256 {
257  struct sqlda_compat *sqlda = (*_sqlda);
258  int i;
259  long offset,
260  next_offset;
261 
262  if (row < 0)
263  return;
264 
265  /* Offset for the first field value */
266  offset = sqlda_compat_empty_size(res);
267 
268  /*
269  * Set sqlvar[i]->sqldata pointers and convert values to correct format
270  */
271  for (i = 0; i < sqlda->sqld; i++)
272  {
273  int isnull;
274  int datalen;
275  bool set_data = true;
276 
277  switch (sqlda->sqlvar[i].sqltype)
278  {
279  case ECPGt_short:
281  ecpg_sqlda_align_add_size(offset, sizeof(short), sizeof(short), &offset, &next_offset);
282  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
283  sqlda->sqlvar[i].sqllen = sizeof(short);
284  break;
285  case ECPGt_int:
286  case ECPGt_unsigned_int:
287  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(int), &offset, &next_offset);
288  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
289  sqlda->sqlvar[i].sqllen = sizeof(int);
290  break;
291  case ECPGt_long:
292  case ECPGt_unsigned_long:
293  ecpg_sqlda_align_add_size(offset, sizeof(long), sizeof(long), &offset, &next_offset);
294  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
295  sqlda->sqlvar[i].sqllen = sizeof(long);
296  break;
297  case ECPGt_long_long:
299  ecpg_sqlda_align_add_size(offset, sizeof(long long), sizeof(long long), &offset, &next_offset);
300  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
301  sqlda->sqlvar[i].sqllen = sizeof(long long);
302  break;
303  case ECPGt_bool:
304  ecpg_sqlda_align_add_size(offset, sizeof(bool), sizeof(bool), &offset, &next_offset);
305  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
306  sqlda->sqlvar[i].sqllen = sizeof(bool);
307  break;
308  case ECPGt_float:
309  ecpg_sqlda_align_add_size(offset, sizeof(float), sizeof(float), &offset, &next_offset);
310  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
311  sqlda->sqlvar[i].sqllen = sizeof(float);
312  break;
313  case ECPGt_double:
314  ecpg_sqlda_align_add_size(offset, sizeof(double), sizeof(double), &offset, &next_offset);
315  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
316  sqlda->sqlvar[i].sqllen = sizeof(double);
317  break;
318  case ECPGt_decimal:
319  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(decimal), &offset, &next_offset);
320  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
321  sqlda->sqlvar[i].sqllen = sizeof(decimal);
322  break;
323  case ECPGt_numeric:
324  {
325  numeric *num;
326  char *val;
327 
328  set_data = false;
329 
330  ecpg_sqlda_align_add_size(offset, sizeof(NumericDigit *), sizeof(numeric), &offset, &next_offset);
331  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
332  sqlda->sqlvar[i].sqllen = sizeof(numeric);
333 
334  if (PQgetisnull(res, row, i))
335  {
337  break;
338  }
339 
340  val = PQgetvalue(res, row, i);
341  num = PGTYPESnumeric_from_asc(val, NULL);
342  if (!num)
343  {
345  break;
346  }
347 
348  memcpy(sqlda->sqlvar[i].sqldata, num, sizeof(numeric));
349 
350  if (num->buf)
351  {
352  ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->digits - num->buf + num->ndigits, &offset, &next_offset);
353  memcpy((char *) sqlda + offset, num->buf, num->digits - num->buf + num->ndigits);
354 
355  ((numeric *) sqlda->sqlvar[i].sqldata)->buf = (NumericDigit *) sqlda + offset;
356  ((numeric *) sqlda->sqlvar[i].sqldata)->digits = (NumericDigit *) sqlda + offset + (num->digits - num->buf);
357  }
358 
359  PGTYPESnumeric_free(num);
360 
361  break;
362  }
363  case ECPGt_date:
364  ecpg_sqlda_align_add_size(offset, sizeof(date), sizeof(date), &offset, &next_offset);
365  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
366  sqlda->sqlvar[i].sqllen = sizeof(date);
367  break;
368  case ECPGt_timestamp:
369  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(timestamp), &offset, &next_offset);
370  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
371  sqlda->sqlvar[i].sqllen = sizeof(timestamp);
372  break;
373  case ECPGt_interval:
374  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(interval), &offset, &next_offset);
375  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
376  sqlda->sqlvar[i].sqllen = sizeof(interval);
377  break;
378  case ECPGt_char:
379  case ECPGt_unsigned_char:
380  case ECPGt_string:
381  default:
382  datalen = strlen(PQgetvalue(res, row, i)) + 1;
383  ecpg_sqlda_align_add_size(offset, sizeof(int), datalen, &offset, &next_offset);
384  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
385  sqlda->sqlvar[i].sqllen = datalen;
386  if (datalen > 32768)
387  sqlda->sqlvar[i].sqlilongdata = sqlda->sqlvar[i].sqldata;
388  break;
389  }
390 
391  isnull = PQgetisnull(res, row, i);
392  ecpg_log("ecpg_set_compat_sqlda on line %d row %d col %d %s\n", lineno, row, i, isnull ? "IS NULL" : "IS NOT NULL");
393  sqlda->sqlvar[i].sqlind = isnull ? &value_is_null : &value_is_not_null;
394  sqlda->sqlvar[i].sqlitype = ECPGt_short;
395  sqlda->sqlvar[i].sqlilen = sizeof(short);
396  if (!isnull)
397  {
398  if (set_data)
399  ecpg_get_data(res, row, i, lineno,
400  sqlda->sqlvar[i].sqltype, ECPGt_NO_INDICATOR,
401  sqlda->sqlvar[i].sqldata, NULL, 0, 0, 0,
402  ECPG_ARRAY_NONE, compat, false);
403  }
404  else
405  ECPGset_noind_null(sqlda->sqlvar[i].sqltype, sqlda->sqlvar[i].sqldata);
406 
407  offset = next_offset;
408  }
409 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int64 timestamp
NumericDigit * buf
static int16 value_is_not_null
Definition: sqlda.c:252
long date
Definition: pgtypes_date.h:9
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:471
char * sqldata
Definition: sqlda-compat.h:12
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33
NumericDigit * digits
Datum numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:955
int16 NumericDigit
Definition: numeric.c:102
void ecpg_log(const char *format,...) pg_attribute_printf(1
static int16 value_is_null
Definition: sqlda.c:251
enum COMPAT_MODE compat
Definition: ecpg.c:25
bool ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno, enum ECPGttype type, enum ECPGttype ind_type, char *var, char *ind, long varcharsize, long offset, long ind_offset, enum ARRAY_TYPE isarray, enum COMPAT_MODE compat, bool force_indicator)
Definition: data.c:206
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:407
void ECPGset_noind_null(enum ECPGttype type, void *ptr)
Definition: misc.c:319
int i
static long sqlda_compat_empty_size(const PGresult *res)
Definition: sqlda.c:45
short * sqlind
Definition: sqlda-compat.h:13
char * sqlilongdata
Definition: sqlda-compat.h:32
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
long val
Definition: informix.c:664
struct sqlvar_compat * sqlvar
Definition: sqlda-compat.h:40
unsigned char bool
Definition: c.h:309

◆ ecpg_set_native_sqlda()

void ecpg_set_native_sqlda ( int  lineno,
struct sqlda_struct **  _sqlda,
const PGresult res,
int  row,
enum COMPAT_MODE  compat 
)

Definition at line 444 of file sqlda.c.

References numeric::buf, numeric::digits, ECPG_ARRAY_NONE, ecpg_get_data(), ecpg_log(), ecpg_sqlda_align_add_size(), ECPGset_noind_null(), ECPGt_bool, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_NO_INDICATOR, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, i, numeric::ndigits, numeric(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PQgetisnull(), PQgetvalue(), sqlda_struct::sqld, sqlda_native_empty_size(), sqlvar_struct::sqldata, sqlvar_struct::sqlind, sqlvar_struct::sqllen, sqlvar_struct::sqltype, sqlda_struct::sqlvar, val, value_is_not_null, and value_is_null.

Referenced by ecpg_process_output().

445 {
446  struct sqlda_struct *sqlda = (*_sqlda);
447  int i;
448  long offset,
449  next_offset;
450 
451  if (row < 0)
452  return;
453 
454  /* Offset for the first field value */
455  offset = sqlda_native_empty_size(res);
456 
457  /*
458  * Set sqlvar[i]->sqldata pointers and convert values to correct format
459  */
460  for (i = 0; i < sqlda->sqld; i++)
461  {
462  int isnull;
463  int datalen;
464  bool set_data = true;
465 
466  switch (sqlda->sqlvar[i].sqltype)
467  {
468  case ECPGt_short:
470  ecpg_sqlda_align_add_size(offset, sizeof(short), sizeof(short), &offset, &next_offset);
471  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
472  sqlda->sqlvar[i].sqllen = sizeof(short);
473  break;
474  case ECPGt_int:
475  case ECPGt_unsigned_int:
476  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(int), &offset, &next_offset);
477  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
478  sqlda->sqlvar[i].sqllen = sizeof(int);
479  break;
480  case ECPGt_long:
481  case ECPGt_unsigned_long:
482  ecpg_sqlda_align_add_size(offset, sizeof(long), sizeof(long), &offset, &next_offset);
483  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
484  sqlda->sqlvar[i].sqllen = sizeof(long);
485  break;
486  case ECPGt_long_long:
488  ecpg_sqlda_align_add_size(offset, sizeof(long long), sizeof(long long), &offset, &next_offset);
489  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
490  sqlda->sqlvar[i].sqllen = sizeof(long long);
491  break;
492  case ECPGt_bool:
493  ecpg_sqlda_align_add_size(offset, sizeof(bool), sizeof(bool), &offset, &next_offset);
494  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
495  sqlda->sqlvar[i].sqllen = sizeof(bool);
496  break;
497  case ECPGt_float:
498  ecpg_sqlda_align_add_size(offset, sizeof(float), sizeof(float), &offset, &next_offset);
499  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
500  sqlda->sqlvar[i].sqllen = sizeof(float);
501  break;
502  case ECPGt_double:
503  ecpg_sqlda_align_add_size(offset, sizeof(double), sizeof(double), &offset, &next_offset);
504  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
505  sqlda->sqlvar[i].sqllen = sizeof(double);
506  break;
507  case ECPGt_decimal:
508  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(decimal), &offset, &next_offset);
509  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
510  sqlda->sqlvar[i].sqllen = sizeof(decimal);
511  break;
512  case ECPGt_numeric:
513  {
514  numeric *num;
515  char *val;
516 
517  set_data = false;
518 
519  ecpg_sqlda_align_add_size(offset, sizeof(NumericDigit *), sizeof(numeric), &offset, &next_offset);
520  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
521  sqlda->sqlvar[i].sqllen = sizeof(numeric);
522 
523  if (PQgetisnull(res, row, i))
524  {
526  break;
527  }
528 
529  val = PQgetvalue(res, row, i);
530  num = PGTYPESnumeric_from_asc(val, NULL);
531  if (!num)
532  {
534  break;
535  }
536 
537  memcpy(sqlda->sqlvar[i].sqldata, num, sizeof(numeric));
538 
539  if (num->buf)
540  {
541  ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->digits - num->buf + num->ndigits, &offset, &next_offset);
542  memcpy((char *) sqlda + offset, num->buf, num->digits - num->buf + num->ndigits);
543 
544  ((numeric *) sqlda->sqlvar[i].sqldata)->buf = (NumericDigit *) sqlda + offset;
545  ((numeric *) sqlda->sqlvar[i].sqldata)->digits = (NumericDigit *) sqlda + offset + (num->digits - num->buf);
546  }
547 
548  PGTYPESnumeric_free(num);
549 
550  break;
551  }
552  case ECPGt_date:
553  ecpg_sqlda_align_add_size(offset, sizeof(date), sizeof(date), &offset, &next_offset);
554  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
555  sqlda->sqlvar[i].sqllen = sizeof(date);
556  break;
557  case ECPGt_timestamp:
558  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(timestamp), &offset, &next_offset);
559  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
560  sqlda->sqlvar[i].sqllen = sizeof(timestamp);
561  break;
562  case ECPGt_interval:
563  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(interval), &offset, &next_offset);
564  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
565  sqlda->sqlvar[i].sqllen = sizeof(interval);
566  break;
567  case ECPGt_char:
568  case ECPGt_unsigned_char:
569  case ECPGt_string:
570  default:
571  datalen = strlen(PQgetvalue(res, row, i)) + 1;
572  ecpg_sqlda_align_add_size(offset, sizeof(int), datalen, &offset, &next_offset);
573  sqlda->sqlvar[i].sqldata = (char *) sqlda + offset;
574  sqlda->sqlvar[i].sqllen = datalen;
575  break;
576  }
577 
578  isnull = PQgetisnull(res, row, i);
579  ecpg_log("ecpg_set_native_sqlda on line %d row %d col %d %s\n", lineno, row, i, isnull ? "IS NULL" : "IS NOT NULL");
580  sqlda->sqlvar[i].sqlind = isnull ? &value_is_null : &value_is_not_null;
581  if (!isnull)
582  {
583  if (set_data)
584  ecpg_get_data(res, row, i, lineno,
585  sqlda->sqlvar[i].sqltype, ECPGt_NO_INDICATOR,
586  sqlda->sqlvar[i].sqldata, NULL, 0, 0, 0,
587  ECPG_ARRAY_NONE, compat, false);
588  }
589 
590  offset = next_offset;
591  }
592 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int64 timestamp
struct sqlvar_struct sqlvar[1]
Definition: sqlda-native.h:40
NumericDigit * buf
static int16 value_is_not_null
Definition: sqlda.c:252
long date
Definition: pgtypes_date.h:9
char * sqldata
Definition: sqlda-native.h:28
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:471
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33
NumericDigit * digits
Datum numeric(PG_FUNCTION_ARGS)
Definition: numeric.c:955
int16 NumericDigit
Definition: numeric.c:102
void ecpg_log(const char *format,...) pg_attribute_printf(1
static int16 value_is_null
Definition: sqlda.c:251
enum COMPAT_MODE compat
Definition: ecpg.c:25
bool ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno, enum ECPGttype type, enum ECPGttype ind_type, char *var, char *ind, long varcharsize, long offset, long ind_offset, enum ARRAY_TYPE isarray, enum COMPAT_MODE compat, bool force_indicator)
Definition: data.c:206
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:407
void ECPGset_noind_null(enum ECPGttype type, void *ptr)
Definition: misc.c:319
short * sqlind
Definition: sqlda-native.h:29
int i
static long sqlda_native_empty_size(const PGresult *res)
Definition: sqlda.c:171
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
long val
Definition: informix.c:664
unsigned char bool
Definition: c.h:309

◆ ecpg_sqlda_align_add_size()

static void ecpg_sqlda_align_add_size ( long  offset,
int  alignment,
int  size,
long *  current,
long *  next 
)
static

Definition at line 33 of file sqlda.c.

Referenced by ecpg_set_compat_sqlda(), ecpg_set_native_sqlda(), sqlda_common_total_size(), sqlda_compat_empty_size(), and sqlda_native_empty_size().

34 {
35  if (offset % alignment)
36  offset += alignment - (offset % alignment);
37  if (current)
38  *current = offset;
39  offset += size;
40  if (next)
41  *next = offset;
42 }
static int32 next
Definition: blutils.c:213

◆ sqlda_common_total_size()

static long sqlda_common_total_size ( const PGresult res,
int  row,
enum COMPAT_MODE  compat,
long  offset 
)
static

Definition at line 65 of file sqlda.c.

References numeric::buf, numeric::digits, ecpg_sqlda_align_add_size(), ECPGt_bool, ECPGt_char, ECPGt_date, ECPGt_decimal, ECPGt_double, ECPGt_float, ECPGt_int, ECPGt_interval, ECPGt_long, ECPGt_long_long, ECPGt_numeric, ECPGt_short, ECPGt_string, ECPGt_timestamp, ECPGt_unsigned_char, ECPGt_unsigned_int, ECPGt_unsigned_long, ECPGt_unsigned_long_long, ECPGt_unsigned_short, i, numeric::ndigits, PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PQftype(), PQgetisnull(), PQgetvalue(), PQnfields(), sqlda_dynamic_type(), generate_unaccent_rules::type, and val.

Referenced by sqlda_compat_total_size(), and sqlda_native_total_size().

66 {
67  int sqld = PQnfields(res);
68  int i;
69  long next_offset;
70 
71  /* Add space for the field values */
72  for (i = 0; i < sqld; i++)
73  {
75 
76  switch (type)
77  {
78  case ECPGt_short:
80  ecpg_sqlda_align_add_size(offset, sizeof(short), sizeof(short), &offset, &next_offset);
81  break;
82  case ECPGt_int:
83  case ECPGt_unsigned_int:
84  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(int), &offset, &next_offset);
85  break;
86  case ECPGt_long:
88  ecpg_sqlda_align_add_size(offset, sizeof(long), sizeof(long), &offset, &next_offset);
89  break;
90  case ECPGt_long_long:
92  ecpg_sqlda_align_add_size(offset, sizeof(long long), sizeof(long long), &offset, &next_offset);
93  break;
94  case ECPGt_bool:
95  ecpg_sqlda_align_add_size(offset, sizeof(bool), sizeof(bool), &offset, &next_offset);
96  break;
97  case ECPGt_float:
98  ecpg_sqlda_align_add_size(offset, sizeof(float), sizeof(float), &offset, &next_offset);
99  break;
100  case ECPGt_double:
101  ecpg_sqlda_align_add_size(offset, sizeof(double), sizeof(double), &offset, &next_offset);
102  break;
103  case ECPGt_decimal:
104  ecpg_sqlda_align_add_size(offset, sizeof(int), sizeof(decimal), &offset, &next_offset);
105  break;
106  case ECPGt_numeric:
107 
108  /*
109  * We align the numeric struct to allow it to store a pointer,
110  * while the digits array is aligned to int (which seems like
111  * overkill, but let's keep compatibility here).
112  *
113  * Unfortunately we need to deconstruct the value twice to
114  * find out the digits array's size and then later fill it.
115  */
116  ecpg_sqlda_align_add_size(offset, sizeof(NumericDigit *), sizeof(numeric), &offset, &next_offset);
117  if (!PQgetisnull(res, row, i))
118  {
119  char *val = PQgetvalue(res, row, i);
120  numeric *num;
121 
122  num = PGTYPESnumeric_from_asc(val, NULL);
123  if (!num)
124  break;
125  if (num->buf)
126  ecpg_sqlda_align_add_size(next_offset, sizeof(int), num->digits - num->buf + num->ndigits, &offset, &next_offset);
127  PGTYPESnumeric_free(num);
128  }
129  break;
130  case ECPGt_date:
131  ecpg_sqlda_align_add_size(offset, sizeof(date), sizeof(date), &offset, &next_offset);
132  break;
133  case ECPGt_timestamp:
134  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(timestamp), &offset, &next_offset);
135  break;
136  case ECPGt_interval:
137  ecpg_sqlda_align_add_size(offset, sizeof(int64), sizeof(interval), &offset, &next_offset);
138  break;
139  case ECPGt_char:
140  case ECPGt_unsigned_char:
141  case ECPGt_string:
142  default:
143  {
144  long datalen = strlen(PQgetvalue(res, row, i)) + 1;
145 
146  ecpg_sqlda_align_add_size(offset, sizeof(int), datalen, &offset, &next_offset);
147  break;
148  }
149  }
150  offset = next_offset;
151  }
152  return offset;
153 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
int64 timestamp
NumericDigit * buf
long date
Definition: pgtypes_date.h:9
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:471
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33
NumericDigit * digits
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3007
int16 NumericDigit
Definition: numeric.c:102
enum COMPAT_MODE compat
Definition: ecpg.c:25
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:407
int sqlda_dynamic_type(Oid, enum COMPAT_MODE)
Definition: typename.c:107
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
long val
Definition: informix.c:664
ECPGttype
Definition: ecpgtype.h:41

◆ sqlda_compat_empty_size()

static long sqlda_compat_empty_size ( const PGresult res)
static

Definition at line 45 of file sqlda.c.

References ecpg_sqlda_align_add_size(), i, PQfname(), and PQnfields().

Referenced by ecpg_set_compat_sqlda(), and sqlda_compat_total_size().

46 {
47  long offset;
48  int i;
49  int sqld = PQnfields(res);
50 
51  /* Initial size to store main structure and field structures */
52  offset = sizeof(struct sqlda_compat) + sqld * sizeof(struct sqlvar_compat);
53 
54  /* Add space for field names */
55  for (i = 0; i < sqld; i++)
56  offset += strlen(PQfname(res, i)) + 1;
57 
58  /* Add padding to the first field value */
59  ecpg_sqlda_align_add_size(offset, sizeof(int), 0, &offset, NULL);
60 
61  return offset;
62 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2855
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33
int i

◆ sqlda_compat_total_size()

static long sqlda_compat_total_size ( const PGresult res,
int  row,
enum COMPAT_MODE  compat 
)
static

Definition at line 157 of file sqlda.c.

References sqlda_common_total_size(), and sqlda_compat_empty_size().

Referenced by ecpg_build_compat_sqlda().

158 {
159  long offset;
160 
161  offset = sqlda_compat_empty_size(res);
162 
163  if (row < 0)
164  return offset;
165 
166  offset = sqlda_common_total_size(res, row, compat, offset);
167  return offset;
168 }
static long sqlda_common_total_size(const PGresult *res, int row, enum COMPAT_MODE compat, long offset)
Definition: sqlda.c:65
enum COMPAT_MODE compat
Definition: ecpg.c:25
static long sqlda_compat_empty_size(const PGresult *res)
Definition: sqlda.c:45

◆ sqlda_native_empty_size()

static long sqlda_native_empty_size ( const PGresult res)
static

Definition at line 171 of file sqlda.c.

References ecpg_sqlda_align_add_size(), and PQnfields().

Referenced by ecpg_set_native_sqlda(), and sqlda_native_total_size().

172 {
173  long offset;
174  int sqld = PQnfields(res);
175 
176  /* Initial size to store main structure and field structures */
177  offset = sizeof(struct sqlda_struct) + (sqld - 1) * sizeof(struct sqlvar_struct);
178 
179  /* Add padding to the first field value */
180  ecpg_sqlda_align_add_size(offset, sizeof(int), 0, &offset, NULL);
181 
182  return offset;
183 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
static void ecpg_sqlda_align_add_size(long offset, int alignment, int size, long *current, long *next)
Definition: sqlda.c:33

◆ sqlda_native_total_size()

static long sqlda_native_total_size ( const PGresult res,
int  row,
enum COMPAT_MODE  compat 
)
static

Definition at line 186 of file sqlda.c.

References sqlda_common_total_size(), and sqlda_native_empty_size().

Referenced by ecpg_build_native_sqlda().

187 {
188  long offset;
189 
190  offset = sqlda_native_empty_size(res);
191 
192  if (row < 0)
193  return offset;
194 
195  offset = sqlda_common_total_size(res, row, compat, offset);
196  return offset;
197 }
static long sqlda_common_total_size(const PGresult *res, int row, enum COMPAT_MODE compat, long offset)
Definition: sqlda.c:65
enum COMPAT_MODE compat
Definition: ecpg.c:25
static long sqlda_native_empty_size(const PGresult *res)
Definition: sqlda.c:171

Variable Documentation

◆ value_is_not_null

int16 value_is_not_null = 0
static

Definition at line 252 of file sqlda.c.

Referenced by ecpg_set_compat_sqlda(), and ecpg_set_native_sqlda().

◆ value_is_null

int16 value_is_null = -1
static

Definition at line 251 of file sqlda.c.

Referenced by ecpg_set_compat_sqlda(), and ecpg_set_native_sqlda().