From fb9996dc7ab038954bcbfdc176f870d801c9e72f Mon Sep 17 00:00:00 2001
From: Michal Skorupinski The set, add and remove callbacks all work in the same way. They are used to inform if a given operation was successfull (provider_ret == true) or not. If an error in the communication occured, the error parameter should contain the error message. Note that in every callback except for the get one the get request is invoked. This way the UI can be updated with the modified hash map. Note that when error information is received, the function pointed by error_recived_cb() is invoked and a popup with the error message will be displayed. Initialization:Sql
+
+
+data_control_sql_create(&s_info.provider); ++ +
A valid provider_id and data_id have to to be set.
++data_control_sql_set_provider_id(s_info.provider, provider_id); +data_control_sql_set_data_id(s_info.provider, data_id); ++
Then, like in the map consumer's case, four request functions and four response callbacks have to be implemented.
+ ++/* Setting the callback pointers */ + s_info.sql_callback.delete_cb = __sql_delete_response_cb; + s_info.sql_callback.insert_cb = __sql_insert_response_cb; + s_info.sql_callback.select_cb = __sql_select_response_cb; + s_info.sql_callback.update_cb = __sql_update_response_cb; + +/* Registering the callbacks */ + ret = data_control_sql_register_response_cb(s_info.provider, &s_info.sql_callback, NULL); + ++ +
Select request
+The data_control_sql_select function is used to read data from the database. +
+data_control_sql_select(data_control_h provider, char **column_list, int column_count, const char *where, const char *order, int *request_id); ++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 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
+The delete request uses only the 'where' string described earlier. Every line in the database which meets the provided condition will be removed.
+ +Update request
+The update allows the user to modify a set of rows. Every row that meets the 'where' condition will be updated. To update given fields of a row a bundle is used in the same way as in the 'insert' request.
+ +Callbacks definitions:
+ +The function below is a helper function used in the 'SELECT' response. The row pointed at the result cursor is taken as the argument. The function returns a string containing the row's values. The values are formatted based on their type.
+ ++static const char *__sql_row_get(result_set_cursor cursor) +{ + /* ... */ + + column_count = data_control_sql_get_column_count(cursor); /* Get the current row's column count */ + + for (i = 0; i < column_count; ++i) { + + /* Get the column type (types available: DATA_CONTROL_SQL_COLUMN_TYPE_INT64, + DATA_CONTROL_SQL_COLUMN_TYPE_DOUBLE, + DATA_CONTROL_SQL_COLUMN_TYPE_TEXT, + DATA_CONTROL_SQL_COLUMN_TYPE_BLOB) */ + data_control_sql_get_column_item_type(cursor, i, &type); + + /* Based on the column type the given field's data is appended to the output string*/ + + } + } + + /* ... */ +} ++ +
The __sql_select_response_cb() is used when the SELECT response was invoked. This function uses the cursor to pass the response. The cursor is an result_set_cursor structure used to represent a sql result set. It can be enumerated to obtain every row in the result. The result is then transformed to an array of strings and sent to the view module so that the UI can be updated. The provider_result boolean value is used to inform whether the data was successfully processed.
+ ++static void __sql_select_response_cb(int request_id, data_control_h provider, result_set_cursor cursor, bool provider_result, const char *error, void *user_data) +{ + /* ... */ + + /* Iterate through all rows recived from the provider*/ + while (data_control_sql_step_next(cursor) == DATA_CONTROL_ERROR_NONE) { + if (!columns) + + /* Gets the columns names for the current row. It is assumed that columns names are the same in every row so this function is invoked only once. + The __column_list_get() function returns the names in one properly formatted string */ + columns = __column_list_get(cursor); + + row = __sql_row_get(cursor); /* The __sql_row_get() function returns the content of the row as one properly formatted string. */ + response = eina_list_append(response, row); + } + + /* The received data is sent to the view module */ + if (s_info.new_data_recived) + s_info.new_data_recived(columns, response); +} ++ +
The __sql_delete_response_cb, __sql_insert_response_cb and __sql_update_response_cb functions are used only to inform about the result of respective requests.
+ ++static void __sql_delete_response_cb(int request_id, data_control_h provider, bool provider_result, const char *error, void *user_data) +{ + /* ... */ +} ++ +
+static void __sql_insert_response_cb(int request_id, data_control_h provider, long long inserted_row_id, bool provider_result, const char *error, void *user_data) +{ + /* ... */ +} ++ +
+static void __sql_update_response_cb(int request_id, data_control_h provider, bool provider_result, const char *error, void *user_data) +{ + /* ... */ +} ++ +
Note that every callback uses the provider_result argument to inform about any erroneous situation (e.g. wrong WHERE filter used). In such a case the variable will equal false and the error argument will contain the error information. In case of an error the s_info.info_received function pointer will be invoked. This call is used to display a popup with the error message.
+ + -- 2.7.4