Grassroots Infrastructure
The Grassroots Infrastructure is a suite of computing tools to help users and developers use scientific data infrastructure that can easily be interconnected.
sqlite_tool.h
Go to the documentation of this file.
1 /*
2 ** Copyright 2014-2016 The Earlham Institute
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
21 /*
22  * sqlite_tool.h
23  *
24  * Created on: 26 Jun 2015
25  * Author: billy
26  */
27 
28 #ifndef SQLITE_TOOL_H_
29 #define SQLITE_TOOL_H_
30 
31 #include "typedefs.h"
32 #include "jansson.h"
33 #include "sqlite_library.h"
34 #include "sqlite3.h"
35 #include "linked_list.h"
36 
37 
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 
40 #ifdef ALLOCATE_SQLITE_TAGS
41  #define SQLITE_PREFIX GRASSROOTS_SQLITE_API
42  #define SQLITE_VAL(x) = x
43 #else
44  #define SQLITE_PREFIX extern GRASSROOTS_SQLITE_API
45  #define SQLITE_VAL(x)
46 #endif
47 
48 #endif /* #ifndef DOXYGEN_SHOULD_SKIP_THIS */
49 
50 
51 
56 SQLITE_PREFIX const char *SQLITE_OP_EQUALS_S SQLITE_VAL("=");
57 
58 SQLITE_PREFIX const char *SQLITE_SET_CLAUSE_S SQLITE_VAL("set");
59 
60 SQLITE_PREFIX const char *SQLITE_WHERE_CLAUSE_S SQLITE_VAL("where");
61 
62 
69 typedef struct SQLiteTool
70 {
76  sqlite3 *sqlt_database_p;
77 
81  char *sqlt_table_s;
82 } SQLiteTool;
83 
84 
85 
86 #ifdef __cplusplus
87 extern "C"
88 {
89 #endif
90 
91 
103 GRASSROOTS_SQLITE_API bool SetSQLiteDatabase (SQLiteTool *tool_p, const char *db_s, int flags);
104 
105 
115 GRASSROOTS_SQLITE_API bool SetSQLiteToolTable (SQLiteTool *tool_p, const char *table_s);
116 
117 
118 
130 GRASSROOTS_SQLITE_API SQLiteTool *AllocateSQLiteTool (const char *db_s, int flags);
131 
132 
142 
143 
144 
153 
154 
167 GRASSROOTS_SQLITE_API char *InsertOrUpdateSQLiteData (SQLiteTool *tool_p, json_t *values_p, const char * const table_s, const char * const primary_key_s);
168 
169 
181 GRASSROOTS_SQLITE_API bool RemoveSQLiteRows (SQLiteTool *tool_p, const json_t *selector_json_p, const bool remove_first_match_only_flag);
182 
183 
199 GRASSROOTS_SQLITE_API json_t *FindMatchingSQLiteDocuments (SQLiteTool *tool_p, LinkedList *where_clauses_p, const char **fields_ss, char **error_ss);
200 
201 
210 
211 
226 GRASSROOTS_SQLITE_API int32 GetAllSQLiteResultsForKeyValuePair (SQLiteTool *tool_p, json_t **docs_pp, const char * const key_s, const char * const value_s, const char **fields_ss);
227 
228 
241 GRASSROOTS_SQLITE_API json_t *GetCurrentValuesFromSQLiteToolAsJSON (SQLiteTool *tool_p, const char **fields_ss, const size_t num_fields);
242 
243 
253 
254 
265 GRASSROOTS_SQLITE_API int32 IsKeyValuePairInDatabase (SQLiteTool *tool_p, const char *database_s, const char *key_s, const char *value_s);
266 
267 
278 GRASSROOTS_SQLITE_API char *InsertOrUpdateSQLiteData (SQLiteTool *tool_p, json_t *values_p, const char * const table_s, const char * const primary_key_s);
279 
280 
288 GRASSROOTS_SQLITE_API void FreeSQLiteToolErrorString (SQLiteTool *tool_p, char *error_s);
289 
290 
303 GRASSROOTS_SQLITE_API char *CreateSQLiteTable (SQLiteTool *tool_p, const char *table_s, LinkedList *columns_p, const bool delete_if_exists_flag);
304 
305 
306 GRASSROOTS_SQLITE_API char *RunSQLiteToolStatement (SQLiteTool *tool_p, const char *sql_s, int (*callback_fn) (void *data_p, int num_columns, char **columns_aa_text_ss, char **column_names_ss), void *data_p);
307 
308 GRASSROOTS_SQLITE_API char *EasyRunSQLiteToolStatement (SQLiteTool *tool_p, const char *sql_s);
309 
310 
311 GRASSROOTS_SQLITE_API bool InsertSQLiteRow (SQLiteTool *tool_p, const json_t *data_p, char **error_ss);
312 
313 
314 GRASSROOTS_SQLITE_API bool PrepareStatement (SQLiteTool *tool_p, sqlite3_stmt **statement_pp, const char *sql_s);
315 
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 
321 
322 
323 #endif /* _TOOL_H_ */
SQLiteTool::GetCurrentValuesFromSQLiteToolAsJSON
json_t * GetCurrentValuesFromSQLiteToolAsJSON(SQLiteTool *tool_p, const char **fields_ss, const size_t num_fields)
When iterating over a SQLite's results get the values and increment the current result that the SQLit...
InsertSQLiteRow
bool InsertSQLiteRow(SQLiteTool *tool_p, const json_t *data_p, char **error_ss)
SQLiteTool::sqlt_table_s
char * sqlt_table_s
The name of the table that this SQLiteTool is currently accessing.
Definition: sqlite_tool.h:81
SQLiteTool::InsertOrUpdateSQLiteData
char * InsertOrUpdateSQLiteData(SQLiteTool *tool_p, json_t *values_p, const char *const table_s, const char *const primary_key_s)
Insert data from a given JSON fragment using a given SQLite.
SQLiteTool::FreeSQLiteToolErrorString
void FreeSQLiteToolErrorString(SQLiteTool *tool_p, char *error_s)
Free an error string that was generated from a given SQLiteTool.
GRASSROOTS_SQLITE_API
#define GRASSROOTS_SQLITE_API
Definition: sqlite_library.h:46
SQLiteTool::CreateSQLiteTable
char * CreateSQLiteTable(SQLiteTool *tool_p, const char *table_s, LinkedList *columns_p, const bool delete_if_exists_flag)
Create a table for the given SQLiteTool.
SQLiteTool::SQLITE_VAL
const SQLITE_PREFIX char *SQLITE_OP_EQUALS_S SQLITE_VAL("=")
typedefs.h
SQLiteTool::SetSQLiteToolTable
bool SetSQLiteToolTable(SQLiteTool *tool_p, const char *table_s)
Set the tablethat a SQLite will use.
SQLiteTool::GetAllSQLiteResultsAsJSON
json_t * GetAllSQLiteResultsAsJSON(SQLiteTool *tool_p)
Get all results from a SQLite database.
LinkedList
A doubly-linked list that can be traversed in either direction.
Definition: linked_list.h:56
SQLiteTool::CloseSQLiteTool
bool CloseSQLiteTool(SQLiteTool *tool_p)
Close the database connection for a given a SQLiteTool.
SQLiteTool::FreeSQLiteTool
void FreeSQLiteTool(SQLiteTool *tool_p)
Delete a SQLiteTool and release the connection that it held This calls CloseSqlTool() and then releas...
SQLiteTool::GetAllSQLiteResultsForKeyValuePair
int32 GetAllSQLiteResultsForKeyValuePair(SQLiteTool *tool_p, json_t **docs_pp, const char *const key_s, const char *const value_s, const char **fields_ss)
Get all results for a given key-value query.
linked_list.h
A doubly-linked list.
RunSQLiteToolStatement
char * RunSQLiteToolStatement(SQLiteTool *tool_p, const char *sql_s, int(*callback_fn)(void *data_p, int num_columns, char **columns_aa_text_ss, char **column_names_ss), void *data_p)
SQLiteTool::IsKeyValuePairInDatabase
int32 IsKeyValuePairInDatabase(SQLiteTool *tool_p, const char *database_s, const char *key_s, const char *value_s)
Check whether a database_s contains any documents with a given key-value pair.
SQLiteTool::AllocateSQLiteTool
SQLiteTool * AllocateSQLiteTool(const char *db_s, int flags)
This allocates a SQLiteTool that connects to the SQLite instance specified in the grassroots....
sqlite_library.h
SQLiteTool::RemoveSQLiteRows
bool RemoveSQLiteRows(SQLiteTool *tool_p, const json_t *selector_json_p, const bool remove_first_match_only_flag)
Remove some documents from a SQLite collection.
SQLiteTool::FindMatchingSQLiteDocuments
json_t * FindMatchingSQLiteDocuments(SQLiteTool *tool_p, LinkedList *where_clauses_p, const char **fields_ss, char **error_ss)
Find matching documents for a given query.
EasyRunSQLiteToolStatement
char * EasyRunSQLiteToolStatement(SQLiteTool *tool_p, const char *sql_s)
SQLiteTool
A SQLiteTool is a datatype that allows access to the data stored within a SQLite instance.
Definition: sqlite_tool.h:69
PrepareStatement
bool PrepareStatement(SQLiteTool *tool_p, sqlite3_stmt **statement_pp, const char *sql_s)
SQLiteTool::GetAllExistingSQLiteResultsAsJSON
json_t * GetAllExistingSQLiteResultsAsJSON(SQLiteTool *tool_p)
Convert all of a SQLiteTool results into JSON array.
SQLiteTool::SetSQLiteDatabase
bool SetSQLiteDatabase(SQLiteTool *tool_p, const char *db_s, int flags)
Set the database an collection that a SQLite will use.