1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2003, 2004, 2005 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 : #if HAVE_CONFIG_H
20 : #include <config.h>
21 : #endif /* HAVE_CONFIG_H */
22 :
23 : #include <stdio.h>
24 :
25 : #if HAVE_STDLIB_H
26 : #include <stdlib.h>
27 : #endif /* HAVE_STDLIB_H */
28 :
29 : #if HAVE_STRING_H
30 : #include <string.h>
31 : #endif /* HAVE_STRING_H */
32 :
33 : #include <assert.h>
34 :
35 : #include "tdsodbc.h"
36 : #include "tdsstring.h"
37 :
38 : #ifdef DMALLOC
39 : #include <dmalloc.h>
40 : #endif
41 :
42 : TDS_RCSID(var, "$Id: odbc_checks.c,v 1.17 2005/07/17 07:48:11 freddy77 Exp $");
43 :
44 : #if ENABLE_EXTRA_CHECKS
45 :
46 : void
47 : odbc_check_env_extra(TDS_ENV * env)
48 336 : {
49 336 : assert(env && env->htype == SQL_HANDLE_ENV);
50 336 : }
51 :
52 : void
53 : odbc_check_dbc_extra(TDS_DBC * dbc)
54 1110 : {
55 1110 : assert(dbc && dbc->htype == SQL_HANDLE_DBC);
56 1110 : }
57 :
58 : void
59 : odbc_check_stmt_extra(TDS_STMT * stmt)
60 10862 : {
61 10862 : assert(stmt && stmt->htype == SQL_HANDLE_STMT);
62 : /* TODO deep check on connection */
63 10862 : assert(stmt->dbc);
64 10862 : odbc_check_desc_extra(stmt->ard);
65 10862 : odbc_check_desc_extra(stmt->ird);
66 10862 : odbc_check_desc_extra(stmt->apd);
67 10862 : odbc_check_desc_extra(stmt->ipd);
68 10862 : assert(!stmt->prepared_query_is_func || stmt->prepared_query_is_rpc);
69 10862 : assert(stmt->param_num <= stmt->param_count + 1);
70 10862 : if (stmt->prepared_query_is_rpc) {
71 222 : char *query = stmt->prepared_query ? stmt->prepared_query : stmt->query;
72 222 : assert(query);
73 222 : assert(stmt->prepared_pos == NULL || (stmt->prepared_pos >= query && stmt->prepared_pos <= strchr(query,0)));
74 : } else {
75 10640 : assert(stmt->prepared_pos == NULL);
76 : }
77 : /* TODO assert dbc has this statement in list */
78 10862 : }
79 :
80 : static void
81 : odbc_check_drecord(TDS_DESC * desc, struct _drecord *drec)
82 62094 : {
83 62094 : assert(drec->sql_desc_concise_type != SQL_INTERVAL && drec->sql_desc_concise_type != SQL_DATETIME);
84 :
85 : /* unbinded columns have type == 0 */
86 : /* TODO test errors on code if type == 0 */
87 113008 : if (desc->type == DESC_IPD || desc->type == DESC_IRD) {
88 50914 : assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0) || odbc_get_concise_sql_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code) == drec->sql_desc_concise_type);
89 : } else {
90 11180 : assert((drec->sql_desc_type == 0 && drec->sql_desc_concise_type == 0) || odbc_get_concise_c_type(drec->sql_desc_type, drec->sql_desc_datetime_interval_code) == drec->sql_desc_concise_type);
91 : }
92 62094 : }
93 :
94 : void
95 : odbc_check_desc_extra(TDS_DESC * desc)
96 44340 : {
97 : int i;
98 :
99 44340 : assert(desc && desc->htype == SQL_HANDLE_DESC);
100 44340 : assert(desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_USER);
101 44340 : assert((desc->type != DESC_IPD && desc->type != DESC_IRD) || desc->header.sql_desc_alloc_type == SQL_DESC_ALLOC_AUTO);
102 106434 : for (i = 0; i < desc->header.sql_desc_count; ++i) {
103 62094 : odbc_check_drecord(desc, &desc->records[i]);
104 : }
105 44340 : }
106 :
107 : void
108 : odbc_check_struct_extra(void *p)
109 7059 : {
110 7059 : const int invalid_htype = 0;
111 :
112 7059 : switch (((struct _hchk *) p)->htype) {
113 : case SQL_HANDLE_ENV:
114 121 : odbc_check_env_extra((TDS_ENV *) p);
115 121 : break;
116 : case SQL_HANDLE_DBC:
117 507 : odbc_check_dbc_extra((TDS_DBC *) p);
118 507 : break;
119 : case SQL_HANDLE_STMT:
120 5985 : odbc_check_stmt_extra((TDS_STMT *) p);
121 5985 : break;
122 : case SQL_HANDLE_DESC:
123 446 : odbc_check_desc_extra((TDS_DESC *) p);
124 446 : break;
125 : default:
126 0 : assert(invalid_htype);
127 : }
128 7059 : }
129 :
130 : #endif /* ENABLE_EXTRA_CHECKS */
|