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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
#include <dlog.h>
#include <bundle.h>
#include <errno.h>
#include <glib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <data-control-sql.h>
#include "data_control_internal.h"
#include "data_control_sql.h"
#include "data_control_log.h"
#define MAX_ROW_COUNT 1024
struct data_control_s {
char *provider_id;
char *data_id;
};
struct datacontrol_s {
char *provider_id;
char *data_id;
};
static GHashTable *response_table = NULL;
datacontrol_sql_response_cb datacontrol_sql_cb;
void
__sql_insert_response(int req_id, datacontrol_h provider, long long insert_rowid, bool provider_result, const char *error, void *user_data)
{
_LOGI("sql_insert_response");
data_control_sql_response_cb *callback = (data_control_sql_response_cb*)g_hash_table_lookup(response_table, provider->provider_id);
if (callback)
{
callback->insert_cb(req_id, (data_control_h)provider, insert_rowid, provider_result, error, user_data);
}
}
void
__sql_update_response(int req_id, datacontrol_h provider, bool provider_result, const char *error, void *user_data)
{
_LOGI("sql_update_response");
data_control_sql_response_cb *callback = (data_control_sql_response_cb*)g_hash_table_lookup(response_table, provider->provider_id);
if (callback)
{
callback->update_cb(req_id, (data_control_h)provider, provider_result, error, user_data);
}
}
void
__sql_delete_response(int req_id, datacontrol_h provider, bool provider_result, const char *error, void *user_data)
{
_LOGI("sql_delete_response");
data_control_sql_response_cb *callback = (data_control_sql_response_cb*)g_hash_table_lookup(response_table, provider->provider_id);
if (callback)
{
callback->delete_cb(req_id, (data_control_h)provider, provider_result, error, user_data);
}
}
void
__sql_select_response(int req_id, datacontrol_h provider, resultset_cursor* enumerator, bool provider_result, const char *error, void *user_data)
{
_LOGI("sql_select_response");
data_control_sql_response_cb *callback = (data_control_sql_response_cb*)g_hash_table_lookup(response_table, provider->provider_id);
if (callback)
{
callback->select_cb(req_id, (data_control_h)provider, (result_set_cursor)enumerator, provider_result, error, user_data);
}
}
static void
__free_data(gpointer data)
{
if (data)
{
g_free(data);
data = NULL;
}
}
static void
__initialize(void)
{
response_table = g_hash_table_new_full(g_str_hash, g_str_equal, __free_data, __free_data);
datacontrol_sql_cb.insert = __sql_insert_response;
datacontrol_sql_cb.delete = __sql_delete_response;
datacontrol_sql_cb.select = __sql_select_response;
datacontrol_sql_cb.update = __sql_update_response;
}
EXPORT_API int
data_control_sql_create(data_control_h *provider)
{
return datacontrol_sql_create((datacontrol_h*)provider);
}
EXPORT_API int
data_control_sql_destroy(data_control_h provider)
{
return datacontrol_sql_destroy((datacontrol_h)provider);
}
EXPORT_API int
data_control_sql_set_provider_id(data_control_h provider, const char *provider_id)
{
return datacontrol_sql_set_provider_id((datacontrol_h)provider, provider_id);
}
EXPORT_API int
data_control_sql_get_provider_id(data_control_h provider, char **provider_id)
{
return datacontrol_sql_get_provider_id((datacontrol_h)provider, provider_id);
}
EXPORT_API int
data_control_sql_set_data_id(data_control_h provider, const char *data_id)
{
return datacontrol_sql_set_data_id((datacontrol_h)provider, data_id);
}
EXPORT_API int
data_control_sql_get_data_id(data_control_h provider, char **data_id)
{
return datacontrol_sql_get_data_id((datacontrol_h)provider, data_id);
}
EXPORT_API int
data_control_sql_register_response_cb(data_control_h provider, data_control_sql_response_cb* callback, void *user_data)
{
if (response_table == NULL)
{
__initialize();
}
if (callback == NULL)
{
return DATA_CONTROL_ERROR_INVALID_PARAMETER;
}
char *id = strdup(provider->provider_id);
if (id == NULL)
{
return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
}
data_control_sql_response_cb *cb
= (data_control_sql_response_cb *)malloc(sizeof(data_control_sql_response_cb));
if (cb == NULL)
{
free(id);
return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
}
memcpy(cb, callback, sizeof(data_control_sql_response_cb));
g_hash_table_insert(response_table, id, cb);
return datacontrol_sql_register_response_cb((datacontrol_h)provider, &datacontrol_sql_cb, user_data);
}
EXPORT_API int
data_control_sql_unregister_response_cb(data_control_h provider)
{
if (provider->provider_id)
{
g_hash_table_remove(response_table, provider->provider_id);
}
return datacontrol_sql_unregister_response_cb((datacontrol_h)provider);
}
EXPORT_API int
data_control_sql_insert(data_control_h provider, const bundle* insert_data, int *request_id)
{
int retval;
retval = data_control_consumer_check_privilege();
if (retval != DATA_CONTROL_ERROR_NONE) {
return retval;
}
return datacontrol_sql_insert((datacontrol_h)provider, insert_data, request_id);
}
EXPORT_API int
data_control_sql_delete(data_control_h provider, const char *where, int *request_id)
{
int retval;
retval = data_control_consumer_check_privilege();
if (retval != DATA_CONTROL_ERROR_NONE) {
return retval;
}
return datacontrol_sql_delete((datacontrol_h)provider, where, request_id);
}
EXPORT_API int
data_control_sql_select(data_control_h provider, char **column_list, int column_count, const char *where, const char *order, int *request_id)
{
int retval;
retval = data_control_consumer_check_privilege();
if (retval != DATA_CONTROL_ERROR_NONE) {
return retval;
}
return datacontrol_sql_select((datacontrol_h)provider, column_list, column_count, where, order, request_id);
}
EXPORT_API int
data_control_sql_select_with_page(data_control_h provider, char **column_list, int column_count, const char *where, const char *order, int page_number, int count_per_page, int *request_id)
{
int retval;
if (count_per_page > MAX_ROW_COUNT)
{
return DATA_CONTROL_ERROR_INVALID_PARAMETER;
}
retval = data_control_consumer_check_privilege();
if (retval != DATA_CONTROL_ERROR_NONE) {
return retval;
}
return datacontrol_sql_select_with_page((datacontrol_h)provider, column_list, column_count, where, order, page_number, count_per_page, request_id);
}
EXPORT_API int
data_control_sql_update(data_control_h provider, const bundle* update_data, const char *where, int *request_id)
{
int retval;
retval = data_control_consumer_check_privilege();
if (retval != DATA_CONTROL_ERROR_NONE) {
return retval;
}
return datacontrol_sql_update((datacontrol_h)provider, update_data, where, request_id);
}
|