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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* Contacts Service
*
* Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "contacts.h"
#include "ctsvc_internal.h"
#include "ctsvc_schema.h"
#include "ctsvc_sqlite.h"
#include "ctsvc_utils.h"
#include "ctsvc_db_init.h"
#include "ctsvc_db_plugin_contact_helper.h"
#include "ctsvc_db_plugin_profile_helper.h"
#include "ctsvc_record.h"
#include "ctsvc_db_query.h"
#include "ctsvc_list.h"
static int __ctsvc_db_profile_insert_record( contacts_record_h record, int *id );
static int __ctsvc_db_profile_get_record( int id, contacts_record_h* out_record );
static int __ctsvc_db_profile_update_record( contacts_record_h record );
static int __ctsvc_db_profile_delete_record( int id );
static int __ctsvc_db_profile_get_all_records( int offset, int limit, contacts_list_h* out_list );
static int __ctsvc_db_profile_get_records_with_query( contacts_query_h query, int offset, int limit, contacts_list_h* out_list );
//static int __ctsvc_db_profile_insert_records(const contacts_list_h in_list, int **ids);
//static int __ctsvc_db_profile_update_records(const contacts_list_h in_list);
//static int __ctsvc_db_profile_delete_records( int ids[], int count);
ctsvc_db_plugin_info_s ctsvc_db_plugin_profile = {
.is_query_only = false,
.insert_record = __ctsvc_db_profile_insert_record,
.get_record = __ctsvc_db_profile_get_record,
.update_record = __ctsvc_db_profile_update_record,
.delete_record = __ctsvc_db_profile_delete_record,
.get_all_records = __ctsvc_db_profile_get_all_records,
.get_records_with_query = __ctsvc_db_profile_get_records_with_query,
.insert_records = NULL,//__ctsvc_db_profile_insert_records,
.update_records = NULL,//__ctsvc_db_profile_update_records,
.delete_records = NULL,//__ctsvc_db_profile_delete_records
.get_count = NULL,
.get_count_with_query = NULL,
.replace_record = NULL,
.replace_records = NULL,
};
static int __ctsvc_db_profile_insert_record( contacts_record_h record, int *id )
{
int ret;
int contact_id;
char query[CTS_SQL_MAX_LEN] = {0};
ctsvc_profile_s *profile = (ctsvc_profile_s *)record;
RETVM_IF(NULL == profile->appsvc_operation, CONTACTS_ERROR_INVALID_PARAMETER,
"Invalid parameter : profile uid is NULL");
ret = ctsvc_begin_trans();
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
return ret;
}
snprintf(query, sizeof(query),
"SELECT contact_id FROM "CTSVC_DB_VIEW_CONTACT" WHERE contact_id = %d", profile->contact_id);
ret = ctsvc_query_get_first_int_result(query, &contact_id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("No data : contact_id (%d) is not exist", contact_id);
ctsvc_end_trans(false);
return CONTACTS_ERROR_INVALID_PARAMETER;
}
ret = ctsvc_db_profile_insert(record, profile->contact_id, false, id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_db_contact_update_changed_time(profile->contact_id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_query_exec() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_end_trans(true);
if (ret < CONTACTS_ERROR_NONE)
{
CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
return ret;
}
else
return CONTACTS_ERROR_NONE;
}
static int __ctsvc_db_profile_get_record( int id, contacts_record_h* out_record )
{
int ret;
int len;
cts_stmt stmt = NULL;
char query[CTS_SQL_MAX_LEN] = {0};
RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
*out_record = NULL;
len = snprintf(query, sizeof(query),
"SELECT id, data.contact_id, is_default, data1, data2, "
"data3, data4, data5, data6, data7, data8, data9, data10, data11, data12 "
"FROM "CTS_TABLE_DATA", "CTSVC_DB_VIEW_CONTACT" "
"ON "CTS_TABLE_DATA".contact_id = "CTSVC_DB_VIEW_CONTACT".contact_id "
"WHERE id = %d AND datatype = %d ",
id, CTSVC_DATA_PROFILE);
stmt = cts_query_prepare(query);
RETVM_IF(NULL == stmt, CONTACTS_ERROR_DB, "DB error : cts_query_prepare() Failed");
ret = cts_stmt_step(stmt);
if (1 /*CTS_TRUE*/ != ret) {
CTS_ERR("cts_stmt_step() Failed(%d)", ret);
cts_stmt_finalize(stmt);
return CONTACTS_ERROR_NO_DATA;
}
ctsvc_db_profile_get_value_from_stmt(stmt, out_record, 0);
cts_stmt_finalize(stmt);
return CONTACTS_ERROR_NONE;
}
static int __ctsvc_db_profile_update_record( contacts_record_h record )
{
int ret;
int contact_id;
char query[CTS_SQL_MAX_LEN] = {0};
ctsvc_profile_s *profile = (ctsvc_profile_s *)record;
ret = ctsvc_begin_trans();
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
return ret;
}
snprintf(query, sizeof(query),
"SELECT contact_id FROM "CTSVC_DB_VIEW_CONTACT" WHERE contact_id = %d", profile->contact_id);
ret = ctsvc_query_get_first_int_result(query, &contact_id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("No data : contact_id (%d) is not exist", contact_id);
ctsvc_end_trans(false);
return CONTACTS_ERROR_INVALID_PARAMETER;
}
ret = ctsvc_db_profile_update(record, profile->contact_id, false);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_db_contact_update_changed_time(profile->contact_id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_query_exec() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_end_trans(true);
if (ret < CONTACTS_ERROR_NONE)
{
CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
return ret;
}
else
return CONTACTS_ERROR_NONE;
}
static int __ctsvc_db_profile_delete_record( int id )
{
int ret;
int contact_id;
char query[CTS_SQL_MAX_LEN] = {0};
ret = ctsvc_begin_trans();
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
return ret;
}
snprintf(query, sizeof(query),
"SELECT contact_id FROM "CTSVC_DB_VIEW_CONTACT " "
"WHERE contact_id = (SELECT contact_id FROM "CTS_TABLE_DATA" WHERE id = %d)", id);
ret = ctsvc_query_get_first_int_result(query, &contact_id);
if( ret != CONTACTS_ERROR_NONE ) {
CTS_ERR("The id(%d) is Invalid(%d)", id, ret);
ctsvc_end_trans(false);
return contact_id;
}
ret = ctsvc_db_profile_delete(id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_db_contact_update_changed_time(contact_id);
if (CONTACTS_ERROR_NONE != ret) {
CTS_ERR("DB error : ctsvc_query_exec() Failed(%d)", ret);
ctsvc_end_trans(false);
return ret;
}
ret = ctsvc_end_trans(true);
if (ret < CONTACTS_ERROR_NONE)
{
CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
return ret;
}
else
return CONTACTS_ERROR_NONE;
}
static int __ctsvc_db_profile_get_all_records( int offset, int limit, contacts_list_h* out_list )
{
int len;
int ret;
contacts_list_h list;
ctsvc_profile_s *profile;
cts_stmt stmt = NULL;
char query[CTS_SQL_MAX_LEN] = {0};
len = snprintf(query, sizeof(query),
"SELECT id, data.contact_id, is_default, data1, data2, "
"data3, data4, data5, data6, data7, data8, data9, data10 "
"FROM "CTS_TABLE_DATA", "CTSVC_DB_VIEW_CONTACT" "
"ON "CTS_TABLE_DATA".contact_id = "CTSVC_DB_VIEW_CONTACT".contact_id "
"WHERE datatype = %d AND is_my_profile=0 ",
CTSVC_DATA_PROFILE);
if (0 < limit) {
len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
if (0 < offset)
len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
}
stmt = cts_query_prepare(query);
RETVM_IF(NULL == stmt, CONTACTS_ERROR_DB, "DB error : cts_query_prepare() Failed");
contacts_list_create(&list);
while ((ret = cts_stmt_step(stmt))) {
if (1 /*CTS_TRUE */ != ret) {
CTS_ERR("DB : cts_stmt_step fail(%d)", ret);
cts_stmt_finalize(stmt);
contacts_list_destroy(list, true);
return ret;
}
ctsvc_db_profile_get_value_from_stmt(stmt, (contacts_record_h*)&profile, 0);
ctsvc_list_prepend(list, (contacts_record_h)profile);
}
cts_stmt_finalize(stmt);
ctsvc_list_reverse(list);
*out_list = list;
return CONTACTS_ERROR_NONE;
}
static int __ctsvc_db_profile_get_records_with_query( contacts_query_h query, int offset,
int limit, contacts_list_h* out_list )
{
int ret;
int i;
int field_count;
ctsvc_query_s *s_query;
cts_stmt stmt;
contacts_list_h list;
ctsvc_profile_s *profile;
RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
s_query = (ctsvc_query_s *)query;
ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
contacts_list_create(&list);
while ((ret = cts_stmt_step(stmt))) {
contacts_record_h record;
if (1 /*CTS_TRUE */ != ret) {
CTS_ERR("DB error : cts_stmt_step() Failed(%d)", ret);
cts_stmt_finalize(stmt);
contacts_list_destroy(list, true);
return ret;
}
contacts_record_create(_contacts_profile._uri, &record);
profile = (ctsvc_profile_s*)record;
if (0 == s_query->projection_count)
field_count = s_query->property_count;
else {
field_count = s_query->projection_count;
ret = ctsvc_record_set_projection_flags(record, s_query->projection,
s_query->projection_count, s_query->property_count);
if(CONTACTS_ERROR_NONE != ret)
ASSERT_NOT_REACHED("To set projection is failed.\n");
}
for(i=0;i<field_count;i++) {
char *temp;
int property_id;
if (0 == s_query->projection_count)
property_id = s_query->properties[i].property_id;
else
property_id = s_query->projection[i];
switch(property_id) {
case CTSVC_PROPERTY_PROFILE_ID:
profile->id = ctsvc_stmt_get_int(stmt, i);
break;
case CTSVC_PROPERTY_PROFILE_CONTACT_ID:
profile->contact_id = ctsvc_stmt_get_int(stmt, i);
break;
case CTSVC_PROPERTY_PROFILE_TYPE:
profile->type = ctsvc_stmt_get_int(stmt, i);
break;
case CTSVC_PROPERTY_PROFILE_LABEL:
temp = ctsvc_stmt_get_text(stmt, i);
profile->label = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_UID:
temp = ctsvc_stmt_get_text(stmt, i);
profile->uid = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_TEXT:
temp = ctsvc_stmt_get_text(stmt, i);
profile->text = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_ORDER:
profile->order = ctsvc_stmt_get_int(stmt, i);
break;
case CTSVC_PROPERTY_PROFILE_APPSVC_OPERATION:
temp = ctsvc_stmt_get_text(stmt, i);
profile->appsvc_operation = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_DATA1:
temp = ctsvc_stmt_get_text(stmt, i);
profile->data1 = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_DATA2:
temp = ctsvc_stmt_get_text(stmt, i);
profile->data2 = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_DATA3:
temp = ctsvc_stmt_get_text(stmt, i);
profile->data3 = SAFE_STRDUP(temp);
break;
case CTSVC_PROPERTY_PROFILE_DATA4:
temp = ctsvc_stmt_get_text(stmt, i);
profile->data4 = SAFE_STRDUP(temp);
break;
default:
break;
}
}
ctsvc_list_prepend(list, record);
}
cts_stmt_finalize(stmt);
ctsvc_list_reverse(list);
*out_list = list;
return CONTACTS_ERROR_NONE;
}
//static int __ctsvc_db_profile_insert_records(const contacts_list_h in_list, int **ids) { return CONTACTS_ERROR_NONE; }
//static int __ctsvc_db_profile_update_records(const contacts_list_h in_list) { return CONTACTS_ERROR_NONE; }
//static int __ctsvc_db_profile_delete_records( int ids[], int count) { return CONTACTS_ERROR_NONE; }
|