Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2003-2008 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 : #include <config.h>
20 :
21 : #include <stdarg.h>
22 : #include <stdio.h>
23 :
24 : #if HAVE_STDLIB_H
25 : #include <stdlib.h>
26 : #endif /* HAVE_STDLIB_H */
27 :
28 : #if HAVE_STRING_H
29 : #include <string.h>
30 : #endif /* HAVE_STRING_H */
31 :
32 : #include <assert.h>
33 :
34 : #include <freetds/odbc.h>
35 : #include <freetds/utils/string.h>
36 :
37 : #if ENABLE_EXTRA_CHECKS
38 :
39 : /* Check common fields in ODBC structures */
40 : #define TEST_ATTRIBUTE(t,sa,fa,sb,fb) \
41 : TDS_COMPILE_CHECK(t,sizeof(((sa*)0)->fa) == sizeof(((sb*)0)->fb) && TDS_OFFSET(sa,fa) == TDS_OFFSET(sb,fb))
42 :
43 : #define SAME_TDS_CHK_FIELDS(t,struct) \
44 : TEST_ATTRIBUTE(t ## _a,TDS_CHK,htype,struct,htype); \
45 : TEST_ATTRIBUTE(t ## _b,TDS_CHK,errs, struct,errs); \
46 : TEST_ATTRIBUTE(t ## _c,TDS_CHK,mtx, struct,mtx)
47 :
48 : SAME_TDS_CHK_FIELDS(t1, TDS_ENV);
49 : SAME_TDS_CHK_FIELDS(t2, TDS_DBC);
50 : SAME_TDS_CHK_FIELDS(t3, TDS_STMT);
51 : SAME_TDS_CHK_FIELDS(t4, TDS_DESC);
52 :
53 : void
54 3644 : odbc_check_env_extra(TDS_ENV * env)
55 : {
56 3644 : assert(env && env->htype == SQL_HANDLE_ENV);
57 3644 : assert(env->attr.odbc_version == SQL_OV_ODBC3 || env->attr.odbc_version == SQL_OV_ODBC2);
58 3644 : }
59 :
60 : void
61 38041 : odbc_check_dbc_extra(TDS_DBC * dbc)
62 : {
63 38041 : assert(dbc && dbc->htype == SQL_HANDLE_DBC);
64 38041 : }
65 :
66 : void
67 246760 : odbc_check_stmt_extra(TDS_STMT * stmt)
68 : {
69 246760 : assert(stmt && stmt->htype == SQL_HANDLE_STMT);
70 : /* TODO deep check on connection */
71 246760 : assert(stmt->dbc);
72 246760 : odbc_check_desc_extra(stmt->ard);
73 246760 : odbc_check_desc_extra(stmt->ird);
74 246760 : odbc_check_desc_extra(stmt->apd);
75 246760 : odbc_check_desc_extra(stmt->ipd);
76 246760 : assert(!stmt->prepared_query_is_func || stmt->prepared_query_is_rpc);
77 246760 : assert(stmt->param_num <= stmt->param_count + 1);
78 246760 : assert(stmt->num_param_rows >= 1);
79 : assert(stmt->curr_param_row >= 0);
80 246760 : assert(stmt->curr_param_row <= stmt->num_param_rows);
81 246760 : if (stmt->prepared_query_is_rpc) {
82 55324 : assert(stmt->prepared_pos == 0 || stmt->prepared_pos <= tds_dstr_len(&stmt->query));
83 : } else {
84 219098 : assert(stmt->prepared_pos == 0);
85 : }
86 : /* TODO assert dbc has this statement in list */
87 246760 : }
88 :
89 : static void
90 693842 : odbc_check_drecord(TDS_DESC * desc, struct _drecord *drec)
91 : {
92 693842 : assert(drec->sql_desc_concise_type != SQL_INTERVAL && drec->sql_desc_concise_type != SQL_DATETIME);
93 :
94 : /* unbound columns have type == 0 */
95 : /* TODO test errors on code if type == 0 */
96 693842 : if (desc->type == DESC_IPD || desc->type == DESC_IRD) {
97 487630 : assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0)
98 : || odbc_get_concise_sql_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code)
99 : == drec->sql_desc_concise_type);
100 : } else {
101 206212 : assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0)
102 : || odbc_get_concise_c_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code)
103 : == drec->sql_desc_concise_type);
104 : }
105 693842 : }
106 :
107 : void
108 1048936 : odbc_check_desc_extra(TDS_DESC * desc)
109 : {
110 : int i;
111 :
112 1048936 : assert(desc && desc->htype == SQL_HANDLE_DESC);
113 1048936 : assert(desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_USER);
114 1048936 : assert((desc->type != DESC_IPD && desc->type != DESC_IRD) || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO);
115 1048936 : assert(desc->parent && (IS_HSTMT(desc->parent) || IS_HDBC(desc->parent)));
116 693842 : for (i = 0; i < desc->header.sql_desc_count; ++i) {
117 693842 : odbc_check_drecord(desc, &desc->records[i]);
118 : }
119 1048936 : }
120 :
121 : void
122 161829 : odbc_check_struct_extra(void *p)
123 : {
124 161829 : const int invalid_htype = 0;
125 :
126 161829 : switch (((TDS_CHK *) p)->htype) {
127 1364 : case SQL_HANDLE_ENV:
128 1364 : odbc_check_env_extra((TDS_ENV *) p);
129 1364 : break;
130 19129 : case SQL_HANDLE_DBC:
131 19129 : odbc_check_dbc_extra((TDS_DBC *) p);
132 19129 : break;
133 139166 : case SQL_HANDLE_STMT:
134 139166 : odbc_check_stmt_extra((TDS_STMT *) p);
135 139166 : break;
136 2170 : case SQL_HANDLE_DESC:
137 2170 : odbc_check_desc_extra((TDS_DESC *) p);
138 2170 : break;
139 : default:
140 0 : assert(invalid_htype);
141 : }
142 161829 : }
143 :
144 : #endif /* ENABLE_EXTRA_CHECKS */
|