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