1 /*-
2  * Copyright 2016 Vsevolod Stakhov
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 #ifndef SRC_LIBUTIL_SQLITE_UTILS_H_
17 #define SRC_LIBUTIL_SQLITE_UTILS_H_
18 
19 #include "config.h"
20 #include "mem_pool.h"
21 #include "sqlite3.h"
22 
23 #define RSPAMD_SQLITE3_STMT_MULTIPLE (1 << 0)
24 
25 #ifdef  __cplusplus
26 extern "C" {
27 #endif
28 
29 struct rspamd_sqlite3_prstmt {
30 	gint idx;
31 	const gchar *sql;
32 	const gchar *args;
33 	sqlite3_stmt *stmt;
34 	gint result;
35 	const gchar *ret;
36 	gint flags;
37 };
38 
39 /**
40  * Create prepared statements for specified database from init statements
41  * @param db
42  * @param max_idx
43  * @param err
44  * @return new prepared statements array or NULL
45  */
46 GArray *rspamd_sqlite3_init_prstmt (sqlite3 *db,
47 									struct rspamd_sqlite3_prstmt *init_stmt,
48 									gint max_idx,
49 									GError **err);
50 
51 /**
52  * Run prepared statements by its index getting parameters and setting results from
53  * varargs structure
54  * @param db
55  * @param stmts
56  * @param idx
57  * @return
58  */
59 gint rspamd_sqlite3_run_prstmt (rspamd_mempool_t *pool, sqlite3 *db, GArray *stmts,
60 								gint idx, ...);
61 
62 /**
63  * Close and free prepared statements
64  * @param db
65  * @param stmts
66  */
67 void rspamd_sqlite3_close_prstmt (sqlite3 *db, GArray *stmts);
68 
69 /**
70  * Creates or opens sqlite database trying to share it between processes
71  * @param path
72  * @param create_sql
73  * @return
74  */
75 sqlite3 *rspamd_sqlite3_open_or_create (rspamd_mempool_t *pool,
76 										const gchar *path, const gchar *create_sql,
77 										guint32 version, GError **err);
78 
79 
80 /**
81  * Sync sqlite3 db ensuring that all wal things are done
82  * @param db
83  */
84 gboolean rspamd_sqlite3_sync (sqlite3 *db, gint *wal_frames, gint *wal_checkpoints);
85 
86 #ifdef  __cplusplus
87 }
88 #endif
89 
90 #endif /* SRC_LIBUTIL_SQLITE_UTILS_H_ */
91