[Internal: merge private->RSA , Patch create momanager.db]
[platform/core/system/sync-agent.git] / src / framework / device-manager / mo_database.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <unistd.h>
19
20 #include "utility/sync_util.h"
21 #include "utility/fw_alloc.h"
22 #include "device-manager/mo_database_handler.h"
23 #include "device-manager/mo_database.h"
24
25 #ifndef SYNC_AGENT_LOG
26 #undef LOG_TAG
27 #define LOG_TAG "AF_MO"
28 #endif
29
30 /* static function define */
31 static int _busy_handler(void *pData, int count);
32 static sync_agent_dm_mo_error_e _query_exec(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, char *err_msg);
33 static daci_stmt _query_prepare(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, int size);
34 static sync_agent_da_return_e _stmt_bind_text(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index, const char *value);
35 static sync_agent_dm_mo_error_e _stmt_bind_int(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index, const int value);
36 static sync_agent_dm_mo_error_e __stmt_bind_null(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index);
37 static sync_agent_da_return_e _stmt_step(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt);
38 static sync_agent_dm_mo_error_e _stmt_finalize(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt);
39 static char *_stmt_column_text(daci_stmt stmt, int index);
40 static int _stmt_column_int(daci_stmt stmt, int index);
41 static sync_agent_dm_mo_error_e _get_table(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, char ***result, int *row_count, int *col_count);
42 static void _free_table(char **result);
43
44 static int _exist_table(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *table_name);
45 static int _last_insert_id(SYNC_AGENT_DA_MO_HANDLER * mo_handler);
46
47 static char *_extract_dmacc_name(const char *mo_full_path);
48
49 static char *__create_table[] = {
50         /* 1. create node_tbl */
51         /*
52         "create table node_tbl "
53             "( " "node_id integer primary key autoincrement, " "name varchar(100) not null, " "value varchar(100) default null, " "parent_node_id integer default null, " "full_path varchar(200) not null, " "node_type integer not null, "
54             "mo_type integer not null, " "server_type integer default null, " "constraint node_tbl_parent_node_id_fk foreign key(parent_node_id) references node_tbl(node_id), " "constraint node_tbl_unique_key unique(full_path, mo_type, server_type) " ");",
55         */
56         "create table node_tbl "
57             "( " "node_id integer primary key autoincrement, " "name varchar(100) not null, " "value varchar(500) default null, " "parent_node_id integer default null, " "full_path varchar(200) not null, " "node_type integer not null, "
58             "mo_type integer not null, " "server_type integer default null, " "constraint node_tbl_parent_node_id_fk foreign key(parent_node_id) references node_tbl(node_id), " "constraint node_tbl_unique_key unique(full_path, mo_type, server_type) " ");",
59
60         /* 2. create framework_property_tbl */
61         "create table framework_property_tbl "
62             "( "
63             "framework_property_id integer primary key autoincrement, "
64             "access_type integer default null, "
65             "default_value varchar(100) default null, " "description varchar(100) default null, " "df_format integer default null, " "occurrence integer default null, " "occurrence_num integer default null, " "scope integer default null, "
66             "df_title varchar(100) default null, " "df_type integer default null, " "df_type_value varchar(20) default null, " "node_id integer not null unique, "
67             "constraint framework_property_tbl_node_id_fk foreign key(node_id) references node_tbl(node_id) " ");",
68
69         /* 3. create runtime_property_tbl */
70         "create table runtime_property_tbl "
71             "( "
72             "runtime_property_id integer primary key autoincrement, "
73             "acl varchar(100) default null, " "format integer default null, " "name varchar(100) default null, " "size varchar(100) default null, " "title varchar(100) default null, " "t_stamp varchar(100) default null, " "type integer default null, "
74             "type_value varchar(30) default null, " "ver_no varchar(100) default null, " "node_id integer not null unique, " "constraint runtime_property_tbl_node_id_fk foreign key(node_id) references node_tbl(node_id) " ");",
75
76 };
77
78 sync_agent_dm_mo_error_e dm_mo_begin_transaction(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
79 {
80         _EXTERN_FUNC_ENTER;
81
82         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
83
84         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
85
86         ret = _query_exec(mo_handler, "begin immediate", "begin_transaction failed");
87
88 /*      if (ret == SYNC_AGENT_DA_ERR_QUERY_FAILED)
89                 ret = SYNC_AGENT_DA_ERR_TRANSACTION_FAILED;*/
90
91         _EXTERN_FUNC_EXIT;
92
93         return ret;
94 }
95
96 sync_agent_dm_mo_error_e dm_mo_end_transaction(SYNC_AGENT_DA_MO_HANDLER * mo_handler, sync_agent_da_transaction_e transaction)
97 {
98         _EXTERN_FUNC_ENTER;
99
100         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
101
102         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
103         char *query = NULL;
104         char *err_msg = NULL;
105
106         if (transaction == SYNC_AGENT_DA_TRANSACTION_COMMIT) {
107                 query = "commit transaction";
108                 err_msg = "commit_transaction failed";
109         } else if (transaction == SYNC_AGENT_DA_TRANSACTION_ROLLBACK) {
110                 query = "rollback transaction";
111                 err_msg = "rollback_transaction failed";
112         }
113
114         ret = _query_exec(mo_handler, query, err_msg);
115         if (ret == SYNC_AGENT_DM_MO_FAIL) {
116                 if (transaction == SYNC_AGENT_DA_TRANSACTION_COMMIT) {
117                         query = "rollback transaction";
118                         err_msg = "rollback_transaction failed";
119                         ret = _query_exec(mo_handler, query, err_msg);
120                         if (ret == SYNC_AGENT_DM_MO_SUCCESS)
121                                 _DEBUG_INFO("rollback_transaction success");
122                 }
123                 ret = SYNC_AGENT_DM_MO_FAIL;
124         }
125
126         _EXTERN_FUNC_EXIT;
127
128         return ret;
129 }
130
131 sync_agent_dm_mo_error_e mo_open_agent(SYNC_AGENT_DA_MO_HANDLER ** mo_handler, char *database_path)
132 {
133         _EXTERN_FUNC_ENTER;
134
135         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
136
137         int ret = 0;
138         char *err_msg = NULL;
139
140         if (*mo_handler == NULL) {
141
142                 /* db open */
143                 /*  ret = db_util_open(DACI_FILE_PATH, &(*t_daci_handler), 0); */
144
145                 ret = sqlite3_open(database_path, mo_handler);
146                 if (ret != SQLITE_OK)
147                         goto DACI_FINISH;
148
149                 /* register busy handler */
150                 ret = sqlite3_busy_handler(*mo_handler, _busy_handler, 0);
151                 if (ret != SQLITE_OK) {
152                         _DEBUG_INFO("fail to register busy handler", err_msg);
153                         goto DACI_FINISH;
154                 }
155
156                 /* enable persist journal mode */
157                 ret = sqlite3_exec(*mo_handler, "PRAGMA journal_mode = PERSIST", 0, 0, &err_msg);
158                 if (ret != SQLITE_OK) {
159                         _DEBUG_INFO("fail to change journal mode: %s", err_msg);
160                         goto DACI_FINISH;
161                 }
162
163                 _DEBUG_INFO("mo_db_open success");
164
165         } else {
166                 _DEBUG_INFO("mo_db_already opened");
167         }
168
169         _EXTERN_FUNC_EXIT;
170
171         return SYNC_AGENT_DM_MO_SUCCESS;
172
173  DACI_FINISH:
174
175         _DEBUG_ERROR("agent_db_open failed(%d) : %s", ret, sqlite3_errmsg(*mo_handler));
176
177         if (err_msg != NULL)
178                 sqlite3_free(err_msg);
179
180         sqlite3_close(*mo_handler);
181         *mo_handler = NULL;
182
183         return SYNC_AGENT_DM_MO_FAIL;
184 }
185
186 sync_agent_dm_mo_error_e mo_close_agent(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
187 {
188         _EXTERN_FUNC_ENTER;
189
190         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
191
192         int ret = 0;
193         ret = sqlite3_close(mo_handler);
194         mo_handler = NULL;
195
196         if (ret != SQLITE_OK) {
197                 _DEBUG_ERROR("mo_db_close failed(%d) : %s", ret, sqlite3_errmsg(mo_handler));
198                 return SYNC_AGENT_DM_MO_FAIL;
199         }
200
201         _DEBUG_INFO("mo_db_close success");
202
203         _EXTERN_FUNC_EXIT;
204
205         return SYNC_AGENT_DM_MO_SUCCESS;
206 }
207
208 sync_agent_dm_mo_error_e dm_mo_create_mo_table(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
209 {
210         _EXTERN_FUNC_ENTER;
211
212         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
213
214         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
215
216         if (mo_handler == NULL) {
217                 _DEBUG_INFO("[%s] sync_agent_dm_mo_error_e !\n", __func__);
218                 return SYNC_AGENT_DM_MO_FAIL;
219         }
220
221         if ((dm_mo_begin_transaction(mo_handler) != SYNC_AGENT_DM_MO_SUCCESS)) {
222                 _DEBUG_ERROR("mo_db_default_tbl_create failed");
223                 return SYNC_AGENT_DM_MO_FAIL;
224         }
225
226         /* 1. create node_tbl */
227         if (_exist_table(mo_handler, "node_tbl") == 0) {
228                 ret = _query_exec(mo_handler, __create_table[0], "node_tbl_create failed");
229                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
230                         goto DACI_FINISH;
231         }
232
233         /* 2. create framework_property_tbl */
234         if (_exist_table(mo_handler, "framework_property_tbl") == 0) {
235                 ret = _query_exec(mo_handler, __create_table[1], "framework_property_tbl_create failed");
236                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
237                         goto DACI_FINISH;
238         }
239
240         /* 3. create runtime_property_tbl */
241         if (_exist_table(mo_handler, "runtime_property_tbl") == 0) {
242                 ret = _query_exec(mo_handler, __create_table[2], "runtime_property_tbl_create failed");
243                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
244                         goto DACI_FINISH;
245         }
246
247  DACI_FINISH:
248
249         if (ret != SYNC_AGENT_DM_MO_SUCCESS) {
250                 dm_mo_end_transaction(mo_handler, SYNC_AGENT_DA_TRANSACTION_ROLLBACK);
251                 ret = SYNC_AGENT_DM_MO_FAIL;
252         } else {
253                 ret = dm_mo_end_transaction(mo_handler, SYNC_AGENT_DA_TRANSACTION_COMMIT);
254         }
255
256         _EXTERN_FUNC_EXIT;
257
258         return ret;
259 }
260
261 sync_agent_dm_mo_error_e dm_mo_add_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler, sync_agent_dm_mo_type_e mo_type, sync_agent_dm_mo_node_s * mo_node)
262 {
263         _EXTERN_FUNC_ENTER;
264
265         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
266         retvm_if(mo_node == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_node_s is NULL !!");
267
268         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
269         daci_stmt stmt = NULL;
270         char *query = NULL;
271
272         query = "insert into node_tbl (name, value, parent_node_id, full_path, node_type, mo_type, server_type) values (?, ?, ?, ?, ?, ?, ?)";
273
274         stmt = _query_prepare(mo_handler, query, strlen(query));
275         if (stmt == NULL) {
276                 ret = SYNC_AGENT_DM_MO_FAIL;
277                 goto DACI_FINISH;
278         }
279
280 /*      _stmt_bind_int(mo_handler, stmt, 1, mo_node->id);*/
281         _stmt_bind_text(mo_handler, stmt, 1, mo_node->name);
282         _stmt_bind_text(mo_handler, stmt, 2, mo_node->value);
283         _stmt_bind_int(mo_handler, stmt, 3, mo_node->parent_id);
284         _stmt_bind_text(mo_handler, stmt, 4, mo_node->full_path);
285         _stmt_bind_int(mo_handler, stmt, 5, mo_node->type);
286         _stmt_bind_int(mo_handler, stmt, 6, mo_type);
287         _stmt_bind_int(mo_handler, stmt, 7, mo_node->server_type);
288
289         ret = _stmt_step(mo_handler, stmt);
290         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
291                 ret = SYNC_AGENT_DM_MO_FAIL;
292
293  DACI_FINISH:
294
295         if (stmt != NULL)
296                 _stmt_finalize(mo_handler, stmt);
297
298         if (ret != SYNC_AGENT_DM_MO_SUCCESS) {
299                 mo_node->id = -1;
300         } else {
301                 mo_node->id = _last_insert_id(mo_handler);
302         }
303
304         _EXTERN_FUNC_EXIT;
305
306         return ret;
307 }
308
309 sync_agent_dm_mo_error_e dm_mo_delete_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id)
310 {
311         _EXTERN_FUNC_ENTER;
312
313         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
314
315         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
316         daci_stmt stmt = NULL;
317         char *query = NULL;
318
319         query = "delete from node_tbl where node_id = ?";
320
321         stmt = _query_prepare(mo_handler, query, strlen(query));
322         if (stmt == NULL) {
323                 ret = SYNC_AGENT_DM_MO_FAIL;
324                 goto DACI_FINISH;
325         }
326
327         _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
328         ret = _stmt_step(mo_handler, stmt);
329
330  DACI_FINISH:
331
332         if (stmt != NULL)
333                 _stmt_finalize(mo_handler, stmt);
334
335         _EXTERN_FUNC_EXIT;
336
337         return ret;
338 }
339
340 sync_agent_dm_mo_error_e dm_mo_update_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler, sync_agent_dm_mo_type_e mo_type, const char *path, const sync_agent_dm_mo_node_s * mo_node)
341 {
342         _EXTERN_FUNC_ENTER;
343
344         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
345         retvm_if(mo_node == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_node_s is NULL !!");
346
347         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
348         daci_stmt stmt = NULL;
349         char *query = NULL;
350
351         query = "update node_tbl set value = ? where full_path = ? and mo_type = ?";
352
353         stmt = _query_prepare(mo_handler, query, strlen(query));
354         if (stmt == NULL) {
355                 ret = SYNC_AGENT_DM_MO_FAIL;
356                 goto DACI_FINISH;
357         }
358
359         _stmt_bind_text(mo_handler, stmt, 1, mo_node->value);
360         _stmt_bind_text(mo_handler, stmt, 2, path);
361         _stmt_bind_int(mo_handler, stmt, 3, mo_type);
362
363         ret = _stmt_step(mo_handler, stmt);
364
365  DACI_FINISH:
366         if (stmt != NULL)
367                 _stmt_finalize(mo_handler, stmt);
368
369         _EXTERN_FUNC_EXIT;
370
371         return ret;
372 }
373
374 sync_agent_dm_mo_error_e dm_mo_get_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *path, sync_agent_dm_mo_node_s ** mo_node)
375 {
376         _EXTERN_FUNC_ENTER;
377
378         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
379
380         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
381         daci_stmt stmt = NULL;
382         char *query = "select * from node_tbl where full_path = ?";
383         sync_agent_dm_mo_node_s *temp_mo_node = NULL;
384
385         stmt = _query_prepare(mo_handler, query, strlen(query));
386         if (stmt != NULL) {
387
388                 _stmt_bind_text(mo_handler, stmt, 1, path);
389                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
390                         temp_mo_node = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_node_s *, sizeof(sync_agent_dm_mo_node_s), 1);
391                         temp_mo_node->id = _stmt_column_int(stmt, 0);
392                         temp_mo_node->name = _stmt_column_text(stmt, 1);
393                         temp_mo_node->value = _stmt_column_text(stmt, 2);
394                         temp_mo_node->parent_id = _stmt_column_int(stmt, 3);
395                         temp_mo_node->full_path = _stmt_column_text(stmt, 4);
396                         temp_mo_node->type = _stmt_column_int(stmt, 5);
397                         temp_mo_node->mo_type = _stmt_column_int(stmt, 6);
398                         temp_mo_node->server_type = _stmt_column_int(stmt, 7);
399                         temp_mo_node->runtime_property = 0;
400                         temp_mo_node->framework_property = 0;
401                 } else {
402                         ret = SYNC_AGENT_DM_MO_FAIL;
403                 }
404                 ret = _stmt_finalize(mo_handler, stmt);
405         } else {
406                 ret = SYNC_AGENT_DM_MO_FAIL;
407         }
408
409         (*mo_node) = temp_mo_node;
410
411         _EXTERN_FUNC_EXIT;
412
413         return ret;
414 }
415
416 sync_agent_dm_mo_error_e dm_mo_get_nodes(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *path, sync_agent_dm_mo_node_s ** mo_node)
417 {
418         _EXTERN_FUNC_ENTER;
419
420         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
421
422         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
423         daci_stmt stmt = NULL;
424         char *query = "select * from node_tbl where full_path = ?";
425         sync_agent_dm_mo_node_s *current_ptr = NULL;
426
427         stmt = _query_prepare(mo_handler, query, strlen(query));
428         if (stmt != NULL) {
429                 _stmt_bind_text(mo_handler, stmt, 1, path);
430
431                 while (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
432                         _DEBUG_INFO("nodes called!!!");
433                         sync_agent_dm_mo_node_s *temp_mo_node = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_node_s *, sizeof(sync_agent_dm_mo_node_s), 1);
434                         if (temp_mo_node == NULL) {
435                                 _DEBUG_ERROR("CALLOC failed !!!");
436                                 return SYNC_AGENT_DM_MO_FAIL;
437                         }
438                         temp_mo_node->id = _stmt_column_int(stmt, 0);
439                         temp_mo_node->name = _stmt_column_text(stmt, 1);
440                         temp_mo_node->value = _stmt_column_text(stmt, 2);
441                         temp_mo_node->parent_id = _stmt_column_int(stmt, 3);
442                         temp_mo_node->full_path = _stmt_column_text(stmt, 4);
443                         temp_mo_node->type = _stmt_column_int(stmt, 5);
444                         temp_mo_node->mo_type = _stmt_column_int(stmt, 6);
445                         temp_mo_node->server_type = _stmt_column_int(stmt, 7);
446                         temp_mo_node->runtime_property = 0;
447                         temp_mo_node->framework_property = 0;
448                         if (current_ptr == NULL) {
449                                 _DEBUG_INFO("nodes first node!!!");
450                                 (*mo_node) = temp_mo_node;
451                                 current_ptr = (*mo_node);
452                         } else {
453                                 _DEBUG_INFO("nodes next node!!!");
454                                 current_ptr->next_node = temp_mo_node;
455                                 current_ptr = temp_mo_node;
456                         }
457                 }
458                 ret = _stmt_finalize(mo_handler, stmt);
459         } else {
460                 ret = SYNC_AGENT_DM_MO_FAIL;
461         }
462
463         _EXTERN_FUNC_EXIT;
464
465         return ret;
466 }
467
468 sync_agent_dm_mo_error_e dm_mo_is_exist_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *path)
469 {
470         _EXTERN_FUNC_ENTER;
471
472         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
473         retvm_if(path == NULL, SYNC_AGENT_DM_MO_FAIL, "path is NULL !!");
474
475         _DEBUG_INFO("[%s] Start path : %s\n", __func__, path);
476
477         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
478         daci_stmt stmt = NULL;
479         int data_count = 0;
480         char query[1024] = { 0, };
481
482         snprintf(query, sizeof(query), "select count(*) from node_tbl where full_path = \'%s\' ", path);
483
484         stmt = _query_prepare(mo_handler, query, strlen(query));
485         if (stmt != NULL) {
486                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA)
487                         data_count = _stmt_column_int(stmt, 0);
488
489                 _stmt_finalize(mo_handler, stmt);
490         }
491
492         if (data_count == 0) {
493                 ret = SYNC_AGENT_DM_MO_NOT_EXIST_NODE;
494         } else {
495                 ret = SYNC_AGENT_DM_MO_EXIST_NODE;
496         }
497
498         _EXTERN_FUNC_EXIT;
499
500         return ret;
501 }
502
503 sync_agent_dm_mo_error_e dm_mo_get_child_node(sync_agent_dm_mo_type_e mo_type, SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *path, sync_agent_dm_mo_node_s ** mo_node, int *count)
504 {
505         _EXTERN_FUNC_ENTER;
506
507         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
508         retvm_if(path == NULL, SYNC_AGENT_DM_MO_FAIL, "path is NULL !!");
509
510         _DEBUG_INFO("[%s] Start ! mopath : %s , mo type : %d\n", __func__, path, mo_type);
511
512         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_FAIL; // SYNC_AGENT_DM_MO_SUCCESS;
513         sync_agent_dm_mo_node_s *temp_mo_node = NULL;
514         char **result = NULL;
515         int row_count = 0;
516         int col_count = 0;
517         int index = 8;
518         int i = 0;
519         char query[SYNC_AGENT_DA_MAX_QUERY_LENGTH] = { 0, };
520
521         if (!strcmp(path, ".")) {
522                 snprintf(query, sizeof(query), "select * from node_tbl where parent_node_id in " "(select node_id from node_tbl where full_path = \'%s\')", path);
523                 ret = _get_table(mo_handler, query, &result, &row_count, &col_count);
524                 if (result == NULL) {
525                         _DEBUG_ERROR("result is NULL!!");
526                         ret = SYNC_AGENT_DM_MO_FAIL;
527                         goto DACI_FINISH;
528                 }
529         } else {
530                 snprintf(query, sizeof(query), "select * from node_tbl where parent_node_id in " "(select node_id from node_tbl where mo_type = %d and full_path = \'%s\')", mo_type, path);
531                 ret = _get_table(mo_handler, query, &result, &row_count, &col_count);
532                 if (result == NULL) {
533                         _DEBUG_ERROR("result is NULL!!");
534                         ret = SYNC_AGENT_DM_MO_FAIL;
535                         goto DACI_FINISH;
536                 }
537         }
538
539         if ((ret == SYNC_AGENT_DM_MO_SUCCESS) && (row_count != 0)) {
540                 *count = row_count;
541                 temp_mo_node = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_node_s *, sizeof(sync_agent_dm_mo_node_s), row_count);
542                 if (temp_mo_node == NULL) {
543                         _DEBUG_ERROR("memory_allocation failed");
544                         ret = SYNC_AGENT_DM_MO_FAIL;
545                         goto DACI_FINISH;
546                 }
547
548                 for (i = 0; i < row_count; i++) {
549                         temp_mo_node[i].id = SYNC_AGENT_DA_ATOI(result[index]);
550                         index++;
551                         temp_mo_node[i].name = SYNC_AGENT_DA_STRDUP(result[index]);
552                         index++;
553                         temp_mo_node[i].value = SYNC_AGENT_DA_STRDUP(result[index]);
554                         index++;
555                         temp_mo_node[i].parent_id = SYNC_AGENT_DA_ATOI(result[index]);
556                         index++;
557                         temp_mo_node[i].full_path = SYNC_AGENT_DA_STRDUP(result[index]);
558                         index++;
559                         temp_mo_node[i].type = SYNC_AGENT_DA_ATOI(result[index]);
560                         index++;
561                         temp_mo_node[i].mo_type = SYNC_AGENT_DA_ATOI(result[index]);
562                         index++;
563                         temp_mo_node[i].server_type = SYNC_AGENT_DA_ATOI(result[index]);
564                         index++;
565                 }
566
567                 ret = SYNC_AGENT_DM_MO_SUCCESS;
568                 if (result != NULL){
569                         _free_table(result);
570                 }
571                 (*mo_node) = temp_mo_node;
572                 _EXTERN_FUNC_EXIT;
573                 return ret;
574         }else {
575                 ret = SYNC_AGENT_DM_MO_FAIL;
576         }
577
578  DACI_FINISH:
579
580         if (result != NULL){
581                 _free_table(result);
582         }
583         //(*mo_node) = temp_mo_node;
584
585         _EXTERN_FUNC_EXIT;
586
587         return ret;
588 }
589
590 sync_agent_dm_mo_error_e dm_mo_delete_all_node(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
591 {
592         _EXTERN_FUNC_ENTER;
593
594         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
595
596         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
597         daci_stmt stmt = NULL;
598         char *query = NULL;
599
600         query = "delete from node_tbl";
601
602         stmt = _query_prepare(mo_handler, query, strlen(query));
603         if (stmt == NULL) {
604                 ret = SYNC_AGENT_DM_MO_FAIL;
605                 goto DACI_FINISH;
606         }
607         ret = _stmt_step(mo_handler, stmt);
608
609  DACI_FINISH:
610
611         if (stmt != NULL)
612                 _stmt_finalize(mo_handler, stmt);
613
614         _EXTERN_FUNC_EXIT;
615
616         return ret;
617 }
618
619 sync_agent_dm_mo_type_e dm_mo_get_mo_type(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *mo_full_path)
620 {
621         _EXTERN_FUNC_ENTER;
622
623         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_TYPE_NO_TYPE, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
624         retvm_if(mo_full_path == NULL, SYNC_AGENT_DM_MO_TYPE_NO_TYPE, "mo_full_path is NULL !!");
625
626         sync_agent_dm_mo_type_e mo_type = SYNC_AGENT_DM_MO_TYPE_NO_TYPE;
627         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
628         daci_stmt stmt = NULL;
629         char *query = "select mo_type from node_tbl where full_path = ?";
630
631         stmt = _query_prepare(mo_handler, query, strlen(query));
632         if (stmt != NULL) {
633
634                 _stmt_bind_text(mo_handler, stmt, 1, mo_full_path);
635                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
636                         mo_type = _stmt_column_int(stmt, 0);
637                 }
638                 ret = _stmt_finalize(mo_handler, stmt);
639         } else {
640                 ret = SYNC_AGENT_DM_MO_FAIL;
641         }
642
643         _EXTERN_FUNC_EXIT;
644
645         return mo_type;
646 }
647
648 sync_agent_dm_mo_error_e dm_mo_get_node_from_id(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_node_s ** mo_node)
649 {
650         _EXTERN_FUNC_ENTER;
651
652         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
653
654         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
655         daci_stmt stmt = NULL;
656         char *query = "select * from node_tbl where node_id = ?";
657         sync_agent_dm_mo_node_s *temp_mo_node = NULL;
658
659         stmt = _query_prepare(mo_handler, query, strlen(query));
660         if (stmt != NULL) {
661
662                 _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
663                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
664                         temp_mo_node = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_node_s *, sizeof(sync_agent_dm_mo_node_s), 1);
665                         temp_mo_node->id = _stmt_column_int(stmt, 0);
666                         temp_mo_node->name = _stmt_column_text(stmt, 1);
667                         temp_mo_node->value = _stmt_column_text(stmt, 2);
668                         temp_mo_node->parent_id = _stmt_column_int(stmt, 3);
669                         temp_mo_node->full_path = _stmt_column_text(stmt, 4);
670                         temp_mo_node->type = _stmt_column_int(stmt, 5);
671                         temp_mo_node->mo_type = _stmt_column_int(stmt, 6);
672                         temp_mo_node->server_type = _stmt_column_int(stmt, 7);
673                         temp_mo_node->runtime_property = 0;
674                         temp_mo_node->framework_property = 0;
675                 }
676                 ret = _stmt_finalize(mo_handler, stmt);
677         } else {
678                 ret = SYNC_AGENT_DM_MO_FAIL;
679         }
680
681         (*mo_node) = temp_mo_node;
682
683         _EXTERN_FUNC_EXIT;
684
685         return ret;
686 }
687
688 sync_agent_dm_mo_error_e dm_mo_get_acc_info(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *server_id, const char *server_id_string, const char *acc_type_string, sync_agent_dm_acc_info_s ** acc_info)
689 {
690         _EXTERN_FUNC_ENTER;
691
692         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
693         retvm_if(server_id == NULL, SYNC_AGENT_DM_MO_FAIL, "server id is NULL !!");
694         retvm_if(server_id_string == NULL, SYNC_AGENT_DM_MO_FAIL, "server id string is NULL !!");
695         retvm_if(acc_type_string == NULL, SYNC_AGENT_DM_MO_FAIL, "acc type string is NULL !!");
696
697         _DEBUG_INFO("get acc type string : %s ", acc_type_string);
698         _DEBUG_INFO("get acc server id string : %s ", server_id_string);
699         _DEBUG_INFO("get acc server id : %s ", server_id);
700
701         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
702         daci_stmt stmt = NULL;
703         char *query = "select full_path from node_tbl where name = ? and value = ? and mo_type = ?";
704         char *full_path = NULL;
705
706         stmt = _query_prepare(mo_handler, query, strlen(query));
707         if (stmt != NULL) {
708
709                 _stmt_bind_text(mo_handler, stmt, 1, server_id_string);
710                 _stmt_bind_text(mo_handler, stmt, 2, server_id);
711                 _stmt_bind_int(mo_handler, stmt, 3, SYNC_AGENT_DM_MO_TYPE_DMACC);
712                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
713                         full_path = _stmt_column_text(stmt, 0);
714                 }
715                 ret = _stmt_finalize(mo_handler, stmt);
716         } else {
717                 ret = SYNC_AGENT_DM_MO_FAIL;
718         }
719
720         _DEBUG_INFO("full_path : %s", full_path);
721         if (full_path == NULL) {
722                 return SYNC_AGENT_DM_MO_FAIL;
723         }
724         /*
725          * Parsing full_path and extract dmacc name
726          */
727         char *dmacc_name = _extract_dmacc_name(full_path);
728         _DEBUG_INFO("dmacc_name : %s", dmacc_name);
729
730         if (dmacc_name == NULL) {
731                 return SYNC_AGENT_DM_MO_FAIL;
732         }
733
734         *acc_info = (sync_agent_dm_acc_info_s *) calloc(1, sizeof(sync_agent_dm_acc_info_s));
735         if (*acc_info == NULL) {
736                 _DEBUG_ERROR("CALLOC failed !!!");
737                 return SYNC_AGENT_DM_MO_FAIL;
738         }
739
740         /*
741          * 1. AAuthName
742          */
743         char query01[2048] = { 0, };
744         snprintf(query01, sizeof(query01), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", "AAuthName", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name, acc_type_string);
745
746         stmt = _query_prepare(mo_handler, query01, strlen(query01));
747         if (stmt != NULL) {
748                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
749                         (*acc_info)->auth_name = _stmt_column_text(stmt, 0);
750                 }
751                 ret = _stmt_finalize(mo_handler, stmt);
752         } else {
753                 ret = SYNC_AGENT_DM_MO_FAIL;
754         }
755
756         /*
757          * 2. AAuthSecret
758          */
759         char query02[2048] = { 0, };
760         snprintf(query02, sizeof(query02), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", "AAuthSecret", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name, acc_type_string);
761
762         stmt = _query_prepare(mo_handler, query02, strlen(query02));
763         if (stmt != NULL) {
764                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
765                         (*acc_info)->auth_secret = _stmt_column_text(stmt, 0);
766                 }
767                 ret = _stmt_finalize(mo_handler, stmt);
768         } else {
769                 ret = SYNC_AGENT_DM_MO_FAIL;
770         }
771
772         /*
773          * 3. AAuthLevel
774          */
775         char query03[2048] = { 0, };
776         snprintf(query03, sizeof(query03), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", "AAuthLevel", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name, acc_type_string);
777
778         stmt = _query_prepare(mo_handler, query03, strlen(query03));
779         if (stmt != NULL) {
780                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
781                         (*acc_info)->auth_level = _stmt_column_text(stmt, 0);
782                 }
783                 ret = _stmt_finalize(mo_handler, stmt);
784         } else {
785                 ret = SYNC_AGENT_DM_MO_FAIL;
786         }
787
788         /*
789          * 4. AAuthData
790          */
791         char query04[2048] = { 0, };
792         snprintf(query04, sizeof(query04), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", "AAuthData", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name, acc_type_string);
793
794         stmt = _query_prepare(mo_handler, query04, strlen(query04));
795         if (stmt != NULL) {
796                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
797                         (*acc_info)->auth_data = _stmt_column_text(stmt, 0);
798                 }
799                 ret = _stmt_finalize(mo_handler, stmt);
800         } else {
801                 ret = SYNC_AGENT_DM_MO_FAIL;
802         }
803
804         /*
805          * 5. AAuthType
806          */
807         char query05[2048] = { 0, };
808         snprintf(query05, sizeof(query05), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", "AAuthType", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name, acc_type_string);
809
810         stmt = _query_prepare(mo_handler, query05, strlen(query05));
811         if (stmt != NULL) {
812                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
813                         (*acc_info)->auth_type = _stmt_column_text(stmt, 0);
814                 }
815                 ret = _stmt_finalize(mo_handler, stmt);
816         } else {
817                 ret = SYNC_AGENT_DM_MO_FAIL;
818         }
819
820         /*
821          * 6. Addr
822          */
823         char query06[2048] = { 0, };
824         snprintf(query06, sizeof(query06), "select value from node_tbl where name = '%s' and mo_type = %d and full_path like '\%%%s\%%'", "Addr", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name);
825
826         stmt = _query_prepare(mo_handler, query06, strlen(query06));
827         if (stmt != NULL) {
828                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
829                         (*acc_info)->addr = _stmt_column_text(stmt, 0);
830                 }
831                 ret = _stmt_finalize(mo_handler, stmt);
832         } else {
833                 ret = SYNC_AGENT_DM_MO_FAIL;
834         }
835
836         if (dmacc_name != NULL) {
837                 free(dmacc_name);
838         }
839
840         _DEBUG_INFO("[%s] End !\n", __func__);
841
842         _EXTERN_FUNC_EXIT;
843
844         return ret;
845 }
846
847 sync_agent_dm_mo_error_e dm_mo_set_acc_info(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *server_id, const char *server_id_string, const char *acc_type_string, sync_agent_dm_acc_info_s * acc_info)
848 {
849         _EXTERN_FUNC_ENTER;
850
851         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
852         retvm_if(server_id == NULL, SYNC_AGENT_DM_MO_FAIL, "server id is NULL !!");
853         retvm_if(server_id_string == NULL, SYNC_AGENT_DM_MO_FAIL, "server id string is NULL !!");
854         retvm_if(acc_type_string == NULL, SYNC_AGENT_DM_MO_FAIL, "acc type string is NULL !!");
855         retvm_if(acc_info == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_acc_info_s is NULL !!");
856
857         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
858
859         daci_stmt stmt = NULL;
860         char *query = "select full_path from node_tbl where name = ? and value = ? and mo_type = ?";
861         char *full_path = NULL;
862
863         stmt = _query_prepare(mo_handler, query, strlen(query));
864         if (stmt != NULL) {
865
866                 _stmt_bind_text(mo_handler, stmt, 1, server_id_string);
867                 _stmt_bind_text(mo_handler, stmt, 2, server_id);
868                 _stmt_bind_int(mo_handler, stmt, 3, SYNC_AGENT_DM_MO_TYPE_DMACC);
869                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
870                         full_path = _stmt_column_text(stmt, 0);
871                 }
872                 ret = _stmt_finalize(mo_handler, stmt);
873         } else {
874                 ret = SYNC_AGENT_DM_MO_FAIL;
875         }
876
877         _DEBUG_INFO("full_path : %s", full_path);
878
879         /*
880          * Parsing full_path and extract dmacc name
881          */
882         char *dmacc_name = _extract_dmacc_name(full_path);
883         _DEBUG_INFO("dmacc_name : %s", dmacc_name);
884
885         if (dmacc_name == NULL) {
886                 return SYNC_AGENT_DM_MO_FAIL;
887         }
888
889         /*
890          * 1. AAuthName
891          */
892         char query01[2048] = { 0, };
893         snprintf(query01, sizeof(query01), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", acc_info->auth_name, "AAuthName", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name,
894                  acc_type_string);
895
896         stmt = _query_prepare(mo_handler, query01, strlen(query01));
897         if (stmt != NULL) {
898                 ret = _stmt_step(mo_handler, stmt);
899
900                 ret = _stmt_finalize(mo_handler, stmt);
901         } else {
902                 ret = SYNC_AGENT_DM_MO_FAIL;
903         }
904
905         /*
906          * 2. AAuthSecret
907          */
908         char query02[2048] = { 0, };
909         snprintf(query02, sizeof(query02), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", acc_info->auth_secret, "AAuthSecret", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name,
910                  acc_type_string);
911
912         stmt = _query_prepare(mo_handler, query02, strlen(query02));
913         if (stmt != NULL) {
914                 ret = _stmt_step(mo_handler, stmt);
915
916                 ret = _stmt_finalize(mo_handler, stmt);
917         } else {
918                 ret = SYNC_AGENT_DM_MO_FAIL;
919         }
920
921         /*
922          * 3. AAuthLevel
923          */
924         char query03[2048] = { 0, };
925         snprintf(query03, sizeof(query03), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", acc_info->auth_level, "AAuthLevel", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name,
926                  acc_type_string);
927
928         stmt = _query_prepare(mo_handler, query03, strlen(query03));
929         if (stmt != NULL) {
930                 ret = _stmt_step(mo_handler, stmt);
931
932                 ret = _stmt_finalize(mo_handler, stmt);
933         } else {
934                 ret = SYNC_AGENT_DM_MO_FAIL;
935         }
936
937         /*
938          * 4. AAuthData
939          */
940         char query04[2048] = { 0, };
941         snprintf(query04, sizeof(query04), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", acc_info->auth_data, "AAuthData", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name,
942                  acc_type_string);
943
944         stmt = _query_prepare(mo_handler, query04, strlen(query04));
945         if (stmt != NULL) {
946                 ret = _stmt_step(mo_handler, stmt);
947
948                 ret = _stmt_finalize(mo_handler, stmt);
949         } else {
950                 ret = SYNC_AGENT_DM_MO_FAIL;
951         }
952
953         /*
954          * 5. AAuthType
955          */
956         char query05[2048] = { 0, };
957         snprintf(query05, sizeof(query05), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%' and full_path like '\%%%s\%%'", acc_info->auth_type, "AAuthType", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name,
958                  acc_type_string);
959
960         stmt = _query_prepare(mo_handler, query05, strlen(query05));
961         if (stmt != NULL) {
962                 ret = _stmt_step(mo_handler, stmt);
963
964                 ret = _stmt_finalize(mo_handler, stmt);
965         } else {
966                 ret = SYNC_AGENT_DM_MO_FAIL;
967         }
968
969         /*
970          * 6. Addr
971          */
972         char query06[2048] = { 0, };
973         snprintf(query06, sizeof(query06), "update node_tbl set value = '%s' where name = '%s' and mo_type = %d and full_path like '\%%%s\%%'", acc_info->addr, "Addr", SYNC_AGENT_DM_MO_TYPE_DMACC, dmacc_name);
974
975         stmt = _query_prepare(mo_handler, query06, strlen(query06));
976         if (stmt != NULL) {
977                 ret = _stmt_step(mo_handler, stmt);
978
979                 ret = _stmt_finalize(mo_handler, stmt);
980         } else {
981                 ret = SYNC_AGENT_DM_MO_FAIL;
982         }
983
984         if (dmacc_name != NULL) {
985                 free(dmacc_name);
986         }
987
988         _EXTERN_FUNC_EXIT;
989
990         return ret;
991 }
992
993 sync_agent_dm_mo_error_e dm_mo_get_server_id_list(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *server_id_string, sync_agent_dm_server_info_s ** head_ptr)
994 {
995         _EXTERN_FUNC_ENTER;
996
997         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
998         retvm_if(server_id_string == NULL, SYNC_AGENT_DM_MO_FAIL, "server id string is NULL !!");
999
1000         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1001         sync_agent_dm_server_info_s *current_ptr = 0;
1002         daci_stmt stmt = 0;
1003         char *query = "select value, server_type from node_tbl where name = ?";
1004
1005         stmt = _query_prepare(mo_handler, query, strlen(query));
1006         if (stmt != NULL) {
1007                 _stmt_bind_text(mo_handler, stmt, 1, server_id_string);
1008                 while (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
1009                         sync_agent_dm_server_info_s *server_info = (sync_agent_dm_server_info_s *) calloc(1, sizeof(sync_agent_dm_server_info_s));
1010                         if (server_info == NULL) {
1011                                 _DEBUG_ERROR("CALLOC failed !!!");
1012                                 return SYNC_AGENT_DM_MO_FAIL;
1013                         }
1014                         server_info->server_id = _stmt_column_text(stmt, 0);
1015                         server_info->server_type = _stmt_column_int(stmt, 1);
1016
1017                         if (current_ptr == NULL) {
1018                                 *head_ptr = server_info;
1019                                 current_ptr = *head_ptr;
1020                         } else {
1021                                 current_ptr->next = server_info;
1022                                 current_ptr = server_info;
1023                         }
1024                 }
1025                 ret = _stmt_finalize(mo_handler, stmt);
1026         } else {
1027                 ret = SYNC_AGENT_DM_MO_FAIL;
1028         }
1029
1030         _EXTERN_FUNC_EXIT;
1031
1032         return ret;
1033 }
1034
1035 sync_agent_dm_mo_error_e dm_mo_get_root_path(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *mo_full_path, char **root_path)
1036 {
1037         _EXTERN_FUNC_ENTER;
1038
1039         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1040         retvm_if(mo_full_path == NULL, SYNC_AGENT_DM_MO_FAIL, "mo_full_path is NULL !!");
1041
1042         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1043         daci_stmt stmt = NULL;
1044
1045         char *query = "select full_path from node_tbl where node_type = ? and mo_type = (select mo_type from node_tbl where full_path = ?)";
1046
1047         stmt = _query_prepare(mo_handler, query, strlen(query));
1048         if (stmt != NULL) {
1049                 _stmt_bind_int(mo_handler, stmt, 1, SYNC_AGENT_DM_MO_NODE_FIRST);
1050                 _stmt_bind_text(mo_handler, stmt, 2, mo_full_path);
1051                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
1052                         *root_path = _stmt_column_text(stmt, 0);
1053                 }
1054                 ret = _stmt_finalize(mo_handler, stmt);
1055         } else {
1056                 ret = SYNC_AGENT_DM_MO_FAIL;
1057         }
1058
1059         _EXTERN_FUNC_EXIT;
1060
1061         return ret;
1062 }
1063
1064 sync_agent_dm_mo_error_e dm_mo_add_framework_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_framework_property_s * framework_property)
1065 {
1066         _EXTERN_FUNC_ENTER;
1067
1068         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1069         retvm_if(framework_property == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_framework_property_s is NULL !!");
1070
1071         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1072         daci_stmt stmt = NULL;
1073         char *query = NULL;
1074
1075         query = "insert into framework_property_tbl " "(access_type, default_value, description, df_format, occurrence, occurrence_num, scope, df_title, df_type, df_type_value, node_id) " "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1076
1077         stmt = _query_prepare(mo_handler, query, strlen(query));
1078         if (stmt == NULL) {
1079
1080                 ret = SYNC_AGENT_DM_MO_FAIL;
1081                 goto DACI_FINISH;
1082         }
1083
1084 /*      _stmt_bind_int(mo_handler, stmt, 1, framework_property->id);*/
1085         _stmt_bind_int(mo_handler, stmt, 1, framework_property->accessType);
1086         _stmt_bind_text(mo_handler, stmt, 2, framework_property->defaultValue);
1087         _stmt_bind_text(mo_handler, stmt, 3, framework_property->description);
1088         _stmt_bind_int(mo_handler, stmt, 4, framework_property->dffFormat);
1089         _stmt_bind_int(mo_handler, stmt, 5, framework_property->occurrence);
1090         _stmt_bind_int(mo_handler, stmt, 6, framework_property->occurrence_num);
1091         _stmt_bind_int(mo_handler, stmt, 7, framework_property->scope);
1092         _stmt_bind_text(mo_handler, stmt, 8, framework_property->dfTitle);
1093         _stmt_bind_int(mo_handler, stmt, 9, framework_property->dfType);
1094         _stmt_bind_text(mo_handler, stmt, 10, framework_property->dfType_Value);
1095         _stmt_bind_int(mo_handler, stmt, 11, mo_node_id);
1096
1097         ret = _stmt_step(mo_handler, stmt);
1098
1099         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
1100                 ret = SYNC_AGENT_DM_MO_FAIL;
1101
1102  DACI_FINISH:
1103
1104         if (stmt != NULL)
1105                 _stmt_finalize(mo_handler, stmt);
1106
1107         _EXTERN_FUNC_EXIT;
1108
1109         return ret;
1110 }
1111
1112 sync_agent_dm_mo_error_e dm_mo_delete_framework_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id)
1113 {
1114         _EXTERN_FUNC_ENTER;
1115
1116         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1117
1118         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1119         daci_stmt stmt = NULL;
1120         char *query = NULL;
1121
1122         query = "delete from framework_property_tbl where node_id = ?";
1123
1124         stmt = _query_prepare(mo_handler, query, strlen(query));
1125         if (stmt == NULL) {
1126                 ret = SYNC_AGENT_DM_MO_FAIL;
1127                 goto DACI_FINISH;
1128         }
1129
1130         _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
1131         ret = _stmt_step(mo_handler, stmt);
1132
1133  DACI_FINISH:
1134
1135         if (stmt != NULL)
1136                 _stmt_finalize(mo_handler, stmt);
1137
1138         _EXTERN_FUNC_EXIT;
1139
1140         return ret;
1141 }
1142
1143 sync_agent_dm_mo_error_e dm_mo_update_framework_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_framework_property_s * framework_property)
1144 {
1145         _EXTERN_FUNC_ENTER;
1146
1147         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1148         retvm_if(framework_property == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_framework_property_s *framework_property is NULL !!");
1149
1150         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1151         daci_stmt stmt = NULL;
1152         char *query = NULL;
1153
1154         query = "update framework_property_tbl set access_type = ?,  default_value = ?, description = ?, df_format = ?, occurrence = ?," "occurrence_num = ?, scope = ?, df_title = ?, df_type = ?, df_type_value = ? where node_id = ?";
1155
1156         stmt = _query_prepare(mo_handler, query, strlen(query));
1157         if (stmt == NULL) {
1158                 ret = SYNC_AGENT_DM_MO_FAIL;
1159                 goto DACI_FINISH;
1160         }
1161         _stmt_bind_int(mo_handler, stmt, 1, framework_property->accessType);
1162         _stmt_bind_text(mo_handler, stmt, 2, framework_property->defaultValue);
1163         _stmt_bind_text(mo_handler, stmt, 3, framework_property->description);
1164         _stmt_bind_int(mo_handler, stmt, 4, framework_property->dffFormat);
1165         _stmt_bind_int(mo_handler, stmt, 5, framework_property->occurrence);
1166         _stmt_bind_int(mo_handler, stmt, 6, framework_property->occurrence_num);
1167         _stmt_bind_int(mo_handler, stmt, 7, framework_property->scope);
1168         _stmt_bind_text(mo_handler, stmt, 8, framework_property->dfTitle);
1169         _stmt_bind_int(mo_handler, stmt, 9, framework_property->dfType);
1170         _stmt_bind_text(mo_handler, stmt, 10, framework_property->dfType_Value);
1171         _stmt_bind_int(mo_handler, stmt, 11, mo_node_id);
1172
1173         ret = _stmt_step(mo_handler, stmt);
1174         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
1175                 ret = SYNC_AGENT_DM_MO_FAIL;
1176
1177  DACI_FINISH:
1178
1179         if (stmt != NULL)
1180                 _stmt_finalize(mo_handler, stmt);
1181
1182         _EXTERN_FUNC_EXIT;
1183
1184         return ret;
1185 }
1186
1187 sync_agent_dm_mo_error_e dm_mo_get_framework_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_framework_property_s ** framework_property)
1188 {
1189         _EXTERN_FUNC_ENTER;
1190
1191         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1192
1193         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1194         daci_stmt stmt = NULL;
1195         char *query = "select * from framework_property_tbl where node_id = ?";
1196         sync_agent_dm_mo_framework_property_s *temp_framework_property = 0;
1197
1198         stmt = _query_prepare(mo_handler, query, strlen(query));
1199         if (stmt != NULL) {
1200                 _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
1201
1202                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
1203                         temp_framework_property = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_framework_property_s *, sizeof(sync_agent_dm_mo_framework_property_s), 1);
1204 /*                      temp_framework_property->framework_property_id = _stmt_column_int(stmt, 0);*/
1205                         temp_framework_property->accessType = _stmt_column_int(stmt, 1);
1206                         temp_framework_property->defaultValue = _stmt_column_text(stmt, 2);
1207                         temp_framework_property->description = _stmt_column_text(stmt, 3);
1208                         temp_framework_property->dffFormat = _stmt_column_int(stmt, 4);
1209                         temp_framework_property->occurrence = _stmt_column_int(stmt, 5);
1210                         temp_framework_property->occurrence_num = _stmt_column_int(stmt, 6);
1211                         temp_framework_property->scope = _stmt_column_int(stmt, 7);
1212                         temp_framework_property->dfTitle = _stmt_column_text(stmt, 8);
1213                         temp_framework_property->dfType = _stmt_column_int(stmt, 9);
1214                         temp_framework_property->dfType_Value = _stmt_column_text(stmt, 10);
1215 /*                      temp_framework_property->node_id = _stmt_column_text(stmt, 9);*/
1216                 }
1217                 ret = _stmt_finalize(mo_handler, stmt);
1218         } else {
1219                 ret = SYNC_AGENT_DM_MO_FAIL;
1220         }
1221
1222         (*framework_property) = temp_framework_property;
1223
1224         _EXTERN_FUNC_EXIT;
1225
1226         return ret;
1227 }
1228
1229 sync_agent_dm_mo_error_e dm_mo_delete_all_framework_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
1230 {
1231         _EXTERN_FUNC_ENTER;
1232
1233         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1234
1235         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1236         daci_stmt stmt = NULL;
1237         char *query = NULL;
1238
1239         query = "delete from framework_property_tbl";
1240
1241         stmt = _query_prepare(mo_handler, query, strlen(query));
1242         if (stmt == NULL) {
1243                 ret = SYNC_AGENT_DM_MO_FAIL;
1244                 goto DACI_FINISH;
1245         }
1246
1247         ret = _stmt_step(mo_handler, stmt);
1248
1249  DACI_FINISH:
1250
1251         if (stmt != NULL)
1252                 _stmt_finalize(mo_handler, stmt);
1253
1254         _EXTERN_FUNC_EXIT;
1255
1256         return ret;
1257 }
1258
1259 sync_agent_dm_mo_error_e dm_mo_add_runtime_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_runtime_property_s * runtime_property)
1260 {
1261         _EXTERN_FUNC_ENTER;
1262
1263         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1264         retvm_if(runtime_property == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_runtime_property_s is NULL !!");
1265
1266         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1267         daci_stmt stmt = NULL;
1268         char *query = NULL;
1269
1270         query = "insert into runtime_property_tbl " "(acl, format, name, size, title, t_stamp, type, type_value, ver_no, node_id) " "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
1271
1272         stmt = _query_prepare(mo_handler, query, strlen(query));
1273         if (stmt == NULL) {
1274                 ret = SYNC_AGENT_DM_MO_FAIL;
1275                 goto DACI_FINISH;
1276         }
1277
1278         _stmt_bind_text(mo_handler, stmt, 1, runtime_property->acl);
1279         _stmt_bind_int(mo_handler, stmt, 2, runtime_property->format);
1280         _stmt_bind_text(mo_handler, stmt, 3, runtime_property->name);
1281         _stmt_bind_text(mo_handler, stmt, 4, runtime_property->size);
1282         _stmt_bind_text(mo_handler, stmt, 5, runtime_property->title);
1283         _stmt_bind_text(mo_handler, stmt, 6, runtime_property->tStamp);
1284         _stmt_bind_int(mo_handler, stmt, 7, runtime_property->type);
1285         _stmt_bind_text(mo_handler, stmt, 8, runtime_property->type_value);
1286         _stmt_bind_text(mo_handler, stmt, 9, runtime_property->verNo);
1287         _stmt_bind_int(mo_handler, stmt, 10, mo_node_id);
1288
1289         ret = _stmt_step(mo_handler, stmt);
1290         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
1291                 ret = SYNC_AGENT_DM_MO_FAIL;
1292
1293  DACI_FINISH:
1294
1295         if (stmt != NULL)
1296                 _stmt_finalize(mo_handler, stmt);
1297
1298         _EXTERN_FUNC_EXIT;
1299
1300         return ret;
1301 }
1302
1303 sync_agent_dm_mo_error_e dm_mo_delete_runtime_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id)
1304 {
1305         _EXTERN_FUNC_ENTER;
1306
1307         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1308
1309         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1310         daci_stmt stmt = NULL;
1311         char *query = NULL;
1312
1313         query = "delete from runtime_property_tbl where node_id = ?";
1314
1315         stmt = _query_prepare(mo_handler, query, strlen(query));
1316         if (stmt == NULL) {
1317                 ret = SYNC_AGENT_DM_MO_FAIL;
1318                 goto DACI_FINISH;
1319         }
1320
1321         _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
1322         ret = _stmt_step(mo_handler, stmt);
1323
1324  DACI_FINISH:
1325
1326         if (stmt != NULL)
1327                 _stmt_finalize(mo_handler, stmt);
1328
1329         _EXTERN_FUNC_EXIT;
1330
1331         return ret;
1332 }
1333
1334 sync_agent_dm_mo_error_e dm_mo_update_runtime_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_runtime_property_s * runtime_property)
1335 {
1336         _EXTERN_FUNC_ENTER;
1337
1338         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1339         retvm_if(runtime_property == NULL, SYNC_AGENT_DM_MO_FAIL, "sync_agent_dm_mo_runtime_property_s is NULL !!");
1340
1341         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1342         daci_stmt stmt = NULL;
1343         char *query = NULL;
1344
1345         query = "update runtime_property_tbl set acl = ?,  format = ?, name = ?, size = ?, title = ?," "t_stamp = ?, type = ?, type_value = ?, ver_no = ? where node_id = ?";
1346
1347         stmt = _query_prepare(mo_handler, query, strlen(query));
1348         if (stmt == NULL) {
1349                 ret = SYNC_AGENT_DM_MO_FAIL;
1350                 goto DACI_FINISH;
1351         }
1352         _stmt_bind_text(mo_handler, stmt, 1, runtime_property->acl);
1353         _stmt_bind_int(mo_handler, stmt, 2, runtime_property->format);
1354         _stmt_bind_text(mo_handler, stmt, 3, runtime_property->name);
1355         _stmt_bind_text(mo_handler, stmt, 4, runtime_property->size);
1356         _stmt_bind_text(mo_handler, stmt, 5, runtime_property->title);
1357         _stmt_bind_text(mo_handler, stmt, 6, runtime_property->tStamp);
1358         _stmt_bind_int(mo_handler, stmt, 7, runtime_property->type);
1359         _stmt_bind_text(mo_handler, stmt, 8, runtime_property->type_value);
1360         _stmt_bind_text(mo_handler, stmt, 9, runtime_property->verNo);
1361         _stmt_bind_int(mo_handler, stmt, 10, mo_node_id);
1362
1363         ret = _stmt_step(mo_handler, stmt);
1364         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
1365                 ret = SYNC_AGENT_DM_MO_FAIL;
1366
1367  DACI_FINISH:
1368
1369         if (stmt != NULL)
1370                 _stmt_finalize(mo_handler, stmt);
1371
1372         _EXTERN_FUNC_EXIT;
1373
1374         return ret;
1375 }
1376
1377 sync_agent_dm_mo_error_e dm_mo_get_runtime_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler, int mo_node_id, sync_agent_dm_mo_runtime_property_s ** runtime_property)
1378 {
1379         _EXTERN_FUNC_ENTER;
1380
1381         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1382
1383         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1384         daci_stmt stmt = NULL;
1385         char *query = "select * from runtime_property_tbl where node_id = ?";
1386         sync_agent_dm_mo_runtime_property_s *temp_runtime_property = 0;
1387
1388         stmt = _query_prepare(mo_handler, query, strlen(query));
1389         if (stmt != NULL) {
1390                 _stmt_bind_int(mo_handler, stmt, 1, mo_node_id);
1391
1392                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA) {
1393                         temp_runtime_property = SYNC_AGENT_DA_MEMORY_CALLOC(sync_agent_dm_mo_runtime_property_s *, sizeof(sync_agent_dm_mo_runtime_property_s), 1);
1394 /*                      temp_runtime_property->runtime_property_id = _stmt_column_int(stmt, 0);*/
1395                         temp_runtime_property->acl = _stmt_column_text(stmt, 1);
1396                         temp_runtime_property->format = _stmt_column_int(stmt, 2);
1397                         temp_runtime_property->name = _stmt_column_text(stmt, 3);
1398                         temp_runtime_property->size = _stmt_column_text(stmt, 4);
1399                         temp_runtime_property->title = _stmt_column_text(stmt, 5);
1400                         temp_runtime_property->tStamp = _stmt_column_text(stmt, 6);
1401                         temp_runtime_property->type = _stmt_column_int(stmt, 7);
1402                         temp_runtime_property->type_value = _stmt_column_text(stmt, 8);
1403                         temp_runtime_property->verNo = _stmt_column_text(stmt, 9);
1404 /*                      temp_runtime_property->node_id = _stmt_column_text(stmt, 10);*/
1405                 }
1406                 ret = _stmt_finalize(mo_handler, stmt);
1407         } else {
1408                 ret = SYNC_AGENT_DM_MO_FAIL;
1409         }
1410
1411         (*runtime_property) = temp_runtime_property;
1412
1413         _EXTERN_FUNC_EXIT;
1414
1415         return ret;
1416 }
1417
1418 sync_agent_dm_mo_error_e dm_mo_delete_all_runtime_property(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
1419 {
1420         _EXTERN_FUNC_ENTER;
1421
1422         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1423
1424         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
1425         daci_stmt stmt = NULL;
1426         char *query = NULL;
1427
1428         query = "delete from runtime_property_tbl";
1429
1430         stmt = _query_prepare(mo_handler, query, strlen(query));
1431         if (stmt == NULL) {
1432                 ret = SYNC_AGENT_DM_MO_FAIL;
1433                 goto DACI_FINISH;
1434         }
1435
1436         ret = _stmt_step(mo_handler, stmt);
1437
1438  DACI_FINISH:
1439
1440         if (stmt != NULL)
1441                 _stmt_finalize(mo_handler, stmt);
1442
1443         _EXTERN_FUNC_EXIT;
1444
1445         return ret;
1446 }
1447
1448 int _busy_handler(void *pData, int count)
1449 {
1450         _INNER_FUNC_ENTER;
1451
1452         _DEBUG_ERROR("_busy_handler %d called", count);
1453
1454         //retvm_if(pData == NULL, 0, "pData is NULL !!");
1455         warn_if(pData == NULL, "pData is NULL !!");
1456
1457         /*  sleep time when SQLITE_LOCK */
1458         //usleep(1500000);
1459         usleep(500000);
1460
1461         _INNER_FUNC_EXIT;
1462
1463         /* retry will be stopped if  busy handler return 0 */
1464         return SYNC_AGENT_DA_RETRY_COUNT - count;
1465 }
1466
1467 sync_agent_dm_mo_error_e _query_exec(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, char *err_msg)
1468 {
1469         _INNER_FUNC_ENTER;
1470
1471         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1472         retvm_if(query == NULL, SYNC_AGENT_DM_MO_FAIL, "query is NULL !!");
1473
1474         char *queryMsg = NULL;
1475         int ret = 0;
1476
1477         ret = sqlite3_exec(mo_handler, query, 0, 0, &queryMsg);
1478         if (ret != SQLITE_OK) {
1479                 _DEBUG_ERROR("%s(%d) : %s", err_msg, ret, queryMsg);
1480
1481                 if (queryMsg != NULL)
1482                         sqlite3_free(queryMsg);
1483
1484 /*
1485                 if (ret == SQLITE_BUSY)
1486                         return SYNC_AGENT_DM_MO_FAIL;
1487 */
1488
1489                 return SYNC_AGENT_DM_MO_FAIL;
1490         }
1491
1492         _INNER_FUNC_EXIT;
1493
1494         return SYNC_AGENT_DM_MO_SUCCESS;
1495 }
1496
1497 daci_stmt _query_prepare(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, int size)
1498 {
1499         _INNER_FUNC_ENTER;
1500
1501         retvm_if(mo_handler == NULL, NULL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1502         retvm_if(query == NULL, NULL, "query is NULL !!");
1503
1504         int ret = 0;
1505         daci_stmt stmt = NULL;
1506
1507         ret = sqlite3_prepare_v2(mo_handler, query, size, &stmt, 0);
1508         if (ret != SQLITE_OK)
1509                 _DEBUG_ERROR("sqlite3_query_prepare failed(%d) : %s ", ret, sqlite3_errmsg(mo_handler));
1510
1511         _INNER_FUNC_EXIT;
1512
1513         return stmt;
1514 }
1515
1516 sync_agent_da_return_e _stmt_bind_text(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index, const char *value)
1517 {
1518         _INNER_FUNC_ENTER;
1519
1520         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1521
1522         int ret = 0;
1523
1524         if (value != NULL) {
1525                 ret = sqlite3_bind_text(stmt, index, value, strlen(value), SQLITE_STATIC);
1526         } else {
1527                 ret = __stmt_bind_null(mo_handler, stmt, index);
1528                 if (ret == SYNC_AGENT_DA_SUCCESS)
1529                         ret = SQLITE_OK;
1530         }
1531
1532         if (ret != SQLITE_OK) {
1533                 _DEBUG_ERROR("sqlite3_stmt_bind_text failed(%d) : %s ", ret, sqlite3_errmsg(mo_handler));
1534                 return SYNC_AGENT_DA_ERR_QUERY_FAILED;
1535         }
1536
1537         _INNER_FUNC_EXIT;
1538
1539         return SYNC_AGENT_DA_SUCCESS;
1540 }
1541
1542 sync_agent_dm_mo_error_e _stmt_bind_int(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index, const int value)
1543 {
1544         _INNER_FUNC_ENTER;
1545
1546         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1547
1548         int ret = 0;
1549
1550         ret = sqlite3_bind_int(stmt, index, value);
1551         if (ret != SQLITE_OK) {
1552                 _DEBUG_ERROR("sqlite3_stmt_bind_int failed(%d) : %s \n", ret, sqlite3_errmsg(mo_handler));
1553                 return SYNC_AGENT_DM_MO_FAIL;
1554         }
1555
1556         _INNER_FUNC_EXIT;
1557
1558         return SYNC_AGENT_DM_MO_SUCCESS;
1559 }
1560
1561 sync_agent_dm_mo_error_e __stmt_bind_null(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt, int index)
1562 {
1563         _INNER_FUNC_ENTER;
1564
1565         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1566
1567         int ret = 0;
1568
1569         ret = sqlite3_bind_null(stmt, index);
1570         if (ret != SQLITE_OK) {
1571                 _DEBUG_ERROR("sqlite3_stmt_bind_null failed(%d) : %s", ret, sqlite3_errmsg(mo_handler));
1572                 return SYNC_AGENT_DM_MO_FAIL;
1573         }
1574
1575         _INNER_FUNC_EXIT;
1576
1577         return SYNC_AGENT_DM_MO_SUCCESS;
1578 }
1579
1580 sync_agent_da_return_e _stmt_step(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt)
1581 {
1582         _INNER_FUNC_ENTER;
1583
1584         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1585
1586         int ret = 0;
1587
1588         ret = sqlite3_step(stmt);
1589
1590         if (ret == SQLITE_ROW)
1591                 return SYNC_AGENT_DA_ERR_MORE_DATA;
1592
1593         if (ret != SQLITE_DONE) {
1594                 _DEBUG_ERROR("sqlite3_stmt_step failed(%d) : %s", ret, sqlite3_errmsg(mo_handler));
1595
1596 /*              if (ret == SQLITE_BUSY)
1597                         return SYNC_AGENT_DM_MO_FAIL;*/
1598
1599                 return SYNC_AGENT_DM_MO_FAIL;
1600         }
1601
1602         _INNER_FUNC_EXIT;
1603
1604         return SYNC_AGENT_DM_MO_SUCCESS;
1605 }
1606
1607 sync_agent_dm_mo_error_e _stmt_finalize(SYNC_AGENT_DA_MO_HANDLER * mo_handler, daci_stmt stmt)
1608 {
1609         _INNER_FUNC_ENTER;
1610
1611         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1612
1613         int ret = 0;
1614
1615         if (sqlite3_finalize(stmt) != SQLITE_OK) {
1616                 _DEBUG_ERROR("sqlite3_stmt_finalize failed(%d) : %s", ret, sqlite3_errmsg(mo_handler));
1617                 return SYNC_AGENT_DM_MO_FAIL;
1618         }
1619
1620         _INNER_FUNC_EXIT;
1621
1622         return SYNC_AGENT_DM_MO_SUCCESS;
1623 }
1624
1625 char *_stmt_column_text(daci_stmt stmt, int index)
1626 {
1627         _INNER_FUNC_ENTER;
1628
1629         retvm_if(stmt == NULL, NULL, "daci_stmt is NULL !!");
1630
1631         char *temp = NULL;
1632         temp = (char *)sqlite3_column_text(stmt, index);
1633
1634         _INNER_FUNC_EXIT;
1635
1636         return SYNC_AGENT_DA_STRDUP(temp);
1637 }
1638
1639 int _stmt_column_int(daci_stmt stmt, int index)
1640 {
1641         _INNER_FUNC_ENTER;
1642
1643         _INNER_FUNC_EXIT;
1644
1645         return (int)sqlite3_column_int(stmt, index);
1646 }
1647
1648 sync_agent_dm_mo_error_e _get_table(SYNC_AGENT_DA_MO_HANDLER * mo_handler, char *query, char ***result, int *row_count, int *col_count)
1649 {
1650         _INNER_FUNC_ENTER;
1651
1652         retvm_if(mo_handler == NULL, SYNC_AGENT_DM_MO_FAIL, "SYNC_AGENT_DA_MO_HANDLER is NULL !!");
1653
1654         int ret = 0;
1655         char *err_msg = NULL;
1656
1657         ret = sqlite3_get_table(mo_handler, query, result, row_count, col_count, &err_msg);
1658
1659         if (ret != SQLITE_OK) {
1660                 _DEBUG_ERROR("sqlite3_get_table failed(%d) : %s", ret, err_msg);
1661
1662                 _free_table(*result);
1663
1664                 if (err_msg != NULL)
1665                         sqlite3_free(err_msg);
1666
1667                 return SYNC_AGENT_DM_MO_FAIL;
1668         }
1669
1670         _INNER_FUNC_EXIT;
1671
1672         return SYNC_AGENT_DM_MO_SUCCESS;
1673 }
1674
1675 void _free_table(char **result)
1676 {
1677         _INNER_FUNC_ENTER;
1678
1679         if (result != NULL) {
1680                 sqlite3_free_table(result);
1681                 result = NULL;
1682         }
1683
1684         _INNER_FUNC_EXIT;
1685 }
1686
1687 static int _last_insert_id(SYNC_AGENT_DA_MO_HANDLER * mo_handler)
1688 {
1689         _INNER_FUNC_ENTER;
1690
1691         _INNER_FUNC_EXIT;
1692
1693         return sqlite3_last_insert_rowid(mo_handler);
1694 }
1695
1696 int _exist_table(SYNC_AGENT_DA_MO_HANDLER * mo_handler, const char *table_name)
1697 {
1698         _INNER_FUNC_ENTER;
1699
1700         daci_stmt stmt = NULL;
1701         int table_count = 0;
1702         char *query = "select count(*) from sqlite_master where tbl_name= ?";
1703
1704         stmt = _query_prepare(mo_handler, query, strlen(query));
1705         if ((stmt != NULL) && (_stmt_bind_text(mo_handler, stmt, 1, table_name) == SYNC_AGENT_DA_ERR_MORE_DATA)) {
1706
1707                 if (_stmt_step(mo_handler, stmt) == SYNC_AGENT_DA_ERR_MORE_DATA)
1708                         table_count = _stmt_column_int(stmt, 0);
1709
1710                 _stmt_finalize(mo_handler, stmt);
1711
1712                 if (table_count > 0) {
1713                         _INNER_FUNC_EXIT;
1714                         return 1;
1715                 }
1716         }
1717
1718         _INNER_FUNC_EXIT;
1719
1720         return 0;
1721 }
1722
1723 static char *_extract_dmacc_name(const char *mo_full_path)
1724 {
1725         _INNER_FUNC_ENTER;
1726
1727         char *dmacc_name = NULL;
1728
1729         int cnt = 0;
1730         const char *cursor = mo_full_path;
1731         const char *start_point = NULL;
1732         while (cursor != NULL) {
1733                 cursor = strchr(cursor, '/');
1734                 if (cursor == NULL) {
1735                         continue;
1736                 }
1737
1738                 cnt++;
1739
1740                 if (cnt == 2) {
1741                         start_point = cursor;
1742                 } else if (cnt == 3) {
1743                         int len = cursor - start_point + 2;
1744
1745                         dmacc_name = (char *)calloc(len, sizeof(char));
1746                         if (dmacc_name == NULL) {
1747                                 _DEBUG_ERROR("CALLOC failed !!!");
1748                                 return NULL;
1749                         }
1750                         strncpy(dmacc_name, start_point, len - 1);
1751
1752                         break;
1753                 }
1754
1755                 cursor += 1;
1756         }
1757
1758         _INNER_FUNC_EXIT;
1759
1760         return dmacc_name;
1761 }