summaryrefslogtreecommitdiff
path: root/native/ctsvc_db_plugin_sdn.c
diff options
context:
space:
mode:
Diffstat (limited to 'native/ctsvc_db_plugin_sdn.c')
-rw-r--r--native/ctsvc_db_plugin_sdn.c390
1 files changed, 390 insertions, 0 deletions
diff --git a/native/ctsvc_db_plugin_sdn.c b/native/ctsvc_db_plugin_sdn.c
new file mode 100644
index 0000000..409c7d2
--- /dev/null
+++ b/native/ctsvc_db_plugin_sdn.c
@@ -0,0 +1,390 @@
+/*
+ * Contacts Service
+ *
+ * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "contacts.h"
+#include "ctsvc_internal.h"
+#include "ctsvc_schema.h"
+#include "ctsvc_sqlite.h"
+#include "ctsvc_utils.h"
+#include "ctsvc_list.h"
+#include "ctsvc_db_plugin_sdn.h"
+#include "ctsvc_db_init.h"
+#include "ctsvc_db_query.h"
+#include "ctsvc_record.h"
+#include "ctsvc_notification.h"
+
+static int __ctsvc_db_sdn_insert_record( contacts_record_h record, int *id );
+static int __ctsvc_db_sdn_get_record( int id, contacts_record_h* record );
+static int __ctsvc_db_sdn_update_record( contacts_record_h record );
+static int __ctsvc_db_sdn_delete_record( int id );
+static int __ctsvc_db_sdn_get_all_records( int offset, int limit, contacts_list_h* out_list );
+static int __ctsvc_db_sdn_get_records_with_query( contacts_query_h query, int offset, int limit, contacts_list_h* out_list );
+//static int __ctsvc_db_sdn_insert_records(const contacts_list_h in_list, int **ids);
+//static int __ctsvc_db_sdn_update_records(const contacts_list_h in_list);
+//static int __ctsvc_db_sdn_delete_records(int ids[], int count);
+
+ctsvc_db_plugin_info_s ctsvc_db_plugin_sdn = {
+ .is_query_only = false,
+ .insert_record = __ctsvc_db_sdn_insert_record,
+ .get_record = __ctsvc_db_sdn_get_record,
+ .update_record = __ctsvc_db_sdn_update_record,
+ .delete_record = __ctsvc_db_sdn_delete_record,
+ .get_all_records = __ctsvc_db_sdn_get_all_records,
+ .get_records_with_query = __ctsvc_db_sdn_get_records_with_query,
+ .insert_records = NULL,//__ctsvc_db_sdn_insert_records,
+ .update_records = NULL,//__ctsvc_db_sdn_update_records,
+ .delete_records = NULL,//__ctsvc_db_sdn_delete_records
+ .get_count = NULL,
+ .get_count_with_query = NULL,
+ .replace_record = NULL,
+ .replace_records = NULL,
+};
+
+static int __ctsvc_db_sdn_value_set(cts_stmt stmt, contacts_record_h *record)
+{
+ int i;
+ int ret;
+ char *temp;
+ ctsvc_sdn_s *sdn;
+
+ ret = contacts_record_create(_contacts_sdn._uri, record);
+ RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create is failed(%d)", ret);
+ sdn = (ctsvc_sdn_s*)*record;
+
+ i = 0;
+ sdn->id = ctsvc_stmt_get_int(stmt, i++);
+ temp = ctsvc_stmt_get_text(stmt, i++);
+ sdn->name = SAFE_STRDUP(temp);
+ temp = ctsvc_stmt_get_text(stmt, i++);
+ sdn->number = SAFE_STRDUP(temp);
+
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_get_record( int id, contacts_record_h* out_record )
+{
+ int ret;
+ int len;
+ cts_stmt stmt = NULL;
+ char query[CTS_SQL_MAX_LEN] = {0};
+ contacts_record_h record;
+
+ RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
+ *out_record = NULL;
+
+ len = snprintf(query, sizeof(query),
+ "SELECT id, name, number FROM "CTS_TABLE_SDN" WHERE id = %d", id);
+
+ stmt = cts_query_prepare(query);
+ RETVM_IF(NULL == stmt, CONTACTS_ERROR_DB, "DB error : cts_query_prepare() Failed");
+
+ ret = cts_stmt_step(stmt);
+ if (1 /*CTS_TRUE*/ != ret) {
+ CTS_ERR("cts_stmt_step() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ return CONTACTS_ERROR_NO_DATA;
+ }
+
+ ret = __ctsvc_db_sdn_value_set(stmt, &record);
+
+ cts_stmt_finalize(stmt);
+ if (CONTACTS_ERROR_NONE != ret) {
+ CTS_ERR("__ctsvc_db_sdn_value_set(ALL) Failed(%d)", ret);
+ return ret;
+ }
+
+ *out_record = record;
+
+ return CONTACTS_ERROR_NONE;
+}
+
+
+static int __ctsvc_db_sdn_insert_record( contacts_record_h record, int *id )
+{
+ int ret;
+ ctsvc_sdn_s *sdn = (ctsvc_sdn_s*)record;
+
+ RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == sdn->name, CONTACTS_ERROR_INVALID_PARAMETER);
+ RETVM_IF(CTSVC_RECORD_SDN != sdn->base.r_type, CONTACTS_ERROR_INVALID_PARAMETER,
+ "Invalid parameter : record is invalid type(%d)", sdn->base.r_type);
+
+ cts_stmt stmt = NULL;
+ char query[CTS_SQL_MAX_LEN] = {0};
+
+ snprintf(query, sizeof(query),
+ "INSERT INTO "CTS_TABLE_SDN"(name, number) VALUES(?, ?)");
+
+ stmt = cts_query_prepare(query);
+ if (NULL == stmt) {
+ CTS_ERR("DB error : cts_query_prepare() Failed");
+ return CONTACTS_ERROR_DB;
+ }
+
+ cts_stmt_bind_text(stmt, 1, sdn->name);
+ cts_stmt_bind_text(stmt, 2, sdn->number);
+
+ ret = ctsvc_begin_trans();
+ if( ret < CONTACTS_ERROR_NONE ) {
+ CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ return ret;
+ }
+
+ ret = cts_stmt_step(stmt);
+ if (CONTACTS_ERROR_NONE != ret) {
+ CTS_ERR("DB error : cts_stmt_step() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ ctsvc_end_trans(false);
+ return ret;
+ }
+
+ //sdn->id = cts_db_get_last_insert_id();
+ if (id)
+ *id = cts_db_get_last_insert_id();
+ cts_stmt_finalize(stmt);
+
+ ctsvc_set_sdn_noti();
+ ret = ctsvc_end_trans(true);
+ if(ret < CONTACTS_ERROR_NONE ) {
+ CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
+ return ret;
+ }
+
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_update_record( contacts_record_h record )
+{
+ int ret;
+ ctsvc_sdn_s *sdn = (ctsvc_sdn_s *)record;
+
+ RETV_IF(NULL == record, CONTACTS_ERROR_INVALID_PARAMETER);
+ RETV_IF(NULL == sdn->name, CONTACTS_ERROR_INVALID_PARAMETER);
+ RETVM_IF(CTSVC_RECORD_SDN != sdn->base.r_type, CONTACTS_ERROR_INVALID_PARAMETER,
+ "Invalid parameter : record is invalid type(%d)", sdn->base.r_type);
+
+ cts_stmt stmt = NULL;
+ char query[CTS_SQL_MIN_LEN] = {0};
+
+ snprintf(query, sizeof(query),
+ "UPDATE "CTS_TABLE_SDN" SET name=?, number=? WHERE id = %d", sdn->id);
+
+ stmt = cts_query_prepare(query);
+ if (NULL == stmt) {
+ CTS_ERR("DB error : cts_query_prepare() Failed");
+ return CONTACTS_ERROR_DB;
+ }
+
+ cts_stmt_bind_text(stmt, 1, sdn->name);
+ cts_stmt_bind_text(stmt, 2, sdn->number);
+
+ ret = ctsvc_begin_trans();
+ if( ret < CONTACTS_ERROR_NONE ) {
+ CTS_ERR("DB error : ctsvc_begin_trans() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ return ret;
+ }
+
+ ret = cts_stmt_step(stmt);
+ if (CONTACTS_ERROR_NONE != ret) {
+ CTS_ERR("DB error : cts_stmt_step() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ ctsvc_end_trans(false);
+ return ret;
+ }
+
+ cts_stmt_finalize(stmt);
+
+ ctsvc_set_sdn_noti();
+
+ ret = ctsvc_end_trans(true);
+ if(ret < CONTACTS_ERROR_NONE ) {
+ CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
+ return ret;
+ }
+
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_delete_record( int sdn_id )
+{
+ int ret;
+
+ char query[CTS_SQL_MAX_LEN] = {0};
+ snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_SDN" WHERE id = %d", sdn_id);
+
+ ret = ctsvc_begin_trans();
+ RETVM_IF(CONTACTS_ERROR_NONE > ret, ret, "DB error : ctsvc_begin_trans() Failed(%d)", ret);
+
+ ret = ctsvc_query_exec(query);
+ if (CONTACTS_ERROR_NONE != ret) {
+ CTS_ERR("DB error : ctsvc_query_exec() Failed(%d)", ret);
+ ctsvc_end_trans(false);
+ return ret;
+ }
+
+ ret = cts_db_change();
+ if (ret <= 0) {
+ ret = CONTACTS_ERROR_NO_DATA;
+ ctsvc_end_trans(false);
+ return ret;
+ }
+
+ ctsvc_set_sdn_noti();
+ ret = ctsvc_end_trans(true);
+ if (ret != CONTACTS_ERROR_NONE) {
+ CTS_ERR("DB error : ctsvc_end_trans() Failed(%d)", ret);
+ return ret;
+ }
+
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_get_all_records( int offset, int limit,
+ contacts_list_h* out_list )
+{
+ int ret;
+ int len;
+ cts_stmt stmt;
+ char query[CTS_SQL_MAX_LEN] = {0};
+ contacts_list_h list;
+
+ len = snprintf(query, sizeof(query),
+ "SELECT id, name, number FROM "CTS_TABLE_SDN);
+
+ if (0 < limit) {
+ len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
+ if (0 < offset)
+ len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
+ }
+
+ stmt = cts_query_prepare(query);
+ RETVM_IF(NULL == stmt, CONTACTS_ERROR_DB , "DB error : cts_query_prepare() Failed");
+
+ contacts_list_create(&list);
+ while ((ret = cts_stmt_step(stmt))) {
+ contacts_record_h record;
+ if (1 != ret) {
+ CTS_ERR("DB error : cts_stmt_step() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ contacts_list_destroy(list, true);
+ return CONTACTS_ERROR_NO_DATA;
+ }
+ __ctsvc_db_sdn_value_set(stmt, &record);
+
+ ctsvc_list_prepend(list, record);
+ }
+ cts_stmt_finalize(stmt);
+ ctsvc_list_reverse(list);
+
+ *out_list = (contacts_list_h)list;
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_get_records_with_query( contacts_query_h query, int offset, int limit, contacts_list_h* out_list )
+{
+ int ret;
+ int i;
+ int field_count;
+ ctsvc_query_s *s_query;
+ cts_stmt stmt;
+ contacts_list_h list;
+ ctsvc_sdn_s *sdn;
+
+ RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
+ s_query = (ctsvc_query_s *)query;
+
+ ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
+ RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
+
+ contacts_list_create(&list);
+ while ((ret = cts_stmt_step(stmt))) {
+ contacts_record_h record;
+ if (1 != ret) {
+ CTS_ERR("DB error : cts_stmt_step() Failed(%d)", ret);
+ cts_stmt_finalize(stmt);
+ contacts_list_destroy(list, true);
+ return CONTACTS_ERROR_NO_DATA;
+ }
+
+ contacts_record_create(_contacts_sdn._uri, &record);
+ sdn = (ctsvc_sdn_s*)record;
+ if (0 == s_query->projection_count)
+ field_count = s_query->property_count;
+ else
+ {
+ field_count = s_query->projection_count;
+
+ if( CONTACTS_ERROR_NONE != ctsvc_record_set_projection_flags(record, s_query->projection, s_query->projection_count, s_query->property_count) )
+ {
+ ASSERT_NOT_REACHED("To set projection is failed.\n");
+ }
+ }
+
+ for(i=0;i<field_count;i++) {
+ char *temp;
+ int property_id;
+ if (0 == s_query->projection_count)
+ property_id = s_query->properties[i].property_id;
+ else
+ property_id = s_query->projection[i];
+
+ switch(property_id) {
+ case CTSVC_PROPERTY_SDN_ID:
+ sdn->id = ctsvc_stmt_get_int(stmt, i);
+ break;
+ case CTSVC_PROPERTY_SDN_NAME:
+ temp = ctsvc_stmt_get_text(stmt, i);
+ sdn->name = SAFE_STRDUP(temp);
+ break;
+ case CTSVC_PROPERTY_SDN_NUMBER:
+ temp = ctsvc_stmt_get_text(stmt, i);
+ sdn->number = SAFE_STRDUP(temp);
+ break;
+ default:
+ break;
+ }
+ }
+ ctsvc_list_prepend(list, record);
+ }
+ cts_stmt_finalize(stmt);
+ ctsvc_list_reverse(list);
+
+ *out_list = (contacts_list_h)list;
+
+ return CONTACTS_ERROR_NONE;
+}
+
+#if 0
+static int __ctsvc_db_sdn_insert_records(const contacts_list_h in_list, int **ids)
+{
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_update_records(const contacts_list_h in_list)
+{
+ return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_db_sdn_delete_records(int ids[], int count)
+{
+ return CONTACTS_ERROR_NONE;
+}
+#endif