2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 #include "query-service.h"
25 #include "query-type.h"
26 #include "query-utility.h"
31 #include <contacts-svc.h>
39 GList* g_addressbook_list = NULL;
40 int total_addressbook_id = 0;
42 static int list_cb(CTSvalue *ab, void *user_data)
44 addressbook_list* list = NULL;
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);
50 g_addressbook_list = g_list_append(g_addressbook_list, list);
51 total_addressbook_id++;
56 void __free_addressbook_list(gpointer data, gpointer user_data)
58 ADVANCED_SVC_TRACE("__free_address_list");
62 API void refresh_addressbook_list(void *data)
64 g_list_foreach(g_addressbook_list, __free_addressbook_list, NULL);
65 g_list_free(g_addressbook_list);
66 g_addressbook_list = NULL;
68 contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
72 API int find_addressbook_id(int account_id, query_error *error_code)
75 int addressbook_id = -1;
77 ADVANCED_SVC_TRACE("account_id : %d", account_id);
82 if(g_addressbook_list == NULL)
84 ADVANCED_SVC_TRACE("GENERATE ADDRESSBOOK LIST");
85 ret = contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
88 GList* tmp_list = g_addressbook_list;
91 addressbook_list* info = (addressbook_list*)tmp_list->data;
92 if(info->account_id == account_id)
94 addressbook_id = info->addressbook_id;
97 tmp_list = g_list_next(tmp_list);
99 if(addressbook_id == -1)
100 *error_code = QUERY_NO_RECORD;
102 *error_code = QUERY_SUCCESS;
104 ADVANCED_SVC_TRACE("addressbook_id : %d", addressbook_id);
106 return addressbook_id;
109 API int* get_all_addressbook_id(int* total_count)
114 if(g_addressbook_list == NULL)
116 ADVANCED_SVC_TRACE("GENERATE ADDRESSBOOK LIST");
117 contacts_svc_list_foreach(CTS_LIST_ALL_ADDRESSBOOK, list_cb, NULL);
120 id_list = g_new0(int, (total_addressbook_id + 2));
123 GList* tmp_list = g_addressbook_list;
127 addressbook_list* info = (addressbook_list*)tmp_list->data;
129 id_list[i] = info->addressbook_id;
130 tmp_list = g_list_next(tmp_list);
133 *total_count = total_addressbook_id + 1;
138 int _value_to_projection(stmt hstmt, char** projection, int count)
144 if((*projection) == NULL)
145 *projection = (char*)g_strdup(_ct_query_column_text(hstmt, pos++));
152 void __convert_number_type_to_str(int db_value, GString* type_str)
154 if((db_value & HOME) == HOME)
155 g_string_append_printf (type_str, "%s", number_type_str[1]);
157 if((db_value & WORK) == WORK)
159 if(type_str->str[0] == '\0')
160 g_string_append_printf (type_str, "%s", number_type_str[2]);
162 g_string_append_printf(type_str, ",%s", number_type_str[2]);
165 if((db_value & VOICE) == VOICE)
167 if(type_str->str[0] == '\0')
168 g_string_append_printf (type_str, "%s", number_type_str[3]);
170 g_string_append_printf(type_str, ",%s", number_type_str[3]);
173 if((db_value & FAX) == FAX)
175 if(type_str->str[0] == '\0')
176 g_string_append_printf (type_str, "%s", number_type_str[4]);
178 g_string_append_printf(type_str, ",%s", number_type_str[4]);
181 if((db_value & MSG) == MSG)
183 if(type_str->str[0] == '\0')
184 g_string_append_printf (type_str, "%s", number_type_str[5]);
186 g_string_append_printf(type_str, ",%s", number_type_str[5]);
189 if((db_value & CELL) == CELL)
191 if(type_str->str[0] == '\0')
192 g_string_append_printf (type_str, "%s", number_type_str[6]);
194 g_string_append_printf(type_str, ",%s", number_type_str[6]);
197 if((db_value & PAGER) == PAGER)
199 if(type_str->str[0] == '\0')
200 g_string_append_printf (type_str, "%s", number_type_str[7]);
202 g_string_append_printf(type_str, ",%s", number_type_str[7]);
205 if((db_value & BBS) == BBS)
207 if(type_str->str[0] == '\0')
208 g_string_append_printf (type_str, "%s", number_type_str[8]);
210 g_string_append_printf(type_str, ",%s", number_type_str[8]);
213 if((db_value & MODEM) == MODEM)
215 if(type_str->str[0] == '\0')
216 g_string_append_printf (type_str, "%s", number_type_str[9]);
218 g_string_append_printf(type_str, ",%s", number_type_str[9]);
221 if((db_value & CAR) == CAR)
223 if(type_str->str[0] == '\0')
224 g_string_append_printf (type_str, "%s", number_type_str[10]);
226 g_string_append_printf(type_str, ",%s", number_type_str[10]);
229 if((db_value & ISDN) == ISDN)
231 if(type_str->str[0] == '\0')
232 g_string_append_printf (type_str, "%s", number_type_str[11]);
234 g_string_append_printf(type_str, ",%s", number_type_str[11]);
237 if((db_value & VIDEO) == VIDEO)
239 if(type_str->str[0] == '\0')
240 g_string_append_printf (type_str, "%s", number_type_str[12]);
242 g_string_append_printf(type_str, ",%s", number_type_str[12]);
245 if((db_value & PCS) == PCS)
247 if(type_str->str[0] == '\0')
248 g_string_append_printf (type_str, "%s", number_type_str[13]);
250 g_string_append_printf(type_str, ",%s", number_type_str[13]);
253 if(type_str->str[0] == '\0')
254 g_string_append_printf (type_str, "%s", number_type_str[3]);
258 void __convert_email_type_to_str(int db_value, GString* type_str)
260 if((db_value & EMAIL_HOME) == EMAIL_HOME)
262 if(type_str->str[0] == '\0')
263 g_string_append_printf (type_str, "%s", email_type_str[1]);
265 g_string_append_printf (type_str, ",%s", email_type_str[1]);
267 if((db_value & EMAIL_WORK) == EMAIL_WORK)
269 if(type_str->str[0] == '\0')
270 g_string_append_printf (type_str, "%s", email_type_str[2]);
272 g_string_append_printf (type_str, ",%s", email_type_str[2]);
275 if(type_str->str[0] == '\0')
276 g_string_append_printf (type_str, "%s", email_type_str[1]);
279 void __convert_url_type_to_str(int db_value, GString* type_str)
281 if(db_value == URL_HOME)
283 if(type_str->str[0] == '\0')
284 g_string_append_printf (type_str, "%s", url_type_str[URL_HOME]);
286 g_string_append_printf (type_str, ",%s", url_type_str[URL_HOME]);
287 }else if(db_value == URL_WORK)
289 if(type_str->str[0] == '\0')
290 g_string_append_printf (type_str, "%s", url_type_str[URL_WORK]);
292 g_string_append_printf (type_str, ",%s", url_type_str[URL_WORK]);
295 if(type_str->str[0] == '\0')
296 g_string_append_printf (type_str, "%s", url_type_str[URL_HOME]);
299 void __convert_event_type_to_str(int db_value, GString* type_str)
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]);
306 if(type_str->str[0] == '\0')
307 g_string_append_printf (type_str, "%s", event_type_str[EVENT_BIRTHDAY]);
310 void __convert_address_type_to_str(int db_value, GString* type_str)
312 if((db_value & ADDRESS_TYPE_HOME) == ADDRESS_TYPE_HOME)
314 if(type_str->str[0] == '\0')
315 g_string_append_printf (type_str, "%s", addr_type_str[1]);
317 g_string_append_printf (type_str, ",%s", addr_type_str[1]);
319 if((db_value & ADDRESS_TYPE_WORK) == ADDRESS_TYPE_WORK)
321 if(type_str->str[0] == '\0')
322 g_string_append_printf (type_str, "%s", addr_type_str[2]);
324 g_string_append_printf (type_str, ",%s", addr_type_str[2]);
326 if((db_value & ADDRESS_TYPE_DOM) == ADDRESS_TYPE_DOM)
328 if(type_str->str[0] == '\0')
329 g_string_append_printf (type_str, "%s", addr_type_str[3]);
331 g_string_append_printf (type_str, ",%s", addr_type_str[3]);
333 if((db_value & ADDRESS_TYPE_INTL) == ADDRESS_TYPE_INTL)
335 if(type_str->str[0] == '\0')
336 g_string_append_printf (type_str, "%s", addr_type_str[4]);
338 g_string_append_printf (type_str, ",%s", addr_type_str[4]);
340 if((db_value & ADDRESS_TYPE_POSTAL) == ADDRESS_TYPE_POSTAL)
342 if(type_str->str[0] == '\0')
343 g_string_append_printf (type_str, "%s", addr_type_str[5]);
345 g_string_append_printf (type_str, ",%s", addr_type_str[5]);
347 if((db_value & ADDRESS_TYPE_PARCEL) == ADDRESS_TYPE_PARCEL)
349 if(type_str->str[0] == '\0')
350 g_string_append_printf (type_str, "%s", addr_type_str[6]);
352 g_string_append_printf (type_str, ",%s", addr_type_str[6]);
355 if(type_str->str[0] == '\0')
356 g_string_append_printf (type_str, "%s", addr_type_str[1]);
359 void __convert_x_info_to_str(int db_value, GString* type_str)
361 g_string_append_printf (type_str, "%d", db_value);
364 void __convert_is_favorite_to_str(int db_value, GString* type_str)
367 g_string_append_printf (type_str, "%s", is_favorite_str[1]);
369 g_string_append_printf (type_str, "%s", is_favorite_str[0]);
372 int _int_value_to_projection(tables_e table, db_datatype datatype, stmt hstmt, char** projection, int count)
376 GString* type_str = g_string_new ("");
380 if((*projection) == NULL)
382 db_value = _ct_query_column_int(hstmt, pos++);
383 if(table == DATA_TABLE)
387 case DB_DATATYPE_NUMBER:
388 __convert_number_type_to_str(db_value, type_str);
390 case DB_DATATYPE_EMAIL:
391 __convert_email_type_to_str(db_value, type_str);
393 case DB_DATATYPE_URL:
394 __convert_url_type_to_str(db_value, type_str);
396 case DB_DATATYPE_EVENT:
397 __convert_event_type_to_str(db_value, type_str);
399 case DB_DATATYPE_ADDRESS:
400 __convert_address_type_to_str(db_value, type_str);
403 __convert_x_info_to_str(db_value, type_str);
406 }else if(table == CONTACT_TABLE)
408 __convert_is_favorite_to_str(db_value, type_str);
409 }else if(table == PERSON_TABLE)
411 __convert_is_favorite_to_str(db_value, type_str);
414 *projection = (char*)g_strdup(type_str->str);
418 g_string_free(type_str, TRUE);
423 int _event_value_to_projection(stmt hstmt, char** projection, int count)
427 GString* type_str = g_string_new ("");
431 if((*projection) == NULL)
433 db_value = _ct_query_column_int(hstmt, pos++);
434 g_string_append_printf (type_str, "%d", db_value);
436 *projection = (char*)g_strdup(type_str->str);
440 g_string_free(type_str, TRUE);
445 target_table_e _check_join_case(advanced_handle_t* q_handle)
447 ADVANCED_RETURN_VAL((q_handle->query_table != INVALID_TABLE), {}, INVALID_TABLE_LIST, ("ERROR"));
448 target_table_e value = INVALID_TABLE_LIST;
450 if(strcmp(q_handle->table_list->str, table_key[TABLE_PERSON]) == 0)
453 }else if(strcmp(q_handle->table_list->str, table_key[TABLE_CONTACT]) == 0)
455 value = ONLY_CONTACT;
456 }else if(strcmp(q_handle->table_list->str, table_key[TABLE_DATA]) == 0)
458 if(q_handle->query_type == QUERY_TO_CONTACT)
460 else if(q_handle->query_type == QUERY_TO_PERSON)
461 value = ONLY_DATA_FROM_PERSON_QUERY;
464 if(value != INVALID_TABLE_LIST)
467 ADVANCED_SVC_TRACE("!! q_handle->query_table : %d", q_handle->query_table);
469 if(((q_handle->query_table & PERSON_TABLE) != 0) && ((q_handle->query_table & CONTACT_TABLE) == 0) && ((q_handle->query_table & DATA_TABLE) != 0))
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))
474 value = JOIN_CONTACT_DATA;
477 ADVANCED_SVC_TRACE("value is %d", value);
482 query_error _make_contact_info(advanced_handle_t* q_handle, projection_type type, target_table_e join_table)
484 query_error error_code = QUERY_SUCCESS;
486 db_datatype datatype_id = DB_DATATYPE_INVALID;
488 char projection[PROJECTION_FIELD_LEN + 1] = {0, };
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;
499 if(type == ONLY_CONTACT_ID_FIELD)
501 contact_id = _ct_query_column_int(q_handle->hstmt, count++);
504 if(join_table == ONLY_PERSON || join_table == ONLY_CONTACT)
506 contact_id = _ct_query_column_int(q_handle->hstmt, count++);
509 datatype_id = _ct_query_column_int(q_handle->hstmt, count++);
510 contact_id = _ct_query_column_int(q_handle->hstmt, count++);
514 if(q_handle->window_size > 0)
516 if(q_handle->start_pos >= contact_id)
517 return QUERY_SUCCESS;
520 if(q_handle->old_contact_id != contact_id)
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);
527 if(join_table == ONLY_PERSON || join_table == JOIN_PERSON_DATA)
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);
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);
537 q_handle->result_set[q_handle->pos].is_occupied = 1;
538 q_handle->old_contact_id = contact_id;
540 q_handle->mapping_id_to_result_set[q_handle->pos].contact_id = contact_id;
541 q_handle->total_result_set_count++;
544 if((q_handle->window_size > 0) && (q_handle->total_result_set_count > q_handle->window_size))
546 q_handle->total_result_set_count--;
547 return QUERY_SET_LIMIT;
552 case DB_DATATYPE_NAME :
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"));
559 case DB_DATATYPE_NUMBER:
561 num_info = g_new0(number_t, 1);
562 ADVANCED_CATCH_SET_ERROR((num_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
565 case DB_DATATYPE_EMAIL:
567 email_info = g_new0(email_t, 1);
568 ADVANCED_CATCH_SET_ERROR((email_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
571 case DB_DATATYPE_URL:
573 url_info = g_new0(url_t, 1);
574 ADVANCED_CATCH_SET_ERROR((url_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
577 case DB_DATATYPE_NICK_NAME:
579 nick_info = g_new0(nick_name_t, 1);
580 ADVANCED_CATCH_SET_ERROR((nick_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
583 case DB_DATATYPE_ADDRESS:
585 address_info = g_new0(address_t, 1);
586 ADVANCED_CATCH_SET_ERROR((address_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
589 case DB_DATATYPE_ORGANIZATION:
591 organization_info = g_new0(organization_t, 1);
592 ADVANCED_CATCH_SET_ERROR((organization_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
595 case DB_DATATYPE_EVENT:
597 event_info = g_new0(event_info_t, 1);
598 ADVANCED_CATCH_SET_ERROR((event_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
603 if(datatype_id > DB_DATATYPE_MAX)
605 x_info = g_new0(x_info_t, 1);
606 ADVANCED_CATCH_SET_ERROR((x_info != NULL), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
613 GList* tmp_projection = q_handle->new_projection;
614 while(tmp_projection)
616 projection_t* projection_info = (projection_t*)tmp_projection->data;
617 memset(projection, 0x00, sizeof(projection));
619 g_strlcpy(projection, projection_info->field, sizeof(projection) - 1);
621 tmp_projection = g_list_next(tmp_projection);
626 case DB_DATATYPE_NAME :
628 if(strcmp(field_key[FIELD_DATA2], projection) == 0 && q_handle->attribute_list[NAME_FIRST] == 1)
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)
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)
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)
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)
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)
645 count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->name_info->display_name), count);
650 case DB_DATATYPE_NUMBER:
652 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[NUMBER_ADDRESS] == 1)
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)
657 count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_NUMBER, q_handle->hstmt, &(num_info->number_type), count);
661 case DB_DATATYPE_EMAIL:
663 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[EMAIL_ADDRESS] == 1)
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)
668 count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_EMAIL, q_handle->hstmt, &(email_info->email_type), count);
673 case DB_DATATYPE_URL:
675 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[URL_ADDRESS] == 1)
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)
680 count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_URL, q_handle->hstmt, &(url_info->url_type), count);
684 case DB_DATATYPE_NICK_NAME:
686 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[NICKNAME_NAME] == 1)
688 count = _value_to_projection(q_handle->hstmt, &(nick_info->nick_name), count);
692 case DB_DATATYPE_ADDRESS:
694 if(strcmp(field_key[FIELD_DATA9], projection) == 0 && q_handle->attribute_list[ADDRESS_COUNTRY] == 1)
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)
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)
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)
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)
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)
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)
714 count = _int_value_to_projection(DATA_TABLE, DB_DATATYPE_ADDRESS, q_handle->hstmt, &(address_info->type), count);
718 case DB_DATATYPE_ORGANIZATION:
720 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[ORGANIZATION_NAME] == 1)
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)
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)
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)
731 count = _value_to_projection(q_handle->hstmt, &(organization_info->role), count);
735 case DB_DATATYPE_EVENT:
737 if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->attribute_list[EVENT_TYPE] == 1)
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)
742 count = _event_value_to_projection(q_handle->hstmt, &(event_info->date), count);
746 case DB_DATATYPE_NOTE:
748 if(strcmp(field_key[FIELD_DATA3], projection) == 0 && q_handle->attribute_list[CONTACT_NOTE] == 1)
750 count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->note), count);
756 if(datatype_id > DB_DATATYPE_MAX)
758 if(strcmp(field_key[FIELD_DATA1], projection) == 0 && q_handle->x_attribute_list[X_DATA1] == 1)
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)
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)
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)
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)
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)
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)
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)
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)
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)
787 count = _value_to_projection(q_handle->hstmt, &(x_info->data10), count);
795 if(join_table == ONLY_PERSON || join_table == JOIN_PERSON_DATA)
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);
830 if(strcmp(field_key[FIELD_RINGTONE], projection) == 0)
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)
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)
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)
846 int db_value = _ct_query_column_int(q_handle->hstmt, count++);
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);
852 }else if(strcmp(field_key[FIELD_UID], projection) == 0)
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)
857 count = _value_to_projection(q_handle->hstmt, &(q_handle->result_set[q_handle->pos].contact_info->raw_contact_info->photo_uri), count);
861 if(count == old_count)
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);
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);
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);
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);
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);
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);
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);
890 if(datatype_id > DB_DATATYPE_MAX)
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);
903 API ADVANCED_HANDLE query_init(query_error* error_code, query_type_e query_type)
905 ADVANCED_RETURN_VAL((query_type >= QUERY_TO_PERSON), {}, NULL, ("INVALID QUERY TYPE"));
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"));
911 *error_code = _contact_db_init();
912 ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
915 handle->query_type = query_type;
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 ("");
925 ADVANCED_SVC_TRACE("QUERY INIT SUCCESS");
928 if(*error_code != QUERY_SUCCESS)
930 _free_handle_info(handle);
934 return (ADVANCED_HANDLE)handle;
937 query_error _check_extra_contact_info(advanced_handle_t* q_handle, extra_info_type type, contact_attribute_e attribute)
939 query_error error_code = QUERY_FAIL;
941 if(type == EXTRA_INFO_TYPE_FIELD)
943 if(attribute == CATEGORY_INFO)
945 q_handle->extra_info = q_handle->extra_info | CATEGORIES;
946 error_code = QUERY_SUCCESS;
948 }else if(type == EXTRA_INFO_TYPE_CONDITION)
950 if(attribute == CATEGORY_INFO)
952 q_handle->condition_extra_info = q_handle->condition_extra_info | CATEGORIES;
953 error_code = QUERY_SUCCESS;
960 API query_error query_set_attribute(ADVANCED_HANDLE handle, contact_attribute_e attribute)
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"));
965 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
967 char* column_name = NULL;
968 char* table_name = NULL;
969 query_error error_code = QUERY_SUCCESS;
971 projection_t* projection_info = NULL;
973 ADVANCED_SVC_TRACE("attribute : %d", attribute);
974 q_handle->attribute_list[attribute] = 1;
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;
981 if(q_handle->query_type == QUERY_TO_PERSON)
983 column_name = _convert_person_attribute_to_column_name(attribute);
984 if(column_name != NULL)
985 q_handle->person_field = attribute;
988 if(column_name == NULL)
989 column_name = _convert_attribute_to_column_name(attribute);
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);
995 if(q_handle->query_type == QUERY_TO_PERSON)
996 table_name = _convert_person_attribute_to_table_name(attribute);
998 if((attribute > CONTACT_LAST_UPDATED_TIME_STAMP) && (table_name == NULL))
1000 error_code = _set_datatype_list(q_handle, attribute);
1001 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_FAIL, ("DATA_TYPE CONVERTING FAIL"));
1004 if(table_name == NULL)
1005 table_name = _convert_attribute_to_table_name(attribute);
1007 if(q_handle->table_list->str[0] != '\0')
1009 loc = strstr(q_handle->table_list->str, table_name);
1011 g_string_append_printf(q_handle->table_list, ", %s", table_name);
1015 g_string_append_printf(q_handle->table_list, "%s", table_name);
1018 tables_e table_type = _contact_table_name_to_table_type(table_name);
1019 q_handle->query_table = q_handle->query_table | table_type;
1021 ADVANCED_SVC_TRACE("table list : %s", q_handle->table_list->str);
1027 query_error _construct_order_query(advanced_handle_t* q_handle, GString* query, target_table_e join_table)
1029 predefine_datatype datatype = DATATYPE_INVALID;
1030 db_datatype db_datatype_id = DB_DATATYPE_NAME;
1031 query_error error_code = QUERY_SUCCESS;
1033 datatype = _convert_attribute_to_mimetype_id(q_handle->order_field);
1034 db_datatype_id = _convert_datatype_to_db_type(datatype);
1036 if(q_handle->query_type == QUERY_TO_CONTACT)
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);
1046 case JOIN_CONTACT_DATA :
1048 if(db_datatype_id == DB_DATATYPE_INVALID)
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);
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);
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);
1070 case ONLY_DATA_FROM_PERSON_QUERY :
1071 case JOIN_PERSON_DATA :
1073 if(db_datatype_id == DB_DATATYPE_INVALID)
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);
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);
1087 ADVANCED_CATCH_SET_ERROR(query->str[0] != '\0', {}, QUERY_INVALID_ATTRIBUTE, ("DO NOT SUPPORT CASE"));
1093 int _make_order_list(advanced_handle_t* q_handle, int count, target_table_e join_table)
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"));
1103 query_error error_code = QUERY_SUCCESS;
1105 ADVANCED_SVC_TRACE("q_handle->order : %s", q_handle->order->str);
1106 query = g_string_new ("");
1108 if(q_handle->limit_iter == 1)
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"));
1113 for(j=0; j<q_handle->total_count; j++)
1115 if(order_pos >= q_handle->total_result_set_count)
1120 high = q_handle->total_result_set_count;
1125 i = ( low + high ) / 2;
1127 if(q_handle->mapping_id_to_result_set[i].contact_id > q_handle->full_ordered_contact_id[j].contact_id)
1129 else if(q_handle->mapping_id_to_result_set[i].contact_id < q_handle->full_ordered_contact_id[j].contact_id)
1131 else if(q_handle->mapping_id_to_result_set[i].contact_id == q_handle->full_ordered_contact_id[j].contact_id)
1133 q_handle->ordering_list[order_pos].ordered_pos = i;
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"));
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"));
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"));
1150 q_handle->hstmt = _contact_query_prepare(query->str);
1151 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1153 rc = _contact_query_step(q_handle->hstmt);
1154 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_ORDERING_FAIL, ("No record in database"));
1156 while(rc == SQLITE_ROW)
1158 if(q_handle->total_count >= count)
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++;
1165 q_handle->total_count = count;
1166 _ct_query_finalize(q_handle->hstmt);
1168 for(j=0; j<q_handle->total_count; j++)
1170 if(order_pos >= q_handle->total_result_set_count)
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)
1177 high = q_handle->total_result_set_count;
1182 i = ( low + high ) / 2;
1184 if(q_handle->mapping_id_to_result_set[i].contact_id > q_handle->full_ordered_contact_id[j].contact_id)
1186 else if(q_handle->mapping_id_to_result_set[i].contact_id < q_handle->full_ordered_contact_id[j].contact_id)
1188 else if(q_handle->mapping_id_to_result_set[i].contact_id == q_handle->full_ordered_contact_id[j].contact_id)
1190 q_handle->ordering_list[order_pos].ordered_pos = i;
1198 q_handle->return_array_pos = q_handle->ordering_list[0].ordered_pos;
1202 g_string_free (query, TRUE);
1206 query_error _make_contact_id_list_by_condition_part(advanced_handle_t* q_handle, int count, GString* sub_query)
1208 ADVANCED_RETURN_VAL((q_handle->condition_text->str[0] != '\0'), {}, QUERY_SUCCESS, ("NO CONDITION PART"));
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;
1218 tmp_query = g_string_new("");
1220 ADVANCED_SVC_TRACE("CHECK CONDITION PART !! ");
1222 GList* tmp_table = q_handle->condition_table_list;
1225 condition_table_t* table_info = (condition_table_t*)tmp_table->data;
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)
1233 if(q_handle->query_type == QUERY_TO_PERSON)
1234 id_field = condition_field_key[FIELD_DATA_PERSON_ID];
1236 id_field = condition_field_key[FIELD_DATA_CONTACT_ID];
1238 if(data_table_alias == ALIAS_INVALID)
1239 data_table_alias = table_info->alias;
1242 if(q_handle->optimize_condition == 1)
1244 if(sub_query->str[0] == '\0')
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;
1253 if(sub_query->str[0] == '\0')
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;
1260 g_string_append_printf (sub_query, ", %s %s", table_info->table, alias_key[table_info->alias]);
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);
1266 alias = table_info->alias;
1267 tmp_table = g_list_next(tmp_table);
1270 g_string_append_printf (sub_query, " where");
1272 if(q_handle->set_number_pref == 1)
1274 GList* tmp_table1 = q_handle->condition_table_list;
1277 condition_table_t* table_info1 = (condition_table_t*)tmp_table1->data;
1278 if(strcmp(table_info1->table, "data") == 0)
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]);
1283 tmp_table1 = g_list_next(tmp_table1);
1287 if(q_handle->set_email_pref == 1)
1289 GList* tmp_table2 = q_handle->condition_table_list;
1292 condition_table_t* table_info2 = (condition_table_t*)tmp_table2->data;
1293 if(strcmp(table_info2->table, "data") == 0)
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]);
1298 tmp_table2 = g_list_next(tmp_table2);
1302 if(tmp_query->str[0] != '\0')
1303 g_string_append_printf (sub_query, " %s", tmp_query->str);
1305 if((q_handle->optimize_condition == 1) && (q_handle->condition_list && q_handle->current_alias > ALIAS_A))
1307 GList* tmp_condition = q_handle->condition_list;
1309 while(tmp_condition)
1311 condition_list_t* condition_info = (condition_list_t*)tmp_condition->data;
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);
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);
1326 tmp_condition = g_list_next(tmp_condition);
1328 g_string_append_printf(sub_query, " ))");
1331 g_string_append_printf (sub_query, " (%s)", q_handle->condition_text->str);
1333 g_string_free (tmp_query, TRUE);
1337 void _construct_query(target_table_e value, advanced_handle_t* q_handle, GString* query, char* condtion_sub_query, projection_type type)
1340 GString* contact_condition;
1342 int use_sub_query = 1;
1344 condition = g_string_new("");
1345 contact_condition = g_string_new("");
1347 if(q_handle->condition_datatype == DATATYPE_INVALID && q_handle->condition_category == NULL)
1350 GList* tmp_datatype_list = q_handle->datatype_list;
1351 while(tmp_datatype_list)
1353 datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1356 g_string_append_printf(condition, "%s = %d", datatype_info->field, datatype_info->datatype);
1358 g_string_append_printf(condition, " or %s = %d", datatype_info->field, datatype_info->datatype);
1360 tmp_datatype_list = g_list_next(tmp_datatype_list);
1368 g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_CONTACT]);
1370 if(strlen(condtion_sub_query) != 0)
1372 if(use_sub_query == 0)
1374 g_string_append_printf(query, " where %s", q_handle->use_contact_table_id->str);
1376 g_string_append_printf(query, " where %s in (%s)", field_key[FIELD_CONTACT_CONTACT_ID], condtion_sub_query);
1379 if(q_handle->order->str[0] != '\0')
1380 g_string_append_printf(query, " order by %s", q_handle->order->str);
1382 g_string_append_printf(query, " order by %s", field_key[FIELD_CONTACT_CONTACT_ID]);
1387 g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_PERSON]);
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);
1392 if(q_handle->order->str[0] != '\0')
1393 g_string_append_printf(query, " order by %s", q_handle->order->str);
1395 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
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]);
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]);
1405 if(strlen(condtion_sub_query) != 0)
1407 if(use_sub_query == 0)
1408 g_string_append_printf(query, " where %s", q_handle->use_normal_field_key->str);
1410 g_string_append_printf(query, " where %s in (%s) and (%s)", field_key[FIELD_DATA_CONTACT_ID], condtion_sub_query, condition->str);
1413 g_string_append_printf(query, " where (%s)", condition->str);
1415 g_string_append_printf(query, " order by %s", field_key[FIELD_DATA_CONTACT_ID]);
1419 case ONLY_DATA_FROM_PERSON_QUERY:
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]);
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]);
1426 if(strlen(condtion_sub_query) != 0)
1428 if(use_sub_query == 0)
1429 g_string_append_printf(query, " where %s = ?", field_key[FIELD_PERSON_CONTACT_ID]);
1431 g_string_append_printf(query, " where %s in (%s)", field_key[FIELD_PERSON_CONTACT_ID], condtion_sub_query);
1434 if(strlen(condtion_sub_query) != 0)
1435 g_string_append_printf(query, " and (%s)", condition->str);
1437 g_string_append_printf(query, " where (%s)", condition->str);
1439 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
1443 case JOIN_CONTACT_DATA:
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]);
1448 if(strlen(condtion_sub_query) != 0)
1450 if(use_sub_query == 0)
1451 g_string_append_printf(query, " %s and", q_handle->use_normal_field_key->str);
1453 g_string_append_printf(query, " %s in (%s) and", field_key[FIELD_CONTACT_CONTACT_ID], condtion_sub_query);
1455 g_string_append_printf(query, " (%s) order by %s", condition->str, field_key[FIELD_CONTACT_CONTACT_ID]);
1459 case JOIN_PERSON_DATA:
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]);
1464 if(strlen(condtion_sub_query) != 0)
1466 if(use_sub_query == 0)
1467 g_string_append_printf(query, " %s = ? and", field_key[FIELD_PERSON_CONTACT_ID]);
1469 g_string_append_printf(query, " %s in (%s) and", field_key[FIELD_PERSON_CONTACT_ID], condtion_sub_query);
1471 g_string_append_printf(query, " (%s) order by %s", condition->str, field_key[FIELD_PERSON_CONTACT_ID]);
1479 g_string_free(condition, TRUE);
1480 g_string_free(contact_condition, TRUE);
1483 query_error _query_result_set(advanced_handle_t* q_handle, int rc, int count, projection_type type, target_table_e join_table)
1485 query_error error_code = QUERY_SUCCESS;
1487 ADVANCED_SVC_TRACE("_query_result_set");
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"));
1492 q_handle->mapping_id_to_result_set = g_new0(mapping_list_t, count + 1);
1493 while(rc == SQLITE_ROW)
1495 error_code = _make_contact_info(q_handle, type, join_table);
1496 if(error_code == QUERY_SET_LIMIT)
1498 error_code = QUERY_SUCCESS;
1501 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_ALLOCATE_MEMORY_FAIL, ("ALLOCATION FAIL"));
1503 rc = _contact_query_step(q_handle->hstmt);
1506 _ct_query_finalize(q_handle->hstmt);
1512 int _make_result_contact_list(advanced_handle_t* q_handle, int count, char* condtion_sub_query)
1514 ADVANCED_RETURN_VAL((q_handle->projection != NULL), {}, QUERY_NULL_PARAMETER, ("PROJECTION LIST IS NULL"));
1517 query_error error_code = QUERY_SUCCESS;
1519 GString* projection;
1521 projection_type type = NORMAL_CASE;
1522 target_table_e value = ONLY_CONTACT;
1524 query = g_string_new("");
1525 projection = g_string_new("");
1527 value = _check_join_case(q_handle);
1528 ADVANCED_SVC_TRACE("value : %d", value);
1530 int table_contact = 0;
1532 int data_field_count = 0;
1534 if(q_handle->query_string == NULL)
1536 GList* tmp = q_handle->projection;
1539 projection_t* projection_info = (projection_t*)tmp->data;
1541 loc = strstr(projection_info->field, "contact.");
1545 loc = strstr(projection_info->field, "data.contact_id");
1549 loc = strstr(projection_info->field, "data.");
1553 tmp = g_list_next(tmp);
1556 if(q_handle->query_type == QUERY_TO_PERSON)
1557 g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
1559 if((table_contact == 1 && reorganize == 1 && data_field_count == 1) || (q_handle->assigned_datatype == 0))
1561 g_string_append_printf(projection, "%s", field_key[FIELD_CONTACT_CONTACT_ID]);
1562 value = ONLY_CONTACT;
1565 if(projection->str[0] == '\0')
1567 g_string_append_printf(projection, "%s", field_key[FIELD_DATA_CONTACT_ID]);
1570 GList* tmp_projection = q_handle->projection;
1571 int projection_duplicate = 0;
1572 while(tmp_projection)
1574 projection_t* projection_info = (projection_t*)tmp_projection->data;
1576 if(strcmp(projection_info->field, field_key[FIELD_CONTACT_CONTACT_ID]) == 0)
1577 projection_duplicate = 1;
1579 if(strcmp(projection_info->field, field_key[FIELD_DATA_CONTACT_ID]) == 0)
1580 projection_duplicate = 1;
1582 loc = strstr(projection->str, projection_info->field);
1583 if(loc == NULL && projection_duplicate == 0)
1585 g_string_append_printf(projection, ", %s", projection_info->field);
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"));
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);
1595 tmp_projection = g_list_next(tmp_projection);
1596 projection_duplicate = 0;
1598 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1599 g_list_free(q_handle->projection);
1601 q_handle->query_string = g_strdup(projection->str);
1603 ADVANCED_SVC_TRACE("projection : %s", q_handle->query_string);
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;
1608 _construct_query(value, q_handle, query, condtion_sub_query, type);
1610 q_handle->hstmt = _contact_query_prepare(query->str);
1611 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1613 if(strlen(condtion_sub_query) != 0)
1616 if(q_handle->condition_category != NULL)
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);
1622 GList* tmp_condition_val = q_handle->condition_value;
1623 while(tmp_condition_val)
1625 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1626 if(tmp_val->indices_filter == 0)
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);
1631 primitive_type type = _convert_check_attribute_primitive_type(tmp_val->attribute);
1632 if(tmp_val->x_attribute == 1)
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);
1642 tmp_condition_val = g_list_next(tmp_condition_val);
1647 rc = _contact_query_step(q_handle->hstmt);
1648 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1650 error_code = _query_result_set(q_handle, rc, count, type, value);
1653 g_string_free(query, TRUE);
1654 g_string_free(projection, TRUE);
1658 int _check_optimized_query(advanced_handle_t* q_handle, target_table_e join_table)
1660 int is_optimized_query = 0;
1662 if((q_handle->extra_info & CATEGORIES) == 0)
1667 case ONLY_DATA_FROM_PERSON_QUERY:
1669 if(q_handle->all_datatype_in_query == DATATYPE_NAME)
1670 is_optimized_query = 1;
1674 is_optimized_query = 1;
1680 ADVANCED_SVC_TRACE("is_optimized_query : %d ", is_optimized_query);
1682 return is_optimized_query;
1685 query_error _optimized_query_to_person_with_data(advanced_handle_t* q_handle, int count)
1687 query_error error_code = QUERY_SUCCESS;
1691 GString* projection;
1694 projection = g_string_new("");
1695 query = g_string_new ("");
1696 condition = g_string_new ("");
1698 if(q_handle->query_string == NULL)
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]);
1703 GList* tmp_projection = q_handle->projection;
1704 while(tmp_projection)
1706 projection_t* projection_info = (projection_t*)tmp_projection->data;
1708 if(strcmp(projection_info->field, field_key[FIELD_PERSON_CONTACT_ID]) != 0)
1710 g_string_append_printf(projection, ", %s", projection_info->field);
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"));
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);
1719 tmp_projection = g_list_next(tmp_projection);
1721 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1722 g_list_free(q_handle->projection);
1724 q_handle->query_string = g_strdup(projection->str);
1727 g_string_append_printf(query, "select %s from %s ", q_handle->query_string, table_key[TABLE_DATA]);
1729 GList* tmp_datatype_list = q_handle->datatype_list;
1730 while(tmp_datatype_list)
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);
1737 if(q_handle->order->str[0] != '\0')
1738 g_string_append_printf(query, " %s order by %s", condition->str, q_handle->order->str);
1741 g_string_append_printf(query, " %s order by %s", condition->str, field_key[FIELD_DATA_PERSON_ID]);
1744 q_handle->hstmt = _contact_query_prepare(query->str);
1745 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1747 if(q_handle->use_normal_field_key->str[0] != '\0')
1749 GList* tmp_condition_val = q_handle->condition_value;
1750 while(tmp_condition_val)
1752 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1753 ADVANCED_SVC_TRACE("condition_val->value : %s", tmp_val->value);
1755 _contact_query_bind_text(q_handle->hstmt, 1, tmp_val->value);
1756 tmp_condition_val = g_list_next(tmp_condition_val);
1760 rc = _contact_query_step(q_handle->hstmt);
1761 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1763 error_code = _query_result_set(q_handle, rc, count, NORMAL_CASE, JOIN_PERSON_DATA);
1766 g_string_free(query, TRUE);
1767 g_string_free(projection, TRUE);
1768 g_string_free(condition, TRUE);
1770 if(error_code != QUERY_SUCCESS)
1772 _ct_query_finalize(q_handle->hstmt);
1778 query_error _optimized_query_join_person_data(advanced_handle_t* q_handle, int count)
1781 query_error error_code = QUERY_SUCCESS;
1783 GString* projection;
1787 query = g_string_new("");
1788 projection = g_string_new("");
1789 condition = g_string_new("");
1791 if(q_handle->query_string == NULL)
1793 g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
1795 GList* tmp_projection = q_handle->projection;
1796 int projection_duplicate = 0;
1797 while(tmp_projection)
1799 projection_t* projection_info = (projection_t*)tmp_projection->data;
1801 loc = strstr(projection->str, projection_info->field);
1804 g_string_append_printf(projection, ", %s", projection_info->field);
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"));
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);
1814 tmp_projection = g_list_next(tmp_projection);
1815 projection_duplicate = 0;
1817 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
1818 g_list_free(q_handle->projection);
1820 q_handle->query_string = g_strdup(projection->str);
1823 ADVANCED_SVC_TRACE("projection : %s", q_handle->query_string);
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);
1828 GList* tmp_datatype_list = q_handle->datatype_list;
1829 while(tmp_datatype_list)
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);
1836 g_string_append_printf(condition, " (%s = %s)", field_key[FIELD_PERSON_CONTACT_ID], field_key[FIELD_DATA_PERSON_ID]);
1838 if(q_handle->order->str[0] != '\0')
1839 g_string_append_printf(query, " %s order by %s", condition->str, q_handle->order->str);
1841 g_string_append_printf(query, " %s order by %s", condition->str, field_key[FIELD_PERSON_CONTACT_ID]);
1843 q_handle->hstmt = _contact_query_prepare(query->str);
1844 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
1846 if(strlen(q_handle->use_normal_field_key->str) != 0)
1848 GList* tmp_condition_val = q_handle->condition_value;
1849 while(tmp_condition_val)
1851 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1852 ADVANCED_SVC_TRACE("condition_val->value : %s", tmp_val->value);
1854 _contact_query_bind_text(q_handle->hstmt, 1, tmp_val->value);
1855 tmp_condition_val = g_list_next(tmp_condition_val);
1859 rc = _contact_query_step(q_handle->hstmt);
1860 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1862 error_code = _query_result_set(q_handle, rc, count, NORMAL_CASE, JOIN_PERSON_DATA);
1865 g_string_free(query, TRUE);
1866 g_string_free(projection, TRUE);
1867 g_string_free(condition, TRUE);
1869 if(error_code != QUERY_SUCCESS)
1871 _ct_query_finalize(q_handle->hstmt);
1877 query_error _get_categories_list(advanced_handle_t* q_handle, int count, char* condition_query)
1883 query_error error_code = QUERY_SUCCESS;
1885 query = g_string_new ("");
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]);
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);
1895 g_string_append_printf(query, " order by a.%s", relation_field_key[FIELD_GROUP_RELATION_CONTACT_ID]);
1897 hstmt = _contact_query_prepare(query->str);
1898 ADVANCED_CATCH_SET_ERROR((hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("Database prepare fail"));
1900 if(q_handle->condition_category != NULL)
1902 _contact_query_bind_text(hstmt, 1, q_handle->condition_category);
1903 }else if(strlen(condition_query) != 0)
1905 GList* tmp_condition_val = q_handle->condition_value;
1907 while(tmp_condition_val)
1909 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
1911 if(tmp_val->indices_filter == 0)
1913 primitive_type type = _convert_check_attribute_primitive_type(tmp_val->attribute);
1914 if(tmp_val->x_attribute == 1)
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);
1924 tmp_condition_val = g_list_next(tmp_condition_val);
1927 rc = _contact_query_step(hstmt);
1928 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
1930 while(rc == SQLITE_ROW)
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));
1939 ADVANCED_SVC_TRACE("q_handle->category[%d].contact_id : %d", pos, q_handle->category[pos].contact_id);
1941 if(q_handle->category[pos].contact_id == q_handle->category[pos - 1].contact_id)
1942 q_handle->category[pos].sibling = 1;
1946 rc = _contact_query_step(hstmt);
1948 q_handle->category_total_count = pos;
1949 _ct_query_finalize(hstmt);
1952 g_string_free(query, TRUE);
1956 query_error _construct_optimize_query(advanced_handle_t* q_handle, GString* query, target_table_e join_table, projection_type type, char* sub_query)
1960 query_error error_code = QUERY_SUCCESS;
1961 condition = g_string_new ("");
1962 int is_sub_query = 0;
1964 if(sub_query[0] != '\0')
1972 g_string_append_printf(condition, " %s in (%s) and ", field_key[FIELD_DATA_CONTACT_ID], sub_query);
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);
1983 if(join_table == ONLY_DATA || join_table == ONLY_DATA_FROM_PERSON_QUERY)
1985 GList* tmp_datatype_list = q_handle->datatype_list;
1987 while(tmp_datatype_list)
1989 datatype_t* datatype_info = (datatype_t*)tmp_datatype_list->data;
1992 if(q_handle->use_normal_field_key->str[0] != '\0' && is_sub_query == 0)
1994 g_string_append_printf(condition, "%s and %s = %d", q_handle->use_normal_field_key->str, datatype_info->field, datatype_info->datatype);
1996 g_string_append_printf(condition, "%s = %d", datatype_info->field, datatype_info->datatype);
1999 g_string_append_printf(condition, " or %s = %d", datatype_info->field, datatype_info->datatype);
2002 tmp_datatype_list = g_list_next(tmp_datatype_list);
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]);
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]);
2016 if(condition->str[0] != '\0')
2017 g_string_append_printf(query, " where (%s)", condition->str);
2019 if(q_handle->order->str[0] != '\0')
2020 g_string_append_printf(query, " order by %s", q_handle->order->str);
2022 g_string_append_printf(query, " order by %s", field_key[FIELD_DATA_CONTACT_ID]);
2025 case ONLY_DATA_FROM_PERSON_QUERY:
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]);
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]);
2032 if(condition->str[0] != '\0')
2033 g_string_append_printf(query, " where (%s)", condition->str);
2035 if(q_handle->order->str[0] != '\0')
2036 g_string_append_printf(query, " order by %s", q_handle->order->str);
2038 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
2043 g_string_append_printf(query, "select %s from %s", q_handle->query_string, table_key[TABLE_PERSON]);
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);
2048 if(q_handle->order->str[0] != '\0')
2049 g_string_append_printf(query, " order by %s", q_handle->order->str);
2051 g_string_append_printf(query, " order by %s", field_key[FIELD_PERSON_CONTACT_ID]);
2058 g_string_free(condition, TRUE);
2062 API query_error query_get_result_contact(ADVANCED_HANDLE handle)
2064 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2067 query_error error_code = QUERY_SUCCESS;
2069 GString* projection;
2072 projection_type type = NORMAL_CASE;
2074 int is_optimized_query = 0;
2075 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2077 if(q_handle->result_set)
2079 ADVANCED_SVC_TRACE("------------------------------ GET NEXT CONTACT INFO ------------------------------ ");
2081 if(q_handle->order->str[0] != '\0')
2083 if(q_handle->is_optimized_query)
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"));
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;
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"));
2102 ADVANCED_SVC_TRACE("------------------------------ GENERATE RESULT SET ------------------------------ ");
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"));
2110 query = g_string_new ("");
2112 target_table_e join_table = _check_join_case(q_handle);
2113 q_handle->join_table = join_table;
2115 is_optimized_query = _check_optimized_query(q_handle, join_table);
2117 if(is_optimized_query == 1)
2119 ADVANCED_SVC_TRACE("!!!!!! EXECUTE OPTIMIZED QUERY !!!!!!");
2120 q_handle->is_optimized_query = 1;
2121 projection = g_string_new("");
2123 if(q_handle->query_string == NULL)
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]);
2128 g_string_append_printf(projection, "%s", field_key[FIELD_PERSON_CONTACT_ID]);
2130 GList* tmp_projection = q_handle->projection;
2131 while(tmp_projection)
2133 projection_t* projection_info = (projection_t*)tmp_projection->data;
2135 loc = strstr(projection->str, projection_info->field);
2138 g_string_append_printf(projection, ", %s", projection_info->field);
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"));
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);
2147 tmp_projection = g_list_next(tmp_projection);
2149 g_list_foreach(q_handle->projection, _free_projection_list, NULL);
2150 g_list_free(q_handle->projection);
2152 q_handle->query_string = g_strdup(projection->str);
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;
2159 sub_query = g_string_new("");
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);
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"));
2167 q_handle->hstmt = _contact_query_prepare(query->str);
2168 ADVANCED_CATCH_SET_ERROR((q_handle->hstmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
2170 if(q_handle->use_normal_field_key->str[0] != '\0')
2172 GList* tmp_condition_val = q_handle->condition_value;
2173 while(tmp_condition_val)
2175 condition_val_t* tmp_val = (condition_val_t*)tmp_condition_val->data;
2176 if(tmp_val->indices_filter == 0)
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)
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);
2191 tmp_condition_val = g_list_next(tmp_condition_val);
2195 rc = _contact_query_step(q_handle->hstmt);
2196 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
2198 error_code = _query_result_set(q_handle, rc, count, type, join_table);
2200 g_string_free(projection, TRUE);
2201 g_string_free(query, TRUE);
2202 g_string_free (sub_query, TRUE);
2206 ADVANCED_SVC_TRACE("!!!!!! EXECUTE NORMAL QUERY !!!!!!");
2208 // generate contact id list
2210 sub_query = g_string_new("");
2212 if(q_handle->condition_category != NULL)
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);
2219 error_code = _make_contact_id_list_by_condition_part(q_handle, count, sub_query);
2221 // generate result set
2222 error_code = _make_result_contact_list(q_handle, count, sub_query->str);
2224 _make_order_list(q_handle, count, join_table);
2227 int category_count = 0;
2228 if((q_handle->extra_info & CATEGORIES) != 0)
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);
2236 g_string_free (sub_query, TRUE);
2238 ADVANCED_CATCH_SET_ERROR((q_handle->total_result_set_count != 0), {}, QUERY_NO_RECORD, ("NO RESULT CONTACT LIST"));
2242 if(error_code != QUERY_SUCCESS)
2249 API query_error query_finalize(ADVANCED_HANDLE handle)
2251 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2253 query_error error_code = QUERY_SUCCESS;
2254 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2256 _free_handle_info(q_handle);
2259 error_code = _contact_db_finish();
2264 query_error _set_exists_condition(ADVANCED_HANDLE handle, contact_attribute_e attribute)
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;
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;
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"));
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;
2283 column_name = _convert_attribute_to_condition_column_name(attribute);
2284 normal_column_name = _convert_attribute_to_column_name(attribute);
2286 datatype = _convert_attribute_to_mimetype_id(attribute);
2287 db_datatype_id = _convert_datatype_to_db_type(datatype);
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;
2293 if(q_handle->current_alias != ALIAS_INVALID)
2295 if((q_handle->condition_datatype & datatype) == 0 )
2297 q_handle->current_alias++;
2298 tmp_current_alias = q_handle->current_alias;
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);
2306 GList* tmp_condition_list = q_handle->condition_list;
2307 while(tmp_condition_list)
2309 condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
2311 if(tmp_info->datatype == db_datatype_id)
2313 tmp_current_alias = tmp_info->current_alias;
2316 tmp_condition_list = g_list_next(tmp_condition_list);
2321 q_handle->current_alias++;
2322 tmp_current_alias = q_handle->current_alias;
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);
2330 if(db_datatype_id != DB_DATATYPE_INVALID)
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);
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);
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);
2348 q_handle->condition_datatype = q_handle->condition_datatype | datatype;
2353 API query_error query_set_condition(ADVANCED_HANDLE handle, contact_attribute_e attribute, condition_e attr, const char* value)
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;
2362 int is_person_attr = 0;
2363 condition_table_t* table_info = NULL;
2364 alias_key_e tmp_current_alias = ALIAS_INVALID;
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"));
2373 error_code = _set_exists_condition(handle, attribute);
2376 ADVANCED_RETURN_VAL((value != NULL), {}, QUERY_NULL_PARAMETER, ("INVALID VALUE"));
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"));
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);
2385 condition = _convert_condition_to_string(attr);
2387 error_code = _check_extra_contact_info(q_handle, EXTRA_INFO_TYPE_CONDITION, attribute);
2388 if(error_code == QUERY_SUCCESS)
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);
2395 error_code = QUERY_SUCCESS;
2397 if(q_handle->query_type == QUERY_TO_PERSON)
2399 column_name = _convert_person_attribute_to_condition_column_name(attribute);
2400 normal_column_name = _convert_person_attribute_to_column_name(attribute);
2403 if(column_name == NULL)
2405 // column_name = _convert_attribute_to_condition_normalize_column(attribute);
2406 // normal_column_name = _convert_attribute_to_normalize_column(attribute);
2408 // if(column_name == NULL)
2410 column_name = _convert_attribute_to_condition_column_name(attribute);
2411 normal_column_name = _convert_attribute_to_column_name(attribute);
2415 if(q_handle->query_type == QUERY_TO_PERSON)
2416 is_person_attr = _convert_person_attribute_to_mimetype_id(attribute);
2418 if(is_person_attr == 0)
2420 datatype = _convert_attribute_to_mimetype_id(attribute);
2421 db_datatype_id = _convert_datatype_to_db_type(datatype);
2424 if(q_handle->query_type == QUERY_TO_PERSON)
2425 table = _convert_person_attribute_to_table_name(attribute);
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;
2432 if(q_handle->current_alias != ALIAS_INVALID)
2434 if((q_handle->condition_datatype & datatype) == 0 )
2436 q_handle->current_alias++;
2437 tmp_current_alias = q_handle->current_alias;
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);
2445 GList* tmp_condition_list = q_handle->condition_list;
2446 while(tmp_condition_list)
2448 condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
2450 if(tmp_info->datatype == db_datatype_id)
2452 tmp_current_alias = tmp_info->current_alias;
2455 tmp_condition_list = g_list_next(tmp_condition_list);
2460 q_handle->current_alias++;
2461 tmp_current_alias = q_handle->current_alias;
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);
2469 if(db_datatype_id != DB_DATATYPE_INVALID)
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);
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);
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;
2484 q_handle->condition_list = g_list_append(q_handle->condition_list, condition_info);
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);
2491 g_string_append_printf(q_handle->use_normal_field_key, "(%s %s ?)", normal_column_name, condition);
2493 g_string_append_printf(q_handle->condition_text, "(%s.%s %s ?)", alias_key[tmp_current_alias], column_name, condition);
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);
2500 condition_txt->attribute = attribute;
2503 if(attribute == ACCOUNT_ID)
2505 int total_count = 0;
2507 char *end_ptr = value;
2509 int integer_value = (int)strtol(value, &end_ptr, 0);
2510 if(errno == ERANGE || end_ptr == value)
2512 return QUERY_INVALID_TYPE;
2515 get_all_addressbook_id(&total_count);
2516 GList* tmp_list = g_addressbook_list;
2519 addressbook_list* info = (addressbook_list*)tmp_list->data;
2520 if(integer_value == info->account_id)
2522 condition_txt->int_value = info->addressbook_id;
2525 tmp_list = g_list_next(tmp_list);
2528 condition_txt->int_value = _convert_type_string_to_int(attribute, value);
2529 }else if(type == STRING)
2531 condition_txt->value = g_strdup(value);
2532 }else if(type == REAL)
2533 condition_txt->real_value = atof(value);
2535 q_handle->condition_value = g_list_append(q_handle->condition_value, condition_txt);
2537 q_handle->condition_datatype = q_handle->condition_datatype | datatype;
2542 API query_error fetch_next_row(ADVANCED_HANDLE handle)
2544 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
2546 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
2547 query_error error_code = QUERY_SUCCESS;
2549 ADVANCED_RETURN_VAL((q_handle->result_set[q_handle->return_array_pos].contact_info != NULL), {}, QUERY_NO_RECORD, ("CONTACT INFO IS NULL"));
2551 switch(q_handle->current_datatype)
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;
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"));
2560 case DATATYPE_NUMBER:
2562 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->number_list;
2565 number_t* num_info = (number_t*)tmp_list->data;
2567 _free_num_info(q_handle->result_set[q_handle->return_array_pos].contact_info->num_info);
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"));
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);
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);
2581 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2585 case DATATYPE_EMAIL:
2587 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->email_list;
2590 email_t* email_info = (email_t*)tmp_list->data;
2592 _free_email_info(q_handle->result_set[q_handle->return_array_pos].contact_info->email_info);
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"));
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);
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);
2606 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2612 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->url_list;
2615 url_t* url_info = (url_t*)tmp_list->data;
2617 _free_url_info(q_handle->result_set[q_handle->return_array_pos].contact_info->url_info);
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"));
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);
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);
2631 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2635 case DATATYPE_NICK_NAME :
2637 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->nickname_list;
2640 nick_name_t* nick_info = (nick_name_t*)tmp_list->data;
2642 _free_nick_info(q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info);
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"));
2647 q_handle->result_set[q_handle->return_array_pos].contact_info->nick_info->nick_name = g_strdup(nick_info->nick_name);
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);
2655 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2659 case DATATYPE_ADDRESS :
2661 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->address_list;
2664 address_t* address_info = (address_t*)tmp_list->data;
2666 _free_address_info(q_handle->result_set[q_handle->return_array_pos].contact_info->address_info);
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"));
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);
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);
2685 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2689 case DATATYPE_ORGANIZATION :
2691 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->organization_list;
2694 organization_t* organization_info = (organization_t*)tmp_list->data;
2696 _free_organization_info(q_handle->result_set[q_handle->return_array_pos].contact_info->organization_info);
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"));
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);
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);
2714 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2718 case DATATYPE_ANNIVERSARY :
2720 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->event_list;
2723 event_info_t* event_info = (event_info_t*)tmp_list->data;
2725 _free_anniversary_info(q_handle->result_set[q_handle->return_array_pos].contact_info->event_info);
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"));
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);
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);
2740 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
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"));
2748 case DATATYPE_CATEGORY :
2750 if((q_handle->extra_info & CATEGORIES) != 0 && q_handle->next_count > -1)
2752 char *value = q_handle->result_set[q_handle->return_array_pos].contact_info->raw_contact_info->contact_id;
2753 char *end_ptr = value;
2755 int contact_id = (int)strtol(value, &end_ptr, 0);
2756 if(errno == ERANGE || end_ptr == value)
2758 return QUERY_INVALID_TYPE;
2761 if(q_handle->next_count != 0)
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;
2772 high = q_handle->category_total_count;
2777 j = ( low + high ) / 2;
2779 if(q_handle->category[j].contact_id > contact_id)
2781 else if(q_handle->category[j].contact_id < contact_id)
2783 else if(q_handle->category[j].contact_id == contact_id)
2785 q_handle->category_pos = j;
2786 q_handle->next_count++;
2787 return QUERY_SUCCESS;
2791 for(i = 0; i < q_handle->category_total_count; i++)
2793 if(q_handle->category[i].contact_id == contact_id)
2795 q_handle->category_pos = i;
2796 q_handle->next_count++;
2797 return QUERY_SUCCESS;
2803 q_handle->next_count = -1;
2804 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2807 case DATATYPE_X_ATTRIBUTE :
2809 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->x_list;
2812 x_info_t* x_info = (x_info_t*)tmp_list->data;
2814 _free_x_info(q_handle->result_set[q_handle->return_array_pos].contact_info->x_info);
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"));
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);
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);
2836 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
2844 ADVANCED_SVC_TRACE("error_code : %d", error_code);
2850 query_error _invalid_datatype_fetch_next_row(advanced_handle_t* handle, predefine_datatype datatype)
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"));
2855 if(datatype == DATATYPE_INVALID)
2857 if(handle->query_type == QUERY_TO_CONTACT)
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)
2862 ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info->get_person_info == 0), {}, QUERY_NO_NEXT_ROW, ("NO ROW"));
2864 }else if(datatype == DATATYPE_NAME)
2866 ADVANCED_RETURN_VAL((handle->result_set[handle->return_array_pos].contact_info->get_name == 0), {}, QUERY_NO_NEXT_ROW, ("NO ROW"));
2872 char* _get_person_attribute_value(advanced_handle_t* q_handle, contact_attribute_e attribute, query_error* error_code)
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;
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;
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;
2890 case CONTACT_PHOTO_URI:
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;
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;
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;
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;
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;
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;
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;
2930 char* _get_x_attribute_value(advanced_handle_t* q_handle, contact_x_attribute_e attribute, query_error* error_code)
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
2983 char* _get_attribute_value(advanced_handle_t* q_handle, contact_attribute_e attribute, query_error* error_code)
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;
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;
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)
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;
3007 int_value = (int)strtol(value, &end_ptr, 0);
3008 if(errno == ERANGE || end_ptr == value)
3010 return QUERY_INVALID_TYPE;
3013 get_all_addressbook_id(&total_count);
3015 GList* tmp_list = g_addressbook_list;
3018 addressbook_list* info = (addressbook_list*)tmp_list->data;
3019 if(int_value == info->addressbook_id)
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;
3026 tmp_list = g_list_next(tmp_list);
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
3167 API char* query_get_attribute(ADVANCED_HANDLE handle, contact_attribute_e attribute, query_error* error_code)
3169 ADVANCED_RETURN_VAL((handle != NULL), {}, NULL, ("HANDLE IS NULL"));
3170 ADVANCED_RETURN_VAL((attribute < ATTRIBUTE_MAX), {}, NULL, ("INVALID ATTRIBUTE"));
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"));
3179 tmp_datatype = _convert_attribute_to_mimetype_id(attribute);
3180 if(q_handle->current_datatype != tmp_datatype || tmp_datatype == DATATYPE_INVALID)
3182 q_handle->current_datatype = tmp_datatype;
3183 q_handle->next_count = 0;
3184 q_handle->category_pos = 0;
3185 *error_code = QUERY_SUCCESS;
3187 if(tmp_datatype == DATATYPE_INVALID || tmp_datatype == DATATYPE_NAME)
3188 *error_code = _invalid_datatype_fetch_next_row(q_handle, tmp_datatype);
3190 *error_code = fetch_next_row(handle);
3192 ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, *error_code, ("fetch next row ERROR"));
3195 if(q_handle->query_type == QUERY_TO_PERSON)
3196 value = _get_person_attribute_value(q_handle, attribute, error_code);
3199 value = _get_attribute_value(q_handle, attribute, error_code);
3201 ADVANCED_SVC_TRACE("%d : %s", attribute, value);
3207 API query_error query_set_order_by(ADVANCED_HANDLE handle, contact_attribute_e attribute, order_e ordering)
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"));
3214 char* column_name = NULL;
3215 query_error error_code = QUERY_SUCCESS;
3216 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3218 if(q_handle->query_type == QUERY_TO_PERSON)
3220 column_name = _convert_person_attribute_to_column_name(attribute);
3223 if(column_name == NULL)
3225 column_name = _convert_attribute_to_column_name(attribute);
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;
3233 g_string_append_printf(q_handle->order, "%s", column_name);
3235 q_handle->ordering = ordering;
3239 g_string_append_printf(q_handle->order, " %s", "ASC");
3240 }else if(ordering == DESC)
3242 g_string_append_printf(q_handle->order, " %s", "DESC");
3245 ADVANCED_SVC_TRACE("order : %s", q_handle->order->str);
3249 API query_error query_set_condition_append(ADVANCED_HANDLE handle, condition_type_e type)
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;
3258 q_handle->optimize_condition = 0;
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);
3268 API query_error query_set_x_attribute(ADVANCED_HANDLE handle, int cts_custom_type_index, contact_x_attribute_e attribute)
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"));
3273 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3275 char* column_name = NULL;
3276 char* table_name = NULL;
3277 query_error error_code = QUERY_SUCCESS;
3279 projection_t* projection_info = NULL;
3281 ADVANCED_SVC_TRACE("x_attribute : %d", attribute);
3283 q_handle->x_attribute_list[attribute] = 1;
3285 column_name = _convert_x_attribute_to_column_name(attribute);
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);
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"));
3294 if(table_name == NULL)
3295 table_name = _convert_x_attribute_to_table_name(attribute);
3297 if(q_handle->table_list->str[0] != '\0')
3299 loc = strstr(q_handle->table_list->str, table_name);
3301 g_string_append_printf(q_handle->table_list, ", %s", table_name);
3305 g_string_append_printf(q_handle->table_list, "%s", table_name);
3308 tables_e table_type = _contact_table_name_to_table_type(table_name);
3309 q_handle->query_table = q_handle->query_table | table_type;
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);
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)
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"));
3324 query_error error_code = QUERY_SUCCESS;
3325 char* column_name = NULL;
3326 char* condition = NULL;
3328 condition_table_t* table_info = NULL;
3329 int new_custom_type = 0;
3330 alias_key_e tmp_current_alias = ALIAS_INVALID;
3334 error_code = _set_exists_condition(handle, attribute);
3337 ADVANCED_RETURN_VAL((value != NULL), {}, QUERY_NULL_PARAMETER, ("INVALID VALUE"));
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"));
3342 ADVANCED_SVC_TRACE("condition value : %s", value);
3343 condition = _convert_condition_to_string(attr);
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;
3350 if(q_handle->current_alias != ALIAS_INVALID)
3352 GList* tmp_condition_list = q_handle->condition_list;
3353 while(tmp_condition_list)
3355 condition_list_t* tmp_info = (condition_list_t*)tmp_condition_list->data;
3357 if(tmp_info->datatype != cts_custom_type_index)
3359 new_custom_type = 1;
3362 new_custom_type = 0;
3363 tmp_current_alias = tmp_info->current_alias;
3366 tmp_condition_list = g_list_next(tmp_condition_list);
3370 q_handle->current_alias++;
3371 tmp_current_alias = q_handle->current_alias;
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);
3380 q_handle->current_alias++;
3381 tmp_current_alias = q_handle->current_alias;
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);
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);
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;
3399 q_handle->condition_list = g_list_append(q_handle->condition_list, condition_info);
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);
3407 q_handle->condition_datatype = q_handle->condition_datatype | DATATYPE_X_ATTRIBUTE;
3412 query_error query_set_limit(ADVANCED_HANDLE handle, int window_size)
3414 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3416 query_error error_code = QUERY_SUCCESS;
3417 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3419 q_handle->start_pos = 0;
3420 q_handle->window_size = window_size;
3425 query_error query_get_next_limit_result_set(ADVANCED_HANDLE handle, int window_size)
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"));
3430 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3431 query_error error_code = QUERY_SUCCESS;
3433 q_handle->start_pos = _free_for_limit_iter(q_handle);
3434 ADVANCED_SVC_TRACE("q_handle->start_pos : %d", q_handle->start_pos);
3436 q_handle->limit_iter = 1;
3437 q_handle->total_result_set_count = 0;
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);
3443 q_handle->result_set = NULL;
3444 q_handle->window_size = window_size;
3446 error_code = query_get_result_contact(handle);
3451 API query_error query_next_custom_type(ADVANCED_HANDLE handle)
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"));
3458 q_handle->x_fetching = NEXT_ROW_FETCH;
3460 GList* tmp_list = q_handle->result_set[q_handle->return_array_pos].contact_info->x_list;
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);
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"));
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);
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);
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);
3487 ADVANCED_CATCH_SET_ERROR(0, {}, QUERY_NO_RECORD, ("no record"));
3489 ADVANCED_SVC_TRACE("error_code : %d", error_code);
3495 API char* query_get_x_attribute(ADVANCED_HANDLE handle, contact_x_attribute_e attribute, int* cts_custom_type_index, query_error* error_code)
3497 ADVANCED_RETURN_VAL((handle != NULL), {}, NULL, ("HANDLE IS NULL"));
3498 ADVANCED_RETURN_VAL((attribute < X_DATA_MAX), {}, NULL, ("INVALID ATTRIBUTE"));
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"));
3506 if(q_handle->x_fetching != NEXT_ROW_FETCH)
3507 q_handle->x_fetching = INIT_FETCH;
3509 if(q_handle->x_fetching == INIT_FETCH)
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 "));
3515 value = _get_x_attribute_value(q_handle, attribute, error_code);
3516 ADVANCED_CATCH_SET_ERROR_POINTER((*error_code == QUERY_SUCCESS), {}, *error_code, ("ERROR "));
3518 *cts_custom_type_index = q_handle->result_set[q_handle->return_array_pos].contact_info->x_info->datatype_id;
3520 ADVANCED_SVC_TRACE("*cts_custom_type_index : %d", *cts_custom_type_index);
3521 ADVANCED_SVC_TRACE("value : %s", value);
3527 API query_error query_set_condition_with_indices_begin(ADVANCED_HANDLE handle)
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;
3533 ADVANCED_RETURN_VAL((q_handle->condition_category == NULL), {}, QUERY_GROUP_CASE, ("GROUP QUERY CASE"));
3535 g_string_append_printf(q_handle->id_list, "(");
3540 API query_error query_set_condition_with_indices_end(ADVANCED_HANDLE handle)
3542 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
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;
3551 condition_table_t* table_info = NULL;
3552 alias_key_e tmp_current_alias = ALIAS_INVALID;
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"));
3557 g_string_append_printf(q_handle->id_list, ")");
3559 column_name = _convert_attribute_to_condition_column_name(CONTACT_ID);
3560 normal_column_name = _convert_attribute_to_column_name(CONTACT_ID);
3562 table = _convert_attribute_to_table_name(CONTACT_ID);
3564 q_handle->current_alias++;
3565 tmp_current_alias = q_handle->current_alias;
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);
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);
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);
3578 condition_val_t* condition_txt = NULL;
3579 condition_txt = g_new0(condition_val_t, 1);
3581 condition_txt->attribute = CONTACT_ID;
3582 condition_txt->indices_filter = 1;
3584 q_handle->condition_value = g_list_append(q_handle->condition_value, condition_txt);
3589 API query_error query_set_condition_with_indices(ADVANCED_HANDLE handle, const char* contact_index)
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;
3595 if(q_handle->is_first_id_in_list == 0)
3597 q_handle->is_first_id_in_list = 1;
3598 g_string_append_printf(q_handle->id_list, "%s", contact_index);
3601 g_string_append_printf(q_handle->id_list, ", %s", contact_index);
3607 API int get_contact_version()
3609 char query[128] = {0, };
3613 query_error error_code = QUERY_SUCCESS;
3615 error_code = _contact_db_init();
3616 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3618 snprintf(query, 128, "%s", "select max(ver) from cts_version");
3620 pStmt = _contact_query_prepare(query);
3621 ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3623 rc = _contact_query_step(pStmt);
3624 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3626 ver = _ct_query_column_int(pStmt, 0);
3627 ADVANCED_SVC_TRACE("get_contact_version :: ver : %d ", ver);
3630 _ct_query_finalize(pStmt);
3631 _contact_db_finish();
3636 API int set_number_pref_to_condition(ADVANCED_HANDLE handle)
3638 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3639 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3641 q_handle->set_number_pref = 1;
3643 return QUERY_SUCCESS;
3646 API int set_email_pref_to_condition(ADVANCED_HANDLE handle)
3648 ADVANCED_RETURN_VAL((handle != NULL), {}, QUERY_HANDLE_NONE, ("HANDLE IS NULL"));
3649 advanced_handle_t* q_handle = (advanced_handle_t*)handle;
3651 q_handle->set_email_pref = 1;
3653 return QUERY_SUCCESS;
3656 API int check_pref_number(int contact_id, char* number)
3658 char query[256 + 1] = {0, };
3662 query_error error_code = QUERY_SUCCESS;
3664 error_code = _contact_db_init();
3665 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3667 snprintf(query, 256, "%s", "select is_default from data where contact_id = ? and data3 = ? and datatype = 8");
3669 pStmt = _contact_query_prepare(query);
3670 ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3672 _contact_query_bind_int(pStmt, 1, contact_id);
3673 _contact_query_bind_text(pStmt, 2, number);
3675 rc = _contact_query_step(pStmt);
3676 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3678 pref = _ct_query_column_int(pStmt, 0);
3679 ADVANCED_SVC_TRACE("pref: %d ", pref);
3682 _ct_query_finalize(pStmt);
3683 _contact_db_finish();
3688 API int check_pref_email(int contact_id, char* email)
3690 char query[256 + 1] = {0, };
3694 query_error error_code = QUERY_SUCCESS;
3696 error_code = _contact_db_init();
3697 ADVANCED_CATCH_SET_ERROR((error_code == QUERY_SUCCESS), {}, QUERY_STATEMENT_FAIL, ("DATABASE INIT FAIL"));
3699 snprintf(query, 256, "%s", "select is_default from data where contact_id = ? and data3 = ? and datatype = 9");
3701 pStmt = _contact_query_prepare(query);
3702 ADVANCED_CATCH_SET_ERROR((pStmt != NULL), {}, QUERY_STATEMENT_FAIL, ("database prepare fail"));
3704 _contact_query_bind_int(pStmt, 1, contact_id);
3705 _contact_query_bind_text(pStmt, 2, email);
3707 rc = _contact_query_step(pStmt);
3708 ADVANCED_CATCH_SET_ERROR((rc == SQLITE_ROW), {}, QUERY_NO_RECORD, ("No record in database"));
3710 pref = _ct_query_column_int(pStmt, 0);
3711 ADVANCED_SVC_TRACE("pref: %d ", pref);
3714 _ct_query_finalize(pStmt);
3715 _contact_db_finish();