-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_clause.c
184 lines (157 loc) · 4.65 KB
/
db_clause.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "db_clause.h"
#include <tg_utility.h>
extern char *tg_strdup( const char *src );
struct _DatabaseClause
{
DatabaseClauseType type;
CHAR* statement;
CursorWindow* win;
};
void db_clause_destroy(DatabaseClause* clause)
{
return_if_fail(clause);
TG_FREE(clause->statement);
cursor_window_destroy(clause->win);
TG_FREE(clause);
}
DatabaseClause* db_clause_create(DatabaseClauseType type,CHAR* statement,INT32 value_num)
{
DatabaseClause* clause = NULL;
return_val_if_fail(statement, NULL);
clause = TG_CALLOC_V2(sizeof(DatabaseClause));
return_val_if_fail(clause, NULL);
clause->statement = tg_strdup(statement);
if(!clause->statement )
{
TG_FREE(clause);
return NULL;
}
if(value_num>0)
{
clause->win = cursor_window_create(value_num);
if(!cursor_window_alloc_row(clause->win))
{
db_clause_destroy(clause);
return NULL;
}
}
return clause;
}
DatabaseClause* db_clause_duplicate(DatabaseClause* src)
{
DatabaseClause* clause = NULL;
return_val_if_fail(src, NULL);
clause = TG_CALLOC_V2(sizeof(DatabaseClause));
return_val_if_fail(clause, NULL);
clause->statement = tg_strdup(src->statement);
if(!clause->statement )
{
TG_FREE(clause);
return NULL;
}
clause->win = cursor_window_duplicate(src->win);
return clause;
}
DatabaseClause* db_where_clause_create(CHAR* statement,INT32 value_num)
{
return db_clause_create(DB_WHERE_CLAUSE,statement,value_num);
}
DatabaseClause* db_having_clause_create(CHAR* statement,INT32 value_num)
{
return db_clause_create(DB_HAVING_CLAUSE,statement,value_num);
}
const CHAR* db_clause_get_statement(DatabaseClause* thiz)
{
return_val_if_fail(thiz, NULL);
return thiz->statement;
}
BOOL db_clause_set_statement(DatabaseClause* thiz,const CHAR* statement)
{
return_val_if_fail(thiz, FALSE);
TG_FREE(thiz->statement);
thiz->statement = tg_strdup(statement);
return thiz->statement?TRUE:FALSE;
}
BOOL db_clause_append_where(DatabaseClause* thiz,const CHAR* where_str)
{
return_val_if_fail(thiz&&where_str, FALSE);
if(!thiz->statement)
thiz->statement = tg_strdup(where_str);
else
{
INT32 len = strlen(thiz->statement)+strlen(where_str)+8;
CHAR* new_statement = TG_CALLOC_V2(len);
return_val_if_fail(new_statement, FALSE);
sprintf(new_statement,"%s AND %s",thiz->statement,where_str);
TG_FREE(thiz->statement);
thiz->statement = new_statement;
}
return thiz->statement?TRUE:FALSE;
}
INT32 db_clause_get_data_count(DatabaseClause* thiz)
{
return_val_if_fail(thiz && thiz->win, 0);
return cursor_window_get_col_num(thiz->win);
}
BOOL db_clause_get_data(DatabaseClause* thiz,INT32 idx,UINT8* type,INT32* len,void** data)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_get_value(thiz->win,0,idx,type,data,len);
}
BOOL db_clause_put_int(DatabaseClause* thiz,INT32 col,INT64 value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_put_int(thiz->win,col,value);
}
BOOL db_clause_put_double(DatabaseClause* thiz,INT32 col,double value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_put_double(thiz->win,col,value);
}
BOOL db_clause_put_string(DatabaseClause* thiz,INT32 col,const CHAR* value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_put_string(thiz->win,col,value);
}
BOOL db_clause_put_string_16(DatabaseClause* thiz,INT32 col,const WCHAR* value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_put_string_16(thiz->win,col,value);
}
BOOL db_clause_put_blob(DatabaseClause* thiz,INT32 col,const void* value,INT32 len)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_put_blob(thiz->win,col,value,len);
}
BOOL db_clause_get_int(DatabaseClause* thiz,INT32 col,INT64* value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_get_int(thiz->win,0,col,value);
}
BOOL db_clause_get_double(DatabaseClause* thiz,INT32 col,double* value)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_get_double(thiz->win,0,col,value);
}
const CHAR* db_clause_get_string(DatabaseClause* thiz,INT32 col)
{
return_val_if_fail(thiz&&thiz->win, NULL);
return cursor_window_get_string(thiz->win,0,col);
}
const WCHAR* db_clause_get_string_16(DatabaseClause* thiz,INT32 col)
{
return_val_if_fail(thiz&&thiz->win, NULL);
return cursor_window_get_string_16(thiz->win,0,col);
}
#if 0
BOOL db_clause_get_blob(DatabaseClause* thiz,INT32 row,INT32 col,void** value,INT32* len)
{
return_val_if_fail(thiz&&thiz->win, FALSE);
return cursor_window_get_blob(thiz->win,0,col,value,len);
}
#endif
const void* db_clause_get_blob(DatabaseClause* thiz,INT32 row,INT32 col,INT32* len)
{
return_val_if_fail(thiz&&thiz->win, NULL);
return cursor_window_get_blob(thiz->win,0,col,len);
}