NCBI C++ ToolKit
descriptor.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2003 Steve Murphree
3  * Copyright (C) 2004, 2005 Ziglio Frediano
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #include <config.h>
22 
23 #include <stdarg.h>
24 #include <stdio.h>
25 
26 #if HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif /* HAVE_STDLIB_H */
29 
30 #if HAVE_STRING_H
31 #include <string.h>
32 #endif /* HAVE_STRING_H */
33 
34 #include <freetds/odbc.h>
35 #include <freetds/string.h>
36 
37 static void desc_free_record(struct _drecord *drec);
38 
39 TDS_DESC *
40 desc_alloc(SQLHANDLE parent, int desc_type, int alloc_type)
41 {
42  TDS_DESC *desc;
43 
44  desc = tds_new0(TDS_DESC, 1);
45  if (!desc || tds_mutex_init(&desc->mtx)) {
46  free(desc);
47  return NULL;
48  }
49 
50  /* set default header values */
51  desc->htype = SQL_HANDLE_DESC;
52  desc->type = desc_type;
53  desc->parent = parent;
54  desc->header.sql_desc_alloc_type = alloc_type;
55  desc->header.sql_desc_count = 0;
56  desc->records = NULL;
57 
58  switch (desc_type) {
59  case DESC_IRD:
60  case DESC_IPD:
61  break;
62  case DESC_ARD:
63  case DESC_APD:
65  desc->header.sql_desc_array_size = 1;
66  break;
67  default:
68  free(desc);
69  return NULL;
70  }
71  return desc;
72 }
73 
74 #define SQL_DESC_STRINGS \
75  STR_OP(sql_desc_base_column_name); \
76  STR_OP(sql_desc_base_table_name); \
77  STR_OP(sql_desc_catalog_name); \
78  STR_OP(sql_desc_label); \
79  STR_OP(sql_desc_local_type_name); \
80  STR_OP(sql_desc_name); \
81  STR_OP(sql_desc_schema_name); \
82  STR_OP(sql_desc_table_name)
83 
85 desc_alloc_records(TDS_DESC * desc, unsigned count)
86 {
87  struct _drecord *drec;
88  int i;
89 
90  /* shrink records */
91  if ((unsigned) desc->header.sql_desc_count >= count) {
92  for (i = count; i < desc->header.sql_desc_count; ++i)
93  desc_free_record(&desc->records[i]);
94  desc->header.sql_desc_count = count;
95  return SQL_SUCCESS;
96  }
97 
98  if (!TDS_RESIZE(desc->records, count))
99  return SQL_ERROR;
100  memset(desc->records + desc->header.sql_desc_count, 0, sizeof(struct _drecord) * (count - desc->header.sql_desc_count));
101 
102  for (i = desc->header.sql_desc_count; (unsigned) i < count; ++i) {
103  drec = &desc->records[i];
104 
105 #define STR_OP(name) tds_dstr_init(&drec->name)
107 #undef STR_OP
108 
109  switch (desc->type) {
110  case DESC_IRD:
111  case DESC_IPD:
113  break;
114  case DESC_ARD:
115  case DESC_APD:
118  break;
119  }
120  }
121  desc->header.sql_desc_count = count;
122  return SQL_SUCCESS;
123 }
124 
125 static void
127 {
128 #define STR_OP(name) tds_dstr_free(&drec->name)
130 #undef STR_OP
131 }
132 
133 SQLRETURN
135 {
136  int i;
137 
138  if (desc->records) {
139  for (i = 0; i < desc->header.sql_desc_count; i++)
140  desc_free_record(&desc->records[i]);
141  TDS_ZERO_FREE(desc->records);
142  }
143 
144  desc->header.sql_desc_count = 0;
145  return SQL_SUCCESS;
146 }
147 
148 SQLRETURN
150 {
151  int i;
152  TDS_DESC tmp = *dest;
153 
154  /* copy header */
155  tmp.header = src->header;
156 
157  /* set no records */
158  tmp.header.sql_desc_count = 0;
159  tmp.records = NULL;
160 
161  tmp.errs.num_errors = 0;
162  tmp.errs.errs = NULL;
163 
165  return SQL_ERROR;
166 
167  for (i = 0; i < src->header.sql_desc_count; ++i) {
168  struct _drecord *src_rec = &src->records[i];
169  struct _drecord *dest_rec = &tmp.records[i];
170 
171  /* copy all integer in one time ! */
172  memcpy(dest_rec, src_rec, sizeof(struct _drecord));
173 
174  /* reinitialize string, avoid doubling pointers */
175 #define STR_OP(name) tds_dstr_init(&dest_rec->name)
177 #undef STR_OP
178 
179  /* copy strings */
180 #define STR_OP(name) if (!tds_dstr_dup(&dest_rec->name, &src_rec->name)) goto Cleanup
182 #undef STR_OP
183  }
184 
185  /* success, copy back to our descriptor */
186  desc_free_records(dest);
187  odbc_errs_reset(&dest->errs);
188  *dest = tmp;
189  return SQL_SUCCESS;
190 
191 Cleanup:
193  odbc_errs_reset(&tmp.errs);
194  return SQL_ERROR;
195 }
196 
197 SQLRETURN
199 {
200  if (desc) {
201  desc_free_records(desc);
202  odbc_errs_reset(&desc->errs);
203  tds_mutex_free(&desc->mtx);
204  free(desc);
205  }
206  return SQL_SUCCESS;
207 }
208 
209 TDS_DBC *
211 {
212  if (IS_HSTMT(desc->parent))
213  return ((TDS_STMT *) desc->parent)->dbc;
214 
215  return (TDS_DBC *) desc->parent;
216 }
217 
#define DESC_IRD
Definition: odbc.h:219
#define IS_HSTMT(x)
Definition: odbc.h:456
void odbc_errs_reset(struct _sql_errors *errs)
reset errors
Definition: error.c:343
#define DESC_IPD
Definition: odbc.h:220
#define DESC_ARD
Definition: odbc.h:221
#define DESC_APD
Definition: odbc.h:222
#define tds_new0(type, n)
Definition: tds.h:1393
#define TDS_RESIZE(p, n_elem)
Definition: tds.h:1390
#define TDS_ZERO_FREE(x)
Definition: tds.h:359
#define tds_mutex_free(x)
Definition: thread.h:419
#define tds_mutex_init(x)
Definition: thread.h:420
SQLRETURN desc_alloc_records(TDS_DESC *desc, unsigned count)
Definition: descriptor.c:85
TDS_DBC * desc_get_dbc(TDS_DESC *desc)
Definition: descriptor.c:210
SQLRETURN desc_free(TDS_DESC *desc)
Definition: descriptor.c:198
static void desc_free_record(struct _drecord *drec)
Definition: descriptor.c:126
#define SQL_DESC_STRINGS
Definition: descriptor.c:74
SQLRETURN desc_free_records(TDS_DESC *desc)
Definition: descriptor.c:134
TDS_DESC * desc_alloc(SQLHANDLE parent, int desc_type, int alloc_type)
Definition: descriptor.c:40
SQLRETURN desc_copy(TDS_DESC *dest, TDS_DESC *src)
Definition: descriptor.c:149
static char tmp[3200]
Definition: utf8.c:42
#define NULL
Definition: ncbistd.hpp:225
int i
#define SQL_SUCCESS
Definition: sql.h:31
#define SQL_HANDLE_DESC
Definition: sql.h:67
#define SQL_ERROR
Definition: sql.h:36
#define SQL_PARAM_INPUT
Definition: sqlext.h:1852
#define SQL_C_DEFAULT
Definition: sqlext.h:519
#define SQL_BIND_BY_COLUMN
Definition: sqlext.h:338
void * SQLHANDLE
Definition: sqltypes.h:213
SQLSMALLINT SQLRETURN
Definition: sqltypes.h:210
SQLUINTEGER sql_desc_array_size
Definition: odbc.h:155
SQLSMALLINT sql_desc_count
Definition: odbc.h:157
SQLSMALLINT sql_desc_alloc_type
Definition: odbc.h:153
SQLINTEGER sql_desc_bind_type
Definition: odbc.h:154
Descriptor record.
Definition: odbc.h:164
SQLSMALLINT sql_desc_parameter_type
Definition: odbc.h:189
SQLSMALLINT sql_desc_type
Definition: odbc.h:196
SQLSMALLINT sql_desc_concise_type
Definition: odbc.h:170
Definition: odbc.h:282
Definition: odbc.h:205
SQLSMALLINT htype
Definition: odbc.h:206
struct _sql_errors errs
Definition: odbc.h:207
struct _drecord * records
Definition: odbc.h:214
tds_mutex mtx
Definition: odbc.h:211
SQLHANDLE parent
Definition: odbc.h:212
struct _dheader header
Definition: odbc.h:213
int type
Definition: odbc.h:211
Definition: odbc.h:390
void free(voidpf ptr)
Modified on Wed Jun 12 11:18:55 2024 by modify_doxy.py rev. 669887