From 72c538db13f7a3081593cacade7d59a78cb52c99 Mon Sep 17 00:00:00 2001 From: Michal Skorupinski Date: Fri, 16 Oct 2015 17:19:48 +0200 Subject: [PATCH] [SAMPLE APP][DATA-CONTROL-PROVIDER] Sql provider callback descriptions Change-Id: I6fdea50ba1c93d05ad90fabfd16c9ec8a399d4a1 Signed-off-by: Michal Skorupinski --- .../html/mobile_n/data_control_consumer_sd_mn.htm | 2 +- .../html/mobile_n/data_control_provider_sd_mn.htm | 138 +++++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/org.tizen.sampledescriptions/html/mobile_n/data_control_consumer_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/data_control_consumer_sd_mn.htm index 7dbc797..4de0c88 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/data_control_consumer_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/data_control_consumer_sd_mn.htm @@ -491,7 +491,7 @@ data_control_sql_select(data_control_h provider, char **column_list, int column_ should be used. The select_array parameter is used as the text after the SELECT keyword in a sql query. The select_array is an array of strings. The column_name_entry entry field in the sql view can be used as the input for the select_array. Note however that the string from the entry has to be converted (using the elm_entry_markup_to_utf8()) from markup to utf8 format. If not, some characters, e.g '<', cannot be used. After the conversion, '\n' can be used as a delimiter when the text from the entry field is transformed into the array of strings. The select_array_count variable is the count of select_array elements. The final_where parameter is a string. It can contain any correct sql WHERE entry. The where_entry entry field is used to provide an input for this parameter. Of course this text (like any text from an entry widget) has to be converted to utf8 as well. If one of the sort methods is chosen using the radiobuttons, a column name has to be provided using the sort_column_entry field. The order_text parameter is a concatenation of the order_text string and an appropriate sort text, e.g. 'Num ASC'.

-

Insert request

+

Insert request

Insert request uses a bundle structure to generate an sql query. The column_name_entry and column_value_entry entries are used to provide input. The obtained strings are then tokenized and added to the bundle as a key value pair. The first line of the column_name_entry and the first line of the value_name_entry are used as the first pair and so on.

Delete request

diff --git a/org.tizen.sampledescriptions/html/mobile_n/data_control_provider_sd_mn.htm b/org.tizen.sampledescriptions/html/mobile_n/data_control_provider_sd_mn.htm index 249372e..f3f46a6 100644 --- a/org.tizen.sampledescriptions/html/mobile_n/data_control_provider_sd_mn.htm +++ b/org.tizen.sampledescriptions/html/mobile_n/data_control_provider_sd_mn.htm @@ -274,6 +274,144 @@ static void __data_map_remove(int request_id, const char* key, const char *value } + +

The SQL module

+

Structure definitions

+ +

The map module uses the following structure to hold its data:

+
+static struct {
+   data_control_provider_sql_cb sql_callback; /* Provider callbacks */
+   sqlite3 *db; /* The map structure */
+} s_info;
+
+ +

Initialization

+

The SQL module uses a SQLite database to hold data. As opposed to the map module, the SQL module uses an external file to store the data so it is not lost when the app is killed. Four callback are used to provide communication with the consumer application.

+ +

The function below is used to initialize the SQL provider module.

+
+void sql_provider_init(void)
+{
+   int result = __create_database(); /* Create and open a database file */
+
+   /* Initialize the callback structure with function pointers */
+   s_info.sql_callback.select_cb = __select_request_cb;
+   s_info.sql_callback.insert_cb = __insert_request_cb;
+   s_info.sql_callback.delete_cb = __delete_request_cb;
+   s_info.sql_callback.update_cb = __update_request_cb;
+
+   /* Register the callbacks */
+   result = data_control_provider_sql_register_cb(&s_info.sql_callback, NULL);
+   /* ... */
+}
+
+ +

Callbacks

+ +

The callback invoked when an 'insert' request is sent:

+
+static void __insert_request_cb(int request_id, data_control_h provider, bundle *insert_data, void *user_data)
+{
+   /* The insert_data argument contains a list of column name / value pairs. Check the consumer app documentation for details.
+   Based on the values stored in the bundle a SQL INSERT query is created. */
+   command = data_control_provider_create_insert_statement(provider, insert_data);
+   /* ... */
+
+   /* The new row is added to the database using standard SQLite API's functions */
+   ret = sqlite3_exec(s_info.db, command, NULL, NULL, NULL);
+   /* ... */
+   inserted_row_id = sqlite3_last_insert_rowid(s_info.db);
+
+   /* The operation result is sent back to the consumer application */
+   ret = data_control_provider_send_insert_result(request_id, inserted_row_id);
+
+   /* ... */
+}
+
+ +

The callback invoked when a 'delete' request is sent:

+
+static void __delete_request_cb(int request_id, data_control_h provider, const char *where, void *user_data)
+{
+   /* ... */
+
+   /* The 'where' text is used to create a SQL DELETE query */
+   command = data_control_provider_create_delete_statement(provider, where);
+   /* ... */
+
+   /* The new row is added to the database using standard SQLite API's functions */
+   ret = sqlite3_exec(s_info.db, command, NULL, NULL, NULL);
+   /* ... */
+
+   /* The operation result is sent back to the consumer application */
+   ret = data_control_provider_send_delete_result(request_id);
+   /* ... */
+}
+
+ +

The callback invoked when a 'select' request is sent:

+
+static void __select_request_cb(int request_id, data_control_h provider, const char **column_list,
+                  int column_count, const char *where, const char *order, void *user_data)
+{
+   /* ... */
+
+   /* A SQL SELECT query is created using the callback arguments. Note that some of the arguments can be NULL.
+   The resulting query:
+   SELECT column_list[0], column_list[1] .. column_list[column_count - 1] FROM Sample_Table WHERE where ORDER BY order;
+
+   If the where string is NULL, there is no filter used and if order string is NULL there is no sorting applied.
+   */
+   command = data_control_provider_create_select_statement(provider, column_list, column_count, where, order);
+   if (!command) {
+      dlog_print(DLOG_ERROR, LOG_TAG, "command == NULL");
+      return;
+   } else {
+      dlog_print(DLOG_ERROR, LOG_TAG, "command == %s", command);
+   }
+
+   /* A SQL statement object is created and sent to the consumer application */
+   ret = sqlite3_prepare_v2(s_info.db, command, strlen(command), &sql_stmt, NULL);
+   /* ... */
+
+   ret = data_control_provider_send_select_result(request_id, (void *)sql_stmt);
+   /* ... */
+
+   sqlite3_finalize(sql_stmt);
+
+   /* ... */
+}
+
+ +

The callback invoked when an 'update' request is sent:

+
+static void __update_request_cb(int request_id, data_control_h provider, bundle *update_data, const char *where, void *user_data)
+{
+   int ret = -1;
+   char *command = NULL;
+
+
+   /* The SQL query is created using the bundle structure similar to the structure used in add query and where string similar to the one used in the delete query.
+   The final query will look like this:
+   UPDATE Sample_Table SET bundle_key1 = bundle_value_1, bundle_key1 = bundle_value_1, ..., columnN = valueN WHERE where
+
+   */
+   command = data_control_provider_create_update_statement(provider, update_data, where);
+   if (!command) {
+      dlog_print(DLOG_ERROR, LOG_TAG, "command == NULL");
+      return;
+   }
+
+   /* The generated command is then used as a SQLite query */
+   ret = sqlite3_exec(s_info.db, command, NULL, NULL, NULL);
+   /* ... */
+
+   /* After the database update information about the update's result is sent to the consumer app*/
+   ret = data_control_provider_send_update_result(request_id);
+   /* ... */
+}
+
-- 2.7.4