c60e2bbd47ad3bb05b0eea46d61bef30b59e1739
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Contact / query-svc / query-svc.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18  * @file                query-svc.h
19  * @author      JihwaPark (82-32-3016262, jh7979.park@samsung.com)
20  * @version      1.0(2011.10.05)
21  * @brief       This file has filter APIs for supporting advanced query
22  */
23
24 #include "query-service.h"
25 #include "query-type.h"
26 #include "query-utility.h"
27 #include <glib.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <contacts-svc.h>
32
33 typedef struct
34 {
35         int addressbook_id;
36         int account_id;
37 }addressbook_list;
38
39 GList* g_addressbook_list = NULL;
40 int total_addressbook_id = 0;
41
42 static int list_cb(CTSvalue *ab, void *user_data)
43 {
44         addressbook_list* list = NULL;
45
46         list = g_new0(addressbook_list, 1);
47         list->addressbook_id = contacts_svc_value_get_int(ab, CTS_LIST_ADDRESSBOOK_ID_INT);
48         list->account_id = contacts_svc_value_get_int(ab, CTS_LIST_ADDRESSBOOK_ACC_ID_INT);
49
50         g_addressbook_list = g_list_append(g_addressbook_list, list);
51         total_addressbook_id++;
52
53         return CTS_SUCCESS;
54 }
55
56 void __free_addressbook_list(gpointer data, gpointer user_data)
57 {
58         ADVANCED_SVC_TRACE("__free_address_list");
59         OBJ_FREE(data);
60 }
61
62 API void refresh_addressbook_list(void *data)
63 {
64         g_list_foreach(g_addressbook_list, __free_addressbook_list, NULL);
65         g_list_free(g_addressbook_list);
66         g_addressbook_list = NULL;
67
68         contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
69 }
70
71
72 API int find_addressbook_id(int account_id, query_error *error_code)
73 {
74         int ret;
75         int addressbook_id = -1;
76
77         ADVANCED_SVC_TRACE("account_id : %d", account_id);
78
79         if(account_id == 0)
80                 return 0;
81
82         if(g_addressbook_list == NULL)
83         {
84                 ADVANCED_SVC_TRACE("GENERATE ADDRESSBOOK LIST");
85                 ret = contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
86         }
87
88         GList* tmp_list = g_addressbook_list;
89         while(tmp_list)
90         {
91                 addressbook_list* info = (addressbook_list*)tmp_list->data;
92                 if(info->account_id == account_id)
93                 {
94                         addressbook_id = info->addressbook_id;
95                         break;
96                 }
97                 tmp_list = g_list_next(tmp_list);
98         }
99         if(addressbook_id == -1)
100                 *error_code = QUERY_NO_RECORD;
101         else
102                 *error_code = QUERY_SUCCESS;
103
104         ADVANCED_SVC_TRACE("addressbook_id : %d", addressbook_id);
105
106         return addressbook_id;
107 }
108
109 API int* get_all_addressbook_id(int* total_count)
110 {
111         int i = 0;
112         int* id_list = NULL;
113
114         if(g_addressbook_list == NULL)
115         {
116                 ADVANCED_SVC_TRACE("GENERATE ADDRESSBOOK LIST");
117                 contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
118         }
119
120         id_list = g_new0(int, (total_addressbook_id + 2));
121         id_list[i] = 0;
122
123         GList* tmp_list = g_addressbook_list;
124         while(tmp_list)
125         {
126                 i++;
127                 addressbook_list* info = (addressbook_list*)tmp_list->data;
128
129                 id_list[i] = info->addressbook_id;
130                 tmp_list = g_list_next(tmp_list);
131         }
132
133         *total_count = total_addressbook_id + 1;
134
135         return id_list;
136 }
137
138 int _value_to_projection(stmt hstmt, char** projection, int count)
139 {
140         int pos = 0;
141
142         pos = count;
143
144         if((*projection) == NULL)
145                 *projection = (char*)g_strdup(_ct_query_column_text(hstmt, pos++));
146         else
147                 pos++;
148
149         return pos;
150 }
151
152 void __convert_number_type_to_str(int db_value, GString* type_str)
153 {
154         if((db_value & HOME) == HOME)
155                 g_string_append_printf (type_str, "%s", number_type_str[1]);
156
157         if((db_value & WORK) == WORK)
158         {
159                 if(type_str->str[0] == '\0')
160                         g_string_append_printf (type_str, "%s", number_type_str[2]);
161                 else
162                         g_string_append_printf(type_str, ",%s", number_type_str[2]);
163         }
164
165         if((db_value & VOICE) == VOICE)
166         {
167                 if(type_str->str[0] == '\0')
168                         g_string_append_printf (type_str, "%s", number_type_str[3]);
169                 else
170                         g_string_append_printf(type_str, ",%s", number_type_str[3]);
171         }
172
173         if((db_value & FAX) == FAX)
174         {
175                 if(type_str->str[0] == '\0')
176                         g_string_append_printf (type_str, "%s", number_type_str[4]);
177                 else
178                         g_string_append_printf(type_str, ",%s", number_type_str[4]);
179         }
180
181         if((db_value & MSG) == MSG)
182         {
183                 if(type_str->str[0] == '\0')
184                         g_string_append_printf (type_str, "%s", number_type_str[5]);
185                 else
186                         g_string_append_printf(type_str, ",%s", number_type_str[5]);
187         }
188
189         if((db_value & CELL) == CELL)
190         {
191                 if(type_str->str[0] == '\0')
192                         g_string_append_printf (type_str, "%s", number_type_str[6]);
193                 else
194                         g_string_append_printf(type_str, ",%s", number_type_str[6]);
195         }
196
197         if((db_value & PAGER) == PAGER)
198         {
199                 if(type_str->str[0] == '\0')
200                         g_string_append_printf (type_str, "%s", number_type_str[7]);
201                 else
202                         g_string_append_printf(type_str, ",%s", number_type_str[7]);
203         }
204
205         if((db_value & BBS) == BBS)
206         {
207                 if(type_str->str[0] == '\0')
208                         g_string_append_printf (type_str, "%s", number_type_str[8]);
209                 else
210                         g_string_append_printf(type_str, ",%s", number_type_str[8]);
211         }
212
213         if((db_value & MODEM) == MODEM)
214         {
215                 if(type_str->str[0] == '\0')
216                         g_string_append_printf (type_str, "%s", number_type_str[9]);
217                 else
218                         g_string_append_printf(type_str, ",%s", number_type_str[9]);
219         }
220
221         if((db_value & CAR) == CAR)
222         {
223                 if(type_str->str[0] == '\0')
224                         g_string_append_printf (type_str, "%s", number_type_str[10]);
225                 else
226                         g_string_append_printf(type_str, ",%s", number_type_str[10]);
227         }
228
229         if((db_value & ISDN) == ISDN)
230         {
231                 if(type_str->str[0] == '\0')
232                         g_string_append_printf (type_str, "%s", number_type_str[11]);
233                 else
234                         g_string_append_printf(type_str, ",%s", number_type_str[11]);
235         }
236
237         if((db_value & VIDEO) == VIDEO)
238         {
239                 if(type_str->str[0] == '\0')
240                         g_string_append_printf (type_str, "%s", number_type_str[12]);
241                 else
242                         g_string_append_printf(type_str, ",%s", number_type_str[12]);
243         }
244
245         if((db_value & PCS) == PCS)
246         {
247                 if(type_str->str[0] == '\0')
248                         g_string_append_printf (type_str, "%s", number_type_str[13]);
249                 else
250                         g_string_append_printf(type_str, ",%s", number_type_str[13]);
251         }
252
253         if(type_str->str[0] == '\0')
254                 g_string_append_printf (type_str, "%s", number_type_str[3]);
255
256 }
257
258 void __convert_email_type_to_str(int db_value, GString* type_str)
259 {
260         if((db_value & EMAIL_HOME) == EMAIL_HOME)
261         {
262                 if(type_str->str[0] == '\0')
263                         g_string_append_printf (type_str, "%s", email_type_str[1]);
264                 else
265                         g_string_append_printf (type_str, ",%s", email_type_str[1]);
266         }
267         if((db_value & EMAIL_WORK) == EMAIL_WORK)
268         {
269                 if(type_str->str[0] == '\0')
270                         g_string_append_printf (type_str, "%s", email_type_str[2]);
271                 else
272                         g_string_append_printf (type_str, ",%s", email_type_str[2]);
273         }
274
275         if(type_str->str[0] == '\0')
276                 g_string_append_printf (type_str, "%s", email_type_str[1]);
277 }
278
279 void __convert_url_type_to_str(int db_value, GString* type_str)
280 {
281         if(db_value == URL_HOME)
282         {
283                 if(type_str->str[0] == '\0')
284                         g_string_append_printf (type_str, "%s", url_type_str[URL_HOME]);
285                 else
286                         g_string_append_printf (type_str, ",%s", url_type_str[URL_HOME]);
287         }else if(db_value == URL_WORK)
288         {
289                 if(type_str->str[0] == '\0')
290                         g_string_append_printf (type_str, "%s", url_type_str[URL_WORK]);
291                 else
292                         g_string_append_printf (type_str, ",%s", url_type_str[URL_WORK]);
293         }
294
295         if(type_str->str[0] == '\0')
296                 g_string_append_printf (type_str, "%s", url_type_str[URL_HOME]);
297 }
298
299 void __convert_event_type_to_str(int db_value, GString* type_str)
300 {
301         if(db_value == 0)
302                 g_string_append_printf (type_str, "%s", event_type_str[EVENT_BIRTHDAY]);
303         else if(db_value == 1)
304                 g_string_append_printf (type_str, "%s", event_type_str[EVENT_ANNIVERSARY]);
305
306         if(type_str->str[0] == '\0')
307                 g_string_append_printf (type_str, "%s", event_type_str[EVENT_BIRTHDAY]);
308 }
309
310 void __convert_address_type_to_str(int db_value, GString* type_str)
311 {
312         if((db_value & ADDRESS_TYPE_HOME) == ADDRESS_TYPE_HOME)
313         {
314                 if(type_str->str[0] == '\0')
315                         g_string_append_printf (type_str, "%s", addr_type_str[1]);
316                 else
317                         g_string_append_printf (type_str, ",%s", addr_type_str[1]);
318         }
319         if((db_value & ADDRESS_TYPE_WORK) == ADDRESS_TYPE_WORK)
320         {
321                 if(type_str->str[0] == '\0')
322                         g_string_append_printf (type_str, "%s", addr_type_str[2]);
323                 else
324                         g_string_append_printf (type_str, ",%s", addr_type_str[2]);
325         }
326         if((db_value & ADDRESS_TYPE_DOM) == ADDRESS_TYPE_DOM)
327         {
328                 if(type_str->str[0] == '\0')
329                         g_string_append_printf (type_str, "%s", addr_type_str[3]);
330                 else
331                         g_string_append_printf (type_str, ",%s", addr_type_str[3]);
332         }
333         if((db_value & ADDRESS_TYPE_INTL) == ADDRESS_TYPE_INTL)
334         {
335                 if(type_str->str[0] == '\0')
336                         g_string_append_printf (type_str, "%s", addr_type_str[4]);
337                 else
338                         g_string_append_printf (type_str, ",%s", addr_type_str[4]);
339         }
340         if((db_value & ADDRESS_TYPE_POSTAL) == ADDRESS_TYPE_POSTAL)
341         {
342                 if(type_str->str[0] == '\0')
343                         g_string_append_printf (type_str, "%s", addr_type_str[5]);
344                 else
345                         g_string_append_printf (type_str, ",%s", addr_type_str[5]);
346         }
347         if((db_value & ADDRESS_TYPE_PARCEL) == ADDRESS_TYPE_PARCEL)
348         {
349                 if(type_str->str[0] == '\0')
350                         g_string_append_printf (type_str, "%s", addr_type_str[6]);
351                 else
352                         g_string_append_printf (type_str, ",%s", addr_type_str[6]);
353         }
354
355         if(type_str->str[0] == '\0')
356                 g_string_append_printf (type_str, "%s", addr_type_str[1]);
357 }
358
359 void __convert_x_info_to_str(int db_value, GString* type_str)
360 {
361         g_string_append_printf (type_str, "%d", db_value);
362 }
363
364 void __convert_is_favorite_to_str(int db_value, GString* type_str)
365 {
366         if(db_value == 1)
367                 g_string_append_printf (type_str, "%s", is_favorite_str[1]);
368         else
369                 g_string_append_printf (type_str, "%s", is_favorite_str[0]);
370 }
371
372 int _int_value_to_projection(tables_e table, db_datatype datatype, stmt hstmt, char** projection, int count)
373 {
374         int pos = 0;
375         int db_value = 0;
376         GString* type_str = g_string_new ("");
377
378         pos = count;
379
380         if((*projection) == NULL)
381         {
382                 db_value = _ct_query_column_int(hstmt, pos++);
383                 if(table == DATA_TABLE)
384                 {
385                         switch(datatype)
386                         {
387                                 case DB_DATATYPE_NUMBER:
388                                         __convert_number_type_to_str(db_value, type_str);
389                                         break;
390                                 case DB_DATATYPE_EMAIL:
391                                         __convert_email_type_to_str(db_value, type_str);
392                                         break;
393                                 case DB_DATATYPE_URL:
394                                         __convert_url_type_to_str(db_value, type_str);
395                                         break;
396                                 case DB_DATATYPE_EVENT:
397                                         __convert_event_type_to_str(db_value, type_str);
398                                         break;
399                                 case DB_DATATYPE_ADDRESS:
400                                         __convert_address_type_to_str(db_value, type_str);
401                                         break;
402                                 default:
403                                         __convert_x_info_to_str(db_value, type_str);
404                                         break;
405                         }
406                 }else if(table == CONTACT_TABLE)
407                 {
408                         __convert_is_favorite_to_str(db_value, type_str);
409                 }else if(table == PERSON_TABLE)
410                 {
411                         __convert_is_favorite_to_str(db_value, type_str);
412                 }
413
414                 *projection = (char*)g_strdup(type_str->str);
415         }else
416                 pos++;
417
418         g_string_free(type_str, TRUE);
419
420         return pos;
421 }
422
423 int _event_value_to_projection(stmt hstmt, char** projection, int count)
424 {
425         int pos = 0;
426         int db_value = 0;
427         GString* type_str = g_string_new ("");
428
429         pos = count;
430
431         if((*projection) == NULL)
432         {
433                 db_value = _ct_query_column_int(hstmt, pos++);
434                 g_string_append_printf (type_str, "%d", db_value);
435
436                 *projection = (char*)g_strdup(type_str->str);
437         }else
438                 pos++;
439
440         g_string_free(type_str, TRUE);
441
442         return pos;
443 }
444
445 target_table_e _check_join_case(advanced_handle_t* q_handle)
446 {
447         ADVANCED_RETURN_VAL((q_handle->query_table  != INVALID_TABLE), {}, INVALID_TABLE_LIST, ("ERROR"));
448         target_table_e value = INVALID_TABLE_LIST;
449
450         if(strcmp(q_handle->table_list->str, table_key[TABLE_PERSON]) == 0)
451         {
452                 value = ONLY_PERSON;
453         }else if(strcmp(q_handle->table_list->str, table_key[TABLE_CONTACT]) == 0)
454         {
455                 value = ONLY_CONTACT;
456         }else if(strcmp(q_handle->table_list->str, table_key[TABLE_DATA]) == 0)
457         {
458                 if(q_handle->query_type == QUERY_TO_CONTACT)
459                         value = ONLY_DATA;
460                 else if(q_handle->query_type == QUERY_TO_PERSON)
461                         value = ONLY_DATA_FROM_PERSON_QUERY;
462         }
463
464         if(value != INVALID_TABLE_LIST)
465                 return value;
466
467         ADVANCED_SVC_TRACE("!! q_handle->query_table : %d", q_handle->query_table);
468
469         if(((q_handle->query_table & PERSON_TABLE) != 0) && ((q_handle->query_table & CONTACT_TABLE) == 0) && ((q_handle->query_table & DATA_TABLE) != 0))
470         {
471                 value = JOIN_PERSON_DATA;
472         }else if(((q_handle->query_table & PERSON_TABLE) == 0) && ((q_handle->query_table & CONTACT_TABLE) != 0) && ((q_handle->query_table & DATA_TABLE) != 0))
473         {
474                 value = JOIN_CONTACT_DATA;
475         }
476
477         ADVANCED_SVC_TRACE("value is %d", value);
478
479         return value;
480 }
481
482 query_error _make_contact_info(advanced_handle_t* q_handle, projection_type type, target_table_e join_table)
483 {
484         query_error error_code = QUERY_SUCCESS;
485         int contact_id = 0;
486         db_datatype datatype_id = DB_DATATYPE_INVALID;
487         int count = 0;
488         char projection[PROJECTION_FIELD_LEN + 1] = {0, };
489
490         number_t* num_info = NULL;
491         email_t* email_info = NULL;
492         url_t* url_info = NULL;
493         nick_name_t* nick_info = NULL;
494         address_t* address_info = NULL;
495         organization_t* organization_info = NULL;
496         event_info_t* event_info = NULL;
497         x_info_t* x_info = NULL;
498
499         if(type == ONLY_CONTACT_ID_FIELD)
500         {
501                 contact_id = _ct_query_column_int(q_handle->hstmt, count++);
502         }else
503         {
504                 if(join_table == ONLY_PERSON || join_table == ONLY_CONTACT)
505                 {
506                         contact_id = _ct_query_column_int(q_handle->hstmt, count++);
507                 }else
508                 {
509                         datatype_id = _ct_query_column_int(q_handle->hstmt, count++);
510                         contact_id = _ct_query_column_int(q_handle->hstmt, count++);
511                 }
512         }
513
514         if(q_handle->window_size > 0)
515         {
516                 if(q_handle->start_pos >= contact_id)
517                         return QUERY_SUCCESS;
518         }
519
520         if(q_handle->old_contact_id != contact_id)
521         {
522                 q_handle->pos++;
523                 q_handle->result_set[q_handle->pos].contact_info = g_new0(contact_t, 1);
524                 q_handle->result_set[q_handle->pos].contact_info->person_info = g_new0(person_contact_t, 1);
525                 q_handle->result_set[q_handle->pos].contact_info->raw_contact_info = g_new0(raw_contact_t, 1);
526
527                 if(join_table == ONLY_PERSON || join_table == JOIN_PERSON_DATA)
528                 {
529                         q_handle->result_set[q_handle->pos].contact_info->person_info->contact_id = g_new0(char, 64);
530                         snprintf(q_handle->result_set[q_handle->pos].contact_info->person_info->contact_id, 64, "%d", contact_id);
531                 }else
532                 {
533                         q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->contact_id = g_new0(char, 64);
534                         snprintf(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->contact_id, 64, "%d", contact_id);
535                 }
536
537                 q_handle->result_set[q_handle->pos].is_occupied = 1;
538                 q_handle->old_contact_id = contact_id;
539
540                 q_handle->mapping_id_to_result_set[q_handle->pos].contact_id = contact_id;
541                 q_handle->total_result_set_count++;
542         }
543
544         if((q_handle->window_size > 0) && (q_handle->total_result_set_count > q_handle->window_size))
545         {
546                 q_handle->total_result_set_count--;
547                 return QUERY_SET_LIMIT;
548         }
549
550         switch(datatype_id)
551         {
552                 case DB_DATATYPE_NAME :
553                 {
554                         ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->pos].contact_info->name_info == NULL), {}, QUERY_FAIL, ("ONLY ONE NAME IS POSSIBLE"));
555                         q_handle->result_set[q_handle->pos].contact_info->name_info = g_new0(name_t, 1);
556                         ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->pos].contact_info->name_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
557                 }
558                         break;
559                 case DB_DATATYPE_NUMBER:
560                 {
561                         num_info = g_new0(number_t, 1);
562                         ADVANCED_CATCH_SET_ERROR((num_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
563                 }
564                         break;
565                 case DB_DATATYPE_EMAIL:
566                 {
567                         email_info = g_new0(email_t, 1);
568                         ADVANCED_CATCH_SET_ERROR((email_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
569                 }
570                         break;
571                 case DB_DATATYPE_URL:
572                 {
573                         url_info = g_new0(url_t, 1);
574                         ADVANCED_CATCH_SET_ERROR((url_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
575                 }
576                         break;
577                 case DB_DATATYPE_NICK_NAME:
578                 {
579                         nick_info = g_new0(nick_name_t, 1);
580                         ADVANCED_CATCH_SET_ERROR((nick_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
581                 }
582                         break;
583                 case DB_DATATYPE_ADDRESS:
584                 {
585                         address_info = g_new0(address_t, 1);
586                         ADVANCED_CATCH_SET_ERROR((address_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
587                 }
588                         break;
589                 case DB_DATATYPE_ORGANIZATION:
590                 {
591                         organization_info = g_new0(organization_t, 1);
592                         ADVANCED_CATCH_SET_ERROR((organization_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
593                 }
594                         break;
595                 case DB_DATATYPE_EVENT:
596                 {
597                         event_info = g_new0(event_info_t, 1);
598                         ADVANCED_CATCH_SET_ERROR((event_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
599                 }
600                         break;
601                 default:
602                 {
603                         if(datatype_id > DB_DATATYPE_MAX)
604                         {
605                                 x_info = g_new0(x_info_t, 1);
606                                 ADVANCED_CATCH_SET_ERROR((x_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
607                         }
608                 }
609                         break;
610         }
611
612         int old_count = 0;
613         GList* tmp_projection = q_handle->new_projection;
614         while(tmp_projection)
615         {
616                 projection_t* projection_info = (projection_t*)tmp_projection->data;
617                 memset(projection, 0x00, sizeof(projection));
618
619                 g_strlcpy(projection, projection_info->field, sizeof(projection) - 1);
620
621                 tmp_projection = g_list_next(tmp_projection);
622
623                 old_count = count;
624                 switch(datatype_id)
625                 {
626                         case DB_DATATYPE_NAME :
627                         {
628                                 if(strcmp(field_key[FIELD_DATA2], projection) == 0 && q_handle->attribute_list[NAME_FIRST] == 1)
629                                 {
630                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->first_name), count);
631                                 }else if(strcmp(field_key[FIELD_DATA4], projection) == 0 && q_handle->attribute_list[NAME_MIDDLE] == 1)
632                                 {
633                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->middle_name), count);
634                                 }else if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[NAME_LAST] == 1)
635                                 {
636                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->last_name), count);
637                                 }else if(strcmp(field_key[FIELD_DATA6], projection) == 0 && q_handle->attribute_list[NAME_PREFIX] == 1)
638                                 {
639                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->prefix), count);
640                                 }else if(strcmp(field_key[FIELD_DATA7], projection) == 0 && q_handle->attribute_list[NAME_PHONETIC] == 1)
641                                 {
642                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->phonetic_name), count);
643                                 }else if(strcmp(field_key[FIELD_DATA5], projection) == 0 && q_handle->attribute_list[NAME_DISPLAY] == 1)
644                                 {
645                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->display_name), count);
646                                 }
647
648                         }
649                                 break;
650                         case DB_DATATYPE_NUMBER:
651                         {
652                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[NUMBER_ADDRESS] == 1)
653                                 {
654                                         count = _value_to_projection(q_handle->hstmt, &(num_info->number), count);
655                                 }else if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[NUMBER_TYPES] == 1)
656                                 {
657                                         count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_NUMBER, q_handle->hstmt, &(num_info->number_type), count);
658                                 }
659                         }
660                                 break;
661                         case DB_DATATYPE_EMAIL:
662                         {
663                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[EMAIL_ADDRESS] == 1)
664                                 {
665                                         count = _value_to_projection(q_handle->hstmt, &(email_info->email_addr), count);
666                                 }else if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[EMAIL_TYPE] == 1)
667                                 {
668                                         count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_EMAIL, q_handle->hstmt, &(email_info->email_type), count);
669                                 }
670
671                         }
672                                 break;
673                         case DB_DATATYPE_URL:
674                         {
675                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[URL_ADDRESS] == 1)
676                                 {
677                                         count = _value_to_projection(q_handle->hstmt, &(url_info->url_addr), count);
678                                 }else if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[URL_TYPE] == 1)
679                                 {
680                                         count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_URL, q_handle->hstmt, &(url_info->url_type), count);
681                                 }
682                         }
683                                 break;
684                         case DB_DATATYPE_NICK_NAME:
685                         {
686                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[NICKNAME_NAME] == 1)
687                                 {
688                                         count = _value_to_projection(q_handle->hstmt, &(nick_info->nick_name), count);
689                                 }
690                         }
691                                 break;
692                         case DB_DATATYPE_ADDRESS:
693                         {
694                                 if(strcmp(field_key[FIELD_DATA9], projection) == 0 && q_handle->attribute_list[ADDRESS_COUNTRY] == 1)
695                                 {
696                                         count = _value_to_projection(q_handle->hstmt, &(address_info->country), count);
697                                 }else if(strcmp(field_key[FIELD_DATA5], projection) == 0 && q_handle->attribute_list[ADDRESS_REGION] == 1)
698                                 {
699                                         count = _value_to_projection(q_handle->hstmt, &(address_info->region), count);
700                                 }else if(strcmp(field_key[FIELD_DATA6], projection) == 0 && q_handle->attribute_list[ADDRESS_CITY] == 1)
701                                 {
702                                         count = _value_to_projection(q_handle->hstmt, &(address_info->city), count);
703                                 }else if(strcmp(field_key[FIELD_DATA7], projection) == 0 && q_handle->attribute_list[ADDRESS_STREET] == 1)
704                                 {
705                                         count = _value_to_projection(q_handle->hstmt, &(address_info->street), count);
706                                 }else if(strcmp(field_key[FIELD_DATA4], projection) == 0 && q_handle->attribute_list[ADDRESS_POSTAL_CODE] == 1)
707                                 {
708                                         count = _value_to_projection(q_handle->hstmt, &(address_info->postal_code), count);
709                                 }else if(strcmp(field_key[FIELD_DATA8], projection) == 0 && q_handle->attribute_list[ADDRESS_ADDITIONAL] == 1)
710                                 {
711                                         count = _value_to_projection(q_handle->hstmt, &(address_info->addtional), count);
712                                 }else if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[ADDRESS_TYPE] == 1)
713                                 {
714                                         count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_ADDRESS, q_handle->hstmt, &(address_info->type), count);
715                                 }
716                         }
717                                 break;
718                         case DB_DATATYPE_ORGANIZATION:
719                         {
720                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[ORGANIZATION_NAME] == 1)
721                                 {
722                                         count = _value_to_projection(q_handle->hstmt, &(organization_info->organization_name), count);
723                                 }else if(strcmp(field_key[FIELD_DATA4], projection) == 0 && q_handle->attribute_list[ORGANIZATION_DEPARTMENT] == 1)
724                                 {
725                                         count = _value_to_projection(q_handle->hstmt, &(organization_info->department), count);
726                                 }else if(strcmp(field_key[FIELD_DATA5], projection) == 0 && q_handle->attribute_list[ORGANIZATION_TITLE] == 1)
727                                 {
728                                         count = _value_to_projection(q_handle->hstmt, &(organization_info->title), count);
729                                 }else if(strcmp(field_key[FIELD_DATA6], projection) == 0 && q_handle->attribute_list[ORGANIZATION_ROLE] == 1)
730                                 {
731                                         count = _value_to_projection(q_handle->hstmt, &(organization_info->role), count);
732                                 }
733                         }
734                                 break;
735                         case DB_DATATYPE_EVENT:
736                         {
737                                 if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[EVENT_TYPE] == 1)
738                                 {
739                                         count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_EVENT, q_handle->hstmt, &(event_info->type), count);
740                                 }else if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[EVENT_DATE] == 1)
741                                 {
742                                         count = _event_value_to_projection(q_handle->hstmt, &(event_info->date), count);
743                                 }
744                         }
745                                 break;
746                         case DB_DATATYPE_NOTE:
747                         {
748                                 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[CONTACT_NOTE] == 1)
749                                 {
750                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->note), count);
751                                 }
752                         }
753                                 break;
754                         default:
755                         {
756                                 if(datatype_id > DB_DATATYPE_MAX)
757                                 {
758                                         if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->x_attribute_list[X_DATA1] == 1)
759                                         {
760                                                 count = _int_value_to_projection(DATA_TABLE, datatype_id, q_handle->hstmt, &(x_info->data1), count);
761                                         }else if(strcmp(field_key[FIELD_DATA2], projection) == 0 && q_handle->x_attribute_list[X_DATA2] == 1)
762                                         {
763                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data2), count);
764                                         }else if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->x_attribute_list[X_DATA3] == 1)
765                                         {
766                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data3), count);
767                                         }else if(strcmp(field_key[FIELD_DATA4], projection) == 0 && q_handle->x_attribute_list[X_DATA4] == 1)
768                                         {
769                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data4), count);
770                                         }else if(strcmp(field_key[FIELD_DATA5], projection) == 0 && q_handle->x_attribute_list[X_DATA5] == 1)
771                                         {
772                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data5), count);
773                                         }else if(strcmp(field_key[FIELD_DATA6], projection) == 0 && q_handle->x_attribute_list[X_DATA6] == 1)
774                                         {
775                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data6), count);
776                                         }else if(strcmp(field_key[FIELD_DATA7], projection) == 0 && q_handle->x_attribute_list[X_DATA7] == 1)
777                                         {
778                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data7), count);
779                                         }else if(strcmp(field_key[FIELD_DATA8], projection) == 0 && q_handle->x_attribute_list[X_DATA8] == 1)
780                                         {
781                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data8), count);
782                                         }else if(strcmp(field_key[FIELD_DATA9], projection) == 0 && q_handle->x_attribute_list[X_DATA9] == 1)
783                                         {
784                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data9), count);
785                                         }else if(strcmp(field_key[FIELD_DATA10], projection) == 0 && q_handle->x_attribute_list[X_DATA10] == 1)
786                                         {
787                                                 count = _value_to_projection(q_handle->hstmt, &(x_info->data10), count);
788                                         }
789                                 }
790                         }
791                                 break;
792                 }
793
794                 {
795                         if(join_table == ONLY_PERSON || join_table == JOIN_PERSON_DATA)
796                         {
797                                 if(strcmp(field_key[FIELD_PERSON_IS_SELF], projection) == 0)
798                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->is_self), count);
799                                 else if(strcmp(field_key[FIELD_CONTACT_ID_FOR_NAME], projection) == 0)
800                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->contact_id_for_name), count);
801                                 else if(strcmp(field_key[FIELD_CONTACT_ID_FOR_PHOTO], projection) == 0)
802                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->contact_id_for_photo), count);
803                                 else if(strcmp(field_key[FIELD_CONTACT_ID_FOR_RINGTONE], projection) == 0)
804                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->contactid_for_ringtone), count);
805                                 else if(strcmp(field_key[FIELD_PERSON_PREFIX_NAME], projection) == 0)
806                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->prefix), count);
807                                 else if(strcmp(field_key[FIELD_PERSON_FIRST_NAME], projection) == 0)
808                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->first_name), count);
809                                 else if(strcmp(field_key[FIELD_PERSON_MIDDLE_NAME], projection) == 0)
810                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->middle_name), count);
811                                 else if(strcmp(field_key[FIELD_PERSON_LAST_NAME], projection) == 0)
812                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->last_name), count);
813                                 else if(strcmp(field_key[FIELD_PERSON_PHONETIC_NAME], projection) == 0)
814                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->phonetic_name), count);
815                                 else if(strcmp(field_key[FIELD_PERSON_DISPLAY_NAME], projection) == 0)
816                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->display_name), count);
817                                 else if(strcmp(field_key[FIELD_PERSON_PHOTO_URI], projection) == 0)
818                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->photo_URI), count);
819                                 else if(strcmp(field_key[FIELD_PERSON_LAST_UPDATED_TIME], projection) == 0)
820                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->last_updated_timestamp), count);
821                                 else if(strcmp(field_key[FIELD_PERSON_IS_FAVORITE], projection) == 0)
822                                         count = _int_value_to_projection(PERSON_TABLE, DB_DATATYPE_INVALID, q_handle->hstmt,
823                                                                 &(q_handle->result_set[q_handle->pos].contact_info->person_info->is_favorite), count);
824                                 else if(strcmp(field_key[FIELD_PERSON_SEND_TO_VOICEMAIL], projection) == 0)
825                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->send_to_voicemail), count);
826                                 else if(strcmp(field_key[FIELD_PERSON_RINGTONE], projection) == 0)
827                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->person_info->ringtone), count);
828                         }else
829                         {
830                                 if(strcmp(field_key[FIELD_RINGTONE], projection) == 0)
831                                 {
832                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->ringtone), count);
833                                 }else if(strcmp(field_key[FIELD_IS_FAVORITE], projection) == 0)
834                                 {
835                                         count = _int_value_to_projection(CONTACT_TABLE, DB_DATATYPE_INVALID, q_handle->hstmt,
836                                                                 &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->is_favorite), count);
837                                 }else if(strcmp(field_key[FIELD_CHANGED_TIME], projection) == 0)
838                                 {
839                                         int changed_time = 0;
840                                         char tmp_string[64] = {0, };
841                                         changed_time = _ct_query_column_int(q_handle->hstmt, count++);
842                                         snprintf(tmp_string, 64, "%d", changed_time);
843                                         q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->last_updated_time_stamp = (char*)g_strdup(tmp_string);
844                                 }else if(strcmp(field_key[FIELD_CONTACT_ACCOUNT_ID], projection) == 0)
845                                 {
846                                         int db_value = _ct_query_column_int(q_handle->hstmt, count++);
847                                         if(db_value != 0)
848                                         {
849                                                 q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->account_id = g_new0(char, 64);
850                                                 snprintf(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->account_id, 64, "%d", db_value);
851                                         }
852                                 }else if(strcmp(field_key[FIELD_UID], projection) == 0)
853                                 {
854                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->uid), count);
855                                 }else if(strcmp(field_key[FIELD_PHOTO_URI], projection) == 0)
856                                 {
857                                         count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->photo_uri), count);
858                                 }
859                         }
860                 }
861                 if(count == old_count)
862                         count++;
863         }
864
865         switch(datatype_id)
866         {
867                 case DB_DATATYPE_NUMBER:
868                         q_handle->result_set[q_handle->pos].contact_info->number_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->number_list, num_info);
869                         break;
870                 case DB_DATATYPE_EMAIL:
871                         q_handle->result_set[q_handle->pos].contact_info->email_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->email_list, email_info);
872                         break;
873                 case DB_DATATYPE_URL:
874                         q_handle->result_set[q_handle->pos].contact_info->url_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->url_list, url_info);
875                         break;
876                 case DB_DATATYPE_NICK_NAME :
877                         q_handle->result_set[q_handle->pos].contact_info->nickname_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->nickname_list, nick_info);
878                         break;
879                 case DB_DATATYPE_ADDRESS :
880                         q_handle->result_set[q_handle->pos].contact_info->address_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->address_list, address_info);
881                         break;
882                 case DB_DATATYPE_ORGANIZATION :
883                         q_handle->result_set[q_handle->pos].contact_info->organization_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->organization_list, organization_info);
884                         break;
885                 case DB_DATATYPE_EVENT :
886                         q_handle->result_set[q_handle->pos].contact_info->event_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->event_list, event_info);
887                         break;
888                 default:
889                 {
890                         if(datatype_id > DB_DATATYPE_MAX)
891                         {
892                                 x_info->datatype_id = datatype_id;
893                                 ADVANCED_SVC_TRACE("x_info->datatype_id : %d", x_info->datatype_id);
894                                 q_handle->result_set[q_handle->pos].contact_info->x_list = g_list_append(q_handle->result_set[q_handle->pos].contact_info->x_list, x_info);
895                         }
896                 }
897                         break;
898         }
899 CATCH:
900         return error_code;
901 }
902
903 API ADVANCED_HANDLE query_init(query_error* error_code, query_type_e query_type)
904 {
905         ADVANCED_RETURN_VAL((query_type >= QUERY_TO_PERSON), {}, NULL, ("INVALID QUERY TYPE"));
906
907         advanced_handle_t* handle = NULL;
908         handle = g_new0(advanced_handle_t, 1);
909         ADVANCED_CATCH_SET_ERROR_POINTER((handle != NULL), {}, QUERY_HANDLE_NONE, ("ALLOCATION FAIL"));
910
911         *error_code = _contact_db_init();
912         ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
913
914         handle->pos = -1;
915         handle->query_type = query_type;
916
917         handle->condition_text = g_string_new ("");
918         handle->table_list = g_string_new ("");
919         handle->order = g_string_new ("");
920         handle->use_normal_field_key = g_string_new ("");
921         handle->use_contact_table_id = g_string_new ("");
922         handle->optimize_condition = 1;
923         handle->id_list = g_string_new ("");
924
925         ADVANCED_SVC_TRACE("QUERY INIT SUCCESS");
926
927 CATCH:
928         if(*error_code != QUERY_SUCCESS)
929         {
930                 _free_handle_info(handle);
931                 handle = NULL;
932         }
933
934         return (ADVANCED_HANDLE)handle;
935 }
936
937 query_error _check_extra_contact_info(advanced_handle_t* q_handle, extra_info_type type, contact_attribute_e attribute)
938 {
939         query_error error_code = QUERY_FAIL;
940
941         if(type == EXTRA_INFO_TYPE_FIELD)
942         {
943                 if(attribute == CATEGORY_INFO)
944                 {
945                         q_handle->extra_info = q_handle->extra_info | CATEGORIES;
946                         error_code = QUERY_SUCCESS;
947                 }
948         }else if(type == EXTRA_INFO_TYPE_CONDITION)
949         {
950                 if(attribute == CATEGORY_INFO)
951                 {
952                         q_handle->condition_extra_info = q_handle->condition_extra_info | CATEGORIES;
953                         error_code = QUERY_SUCCESS;
954                 }
955         }
956
957         return error_code;
958 }
959
960 API query_error query_set_attribute(ADVANCED_HANDLE handle, contact_attribute_e attribute)
961 {
962         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
963         ADVANCED_RETURN_VAL((attribute < ATTRIBUTE_MAX), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID CONTACT ATTRIBUTE"));
964
965         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
966
967         char* column_name = NULL;
968         char* table_name = NULL;
969         query_error error_code = QUERY_SUCCESS;
970         char* loc = NULL;
971         projection_t* projection_info = NULL;
972
973         ADVANCED_SVC_TRACE("attribute : %d", attribute);
974         q_handle->attribute_list[attribute] = 1;
975
976         error_code = _check_extra_contact_info(q_handle, EXTRA_INFO_TYPE_FIELD, attribute);
977         if(error_code == QUERY_SUCCESS)
978                 attribute = CONTACT_ID;
979         error_code = QUERY_SUCCESS;
980
981         if(q_handle->query_type == QUERY_TO_PERSON)
982         {
983                 column_name = _convert_person_attribute_to_column_name(attribute);
984                 if(column_name != NULL)
985                         q_handle->person_field = attribute;
986         }
987
988         if(column_name == NULL)
989                 column_name = _convert_attribute_to_column_name(attribute);
990
991         projection_info = g_new0(projection_t, 1);
992         projection_info->field = g_strdup(column_name);
993         q_handle->projection = g_list_append(q_handle->projection, projection_info);
994
995         if(q_handle->query_type == QUERY_TO_PERSON)
996                 table_name = _convert_person_attribute_to_table_name(attribute);
997
998         if((attribute > CONTACT_LAST_UPDATED_TIME_STAMP) && (table_name == NULL))
999         {
1000                 error_code = _set_datatype_list(q_handle, attribute);
1001                 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_FAIL, ("DATA_TYPE CONVERTING FAIL"));
1002         }
1003
1004         if(table_name == NULL)
1005                 table_name = _convert_attribute_to_table_name(attribute);
1006
1007         if(q_handle->table_list->str[0] != '\0')
1008         {
1009                 loc = strstr(q_handle->table_list->str, table_name);
1010                 if(loc == NULL)
1011                         g_string_append_printf(q_handle->table_list, ", %s", table_name);
1012         }
1013         else
1014         {
1015                 g_string_append_printf(q_handle->table_list, "%s", table_name);
1016         }
1017
1018         tables_e table_type = _contact_table_name_to_table_type(table_name);
1019         q_handle->query_table = q_handle->query_table | table_type;
1020
1021         ADVANCED_SVC_TRACE("table list : %s", q_handle->table_list->str);
1022
1023 CATCH:
1024         return error_code;
1025 }
1026
1027 query_error _construct_order_query(advanced_handle_t* q_handle, GString* query, target_table_e join_table)
1028 {
1029         predefine_datatype datatype = DATATYPE_INVALID;
1030         db_datatype db_datatype_id = DB_DATATYPE_NAME;
1031         query_error error_code = QUERY_SUCCESS;
1032
1033         datatype = _convert_attribute_to_mimetype_id(q_handle->order_field);
1034         db_datatype_id = _convert_datatype_to_db_type(datatype);
1035
1036         if(q_handle->query_type == QUERY_TO_CONTACT)
1037         {
1038                 switch(join_table)
1039                 {
1040                         case ONLY_CONTACT :
1041                         {
1042                                 g_string_printf (query, "select distinct(%s) from %s order by %s", field_key[FIELD_CONTACT_CONTACT_ID], table_key[TABLE_CONTACT], q_handle->order->str);
1043                         }
1044                                 break;
1045                         case ONLY_DATA :
1046                         case JOIN_CONTACT_DATA :
1047                         {
1048                                 if(db_datatype_id == DB_DATATYPE_INVALID)
1049                                 {
1050                                         g_string_printf (query, "select distinct(%s) from %s order by %s", field_key[FIELD_DATA_CONTACT_ID], table_key[TABLE_DATA], q_handle->order->str);
1051                                 }else
1052                                 {
1053                                         g_string_printf (query, "select distinct(%s) from %s where %s = %d order by %s",
1054                                                 field_key[FIELD_DATA_CONTACT_ID], table_key[TABLE_DATA], field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id, q_handle->order->str);
1055                                 }
1056                         }
1057                                 break;
1058                         default:
1059                                 break;
1060                 }
1061         }else
1062         {
1063                 switch(join_table)
1064                 {
1065                         case ONLY_PERSON :
1066                         {
1067                                 g_string_printf (query, "select distinct(%s) from %s order by %s", field_key[FIELD_PERSON_CONTACT_ID], table_key[TABLE_PERSON], q_handle->order->str);
1068                         }
1069                                 break;
1070                         case ONLY_DATA_FROM_PERSON_QUERY :
1071                         case JOIN_PERSON_DATA :
1072                         {
1073                                 if(db_datatype_id == DB_DATATYPE_INVALID)
1074                                 {
1075                                         g_string_printf (query, "select distinct(%s) from %s order by %s", field_key[FIELD_DATA_PERSON_ID], table_key[TABLE_DATA], q_handle->order->str);
1076                                 }else
1077                                 {
1078                                         g_string_printf (query, "select distinct(%s) from %s where %s = %d order by %s",
1079                                                 field_key[FIELD_DATA_PERSON_ID], table_key[TABLE_DATA], field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id, q_handle->order->str);
1080                                 }
1081                         }
1082                                 break;
1083                         default:
1084                                 break;
1085                 }
1086         }
1087         ADVANCED_CATCH_SET_ERROR(query->str[0] != '\0', {}, QUERY_INVALID_ATTRIBUTE, ("DO NOT SUPPORT CASE"));
1088
1089 CATCH:
1090         return error_code;
1091 }
1092
1093 int _make_order_list(advanced_handle_t* q_handle, int count, target_table_e join_table)
1094 {
1095         ADVANCED_RETURN_VAL((q_handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
1096         ADVANCED_RETURN_VAL((q_handle->order->str[0] != '\0'), {}, QUERY_NULL_PARAMETER, ("NO ORDER STRING"));
1097
1098         GString* query;
1099         int rc = -1;
1100         int order_pos = 0;
1101         int i = 0;
1102         int j = 0;
1103         query_error error_code = QUERY_SUCCESS;
1104
1105         ADVANCED_SVC_TRACE("q_handle->order : %s", q_handle->order->str);
1106         query = g_string_new ("");
1107
1108         if(q_handle->limit_iter == 1)
1109         {
1110                 q_handle->ordering_list = g_new0(ordering_list_t, q_handle->total_result_set_count);
1111                 ADVANCED_CATCH_SET_ERROR((q_handle->ordering_list != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1112
1113                 for(j=0; j<q_handle->total_count; j++)
1114                 {
1115                         if(order_pos >= q_handle->total_result_set_count)
1116                                 break;
1117
1118                         int high, low;
1119
1120                         high = q_handle->total_result_set_count;
1121                         low = 0;
1122
1123                         while(high >= low)
1124                         {
1125                                 i = ( low + high ) / 2;
1126
1127                                 if(q_handle->mapping_id_to_result_set[i].contact_id > q_handle->full_ordered_contact_id[j].contact_id)
1128                                         high = i - 1;
1129                                 else if(q_handle->mapping_id_to_result_set[i].contact_id < q_handle->full_ordered_contact_id[j].contact_id)
1130                                         low = i + 1;
1131                                 else if(q_handle->mapping_id_to_result_set[i].contact_id == q_handle->full_ordered_contact_id[j].contact_id)
1132                                 {
1133                                         q_handle->ordering_list[order_pos].ordered_pos = i;
1134                                         order_pos++;
1135                                         break;
1136                                 }
1137                         }
1138                 }
1139         }else
1140         {
1141                 error_code = _construct_order_query(q_handle, query, join_table);
1142                 ADVANCED_CATCH_SET_ERROR(error_code == QUERY_SUCCESS, {}, error_code, ("QUERY CONSTRUCTION FAIL"));
1143
1144                 q_handle->full_ordered_contact_id = g_new0(mapping_list_t, count);
1145                 ADVANCED_CATCH_SET_ERROR((q_handle->full_ordered_contact_id != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1146
1147                 q_handle->ordering_list = g_new0(ordering_list_t, q_handle->total_result_set_count);
1148                 ADVANCED_CATCH_SET_ERROR((q_handle->ordering_list != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1149
1150             q_handle->hstmt = _contact_query_prepare(query->str);
1151                 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1152
1153                 rc = _contact_query_step(q_handle->hstmt);
1154                 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_ORDERING_FAIL, ("No record in database"));
1155
1156                 while(rc == SQLITE_ROW)
1157                 {
1158                         if(q_handle->total_count >= count)
1159                                 break;
1160
1161                         q_handle->full_ordered_contact_id[q_handle->total_count].contact_id = _ct_query_column_int(q_handle->hstmt, 0);
1162                         rc = _contact_query_step(q_handle->hstmt);
1163                         q_handle->total_count++;
1164                 }
1165                 q_handle->total_count = count;
1166                 _ct_query_finalize(q_handle->hstmt);
1167
1168                 for(j=0; j<q_handle->total_count; j++)
1169                 {
1170                         if(order_pos >= q_handle->total_result_set_count)
1171                                 break;
1172
1173                         if(q_handle->full_ordered_contact_id[j].contact_id <= q_handle->mapping_id_to_result_set[q_handle->total_result_set_count - 1].contact_id)
1174                         {
1175                                 int high, low;
1176
1177                                 high = q_handle->total_result_set_count;
1178                                 low = 0;
1179
1180                                 while(high >= low)
1181                                 {
1182                                         i = ( low + high ) / 2;
1183
1184                                         if(q_handle->mapping_id_to_result_set[i].contact_id > q_handle->full_ordered_contact_id[j].contact_id)
1185                                                 high = i - 1;
1186                                         else if(q_handle->mapping_id_to_result_set[i].contact_id < q_handle->full_ordered_contact_id[j].contact_id)
1187                                                 low = i + 1;
1188                                         else if(q_handle->mapping_id_to_result_set[i].contact_id == q_handle->full_ordered_contact_id[j].contact_id)
1189                                         {
1190                                                 q_handle->ordering_list[order_pos].ordered_pos = i;
1191                                                 order_pos++;
1192                                                 break;
1193                                         }
1194                                 }
1195                         }
1196                 }
1197         }
1198         q_handle->return_array_pos = q_handle->ordering_list[0].ordered_pos;
1199         q_handle->pos = 0;
1200
1201 CATCH:
1202         g_string_free (query, TRUE);
1203         return error_code;
1204 }
1205
1206 query_error _make_contact_id_list_by_condition_part(advanced_handle_t* q_handle, int count, GString* sub_query)
1207 {
1208         ADVANCED_RETURN_VAL((q_handle->condition_text->str[0] != '\0'), {}, QUERY_SUCCESS, ("NO CONDITION PART"));
1209
1210         query_error error_code = QUERY_SUCCESS;
1211         alias_key_e alias = ALIAS_A;
1212         alias_key_e data_table_alias = ALIAS_INVALID;
1213         char* id_field = NULL;
1214         char* default_id_field = NULL;
1215         alias_key_e default_alias = ALIAS_INVALID;
1216
1217         GString* tmp_query;
1218         tmp_query = g_string_new("");
1219
1220         ADVANCED_SVC_TRACE("CHECK CONDITION PART !! ");
1221
1222         GList* tmp_table = q_handle->condition_table_list;
1223         while(tmp_table)
1224         {
1225                 condition_table_t* table_info = (condition_table_t*)tmp_table->data;
1226
1227                 if(strcmp(table_info->table, table_key[TABLE_PERSON]) == 0)
1228                         id_field = condition_field_key[FIELD_PERSON_CONTACT_ID];
1229                 else if(strcmp(table_info->table, table_key[TABLE_CONTACT]) == 0)
1230                         id_field = condition_field_key[FIELD_CONTACT_CONTACT_ID];
1231                 else if(strcmp(table_info->table, table_key[TABLE_DATA]) == 0)
1232                 {
1233                         if(q_handle->query_type == QUERY_TO_PERSON)
1234                                 id_field = condition_field_key[FIELD_DATA_PERSON_ID];
1235                         else
1236                                 id_field = condition_field_key[FIELD_DATA_CONTACT_ID];
1237
1238                         if(data_table_alias == ALIAS_INVALID)
1239                                 data_table_alias = table_info->alias;
1240                 }
1241
1242                 if(q_handle->optimize_condition == 1)
1243                 {
1244                         if(sub_query->str[0] == '\0')
1245                         {
1246                                 g_string_append_printf (sub_query, "select distinct(%s.%s) from %s %s",
1247                                         alias_key[table_info->alias], id_field, table_info->table, alias_key[table_info->alias]);
1248                                 default_id_field = id_field;
1249                                 default_alias = table_info->alias;
1250                         }
1251                 }else
1252                 {
1253                         if(sub_query->str[0] == '\0')
1254                         {
1255                                 g_string_append_printf (sub_query, "select distinct(%s.%s) from %s %s",
1256                                         alias_key[table_info->alias], id_field, table_info->table, alias_key[table_info->alias]);
1257                                 default_id_field = id_field;
1258                                 default_alias = table_info->alias;
1259                         }else
1260                                 g_string_append_printf (sub_query, ", %s %s", table_info->table, alias_key[table_info->alias]);
1261
1262                         if(alias != table_info->alias)
1263                                 g_string_append_printf (tmp_query, " (%s.%s = %s.%s) and",      alias_key[default_alias], default_id_field, alias_key[table_info->alias], id_field);
1264                 }
1265
1266                 alias = table_info->alias;
1267                 tmp_table = g_list_next(tmp_table);
1268         }
1269
1270         g_string_append_printf (sub_query, " where");
1271
1272         if(q_handle->set_number_pref == 1)
1273         {
1274                 GList* tmp_table1 = q_handle->condition_table_list;
1275                 while(tmp_table1)
1276                 {
1277                         condition_table_t* table_info1 = (condition_table_t*)tmp_table1->data;
1278                         if(strcmp(table_info1->table, "data") == 0)
1279                         {
1280                                 g_string_append_printf (sub_query, " (%s.is_default = 1 and %s.datatype = 8) and", alias_key[table_info1->alias], alias_key[table_info1->alias]);
1281                                 break;
1282                         }
1283                         tmp_table1 = g_list_next(tmp_table1);
1284                 }
1285         }
1286
1287         if(q_handle->set_email_pref == 1)
1288         {
1289                 GList* tmp_table2 = q_handle->condition_table_list;
1290                 while(tmp_table2)
1291                 {
1292                         condition_table_t* table_info2 = (condition_table_t*)tmp_table2->data;
1293                         if(strcmp(table_info2->table, "data") == 0)
1294                         {
1295                                 g_string_append_printf (sub_query, " (%s.is_default = 1 and %s.datatype = 9) and", alias_key[table_info2->alias], alias_key[table_info2->alias]);
1296                                 break;
1297                         }
1298                         tmp_table2 = g_list_next(tmp_table2);
1299                 }
1300         }
1301
1302         if(tmp_query->str[0] != '\0')
1303                 g_string_append_printf (sub_query, " %s", tmp_query->str);
1304
1305         if((q_handle->optimize_condition == 1) && (q_handle->condition_list && q_handle->current_alias > ALIAS_A))
1306         {
1307                 GList* tmp_condition = q_handle->condition_list;
1308                 int is_first = 1;
1309                 while(tmp_condition)
1310                 {
1311                         condition_list_t* condition_info = (condition_list_t*)tmp_condition->data;
1312
1313                         if(is_first)
1314                         {
1315                                 g_string_append_printf(sub_query, " (%s.id in (select id from %s where %s %s ? and %s = %d",
1316                                         alias_key[data_table_alias], table_key[TABLE_DATA], condition_info->column_name,
1317                                         condition_info->condition, condition_field_key[FIELD_DATA_DATATYPE_ID], condition_info->datatype);
1318                                 is_first = 0;
1319                         }else
1320                         {
1321                                 g_string_append_printf(sub_query, " union select id from %s where %s %s ? and %s = %d",
1322                                         table_key[TABLE_DATA], condition_info->column_name,
1323                                         condition_info->condition, condition_field_key[FIELD_DATA_DATATYPE_ID], condition_info->datatype);
1324                         }
1325
1326                         tmp_condition = g_list_next(tmp_condition);
1327                 }
1328                 g_string_append_printf(sub_query, " ))");
1329
1330         }else
1331                 g_string_append_printf (sub_query, " (%s)", q_handle->condition_text->str);
1332
1333         g_string_free (tmp_query, TRUE);
1334         return error_code;
1335 }
1336
1337 void _construct_query(target_table_e value, advanced_handle_t* q_handle, GString* query, char* condtion_sub_query, projection_type type)
1338 {
1339         GString* condition;
1340         GString* contact_condition;
1341         int is_first = 1;
1342         int use_sub_query = 1;
1343
1344         condition = g_string_new("");
1345         contact_condition = g_string_new("");
1346
1347         if(q_handle->condition_datatype == DATATYPE_INVALID && q_handle->condition_category == NULL)
1348                 use_sub_query = 0;
1349
1350         GList* tmp_datatype_list = q_handle->datatype_list;
1351         while(tmp_datatype_list)
1352         {
1353                 datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1354
1355                 if(is_first)
1356                         g_string_append_printf(condition, "%s = %d", datatype_info->field, datatype_info->datatype);
1357                 else
1358                         g_string_append_printf(condition, " or %s = %d", datatype_info->field, datatype_info->datatype);
1359
1360                 tmp_datatype_list = g_list_next(tmp_datatype_list);
1361                 is_first = 0;
1362         }
1363
1364         switch(value)
1365         {
1366                 case ONLY_CONTACT:
1367                 {
1368                         g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_CONTACT]);
1369
1370                         if(strlen(condtion_sub_query) != 0)
1371                         {
1372                                 if(use_sub_query == 0)
1373                                 {
1374                                         g_string_append_printf(query, " where %s", q_handle->use_contact_table_id->str);
1375                                 }else
1376                                         g_string_append_printf(query, " where %s in (%s)", field_key[FIELD_CONTACT_CONTACT_ID], condtion_sub_query);
1377                         }
1378
1379                         if(q_handle->order->str[0] != '\0')
1380                                 g_string_append_printf(query, " order by %s", q_handle->order->str);
1381                         else
1382                                 g_string_append_printf(query, " order by %s", field_key[FIELD_CONTACT_CONTACT_ID]);
1383                 }
1384                         break;
1385                 case ONLY_PERSON:
1386                 {
1387                         g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_PERSON]);
1388
1389                         if(strlen(condtion_sub_query) != 0)
1390                                 g_string_append_printf(query, " where %s in (%s)", field_key[FIELD_PERSON_CONTACT_ID], condtion_sub_query);
1391
1392                         if(q_handle->order->str[0] != '\0')
1393                                 g_string_append_printf(query, " order by %s", q_handle->order->str);
1394                         else
1395                                 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
1396                 }
1397                         break;
1398                 case ONLY_DATA:
1399                 {
1400                         if(type == ONLY_CONTACT_ID_FIELD)
1401                                 g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_DATA]);
1402                         else
1403                                 g_string_append_printf(query, "select %s, %s from %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string, table_key[TABLE_DATA]);
1404
1405                         if(strlen(condtion_sub_query) != 0)
1406                         {
1407                                 if(use_sub_query == 0)
1408                                         g_string_append_printf(query, " where %s", q_handle->use_normal_field_key->str);
1409                                 else
1410                                         g_string_append_printf(query, " where %s in (%s) and (%s)", field_key[FIELD_DATA_CONTACT_ID], condtion_sub_query, condition->str);
1411                         }
1412                         else
1413                                 g_string_append_printf(query, " where (%s)", condition->str);
1414
1415                         g_string_append_printf(query, " order by %s", field_key[FIELD_DATA_CONTACT_ID]);
1416
1417                 }
1418                         break;
1419                 case ONLY_DATA_FROM_PERSON_QUERY:
1420                 {
1421                         if(type == ONLY_CONTACT_ID_FIELD)
1422                                 g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_DATA]);
1423                         else
1424                                 g_string_append_printf(query, "select %s, %s from %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string, table_key[TABLE_DATA]);
1425
1426                         if(strlen(condtion_sub_query) != 0)
1427                         {
1428                                 if(use_sub_query == 0)
1429                                         g_string_append_printf(query, " where %s = ?", field_key[FIELD_PERSON_CONTACT_ID]);
1430                                 else
1431                                         g_string_append_printf(query, " where %s in (%s)", field_key[FIELD_PERSON_CONTACT_ID], condtion_sub_query);
1432                         }
1433
1434                         if(strlen(condtion_sub_query) != 0)
1435                                 g_string_append_printf(query, " and (%s)", condition->str);
1436                         else
1437                                 g_string_append_printf(query, " where (%s)", condition->str);
1438
1439                         g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
1440
1441                 }
1442                         break;
1443                 case JOIN_CONTACT_DATA:
1444                 {
1445                         g_string_append_printf(query, "select %s, %s from %s, %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string, table_key[TABLE_CONTACT], table_key[TABLE_DATA]);
1446                         g_string_append_printf(query, " where (%s = %s) and", field_key[FIELD_CONTACT_CONTACT_ID], field_key[FIELD_DATA_CONTACT_ID]);
1447
1448                         if(strlen(condtion_sub_query) != 0)
1449                         {
1450                                 if(use_sub_query == 0)
1451                                         g_string_append_printf(query, " %s and", q_handle->use_normal_field_key->str);
1452                                 else
1453                                         g_string_append_printf(query, " %s in (%s) and", field_key[FIELD_CONTACT_CONTACT_ID], condtion_sub_query);
1454                         }
1455                         g_string_append_printf(query, " (%s) order by %s", condition->str, field_key[FIELD_CONTACT_CONTACT_ID]);
1456
1457                 }
1458                         break;
1459                 case JOIN_PERSON_DATA:
1460                 {
1461                         g_string_append_printf(query, "select %s, %s from %s, %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string,  table_key[TABLE_PERSON], table_key[TABLE_DATA]);
1462                         g_string_append_printf(query, " where (%s = %s) and", field_key[FIELD_PERSON_CONTACT_ID], field_key[FIELD_DATA_PERSON_ID]);
1463
1464                         if(strlen(condtion_sub_query) != 0)
1465                         {
1466                                 if(use_sub_query == 0)
1467                                         g_string_append_printf(query, " %s = ? and", field_key[FIELD_PERSON_CONTACT_ID]);
1468                                 else
1469                                         g_string_append_printf(query, " %s in (%s) and", field_key[FIELD_PERSON_CONTACT_ID], condtion_sub_query);
1470                         }
1471                         g_string_append_printf(query, " (%s) order by %s", condition->str, field_key[FIELD_PERSON_CONTACT_ID]);
1472
1473                 }
1474                         break;
1475                 default:
1476                         break;
1477         }
1478
1479         g_string_free(condition, TRUE);
1480         g_string_free(contact_condition, TRUE);
1481 }
1482
1483 query_error _query_result_set(advanced_handle_t* q_handle, int rc, int count, projection_type type, target_table_e join_table)
1484 {
1485         query_error error_code = QUERY_SUCCESS;
1486
1487         ADVANCED_SVC_TRACE("_query_result_set");
1488
1489         q_handle->result_set = g_new0(result_t, count + 1);
1490         ADVANCED_CATCH_SET_ERROR((q_handle->result_set != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1491
1492         q_handle->mapping_id_to_result_set = g_new0(mapping_list_t, count + 1);
1493         while(rc == SQLITE_ROW)
1494         {
1495                 error_code = _make_contact_info(q_handle, type, join_table);
1496                 if(error_code == QUERY_SET_LIMIT)
1497                 {
1498                         error_code = QUERY_SUCCESS;
1499                         break;
1500                 }
1501                 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1502
1503                 rc = _contact_query_step(q_handle->hstmt);
1504         }
1505
1506         _ct_query_finalize(q_handle->hstmt);
1507
1508 CATCH:
1509         return error_code;
1510 }
1511
1512 int _make_result_contact_list(advanced_handle_t* q_handle, int count, char* condtion_sub_query)
1513 {
1514         ADVANCED_RETURN_VAL((q_handle->projection != NULL), {}, QUERY_NULL_PARAMETER, ("PROJECTION LIST IS NULL"));
1515
1516         int rc = -1;
1517         query_error error_code = QUERY_SUCCESS;
1518         GString* query;
1519         GString* projection;
1520         char* loc = NULL;
1521         projection_type type = NORMAL_CASE;
1522         target_table_e value = ONLY_CONTACT;
1523
1524         query = g_string_new("");
1525         projection = g_string_new("");
1526
1527         value = _check_join_case(q_handle);
1528         ADVANCED_SVC_TRACE("value : %d", value);
1529
1530         int table_contact = 0;
1531         int reorganize = 0;
1532         int data_field_count = 0;
1533
1534         if(q_handle->query_string == NULL)
1535         {
1536                 GList* tmp = q_handle->projection;
1537                 while(tmp)
1538                 {
1539                         projection_t* projection_info = (projection_t*)tmp->data;
1540
1541                         loc = strstr(projection_info->field, "contact.");
1542                         if(loc != NULL)
1543                                 table_contact = 1;
1544
1545                         loc = strstr(projection_info->field, "data.contact_id");
1546                         if(loc != NULL)
1547                                 reorganize = 1;
1548
1549                         loc = strstr(projection_info->field, "data.");
1550                         if(loc != NULL)
1551                                 data_field_count++;
1552
1553                         tmp = g_list_next(tmp);
1554                 }
1555
1556                 if(q_handle->query_type == QUERY_TO_PERSON)
1557                         g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
1558
1559                 if((table_contact == 1 && reorganize == 1 && data_field_count == 1) || (q_handle->assigned_datatype == 0))
1560                 {
1561                         g_string_append_printf(projection, "%s", field_key[FIELD_CONTACT_CONTACT_ID]);
1562                         value = ONLY_CONTACT;
1563                 }
1564
1565                 if(projection->str[0] == '\0')
1566                 {
1567                         g_string_append_printf(projection, "%s", field_key[FIELD_DATA_CONTACT_ID]);
1568                 }
1569
1570                 GList* tmp_projection = q_handle->projection;
1571                 int projection_duplicate = 0;
1572                 while(tmp_projection)
1573                 {
1574                         projection_t* projection_info = (projection_t*)tmp_projection->data;
1575
1576                         if(strcmp(projection_info->field, field_key[FIELD_CONTACT_CONTACT_ID]) == 0)
1577                                 projection_duplicate = 1;
1578
1579                         if(strcmp(projection_info->field, field_key[FIELD_DATA_CONTACT_ID]) == 0)
1580                                 projection_duplicate = 1;
1581
1582                         loc = strstr(projection->str, projection_info->field);
1583                         if(loc == NULL && projection_duplicate == 0)
1584                         {
1585                                 g_string_append_printf(projection, ", %s", projection_info->field);
1586
1587                                 projection_t* new_projection_info = NULL;
1588                                 new_projection_info = g_new0(projection_t, 1);
1589                                 ADVANCED_CATCH_SET_ERROR((new_projection_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1590
1591                                 new_projection_info->field = g_strdup(projection_info->field);
1592                                 q_handle->new_projection = g_list_append(q_handle->new_projection, new_projection_info);
1593                         }
1594
1595                         tmp_projection = g_list_next(tmp_projection);
1596                         projection_duplicate = 0;
1597                 }
1598                 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1599                 g_list_free(q_handle->projection);
1600
1601                 q_handle->query_string  = g_strdup(projection->str);
1602         }
1603         ADVANCED_SVC_TRACE("projection : %s", q_handle->query_string);
1604
1605         if((strcmp(q_handle->query_string, field_key[FIELD_DATA_CONTACT_ID]) == 0) || (strcmp(q_handle->query_string, field_key[FIELD_CONTACT_CONTACT_ID]) == 0))
1606                 type = ONLY_CONTACT_ID_FIELD;
1607
1608         _construct_query(value, q_handle, query, condtion_sub_query, type);
1609
1610     q_handle->hstmt = _contact_query_prepare(query->str);
1611         ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1612
1613         if(strlen(condtion_sub_query) != 0)
1614         {
1615                 int i = 1;
1616                 if(q_handle->condition_category != NULL)
1617                 {
1618                         ADVANCED_SVC_TRACE("q_handle->condition_category : %s", q_handle->condition_category);
1619                         _contact_query_bind_text(q_handle->hstmt, i, q_handle->condition_category);
1620                 }else
1621                 {
1622                         GList* tmp_condition_val = q_handle->condition_value;
1623                         while(tmp_condition_val)
1624                         {
1625                                 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1626                                 if(tmp_val->indices_filter == 0)
1627                                 {
1628                                 ADVANCED_SVC_TRACE("condition_val->value_str : %s", tmp_val->value);
1629                                 ADVANCED_SVC_TRACE("condition_val->value_int : %d", tmp_val->int_value);
1630
1631                                 primitive_type type = _convert_check_attribute_primitive_type(tmp_val->attribute);
1632                                 if(tmp_val->x_attribute == 1)
1633                                         type = STRING;
1634
1635                                 if(type == INTEGER)
1636                                         _contact_query_bind_int(q_handle->hstmt, i++, tmp_val->int_value);
1637                                 else if(type == STRING)
1638                                         _contact_query_bind_text(q_handle->hstmt, i++, tmp_val->value);
1639                                 else if(type == REAL)
1640                                         _contact_query_bind_double(q_handle->hstmt, i++, tmp_val->real_value);
1641                                 }
1642                                 tmp_condition_val = g_list_next(tmp_condition_val);
1643                         }
1644                 }
1645         }
1646
1647         rc = _contact_query_step(q_handle->hstmt);
1648         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1649
1650         error_code = _query_result_set(q_handle, rc, count, type, value);
1651
1652 CATCH:
1653         g_string_free(query, TRUE);
1654         g_string_free(projection, TRUE);
1655         return error_code;
1656 }
1657
1658 int _check_optimized_query(advanced_handle_t* q_handle, target_table_e join_table)
1659 {
1660         int is_optimized_query = 0;
1661
1662         if((q_handle->extra_info & CATEGORIES) == 0)
1663         {
1664                 switch(join_table)
1665                 {
1666                         case ONLY_DATA:
1667                         case ONLY_DATA_FROM_PERSON_QUERY:
1668                         {
1669                                 if(q_handle->all_datatype_in_query == DATATYPE_NAME)
1670                                         is_optimized_query = 1;
1671                         }
1672                                 break;
1673                         case ONLY_PERSON:
1674                                 is_optimized_query = 1;
1675                                 break;
1676                         default:
1677                                 break;
1678                 }
1679         }
1680         ADVANCED_SVC_TRACE("is_optimized_query : %d ", is_optimized_query);
1681
1682         return is_optimized_query;
1683 }
1684
1685 query_error _optimized_query_to_person_with_data(advanced_handle_t* q_handle, int count)
1686 {
1687         query_error error_code = QUERY_SUCCESS;
1688         int rc = -1;
1689
1690         GString* query;
1691         GString* projection;
1692         GString* condition;
1693
1694         projection = g_string_new("");
1695         query = g_string_new ("");
1696         condition = g_string_new ("");
1697
1698         if(q_handle->query_string == NULL)
1699         {
1700                 g_string_append_printf(projection, "%s", field_key[FIELD_DATA_DATATYPE_ID]);
1701                 g_string_append_printf(projection, ", %s", field_key[FIELD_DATA_PERSON_ID]);
1702
1703                 GList* tmp_projection = q_handle->projection;
1704                 while(tmp_projection)
1705                 {
1706                         projection_t* projection_info = (projection_t*)tmp_projection->data;
1707
1708                         if(strcmp(projection_info->field, field_key[FIELD_PERSON_CONTACT_ID]) != 0)
1709                         {
1710                                 g_string_append_printf(projection, ", %s", projection_info->field);
1711
1712                                 projection_t* new_projection_info = NULL;
1713                                 new_projection_info = g_new0(projection_t, 1);
1714                                 ADVANCED_CATCH_SET_ERROR((new_projection_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1715
1716                                 new_projection_info->field = g_strdup(projection_info->field);
1717                                 q_handle->new_projection = g_list_append(q_handle->new_projection, new_projection_info);
1718                         }
1719                         tmp_projection = g_list_next(tmp_projection);
1720                 }
1721                 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1722                 g_list_free(q_handle->projection);
1723
1724                 q_handle->query_string = g_strdup(projection->str);
1725         }
1726
1727         g_string_append_printf(query, "select %s from %s ", q_handle->query_string, table_key[TABLE_DATA]);
1728
1729         GList* tmp_datatype_list = q_handle->datatype_list;
1730         while(tmp_datatype_list)
1731         {
1732                 datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1733                 g_string_append_printf(condition, "where (%s and %s = %d)", q_handle->use_normal_field_key->str, datatype_info->field, datatype_info->datatype);
1734                 tmp_datatype_list = g_list_next(tmp_datatype_list);
1735         }
1736
1737         if(q_handle->order->str[0] != '\0')
1738                 g_string_append_printf(query, " %s order by %s", condition->str, q_handle->order->str);
1739         else
1740         {
1741                 g_string_append_printf(query, " %s order by %s", condition->str, field_key[FIELD_DATA_PERSON_ID]);
1742         }
1743
1744     q_handle->hstmt = _contact_query_prepare(query->str);
1745         ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1746
1747         if(q_handle->use_normal_field_key->str[0] != '\0')
1748         {
1749                 GList* tmp_condition_val = q_handle->condition_value;
1750                 while(tmp_condition_val)
1751                 {
1752                         condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1753                         ADVANCED_SVC_TRACE("condition_val->value : %s", tmp_val->value);
1754
1755                         _contact_query_bind_text(q_handle->hstmt, 1, tmp_val->value);
1756                         tmp_condition_val = g_list_next(tmp_condition_val);
1757                 }
1758         }
1759
1760         rc = _contact_query_step(q_handle->hstmt);
1761         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1762
1763         error_code = _query_result_set(q_handle, rc, count, NORMAL_CASE, JOIN_PERSON_DATA);
1764
1765 CATCH:
1766         g_string_free(query, TRUE);
1767         g_string_free(projection, TRUE);
1768         g_string_free(condition, TRUE);
1769
1770         if(error_code != QUERY_SUCCESS)
1771         {
1772                 _ct_query_finalize(q_handle->hstmt);
1773         }
1774
1775         return error_code;
1776 }
1777
1778 query_error _optimized_query_join_person_data(advanced_handle_t* q_handle, int count)
1779 {
1780         int rc = -1;
1781         query_error error_code = QUERY_SUCCESS;
1782         GString* query;
1783         GString* projection;
1784         GString* condition;
1785
1786         char* loc = NULL;
1787         query = g_string_new("");
1788         projection = g_string_new("");
1789         condition = g_string_new("");
1790
1791         if(q_handle->query_string == NULL)
1792         {
1793                 g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
1794
1795                 GList* tmp_projection = q_handle->projection;
1796                 int projection_duplicate = 0;
1797                 while(tmp_projection)
1798                 {
1799                         projection_t* projection_info = (projection_t*)tmp_projection->data;
1800
1801                         loc = strstr(projection->str, projection_info->field);
1802                         if(loc == NULL)
1803                         {
1804                                 g_string_append_printf(projection, ", %s", projection_info->field);
1805
1806                                 projection_t* new_projection_info = NULL;
1807                                 new_projection_info = g_new0(projection_t, 1);
1808                                 ADVANCED_CATCH_SET_ERROR((new_projection_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1809
1810                                 new_projection_info->field = g_strdup(projection_info->field);
1811                                 q_handle->new_projection = g_list_append(q_handle->new_projection, new_projection_info);
1812                         }
1813
1814                         tmp_projection = g_list_next(tmp_projection);
1815                         projection_duplicate = 0;
1816                 }
1817                 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1818                 g_list_free(q_handle->projection);
1819
1820                 q_handle->query_string  = g_strdup(projection->str);
1821         }
1822
1823         ADVANCED_SVC_TRACE("projection : %s", q_handle->query_string);
1824
1825         g_string_append_printf(query, "select %s, %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string);
1826         g_string_append_printf(query, " from %s", q_handle->table_list->str);
1827
1828         GList* tmp_datatype_list = q_handle->datatype_list;
1829         while(tmp_datatype_list)
1830         {
1831                 datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1832                 g_string_append_printf(condition, "where (%s and %s = %d) and ", q_handle->use_normal_field_key->str, datatype_info->field, datatype_info->datatype);
1833                 tmp_datatype_list = g_list_next(tmp_datatype_list);
1834         }
1835
1836         g_string_append_printf(condition, " (%s = %s)", field_key[FIELD_PERSON_CONTACT_ID], field_key[FIELD_DATA_PERSON_ID]);
1837
1838         if(q_handle->order->str[0] != '\0')
1839                 g_string_append_printf(query, " %s order by %s", condition->str, q_handle->order->str);
1840         else
1841                 g_string_append_printf(query, " %s order by %s", condition->str, field_key[FIELD_PERSON_CONTACT_ID]);
1842
1843     q_handle->hstmt = _contact_query_prepare(query->str);
1844         ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1845
1846         if(strlen(q_handle->use_normal_field_key->str) != 0)
1847         {
1848                 GList* tmp_condition_val = q_handle->condition_value;
1849                 while(tmp_condition_val)
1850                 {
1851                         condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1852                         ADVANCED_SVC_TRACE("condition_val->value : %s", tmp_val->value);
1853
1854                         _contact_query_bind_text(q_handle->hstmt, 1, tmp_val->value);
1855                         tmp_condition_val = g_list_next(tmp_condition_val);
1856                 }
1857         }
1858
1859         rc = _contact_query_step(q_handle->hstmt);
1860         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1861
1862         error_code = _query_result_set(q_handle, rc, count, NORMAL_CASE, JOIN_PERSON_DATA);
1863
1864 CATCH:
1865         g_string_free(query, TRUE);
1866         g_string_free(projection, TRUE);
1867         g_string_free(condition, TRUE);
1868
1869         if(error_code != QUERY_SUCCESS)
1870         {
1871                 _ct_query_finalize(q_handle->hstmt);
1872         }
1873
1874         return error_code;
1875 }
1876
1877 query_error _get_categories_list(advanced_handle_t* q_handle, int count, char* condition_query)
1878 {
1879         GString* query;
1880         stmt    hstmt;
1881         int rc = -1;
1882         int pos = 0;
1883         query_error error_code = QUERY_SUCCESS;
1884
1885         query = g_string_new ("");
1886
1887         g_string_append_printf(query, "select a.%s, a.%s, b.%s from %s a, %s b where (a.%s = b.%s)",
1888                 relation_field_key[FIELD_GROUP_RELATION_CONTACT_ID], relation_field_key[FIELD_GROUP_RELATION_GROUP_ID], group_field_key[FIELD_GROUP_NAME],
1889                 table_key[TABLE_GROUP_RELATION], table_key[TABLE_GROUP],
1890                 group_field_key[FIELD_GROUP_ID], relation_field_key[FIELD_GROUP_RELATION_GROUP_ID]);
1891
1892         if(strlen(condition_query) != 0)
1893                 g_string_append_printf(query, " and a.%s in (%s)", relation_field_key[FIELD_GROUP_RELATION_CONTACT_ID], condition_query);
1894
1895         g_string_append_printf(query, " order by a.%s", relation_field_key[FIELD_GROUP_RELATION_CONTACT_ID]);
1896
1897     hstmt = _contact_query_prepare(query->str);
1898         ADVANCED_CATCH_SET_ERROR((hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("Database prepare fail"));
1899
1900         if(q_handle->condition_category != NULL)
1901         {
1902                 _contact_query_bind_text(hstmt, 1, q_handle->condition_category);
1903         }else if(strlen(condition_query) != 0)
1904         {
1905                 GList* tmp_condition_val = q_handle->condition_value;
1906                 int i = 1;
1907                 while(tmp_condition_val)
1908                 {
1909                         condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1910
1911                         if(tmp_val->indices_filter == 0)
1912                         {
1913                         primitive_type type = _convert_check_attribute_primitive_type(tmp_val->attribute);
1914                         if(tmp_val->x_attribute == 1)
1915                                 type = STRING;
1916
1917                         if(type == INTEGER)
1918                                 _contact_query_bind_int(hstmt, i++, tmp_val->int_value);
1919                         else if(type == STRING)
1920                                 _contact_query_bind_text(hstmt, i++, tmp_val->value);
1921                         else if(type == REAL)
1922                                 _contact_query_bind_double(hstmt, i++, tmp_val->real_value);
1923                         }
1924                         tmp_condition_val = g_list_next(tmp_condition_val);
1925                 }
1926         }
1927         rc = _contact_query_step(hstmt);
1928         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1929
1930         while(rc == SQLITE_ROW)
1931         {
1932                 if(pos > count)
1933                         break;
1934
1935                 q_handle->category[pos].contact_id = _ct_query_column_int(hstmt, 0);
1936                 q_handle->category[pos].group_id = _ct_query_column_int(hstmt, 1);
1937                 q_handle->category[pos].name = g_strdup(_ct_query_column_text(hstmt, 2));
1938
1939                 ADVANCED_SVC_TRACE("q_handle->category[%d].contact_id : %d", pos, q_handle->category[pos].contact_id);
1940
1941                 if(q_handle->category[pos].contact_id == q_handle->category[pos - 1].contact_id)
1942                         q_handle->category[pos].sibling = 1;
1943
1944                 pos++;
1945
1946                 rc = _contact_query_step(hstmt);
1947         }
1948         q_handle->category_total_count = pos;
1949         _ct_query_finalize(hstmt);
1950
1951 CATCH:
1952         g_string_free(query, TRUE);
1953         return error_code;
1954 }
1955
1956 query_error _construct_optimize_query(advanced_handle_t* q_handle, GString* query, target_table_e join_table, projection_type type, char* sub_query)
1957 {
1958         GString* condition;
1959         int is_first = 1;
1960         query_error error_code = QUERY_SUCCESS;
1961         condition = g_string_new ("");
1962         int is_sub_query = 0;
1963
1964         if(sub_query[0] != '\0')
1965                 is_sub_query = 1;
1966
1967         if(is_sub_query)
1968         {
1969                 switch(join_table)
1970                 {
1971                         case ONLY_DATA:
1972                                 g_string_append_printf(condition, " %s in (%s) and ", field_key[FIELD_DATA_CONTACT_ID], sub_query);
1973                                 break;
1974                         case ONLY_PERSON:
1975                         case ONLY_DATA_FROM_PERSON_QUERY:
1976                                 g_string_append_printf(condition, " %s in (%s) and ", field_key[FIELD_PERSON_CONTACT_ID], sub_query);
1977                                 break;
1978                         default:
1979                                 break;
1980                 }
1981         }
1982
1983         if(join_table == ONLY_DATA || join_table == ONLY_DATA_FROM_PERSON_QUERY)
1984         {
1985                 GList* tmp_datatype_list = q_handle->datatype_list;
1986                 is_first = 1;
1987                 while(tmp_datatype_list)
1988                 {
1989                         datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1990                         if(is_first)
1991                         {
1992                                 if(q_handle->use_normal_field_key->str[0] != '\0' && is_sub_query == 0)
1993                                 {
1994                                         g_string_append_printf(condition, "%s and %s = %d", q_handle->use_normal_field_key->str, datatype_info->field, datatype_info->datatype);
1995                                 }else
1996                                         g_string_append_printf(condition, "%s = %d", datatype_info->field, datatype_info->datatype);
1997                         }else
1998                         {
1999                                 g_string_append_printf(condition, " or %s = %d", datatype_info->field, datatype_info->datatype);
2000                         }
2001
2002                         tmp_datatype_list = g_list_next(tmp_datatype_list);
2003                         is_first = 0;
2004                 }
2005         }
2006
2007         switch(join_table)
2008         {
2009                 case ONLY_DATA:
2010                 {
2011                         if(type == ONLY_CONTACT_ID_FIELD)
2012                                 g_string_append_printf(query, "select distinct(%s) from %s", q_handle->query_string, table_key[TABLE_DATA]);
2013                         else
2014                                 g_string_append_printf(query, "select %s, %s from %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string, table_key[TABLE_DATA]);
2015
2016                         if(condition->str[0] != '\0')
2017                                 g_string_append_printf(query, " where (%s)", condition->str);
2018
2019                         if(q_handle->order->str[0] != '\0')
2020                                 g_string_append_printf(query, " order by %s", q_handle->order->str);
2021                         else
2022                                 g_string_append_printf(query, " order by %s", field_key[FIELD_DATA_CONTACT_ID]);
2023                 }
2024                         break;
2025                 case ONLY_DATA_FROM_PERSON_QUERY:
2026                 {
2027                         if(type == ONLY_CONTACT_ID_FIELD)
2028                                 g_string_append_printf(query, "select distinct(%s) from %s", q_handle->query_string, table_key[TABLE_DATA]);
2029                         else
2030                                 g_string_append_printf(query, "select %s, %s from %s", field_key[FIELD_DATA_DATATYPE_ID], q_handle->query_string, table_key[TABLE_DATA]);
2031
2032                         if(condition->str[0] != '\0')
2033                                 g_string_append_printf(query, " where (%s)", condition->str);
2034
2035                         if(q_handle->order->str[0] != '\0')
2036                                 g_string_append_printf(query, " order by %s", q_handle->order->str);
2037                         else
2038                                 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
2039                 }
2040                         break;
2041                 case ONLY_PERSON:
2042                 {
2043                         g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_PERSON]);
2044
2045                         if(q_handle->use_normal_field_key->str[0] != '\0')
2046                                 g_string_append_printf(query, " where (%s)", q_handle->use_normal_field_key->str);
2047
2048                         if(q_handle->order->str[0] != '\0')
2049                                 g_string_append_printf(query, " order by %s", q_handle->order->str);
2050                         else
2051                                 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
2052                 }
2053                         break;
2054                 default:
2055                         break;
2056         }
2057
2058         g_string_free(condition, TRUE);
2059         return error_code;
2060 }
2061
2062 API query_error query_get_result_contact(ADVANCED_HANDLE handle)
2063 {
2064         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2065
2066         int rc = -1;
2067         query_error error_code = QUERY_SUCCESS;
2068         GString* query;
2069         GString* projection;
2070         char* loc = NULL;
2071         int count = 0;
2072         projection_type type = NORMAL_CASE;
2073         int i = 1;
2074         int is_optimized_query = 0;
2075         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2076
2077         if(q_handle->result_set)
2078         {
2079                 ADVANCED_SVC_TRACE("------------------------------ GET NEXT CONTACT INFO ------------------------------ ");
2080
2081                 if(q_handle->order->str[0] != '\0')
2082                 {
2083                         if(q_handle->is_optimized_query)
2084                         {
2085                                 q_handle->return_array_pos++;
2086                                 ADVANCED_CATCH_SET_ERROR((q_handle->return_array_pos < q_handle->total_result_set_count), {}, QUERY_NO_RECORD, ("no record"));
2087                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].is_occupied == 1), {}, QUERY_NO_RECORD, ("no record"));
2088                         }else
2089                         {
2090                                 q_handle->pos++;
2091                                 ADVANCED_CATCH_SET_ERROR((q_handle->pos < q_handle->total_result_set_count), {}, QUERY_NO_RECORD, ("no record"));
2092                                 q_handle->return_array_pos = q_handle->ordering_list[q_handle->pos].ordered_pos;
2093                         }
2094                 }else
2095                 {
2096                         q_handle->return_array_pos++;
2097                         ADVANCED_CATCH_SET_ERROR((q_handle->return_array_pos < q_handle->total_result_set_count), {}, QUERY_NO_RECORD, ("no record"));
2098                         ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].is_occupied == 1), {}, QUERY_NO_RECORD, ("no record"));
2099                 }
2100         }else
2101         {
2102                 ADVANCED_SVC_TRACE("------------------------------ GENERATE RESULT SET ------------------------------ ");
2103
2104                 if(q_handle->query_type == QUERY_TO_PERSON)
2105                         _query_get_count(table_key[TABLE_PERSON], q_handle->query_type, &count);
2106                 else if(q_handle->query_type == QUERY_TO_CONTACT)
2107                         _query_get_count(table_key[TABLE_CONTACT], q_handle->query_type, &count);
2108                 ADVANCED_RETURN_VAL((count != 0), {}, QUERY_NO_RECORD, ("NO RECORD"));
2109
2110                 query = g_string_new ("");
2111
2112                 target_table_e join_table = _check_join_case(q_handle);
2113                 q_handle->join_table = join_table;
2114
2115                 is_optimized_query = _check_optimized_query(q_handle, join_table);
2116
2117                 if(is_optimized_query == 1)
2118                 {
2119                         ADVANCED_SVC_TRACE("!!!!!! EXECUTE OPTIMIZED QUERY !!!!!!");
2120                         q_handle->is_optimized_query = 1;
2121                         projection = g_string_new("");
2122
2123                         if(q_handle->query_string == NULL)
2124                         {
2125                                 if(join_table == ONLY_DATA || join_table == ONLY_DATA_FROM_PERSON_QUERY)
2126                                         g_string_append_printf(projection, "%s", field_key[FIELD_DATA_CONTACT_ID]);
2127                                 else
2128                                         g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
2129
2130                                 GList* tmp_projection = q_handle->projection;
2131                                 while(tmp_projection)
2132                                 {
2133                                         projection_t* projection_info = (projection_t*)tmp_projection->data;
2134
2135                                         loc = strstr(projection->str, projection_info->field);
2136                                         if(loc == NULL)
2137                                         {
2138                                                 g_string_append_printf(projection, ", %s", projection_info->field);
2139
2140                                                 projection_t* new_projection_info = NULL;
2141                                                 new_projection_info = g_new0(projection_t, 1);
2142                                                 ADVANCED_CATCH_SET_ERROR((new_projection_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2143
2144                                                 new_projection_info->field = g_strdup(projection_info->field);
2145                                                 q_handle->new_projection = g_list_append(q_handle->new_projection, new_projection_info);
2146                                         }
2147                                         tmp_projection = g_list_next(tmp_projection);
2148                                 }
2149                                 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
2150                                 g_list_free(q_handle->projection);
2151
2152                                 q_handle->query_string = g_strdup(projection->str);
2153                         }
2154
2155                         if((strcmp(q_handle->query_string, field_key[FIELD_DATA_CONTACT_ID]) == 0) || (strcmp(q_handle->query_string, field_key[FIELD_PERSON_CONTACT_ID]) == 0))
2156                                 type = ONLY_CONTACT_ID_FIELD;
2157
2158                         GString* sub_query;
2159                         sub_query = g_string_new("");
2160
2161                         if(q_handle->condition_datatype != DATATYPE_NAME && q_handle->condition_datatype != DATATYPE_INVALID)
2162                                 _make_contact_id_list_by_condition_part(q_handle, count, sub_query);
2163
2164                         error_code = _construct_optimize_query(q_handle, query, join_table, type, sub_query->str);
2165                         ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, error_code, ("CONSTRUCT QUERY FAIL"));
2166
2167                     q_handle->hstmt = _contact_query_prepare(query->str);
2168                         ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
2169
2170                         if(q_handle->use_normal_field_key->str[0] != '\0')
2171                         {
2172                                 GList* tmp_condition_val = q_handle->condition_value;
2173                                 while(tmp_condition_val)
2174                                 {
2175                                         condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
2176                                         if(tmp_val->indices_filter == 0)
2177                                         {
2178                                         ADVANCED_SVC_TRACE("condition_val->value : %s", tmp_val->value);
2179                                         primitive_type type = _convert_check_attribute_primitive_type(tmp_val->attribute);
2180                                         if(tmp_val->x_attribute == 1)
2181                                                 type = STRING;
2182
2183                                         if(type == INTEGER)
2184                                                 _contact_query_bind_int(q_handle->hstmt, i++, tmp_val->int_value);
2185                                         else if(type == STRING)
2186                                                 _contact_query_bind_text(q_handle->hstmt, i++, tmp_val->value);
2187                                         else if(type == REAL)
2188                                                 _contact_query_bind_double(q_handle->hstmt, i++, tmp_val->real_value);
2189                                         }
2190
2191                                         tmp_condition_val = g_list_next(tmp_condition_val);
2192                                 }
2193                         }
2194
2195                         rc = _contact_query_step(q_handle->hstmt);
2196                         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
2197
2198                         error_code = _query_result_set(q_handle, rc, count, type, join_table);
2199
2200                         g_string_free(projection, TRUE);
2201                         g_string_free(query, TRUE);
2202                         g_string_free (sub_query, TRUE);
2203
2204                 }else
2205                 {
2206                         ADVANCED_SVC_TRACE("!!!!!! EXECUTE NORMAL QUERY !!!!!!");
2207
2208 // generate contact id list
2209                         GString* sub_query;
2210                         sub_query = g_string_new("");
2211
2212                         if(q_handle->condition_category != NULL)
2213                         {
2214                                 g_string_append_printf(sub_query, "select distinct(a.%s) from %s a, %s b where (a.%s = b.%s) and b.%s %s ?",
2215                                         relation_field_key[FIELD_GROUP_RELATION_CONTACT_ID], table_key[TABLE_GROUP_RELATION],
2216                                         table_key[TABLE_GROUP], group_field_key[FIELD_GROUP_ID], relation_field_key[FIELD_GROUP_RELATION_GROUP_ID],
2217                                         group_field_key[FIELD_GROUP_NAME], q_handle->condition_category_op);
2218                         }else
2219                                 error_code = _make_contact_id_list_by_condition_part(q_handle, count, sub_query);
2220
2221 // generate result set
2222                         error_code = _make_result_contact_list(q_handle, count, sub_query->str);
2223 // ordering
2224                         _make_order_list(q_handle, count, join_table);
2225
2226 // get categories
2227                         int category_count = 0;
2228                         if((q_handle->extra_info & CATEGORIES) != 0)
2229                         {
2230                                 _query_get_count(table_key[TABLE_GROUP_RELATION], q_handle->query_type, &category_count);
2231                                 q_handle->category = g_new0(categories_info, category_count + 1);
2232                                 if(category_count != 0)
2233                                         _get_categories_list(q_handle, category_count, sub_query->str);
2234                         }
2235
2236                         g_string_free (sub_query, TRUE);
2237                 }
2238                 ADVANCED_CATCH_SET_ERROR((q_handle->total_result_set_count != 0), {}, QUERY_NO_RECORD, ("NO RESULT CONTACT LIST"));
2239         }
2240
2241 CATCH:
2242         if(error_code != QUERY_SUCCESS)
2243         {
2244         }
2245
2246         return error_code;
2247 }
2248
2249 API query_error query_finalize(ADVANCED_HANDLE handle)
2250 {
2251         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2252
2253         query_error error_code = QUERY_SUCCESS;
2254         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2255
2256         _free_handle_info(q_handle);
2257         q_handle = NULL;
2258
2259         error_code = _contact_db_finish();
2260
2261         return error_code;
2262 }
2263
2264 query_error _set_exists_condition(ADVANCED_HANDLE handle, contact_attribute_e attribute)
2265 {
2266         query_error error_code = QUERY_SUCCESS;
2267         predefine_datatype datatype = DATATYPE_INVALID;
2268         db_datatype db_datatype_id = DB_DATATYPE_INVALID;
2269         //char* condition = NULL;
2270         char* table = NULL;
2271         condition_table_t* table_info = NULL;
2272         alias_key_e tmp_current_alias = ALIAS_INVALID;
2273         char* column_name = NULL;
2274         char* normal_column_name = NULL;
2275
2276         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2277         ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
2278
2279         error_code = _check_extra_contact_info(q_handle, EXTRA_INFO_TYPE_CONDITION, attribute);
2280         ADVANCED_RETURN_VAL((error_code != QUERY_SUCCESS), {}, QUERY_FAIL, ("NOT SUPPORT"));
2281         error_code = QUERY_SUCCESS;
2282
2283         column_name = _convert_attribute_to_condition_column_name(attribute);
2284         normal_column_name = _convert_attribute_to_column_name(attribute);
2285
2286         datatype = _convert_attribute_to_mimetype_id(attribute);
2287         db_datatype_id = _convert_datatype_to_db_type(datatype);
2288
2289         table = _convert_attribute_to_table_name(attribute);
2290         tables_e table_type = _contact_table_name_to_table_type(table);
2291         q_handle->query_table = q_handle->query_table | table_type;
2292
2293         if(q_handle->current_alias != ALIAS_INVALID)
2294         {
2295                 if((q_handle->condition_datatype & datatype) == 0 )
2296                 {
2297                         q_handle->current_alias++;
2298                         tmp_current_alias = q_handle->current_alias;
2299
2300                         table_info = g_new0(condition_table_t, 1);
2301                         table_info->alias = q_handle->current_alias;
2302                         table_info->table = g_strdup(table);
2303                         q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
2304                 }else
2305                 {
2306                         GList* tmp_condition_list = q_handle->condition_list;
2307                         while(tmp_condition_list)
2308                         {
2309                                 condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
2310
2311                                 if(tmp_info->datatype == db_datatype_id)
2312                                 {
2313                                         tmp_current_alias = tmp_info->current_alias;
2314                                         break;
2315                                 }
2316                                 tmp_condition_list = g_list_next(tmp_condition_list);
2317                         }
2318                 }
2319         }else
2320         {
2321                 q_handle->current_alias++;
2322                 tmp_current_alias = q_handle->current_alias;
2323
2324                 table_info = g_new0(condition_table_t, 1);
2325                 table_info->alias = q_handle->current_alias;
2326                 table_info->table = g_strdup(table);
2327                 q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
2328         }
2329
2330         if(db_datatype_id != DB_DATATYPE_INVALID)
2331         {
2332                 g_string_append_printf(q_handle->use_normal_field_key, "(%s = %d)", condition_field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id);
2333                 g_string_append_printf(q_handle->condition_text, "(%s.%s = %d)", alias_key[tmp_current_alias], condition_field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id);
2334
2335                 condition_list_t* condition_info = NULL;
2336                 condition_info = g_new0(condition_list_t, 1);
2337                 condition_info->current_alias = tmp_current_alias;
2338                 condition_info->datatype = db_datatype_id;
2339                 q_handle->condition_list = g_list_append(q_handle->condition_list, condition_info);
2340         }else
2341         {
2342                 q_handle->optimize_condition = 0;
2343                 g_string_append_printf(q_handle->use_contact_table_id, "(%s is not null)", field_key[FIELD_CONTACT_CONTACT_ID]);
2344                 g_string_append_printf(q_handle->use_normal_field_key, "(%s is not null)", normal_column_name);
2345                 g_string_append_printf(q_handle->condition_text, "(%s.%s is not null)", alias_key[tmp_current_alias], column_name);
2346         }
2347
2348         q_handle->condition_datatype = q_handle->condition_datatype | datatype;
2349
2350         return error_code;
2351 }
2352
2353 API query_error query_set_condition(ADVANCED_HANDLE handle, contact_attribute_e attribute, condition_e attr, const char* value)
2354 {
2355         query_error error_code = QUERY_SUCCESS;
2356         predefine_datatype datatype = DATATYPE_INVALID;
2357         db_datatype db_datatype_id = DB_DATATYPE_INVALID;
2358         char* column_name = NULL;
2359         char* normal_column_name = NULL;
2360         char* condition = NULL;
2361         char* table = NULL;
2362         int is_person_attr = 0;
2363         condition_table_t* table_info = NULL;
2364         alias_key_e tmp_current_alias = ALIAS_INVALID;
2365
2366         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2367         ADVANCED_RETURN_VAL((attribute < ATTRIBUTE_MAX), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID CONTACT ATTRIBUTE"));
2368         ADVANCED_RETURN_VAL((attr < CONDITION_MAX), {}, QUERY_INVALID_TYPE, ("INVALID CONTACT CONDITION"));
2369         ADVANCED_RETURN_VAL((attribute != CONTACT_PHOTO_URI), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID CONTACT ATTRIBUTE"));
2370
2371         if(attr == EXISTS)
2372         {
2373                 error_code = _set_exists_condition(handle, attribute);
2374                 return error_code;
2375         }
2376         ADVANCED_RETURN_VAL((value != NULL), {}, QUERY_NULL_PARAMETER, ("INVALID VALUE"));
2377
2378         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2379         ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
2380
2381         ADVANCED_SVC_TRACE("condition value : %s", value);
2382         if(_is_type_bitwise_int(attribute) && attr == LIKE)
2383                 condition = _convert_condition_to_string(BITWISE_AND);
2384         else
2385                 condition = _convert_condition_to_string(attr);
2386
2387         error_code = _check_extra_contact_info(q_handle, EXTRA_INFO_TYPE_CONDITION, attribute);
2388         if(error_code == QUERY_SUCCESS)
2389         {
2390                 ADVANCED_RETURN_VAL((q_handle->condition_text->str[0] == '\0'), {}, QUERY_GROUP_CASE, ("CAN NOT ASSIGN GROUP CONDITION"));
2391                 q_handle->condition_category =  g_strdup(value);
2392                 q_handle->condition_category_op =  g_strdup(condition);
2393                 return error_code;
2394         }
2395         error_code = QUERY_SUCCESS;
2396
2397         if(q_handle->query_type == QUERY_TO_PERSON)
2398         {
2399                 column_name = _convert_person_attribute_to_condition_column_name(attribute);
2400                 normal_column_name = _convert_person_attribute_to_column_name(attribute);
2401         }
2402
2403         if(column_name == NULL)
2404         {
2405 //              column_name = _convert_attribute_to_condition_normalize_column(attribute);
2406 //              normal_column_name = _convert_attribute_to_normalize_column(attribute);
2407
2408 //              if(column_name == NULL)
2409                 {
2410                         column_name = _convert_attribute_to_condition_column_name(attribute);
2411                         normal_column_name = _convert_attribute_to_column_name(attribute);
2412                 }
2413         }
2414
2415         if(q_handle->query_type == QUERY_TO_PERSON)
2416                 is_person_attr = _convert_person_attribute_to_mimetype_id(attribute);
2417
2418         if(is_person_attr == 0)
2419         {
2420                 datatype = _convert_attribute_to_mimetype_id(attribute);
2421                 db_datatype_id = _convert_datatype_to_db_type(datatype);
2422         }
2423
2424         if(q_handle->query_type == QUERY_TO_PERSON)
2425                 table = _convert_person_attribute_to_table_name(attribute);
2426
2427         if(table == NULL)
2428                 table = _convert_attribute_to_table_name(attribute);
2429         tables_e table_type = _contact_table_name_to_table_type(table);
2430         q_handle->query_table = q_handle->query_table | table_type;
2431
2432         if(q_handle->current_alias != ALIAS_INVALID)
2433         {
2434                 if((q_handle->condition_datatype & datatype) == 0 )
2435                 {
2436                         q_handle->current_alias++;
2437                         tmp_current_alias = q_handle->current_alias;
2438
2439                         table_info = g_new0(condition_table_t, 1);
2440                         table_info->alias = q_handle->current_alias;
2441                         table_info->table = g_strdup(table);
2442                         q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
2443                 }else
2444                 {
2445                         GList* tmp_condition_list = q_handle->condition_list;
2446                         while(tmp_condition_list)
2447                         {
2448                                 condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
2449
2450                                 if(tmp_info->datatype == db_datatype_id)
2451                                 {
2452                                         tmp_current_alias = tmp_info->current_alias;
2453                                         break;
2454                                 }
2455                                 tmp_condition_list = g_list_next(tmp_condition_list);
2456                         }
2457                 }
2458         }else
2459         {
2460                 q_handle->current_alias++;
2461                 tmp_current_alias = q_handle->current_alias;
2462
2463                 table_info = g_new0(condition_table_t, 1);
2464                 table_info->alias = q_handle->current_alias;
2465                 table_info->table = g_strdup(table);
2466                 q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
2467         }
2468
2469         if(db_datatype_id != DB_DATATYPE_INVALID)
2470         {
2471                 g_string_append_printf(q_handle->use_normal_field_key, "(%s %s ? and %s = %d)",
2472                         normal_column_name, condition, condition_field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id);
2473
2474                 g_string_append_printf(q_handle->condition_text, "(%s.%s %s ? and %s.%s = %d)",
2475                         alias_key[tmp_current_alias], column_name, condition, alias_key[tmp_current_alias], condition_field_key[FIELD_DATA_DATATYPE_ID], db_datatype_id);
2476
2477                 condition_list_t* condition_info = NULL;
2478                 condition_info = g_new0(condition_list_t, 1);
2479                 condition_info->column_name = g_strdup(column_name);
2480                 condition_info->current_alias = tmp_current_alias;
2481                 condition_info->condition = g_strdup(condition);
2482                 condition_info->datatype = db_datatype_id;
2483
2484                 q_handle->condition_list = g_list_append(q_handle->condition_list, condition_info);
2485
2486         }else
2487         {
2488                 q_handle->optimize_condition = 0;
2489                 g_string_append_printf(q_handle->use_contact_table_id, "(%s %s ?)", field_key[FIELD_CONTACT_CONTACT_ID], condition);
2490
2491                 g_string_append_printf(q_handle->use_normal_field_key, "(%s %s ?)", normal_column_name, condition);
2492
2493                 g_string_append_printf(q_handle->condition_text, "(%s.%s %s ?)", alias_key[tmp_current_alias], column_name, condition);
2494         }
2495
2496         primitive_type type = _convert_check_attribute_primitive_type(attribute);
2497         condition_val_t* condition_txt = NULL;
2498         condition_txt = g_new0(condition_val_t, 1);
2499
2500         condition_txt->attribute = attribute;
2501         if(type == INTEGER)
2502         {
2503                 if(attribute == ACCOUNT_ID)
2504                 {
2505                         int total_count = 0;
2506
2507                         char *end_ptr = value;
2508                         errno = 0;
2509                         int     integer_value = (int)strtol(value, &end_ptr, 0);
2510                         if(errno == ERANGE || end_ptr == value)
2511                         {
2512                                 return QUERY_INVALID_TYPE;
2513                         }
2514
2515                         get_all_addressbook_id(&total_count);
2516                         GList* tmp_list = g_addressbook_list;
2517                         while(tmp_list)
2518                         {
2519                                 addressbook_list* info = (addressbook_list*)tmp_list->data;
2520                                 if(integer_value == info->account_id)
2521                                 {
2522                                         condition_txt->int_value = info->addressbook_id;
2523                                         break;
2524                                 }
2525                                 tmp_list = g_list_next(tmp_list);
2526                         }
2527                 }else
2528                         condition_txt->int_value = _convert_type_string_to_int(attribute, value);
2529         }else if(type == STRING)
2530         {
2531                 condition_txt->value = g_strdup(value);
2532         }else if(type == REAL)
2533                 condition_txt->real_value = atof(value);
2534
2535         q_handle->condition_value = g_list_append(q_handle->condition_value, condition_txt);
2536
2537         q_handle->condition_datatype = q_handle->condition_datatype | datatype;
2538
2539         return error_code;
2540 }
2541
2542 API query_error fetch_next_row(ADVANCED_HANDLE handle)
2543 {
2544         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2545
2546         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2547         query_error error_code = QUERY_SUCCESS;
2548
2549         ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->return_array_pos].contact_info != NULL), {}, QUERY_NO_RECORD, ("CONTACT INFO IS NULL"));
2550
2551         switch(q_handle->current_datatype)
2552         {
2553                 case DATATYPE_INVALID:
2554                         if(q_handle->query_type == QUERY_TO_CONTACT)
2555                                 q_handle->result_set[q_handle->return_array_pos].contact_info->get_raw_contact_info = 1;
2556                         else
2557                                 q_handle->result_set[q_handle->return_array_pos].contact_info->get_person_info = 1;
2558                         ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_NEXT_ROW, ("NOT MULTIPLE VALUE"));
2559                         break;
2560                 case DATATYPE_NUMBER:
2561                 {
2562                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->number_list;
2563                         if(tmp_list)
2564                         {
2565                                 number_t* num_info = (number_t*)tmp_list->data;
2566
2567                                 _free_num_info(q_handle->result_set[q_handle->return_array_pos].contact_info->num_info);
2568
2569                                 q_handle->result_set[q_handle->return_array_pos].contact_info->num_info = g_new0(number_t, 1);
2570                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->num_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2571
2572                                 q_handle->result_set[q_handle->return_array_pos].contact_info->num_info->number = g_strdup(num_info->number);
2573                                 q_handle->result_set[q_handle->return_array_pos].contact_info->num_info->number_type = g_strdup(num_info->number_type);
2574
2575                                 ADVANCED_SVC_TRACE("number : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->num_info->number);
2576                                 _free_num_info((number_t*)tmp_list->data);
2577                                 q_handle->result_set[q_handle->return_array_pos].contact_info->number_list = g_list_next(tmp_list);
2578                         }
2579                         else
2580                         {
2581                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2582                         }
2583                 }
2584                         break;
2585                 case DATATYPE_EMAIL:
2586                 {
2587                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->email_list;
2588                         if(tmp_list)
2589                         {
2590                                 email_t* email_info = (email_t*)tmp_list->data;
2591
2592                                 _free_email_info(q_handle->result_set[q_handle->return_array_pos].contact_info->email_info);
2593
2594                                 q_handle->result_set[q_handle->return_array_pos].contact_info->email_info = g_new0(email_t, 1);
2595                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->email_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2596
2597                                 q_handle->result_set[q_handle->return_array_pos].contact_info->email_info->email_addr = g_strdup(email_info->email_addr);
2598                                 q_handle->result_set[q_handle->return_array_pos].contact_info->email_info->email_type = g_strdup(email_info->email_type);
2599
2600                                 ADVANCED_SVC_TRACE("email_addr : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->email_info->email_addr);
2601                                 _free_email_info((email_t*)tmp_list->data);
2602                                 q_handle->result_set[q_handle->return_array_pos].contact_info->email_list = g_list_next(tmp_list);
2603                         }
2604                         else
2605                         {
2606                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2607                         }
2608                 }
2609                         break;
2610                 case DATATYPE_URL:
2611                 {
2612                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->url_list;
2613                         if(tmp_list)
2614                         {
2615                                 url_t* url_info = (url_t*)tmp_list->data;
2616
2617                                 _free_url_info(q_handle->result_set[q_handle->return_array_pos].contact_info->url_info);
2618
2619                                 q_handle->result_set[q_handle->return_array_pos].contact_info->url_info = g_new0(url_t, 1);
2620                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->url_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2621
2622                                 q_handle->result_set[q_handle->return_array_pos].contact_info->url_info->url_addr = g_strdup(url_info->url_addr);
2623                                 q_handle->result_set[q_handle->return_array_pos].contact_info->url_info->url_type = g_strdup(url_info->url_type);
2624
2625                                 ADVANCED_SVC_TRACE("url_addr : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->url_info->url_addr);
2626                                 _free_url_info((url_t*)tmp_list->data);
2627                                 q_handle->result_set[q_handle->return_array_pos].contact_info->url_list = g_list_next(tmp_list);
2628                         }
2629                         else
2630                         {
2631                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2632                         }
2633                 }
2634                         break;
2635                 case DATATYPE_NICK_NAME :
2636                 {
2637                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->nickname_list;
2638                         if(tmp_list)
2639                         {
2640                                 nick_name_t* nick_info = (nick_name_t*)tmp_list->data;
2641
2642                                 _free_nick_info(q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info);
2643
2644                                 q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info = g_new0(nick_name_t, 1);
2645                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2646
2647                                 q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info->nick_name = g_strdup(nick_info->nick_name);
2648
2649                                 ADVANCED_SVC_TRACE("nick_name : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info->nick_name);
2650                                 _free_nick_info((nick_name_t*)tmp_list->data);
2651                                 q_handle->result_set[q_handle->return_array_pos].contact_info->nickname_list = g_list_next(tmp_list);
2652                         }
2653                         else
2654                         {
2655                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2656                         }
2657                 }
2658                         break;
2659                 case DATATYPE_ADDRESS :
2660                 {
2661                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->address_list;
2662                         if(tmp_list)
2663                         {
2664                                 address_t* address_info = (address_t*)tmp_list->data;
2665
2666                                 _free_address_info(q_handle->result_set[q_handle->return_array_pos].contact_info->address_info);
2667
2668                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info = g_new0(address_t, 1);
2669                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2670
2671                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->country = g_strdup(address_info->country);
2672                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->region = g_strdup(address_info->region);
2673                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->county= g_strdup(address_info->county);
2674                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->city = g_strdup(address_info->city);
2675                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->street = g_strdup(address_info->street);
2676                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->postal_code = g_strdup(address_info->postal_code);
2677                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->addtional = g_strdup(address_info->addtional);
2678
2679                                 ADVANCED_SVC_TRACE("street : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->street);
2680                                 _free_address_info((address_t*)tmp_list->data);
2681                                 q_handle->result_set[q_handle->return_array_pos].contact_info->address_list = g_list_next(tmp_list);
2682                         }
2683                         else
2684                         {
2685                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2686                         }
2687                 }
2688                         break;
2689                 case DATATYPE_ORGANIZATION :
2690                 {
2691                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_list;
2692                         if(tmp_list)
2693                         {
2694                                 organization_t* organization_info = (organization_t*)tmp_list->data;
2695
2696                                 _free_organization_info(q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info);
2697
2698                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info = g_new0(organization_t, 1);
2699                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2700
2701                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->organization_name = g_strdup(organization_info->organization_name);
2702                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->department = g_strdup(organization_info->department);
2703                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->office = g_strdup(organization_info->office);
2704                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->title = g_strdup(organization_info->title);
2705                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->role = g_strdup(organization_info->role);
2706                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->logo_uri = g_strdup(organization_info->logo_uri);
2707
2708                                 ADVANCED_SVC_TRACE("title : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->title);
2709                                 _free_organization_info((organization_t*)tmp_list->data);
2710                                 q_handle->result_set[q_handle->return_array_pos].contact_info->organization_list = g_list_next(tmp_list);
2711                         }
2712                         else
2713                         {
2714                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2715                         }
2716                 }
2717                         break;
2718                 case DATATYPE_ANNIVERSARY :
2719                 {
2720                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->event_list;
2721                         if(tmp_list)
2722                         {
2723                                 event_info_t* event_info = (event_info_t*)tmp_list->data;
2724
2725                                 _free_anniversary_info(q_handle->result_set[q_handle->return_array_pos].contact_info->event_info);
2726
2727                                 q_handle->result_set[q_handle->return_array_pos].contact_info->event_info = g_new0(event_info_t, 1);
2728                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->event_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2729
2730                                 q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->type = g_strdup(event_info->type);
2731                                 q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->date = g_strdup(event_info->date);
2732                                 q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->label = g_strdup(event_info->label);
2733
2734                                 ADVANCED_SVC_TRACE("date : %s", q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->date);
2735                                 _free_anniversary_info((event_info_t*)tmp_list->data);
2736                                 q_handle->result_set[q_handle->return_array_pos].contact_info->event_list = g_list_next(tmp_list);
2737                         }
2738                         else
2739                         {
2740                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2741                         }
2742                 }
2743                         break;
2744                 case DATATYPE_NAME :
2745                         q_handle->result_set[q_handle->return_array_pos].contact_info->get_name = 1;
2746                         ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_NEXT_ROW, ("NOT MULTIPLE VALUE"));
2747                         break;
2748                 case DATATYPE_CATEGORY :
2749                 {
2750                         if((q_handle->extra_info & CATEGORIES) != 0 && q_handle->next_count > -1)
2751                         {
2752                                 char *value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->contact_id;
2753                                 char *end_ptr = value;
2754                                 errno = 0;
2755                                 int     contact_id = (int)strtol(value, &end_ptr, 0);
2756                                 if(errno == ERANGE || end_ptr == value)
2757                                 {
2758                                         return QUERY_INVALID_TYPE;
2759                                 }
2760
2761                                 if(q_handle->next_count != 0)
2762                                 {
2763                                         ADVANCED_CATCH_SET_ERROR(((q_handle->category_pos + q_handle->next_count) < q_handle->category_total_count), {}, QUERY_NO_RECORD, ("no record"));
2764                                         ADVANCED_CATCH_SET_ERROR((q_handle->category[q_handle->category_pos + q_handle->next_count].sibling != 0), {}, QUERY_NO_RECORD, ("no record"));
2765                                         q_handle->category_pos = q_handle->category_pos + q_handle->next_count;
2766                                         q_handle->next_count++;
2767                                         return QUERY_SUCCESS;
2768                                 }else
2769                                 {
2770 #if 1
2771                                         int high, low, j;
2772                                         high = q_handle->category_total_count;
2773                                         low = 0;
2774
2775                                         while(high >= low)
2776                                         {
2777                                                 j = ( low + high ) / 2;
2778
2779                                                 if(q_handle->category[j].contact_id > contact_id)
2780                                                         high = j - 1;
2781                                                 else if(q_handle->category[j].contact_id < contact_id)
2782                                                         low = j + 1;
2783                                                 else if(q_handle->category[j].contact_id == contact_id)
2784                                                 {
2785                                                         q_handle->category_pos = j;
2786                                                         q_handle->next_count++;
2787                                                         return QUERY_SUCCESS;
2788                                                 }
2789                                         }
2790 #else
2791                                         for(i = 0; i < q_handle->category_total_count; i++)
2792                                         {
2793                                                 if(q_handle->category[i].contact_id == contact_id)
2794                                                 {
2795                                                         q_handle->category_pos = i;
2796                                                         q_handle->next_count++;
2797                                                         return QUERY_SUCCESS;
2798                                                 }
2799                                         }
2800 #endif
2801                                 }
2802                         }
2803                         q_handle->next_count = -1;
2804                         ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2805                 }
2806                         break;
2807                 case DATATYPE_X_ATTRIBUTE :
2808                 {
2809                         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->x_list;
2810                         if(tmp_list)
2811                         {
2812                                 x_info_t* x_info = (x_info_t*)tmp_list->data;
2813
2814                                 _free_x_info(q_handle->result_set[q_handle->return_array_pos].contact_info->x_info);
2815
2816                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info = g_new0(x_info_t, 1);
2817                                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
2818
2819                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->datatype_id = x_info->datatype_id;
2820                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data1 = g_strdup(x_info->data1);
2821                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data2 = g_strdup(x_info->data2);
2822                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data3 = g_strdup(x_info->data3);
2823                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data4 = g_strdup(x_info->data4);
2824                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data5 = g_strdup(x_info->data5);
2825                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data6 = g_strdup(x_info->data6);
2826                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data7 = g_strdup(x_info->data7);
2827                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data8 = g_strdup(x_info->data8);
2828                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data9 = g_strdup(x_info->data9);
2829                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data10 = g_strdup(x_info->data10);
2830
2831                                 _free_x_info((x_info_t*)tmp_list->data);
2832                                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_list = g_list_next(tmp_list);
2833                         }
2834                         else
2835                         {
2836                                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2837                         }
2838                 }
2839                         break;
2840                 default:
2841                         break;
2842         }
2843
2844         ADVANCED_SVC_TRACE("error_code : %d", error_code);
2845
2846 CATCH:
2847         return error_code;
2848 }
2849
2850 query_error _invalid_datatype_fetch_next_row(advanced_handle_t* handle, predefine_datatype datatype)
2851 {
2852         query_error error_code = QUERY_SUCCESS;
2853         ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info != NULL), {}, QUERY_NO_RECORD, ("CONTACT INFO IS NULL"));
2854
2855         if(datatype == DATATYPE_INVALID)
2856         {
2857                 if(handle->query_type == QUERY_TO_CONTACT)
2858                 {
2859                         ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info->get_raw_contact_info == 0), {}, QUERY_NO_NEXT_ROW, ("NO ROW"));
2860                 }else if(handle->query_type == QUERY_TO_PERSON)
2861                 {
2862                         ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info->get_person_info == 0), {}, QUERY_NO_NEXT_ROW, ("NO ROW"));
2863                 }
2864         }else if(datatype == DATATYPE_NAME)
2865         {
2866                 ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info->get_name == 0), {}, QUERY_NO_NEXT_ROW, ("NO ROW"));
2867         }
2868
2869         return error_code;
2870 }
2871
2872 char* _get_person_attribute_value(advanced_handle_t* q_handle, contact_attribute_e attribute, query_error* error_code)
2873 {
2874         char* value = NULL;
2875
2876         switch(attribute)
2877         {
2878                 case CONTACT_ID :
2879                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2880                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->contact_id;
2881                         break;
2882                 case CONTACT_IS_FAVORITE:
2883                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2884                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->is_favorite;
2885                         break;
2886                 case CONTACT_RINGTONE:
2887                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2888                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->ringtone;
2889                         break;
2890                 case CONTACT_PHOTO_URI:
2891                         break;
2892                 case CONTACT_LAST_UPDATED_TIME_STAMP:
2893                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2894                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->last_updated_timestamp;
2895                         break;
2896                 case CONTACT_UID:
2897                         break;
2898                 case NAME_FIRST :
2899                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2900                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->first_name;
2901                         break;
2902                 case NAME_MIDDLE:
2903                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2904                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->middle_name;
2905                         break;
2906                 case NAME_LAST:
2907                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2908                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->last_name;
2909                         break;
2910                 case NAME_PREFIX:
2911                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2912                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->prefix;
2913                         break;
2914                 case NAME_PHONETIC:
2915                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2916                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->phonetic_name;
2917                         break;
2918                 case NAME_DISPLAY :
2919                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->person_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2920                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->person_info->display_name;
2921                         break;
2922                 default:
2923                         break;
2924         }
2925
2926 CATCH:
2927         return value;
2928 }
2929
2930 char* _get_x_attribute_value(advanced_handle_t* q_handle, contact_x_attribute_e attribute, query_error* error_code)
2931 {
2932         char* value = NULL;
2933
2934         switch(attribute)
2935         {
2936                 case X_DATA1:
2937                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2938                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data1;
2939                         break;
2940                 case X_DATA2:
2941                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2942                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data2;
2943                         break;
2944                 case X_DATA3:
2945                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2946                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data3;
2947                         break;
2948                 case X_DATA4:
2949                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2950                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data4;
2951                         break;
2952                 case X_DATA5:
2953                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2954                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data5;
2955                         break;
2956                 case X_DATA6:
2957                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2958                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data6;
2959                         break;
2960                 case X_DATA7:
2961                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2962                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data7;
2963                         break;
2964                 case X_DATA8:
2965                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2966                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data8;
2967                         break;
2968                 case X_DATA9:
2969                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2970                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data9;
2971                         break;
2972                 case X_DATA10:
2973                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2974                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data10;
2975                         break;
2976                 default:
2977                         break;
2978         }
2979 CATCH:
2980         return value;
2981 }
2982
2983 char* _get_attribute_value(advanced_handle_t* q_handle, contact_attribute_e attribute, query_error* error_code)
2984 {
2985         char* value = NULL;
2986
2987         switch(attribute)
2988         {
2989                 case CONTACT_ID :
2990                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2991                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->contact_id;
2992                         break;
2993                 case CONTACT_UID:
2994                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
2995                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->uid;
2996                         break;
2997                 case ACCOUNT_ID :
2998                 {
2999                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3000                         if(q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->account_id != NULL)
3001                         {
3002                                 int int_value = 0;
3003                                 int total_count = 0;
3004                                 char *value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->account_id;
3005                                 char *end_ptr = value;
3006                                 errno = 0;
3007                                 int_value = (int)strtol(value, &end_ptr, 0);
3008                                 if(errno == ERANGE || end_ptr == value)
3009                                 {
3010                                         return QUERY_INVALID_TYPE;
3011                                 }
3012
3013                                 get_all_addressbook_id(&total_count);
3014
3015                                 GList* tmp_list = g_addressbook_list;
3016                                 while(tmp_list)
3017                                 {
3018                                         addressbook_list* info = (addressbook_list*)tmp_list->data;
3019                                         if(int_value == info->addressbook_id)
3020                                         {
3021                                                 memset(q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->account_id, 0x00, 64);
3022                                                 snprintf(q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->account_id, 64, "%d", info->account_id);
3023                                                 value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->account_id;
3024                                                 break;
3025                                         }
3026                                         tmp_list = g_list_next(tmp_list);
3027                                 }
3028                         }
3029                 }
3030                         break;
3031                 case CONTACT_IS_FAVORITE:
3032                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3033                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->is_favorite;
3034                         break;
3035                 case CONTACT_RINGTONE:
3036                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3037                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->ringtone;
3038                         break;
3039                 case CONTACT_NOTE:
3040                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3041                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->note;
3042                         break;
3043                 case CONTACT_PHOTO_URI:
3044                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3045                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->photo_uri;
3046                         break;
3047                 case CONTACT_LAST_UPDATED_TIME_STAMP:
3048                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3049                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->last_updated_time_stamp;
3050                         break;
3051                 case NAME_FIRST :
3052                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3053                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->first_name;
3054                         break;
3055                 case NAME_MIDDLE:
3056                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3057                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->middle_name;
3058                         break;
3059                 case NAME_LAST:
3060                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3061                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->last_name;
3062                         break;
3063                 case NAME_PREFIX:
3064                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3065                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->prefix;
3066                         break;
3067                 case NAME_PHONETIC:
3068                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3069                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->phonetic_name;
3070                         break;
3071                 case NAME_DISPLAY :
3072                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->name_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3073                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->name_info->display_name;
3074                         break;
3075                 case NICKNAME_NAME:
3076                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3077                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info->nick_name;
3078                         break;
3079                 case NUMBER_ADDRESS:
3080                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->num_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3081                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->num_info->number;
3082                         break;
3083                 case NUMBER_TYPES:
3084                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->num_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3085                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->num_info->number_type;
3086                         break;
3087                 case EMAIL_ADDRESS:
3088                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->email_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3089                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->email_info->email_addr;
3090                         break;
3091                 case EMAIL_TYPE:
3092                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->email_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3093                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->email_info->email_type;
3094                         break;
3095                 case URL_ADDRESS:
3096                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->url_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3097                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->url_info->url_addr;
3098                         break;
3099                 case URL_TYPE:
3100                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->url_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3101                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->url_info->url_type;
3102                         break;
3103                 case ADDRESS_COUNTRY:
3104                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3105                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->country;
3106                         break;
3107                 case ADDRESS_REGION:
3108                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3109                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->region;
3110                         break;
3111                 case ADDRESS_CITY:
3112                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3113                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->city;
3114                         break;
3115                 case ADDRESS_STREET:
3116                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3117                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->street;
3118                         break;
3119                 case ADDRESS_POSTAL_CODE:
3120                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3121                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->postal_code;
3122                         break;
3123                 case ADDRESS_ADDITIONAL:
3124                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3125                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->addtional;
3126                         break;
3127                 case ADDRESS_TYPE:
3128                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->address_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3129                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->address_info->type;
3130                         break;
3131                 case CATEGORY_INFO:
3132                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->category[q_handle->category_pos].name != NULL), {}, QUERY_NO_RECORD, ("no data"));
3133                         value = q_handle->category[q_handle->category_pos].name;
3134                         break;
3135                 case EVENT_DATE:
3136                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->event_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3137                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->date;
3138                         break;
3139                 case EVENT_TYPE:
3140                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->event_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3141                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->event_info->type;
3142                         break;
3143                 case ORGANIZATION_NAME:
3144                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3145                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->organization_name;
3146                         break;
3147                 case ORGANIZATION_DEPARTMENT:
3148                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3149                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->department;
3150                         break;
3151                 case ORGANIZATION_TITLE:
3152                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3153                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->title;
3154                         break;
3155                 case ORGANIZATION_ROLE:
3156                         ADVANCED_CATCH_SET_ERROR_POINTER((q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info != NULL), {}, QUERY_NO_RECORD, ("no data"));
3157                         value = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info->role;
3158                         break;
3159                 default:
3160                         break;
3161         }
3162
3163 CATCH:
3164         return value;
3165 }
3166
3167 API char* query_get_attribute(ADVANCED_HANDLE handle, contact_attribute_e attribute, query_error* error_code)
3168 {
3169         ADVANCED_RETURN_VAL((handle != NULL), {}, NULL, ("HANDLE IS NULL"));
3170         ADVANCED_RETURN_VAL((attribute < ATTRIBUTE_MAX), {}, NULL, ("INVALID ATTRIBUTE"));
3171
3172         char* value = NULL;
3173         predefine_datatype tmp_datatype = DATATYPE_INVALID;
3174         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3175         ADVANCED_SVC_TRACE("q_handle->return_array_pos : %d", q_handle->return_array_pos);
3176         ADVANCED_RETURN_VAL((q_handle->return_array_pos < q_handle->total_result_set_count), {}, NULL, ("RESULT SET IS NULL"));
3177         ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->return_array_pos].contact_info != NULL), {}, NULL, ("CONTACT INFO IS NULL"));
3178
3179         tmp_datatype = _convert_attribute_to_mimetype_id(attribute);
3180         if(q_handle->current_datatype != tmp_datatype || tmp_datatype == DATATYPE_INVALID)
3181         {
3182                 q_handle->current_datatype = tmp_datatype;
3183                 q_handle->next_count = 0;
3184                 q_handle->category_pos = 0;
3185                 *error_code = QUERY_SUCCESS;
3186
3187                 if(tmp_datatype == DATATYPE_INVALID || tmp_datatype == DATATYPE_NAME)
3188                         *error_code = _invalid_datatype_fetch_next_row(q_handle, tmp_datatype);
3189                 else
3190                         *error_code = fetch_next_row(handle);
3191
3192                 ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, *error_code, ("fetch next row ERROR"));
3193         }
3194
3195         if(q_handle->query_type == QUERY_TO_PERSON)
3196                 value = _get_person_attribute_value(q_handle, attribute, error_code);
3197
3198         if(value == NULL)
3199                 value = _get_attribute_value(q_handle, attribute, error_code);
3200
3201         ADVANCED_SVC_TRACE("%d : %s", attribute, value);
3202
3203 CATCH:
3204         return value;
3205 }
3206
3207 API query_error query_set_order_by(ADVANCED_HANDLE handle, contact_attribute_e attribute, order_e ordering)
3208 {
3209         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3210         ADVANCED_RETURN_VAL((attribute < ATTRIBUTE_MAX), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID CONTACT ATTRIBUTE"));
3211         ADVANCED_RETURN_VAL((ordering <= DESC), {}, QUERY_INVALID_TYPE, ("INVALID CONTACT ORDERING"));
3212         ADVANCED_RETURN_VAL((attribute != CONTACT_ID), {}, QUERY_SUCCESS, ("DEFAULT ORDER ATTRIBUTE"));
3213
3214         char* column_name = NULL;
3215         query_error error_code = QUERY_SUCCESS;
3216         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3217
3218         if(q_handle->query_type == QUERY_TO_PERSON)
3219         {
3220                 column_name = _convert_person_attribute_to_column_name(attribute);
3221         }
3222
3223         if(column_name == NULL)
3224         {
3225                 column_name = _convert_attribute_to_column_name(attribute);
3226
3227                 predefine_datatype datatype = DATATYPE_INVALID;
3228                 datatype = _convert_attribute_to_mimetype_id(attribute);
3229                 q_handle->all_datatype_in_query = (q_handle->all_datatype_in_query | datatype);
3230                 q_handle->order_field = attribute;
3231         }
3232
3233         g_string_append_printf(q_handle->order, "%s", column_name);
3234
3235         q_handle->ordering = ordering;
3236
3237         if(ordering == ASC)
3238         {
3239                 g_string_append_printf(q_handle->order, " %s", "ASC");
3240         }else if(ordering == DESC)
3241         {
3242                 g_string_append_printf(q_handle->order, " %s", "DESC");
3243         }
3244
3245         ADVANCED_SVC_TRACE("order : %s", q_handle->order->str);
3246         return error_code;
3247 }
3248
3249 API query_error query_set_condition_append(ADVANCED_HANDLE handle, condition_type_e type)
3250 {
3251         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3252         ADVANCED_RETURN_VAL((type < CONDITION_TYPE_MAX), {}, QUERY_INVALID_TYPE, ("INVALID CONDITION TYPE"));
3253         query_error error_code = QUERY_SUCCESS;
3254         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3255         char* type_string = NULL;
3256
3257         if(type == AND)
3258                 q_handle->optimize_condition = 0;
3259
3260         type_string = _convert_condition_type_to_string(type);
3261         g_string_append_printf (q_handle->condition_text, " %s ", type_string);
3262         g_string_append_printf (q_handle->use_normal_field_key, " %s ", type_string);
3263         g_string_append_printf(q_handle->use_contact_table_id, " %s ", type_string);
3264
3265         return error_code;
3266 }
3267
3268 API query_error query_set_x_attribute(ADVANCED_HANDLE handle, int cts_custom_type_index, contact_x_attribute_e attribute)
3269 {
3270         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3271         ADVANCED_RETURN_VAL((attribute < X_DATA_MAX), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID X-ATTRIBUTE"));
3272
3273         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3274
3275         char* column_name = NULL;
3276         char* table_name = NULL;
3277         query_error error_code = QUERY_SUCCESS;
3278         char* loc = NULL;
3279         projection_t* projection_info = NULL;
3280
3281         ADVANCED_SVC_TRACE("x_attribute : %d", attribute);
3282
3283         q_handle->x_attribute_list[attribute] = 1;
3284
3285         column_name = _convert_x_attribute_to_column_name(attribute);
3286
3287         projection_info = g_new0(projection_t, 1);
3288         projection_info->field = g_strdup(column_name);
3289         q_handle->projection = g_list_append(q_handle->projection, projection_info);
3290
3291         error_code = _set_datatype_list_by_x_attribute(q_handle, cts_custom_type_index, attribute);
3292         ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_FAIL, ("X DATA_TYPE CONVERTING FAIL"));
3293
3294         if(table_name == NULL)
3295                 table_name = _convert_x_attribute_to_table_name(attribute);
3296
3297         if(q_handle->table_list->str[0] != '\0')
3298         {
3299                 loc = strstr(q_handle->table_list->str, table_name);
3300                 if(loc == NULL)
3301                         g_string_append_printf(q_handle->table_list, ", %s", table_name);
3302         }
3303         else
3304         {
3305                 g_string_append_printf(q_handle->table_list, "%s", table_name);
3306         }
3307
3308         tables_e table_type = _contact_table_name_to_table_type(table_name);
3309         q_handle->query_table = q_handle->query_table | table_type;
3310
3311         ADVANCED_SVC_TRACE("table list : %s", q_handle->table_list->str);
3312         q_handle->all_datatype_in_query = (q_handle->all_datatype_in_query | DATATYPE_X_ATTRIBUTE);
3313
3314 CATCH:
3315         return error_code;
3316 }
3317
3318 API query_error query_set_x_condition(ADVANCED_HANDLE handle, int cts_custom_type_index, contact_x_attribute_e attribute, condition_e attr, const char* value)
3319 {
3320         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3321         ADVANCED_RETURN_VAL((attribute < X_DATA_MAX), {}, QUERY_INVALID_ATTRIBUTE, ("INVALID X-ATTRIBUTE"));
3322         ADVANCED_RETURN_VAL((attr < CONDITION_MAX), {}, QUERY_INVALID_TYPE, ("INVALID CONTACT CONDITION"));
3323
3324         query_error error_code = QUERY_SUCCESS;
3325         char* column_name = NULL;
3326         char* condition = NULL;
3327         char* table = NULL;
3328         condition_table_t* table_info = NULL;
3329         int new_custom_type = 0;
3330         alias_key_e tmp_current_alias = ALIAS_INVALID;
3331
3332         if(attr == EXISTS)
3333         {
3334                 error_code = _set_exists_condition(handle, attribute);
3335                 return error_code;
3336         }
3337         ADVANCED_RETURN_VAL((value != NULL), {}, QUERY_NULL_PARAMETER, ("INVALID VALUE"));
3338
3339         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3340         ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
3341
3342         ADVANCED_SVC_TRACE("condition value : %s", value);
3343         condition = _convert_condition_to_string(attr);
3344
3345         column_name = _convert_x_attribute_to_condition_column_name(attribute);
3346         table = _convert_x_attribute_to_table_name(attribute);
3347         tables_e table_type = _contact_table_name_to_table_type(table);
3348         q_handle->query_table = q_handle->query_table | table_type;
3349
3350         if(q_handle->current_alias != ALIAS_INVALID)
3351         {
3352                 GList* tmp_condition_list = q_handle->condition_list;
3353                 while(tmp_condition_list)
3354                 {
3355                         condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
3356
3357                         if(tmp_info->datatype != cts_custom_type_index)
3358                         {
3359                                 new_custom_type = 1;
3360                         }else
3361                         {
3362                                 new_custom_type = 0;
3363                                 tmp_current_alias = tmp_info->current_alias;
3364                                 break;
3365                         }
3366                         tmp_condition_list = g_list_next(tmp_condition_list);
3367                 }
3368                 if(new_custom_type)
3369                 {
3370                         q_handle->current_alias++;
3371                         tmp_current_alias = q_handle->current_alias;
3372
3373                         table_info = g_new0(condition_table_t, 1);
3374                         table_info->alias = q_handle->current_alias;
3375                         table_info->table = g_strdup(table);
3376                         q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
3377                 }
3378         }else
3379         {
3380                 q_handle->current_alias++;
3381                 tmp_current_alias = q_handle->current_alias;
3382
3383                 table_info = g_new0(condition_table_t, 1);
3384                 table_info->alias = q_handle->current_alias;
3385                 table_info->table = g_strdup(table);
3386                 q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
3387         }
3388
3389         g_string_append_printf(q_handle->condition_text, "(%s.%s %s ? and %s.%s = %d)",
3390                 alias_key[tmp_current_alias], column_name, condition, alias_key[tmp_current_alias], condition_field_key[FIELD_DATA_DATATYPE_ID], cts_custom_type_index);
3391
3392         condition_list_t* condition_info = NULL;
3393         condition_info = g_new0(condition_list_t, 1);
3394         condition_info->current_alias = tmp_current_alias;
3395         condition_info->column_name = g_strdup(column_name);
3396         condition_info->condition = g_strdup(condition);
3397         condition_info->datatype = cts_custom_type_index;
3398
3399         q_handle->condition_list = g_list_append(q_handle->condition_list, condition_info);
3400
3401         condition_val_t* condition_txt = NULL;
3402         condition_txt = g_new0(condition_val_t, 1);
3403         condition_txt->value = g_strdup(value);
3404         condition_txt->x_attribute = 1;
3405         q_handle->condition_value = g_list_append(q_handle->condition_value, condition_txt);
3406
3407         q_handle->condition_datatype = q_handle->condition_datatype | DATATYPE_X_ATTRIBUTE;
3408
3409         return error_code;
3410 }
3411
3412 query_error query_set_limit(ADVANCED_HANDLE handle, int window_size)
3413 {
3414         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3415
3416         query_error error_code = QUERY_SUCCESS;
3417         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3418
3419         q_handle->start_pos = 0;
3420         q_handle->window_size = window_size;
3421
3422 return error_code;
3423 }
3424
3425 query_error query_get_next_limit_result_set(ADVANCED_HANDLE handle, int window_size)
3426 {
3427         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3428         ADVANCED_RETURN_VAL((window_size >= 0), {}, QUERY_INVALID_ATTRIBUTE, ("RETURN COUNT SHOULD BE POSITIVE VALUE"));
3429
3430         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3431         query_error error_code = QUERY_SUCCESS;
3432
3433         q_handle->start_pos = _free_for_limit_iter(q_handle);
3434         ADVANCED_SVC_TRACE("q_handle->start_pos : %d", q_handle->start_pos);
3435
3436         q_handle->limit_iter = 1;
3437         q_handle->total_result_set_count = 0;
3438         q_handle->pos = -1;
3439         q_handle->old_contact_id = 0;
3440         q_handle->return_array_pos = 0;
3441         ADVANCED_SVC_TRACE("q_handle->total_result_set_count : %d", q_handle->total_result_set_count);
3442
3443         q_handle->result_set = NULL;
3444         q_handle->window_size = window_size;
3445
3446         error_code = query_get_result_contact(handle);
3447
3448         return error_code;
3449 }
3450
3451 API query_error query_next_custom_type(ADVANCED_HANDLE handle)
3452 {
3453         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3454         query_error error_code = QUERY_SUCCESS;
3455         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3456         ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->return_array_pos].contact_info != NULL), {}, QUERY_NO_RECORD, ("CONTACT INFO IS NULL"));
3457
3458         q_handle->x_fetching = NEXT_ROW_FETCH;
3459
3460         GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->x_list;
3461         if(tmp_list)
3462         {
3463                 x_info_t* x_info = (x_info_t*)tmp_list->data;
3464                 _free_x_info(q_handle->result_set[q_handle->return_array_pos].contact_info->x_info);
3465
3466                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info = g_new0(x_info_t, 1);
3467                 ADVANCED_CATCH_SET_ERROR((q_handle->result_set[q_handle->return_array_pos].contact_info->x_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
3468
3469                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->datatype_id = x_info->datatype_id;
3470                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data1 = g_strdup(x_info->data1);
3471
3472                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data2 = g_strdup(x_info->data2);
3473                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data3 = g_strdup(x_info->data3);
3474                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data4 = g_strdup(x_info->data4);
3475                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data5 = g_strdup(x_info->data5);
3476                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data6 = g_strdup(x_info->data6);
3477                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data7 = g_strdup(x_info->data7);
3478                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data8 = g_strdup(x_info->data8);
3479                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data9 = g_strdup(x_info->data9);
3480                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->data10 = g_strdup(x_info->data10);
3481
3482                 _free_x_info((x_info_t*)tmp_list->data);
3483                 q_handle->result_set[q_handle->return_array_pos].contact_info->x_list = g_list_next(tmp_list);
3484         }
3485         else
3486         {
3487                 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
3488         }
3489         ADVANCED_SVC_TRACE("error_code : %d", error_code);
3490
3491 CATCH:
3492         return error_code;
3493 }
3494
3495 API char* query_get_x_attribute(ADVANCED_HANDLE handle, contact_x_attribute_e attribute, int* cts_custom_type_index, query_error* error_code)
3496 {
3497         ADVANCED_RETURN_VAL((handle != NULL), {}, NULL, ("HANDLE IS NULL"));
3498         ADVANCED_RETURN_VAL((attribute < X_DATA_MAX), {}, NULL, ("INVALID ATTRIBUTE"));
3499
3500         char* value = NULL;
3501         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3502         ADVANCED_SVC_TRACE("q_handle->return_array_pos : %d", q_handle->return_array_pos);
3503         ADVANCED_RETURN_VAL((q_handle->return_array_pos < q_handle->total_result_set_count), {}, NULL, ("RESULT SET IS NULL"));
3504         ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->return_array_pos].contact_info != NULL), {}, NULL, ("CONTACT INFO IS NULL"));
3505
3506         if(q_handle->x_fetching != NEXT_ROW_FETCH)
3507                 q_handle->x_fetching = INIT_FETCH;
3508
3509         if(q_handle->x_fetching == INIT_FETCH)
3510         {
3511                 *error_code = query_next_custom_type(handle);
3512                 ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, *error_code, ("query_next_custom_type ERROR "));
3513         }
3514
3515         value = _get_x_attribute_value(q_handle, attribute, error_code);
3516         ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, *error_code, ("ERROR "));
3517
3518         *cts_custom_type_index = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->datatype_id;
3519
3520         ADVANCED_SVC_TRACE("*cts_custom_type_index : %d", *cts_custom_type_index);
3521         ADVANCED_SVC_TRACE("value : %s", value);
3522
3523 CATCH:
3524         return value;
3525 }
3526
3527 API query_error query_set_condition_with_indices_begin(ADVANCED_HANDLE handle)
3528 {
3529         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3530         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3531         query_error error_code = QUERY_SUCCESS;
3532
3533         ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
3534
3535         g_string_append_printf(q_handle->id_list, "(");
3536
3537         return error_code;
3538 }
3539
3540 API query_error query_set_condition_with_indices_end(ADVANCED_HANDLE handle)
3541 {
3542         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3543
3544         query_error error_code = QUERY_SUCCESS;
3545         //predefine_datatype datatype = DATATYPE_INVALID;
3546         //db_datatype db_datatype_id = DB_DATATYPE_INVALID;
3547         char* column_name = NULL;
3548         char* normal_column_name = NULL;
3549         //char* condition = NULL;
3550         char* table = NULL;
3551         condition_table_t* table_info = NULL;
3552         alias_key_e tmp_current_alias = ALIAS_INVALID;
3553
3554         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3555         ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
3556
3557         g_string_append_printf(q_handle->id_list, ")");
3558
3559         column_name = _convert_attribute_to_condition_column_name(CONTACT_ID);
3560         normal_column_name = _convert_attribute_to_column_name(CONTACT_ID);
3561
3562         table = _convert_attribute_to_table_name(CONTACT_ID);
3563
3564         q_handle->current_alias++;
3565         tmp_current_alias = q_handle->current_alias;
3566
3567         table_info = g_new0(condition_table_t, 1);
3568         table_info->alias = q_handle->current_alias;
3569         table_info->table = g_strdup(table);
3570         q_handle->condition_table_list = g_list_append(q_handle->condition_table_list, table_info);
3571
3572         q_handle->optimize_condition = 0;
3573         g_string_append_printf(q_handle->use_contact_table_id, "(%s in %s)", field_key[FIELD_CONTACT_CONTACT_ID], q_handle->id_list->str);
3574         g_string_append_printf(q_handle->use_normal_field_key, "(%s in %s)", normal_column_name, q_handle->id_list->str);
3575
3576         g_string_append_printf(q_handle->condition_text, "(%s.%s in %s)", alias_key[tmp_current_alias], column_name, q_handle->id_list->str);
3577
3578         condition_val_t* condition_txt = NULL;
3579         condition_txt = g_new0(condition_val_t, 1);
3580
3581         condition_txt->attribute = CONTACT_ID;
3582         condition_txt->indices_filter = 1;
3583
3584         q_handle->condition_value = g_list_append(q_handle->condition_value, condition_txt);
3585
3586         return error_code;
3587 }
3588
3589 API query_error query_set_condition_with_indices(ADVANCED_HANDLE handle, const char* contact_index)
3590 {
3591         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3592         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3593         query_error error_code = QUERY_SUCCESS;
3594
3595         if(q_handle->is_first_id_in_list == 0)
3596         {
3597                 q_handle->is_first_id_in_list = 1;
3598                 g_string_append_printf(q_handle->id_list, "%s", contact_index);
3599         }else
3600         {
3601                 g_string_append_printf(q_handle->id_list, ", %s", contact_index);
3602         }
3603
3604         return error_code;
3605 }
3606
3607 API int get_contact_version()
3608 {
3609         char query[128] = {0, };
3610         int rc = -1;
3611         stmt pStmt = NULL;
3612         int ver = 0;
3613         query_error error_code = QUERY_SUCCESS;
3614
3615         error_code = _contact_db_init();
3616         ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3617
3618         snprintf(query, 128, "%s", "select max(ver) from cts_version");
3619
3620         pStmt = _contact_query_prepare(query);
3621         ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3622
3623         rc = _contact_query_step(pStmt);
3624         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3625
3626         ver = _ct_query_column_int(pStmt, 0);
3627         ADVANCED_SVC_TRACE("get_contact_version :: ver : %d ", ver);
3628
3629         CATCH:
3630         _ct_query_finalize(pStmt);
3631         _contact_db_finish();
3632
3633         return ver;
3634 }
3635
3636 API int set_number_pref_to_condition(ADVANCED_HANDLE handle)
3637 {
3638         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3639         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3640
3641         q_handle->set_number_pref = 1;
3642
3643         return QUERY_SUCCESS;
3644 }
3645
3646 API int set_email_pref_to_condition(ADVANCED_HANDLE handle)
3647 {
3648         ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3649         advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3650
3651         q_handle->set_email_pref = 1;
3652
3653         return QUERY_SUCCESS;
3654 }
3655
3656 API int check_pref_number(int contact_id, char* number)
3657 {
3658         char query[256 + 1] = {0, };
3659         int rc = -1;
3660         stmt pStmt = NULL;
3661         int pref = 0;
3662         query_error error_code = QUERY_SUCCESS;
3663
3664         error_code = _contact_db_init();
3665         ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3666
3667         snprintf(query, 256, "%s", "select is_default from data where contact_id = ? and data3 = ? and datatype = 8");
3668
3669         pStmt = _contact_query_prepare(query);
3670         ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3671
3672         _contact_query_bind_int(pStmt, 1, contact_id);
3673         _contact_query_bind_text(pStmt, 2, number);
3674
3675         rc = _contact_query_step(pStmt);
3676         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3677
3678         pref = _ct_query_column_int(pStmt, 0);
3679         ADVANCED_SVC_TRACE("pref: %d ", pref);
3680
3681 CATCH:
3682         _ct_query_finalize(pStmt);
3683         _contact_db_finish();
3684
3685         return pref;
3686 }
3687
3688 API int check_pref_email(int contact_id, char* email)
3689 {
3690         char query[256 + 1] = {0, };
3691         int rc = -1;
3692         stmt pStmt = NULL;
3693         int pref = 0;
3694         query_error error_code = QUERY_SUCCESS;
3695
3696         error_code = _contact_db_init();
3697         ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3698
3699         snprintf(query, 256, "%s", "select is_default from data where contact_id = ? and data3 = ? and datatype = 9");
3700
3701         pStmt = _contact_query_prepare(query);
3702         ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3703
3704         _contact_query_bind_int(pStmt, 1, contact_id);
3705         _contact_query_bind_text(pStmt, 2, email);
3706
3707         rc = _contact_query_step(pStmt);
3708         ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3709
3710         pref = _ct_query_column_int(pStmt, 0);
3711         ADVANCED_SVC_TRACE("pref: %d ", pref);
3712
3713 CATCH:
3714         _ct_query_finalize(pStmt);
3715         _contact_db_finish();
3716
3717         return pref;
3718 }
3719