Mercurial > dovecot > core-2.2
annotate src/lib-dict/dict-sql.c @ 22355:d950ffd7cbe0
dict-sql: Add support for sql_result_more()
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Mon, 17 Jul 2017 17:54:36 +0300 |
parents | 66a1a4338260 |
children | 2356b44ebec8 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21389
diff
changeset
|
1 /* Copyright (c) 2005-2017 Dovecot authors, see the included COPYING file */ |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
3943
cbe5c6772e0d
Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
4 #include "array.h" |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 #include "istream.h" |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
6 #include "hex-binary.h" |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 #include "str.h" |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 #include "sql-api-private.h" |
11252
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
9 #include "sql-db-cache.h" |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 #include "dict-private.h" |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
11 #include "dict-sql-settings.h" |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 #include "dict-sql.h" |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 #include <unistd.h> |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 #include <fcntl.h> |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 |
6480 | 17 #define DICT_SQL_MAX_UNUSED_CONNECTIONS 10 |
18 | |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
19 enum sql_recurse_type { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
20 SQL_DICT_RECURSE_NONE, |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
21 SQL_DICT_RECURSE_ONE, |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
22 SQL_DICT_RECURSE_FULL |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
23 }; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
24 |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 struct sql_dict { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 struct dict dict; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 pool_t pool; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 struct sql_db *db; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
30 const char *username; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
31 const struct dict_sql_settings *set; |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
32 |
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
33 unsigned int has_on_duplicate_key:1; |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
34 unsigned int has_using_timestamp:1; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 }; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 struct sql_dict_iterate_context { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 struct dict_iterate_context ctx; |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
39 pool_t pool; |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
40 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
41 enum dict_iterate_flags flags; |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
42 const char **paths; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 struct sql_result *result; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
45 string_t *key; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
46 const struct dict_sql_map *map; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
47 size_t key_prefix_len, pattern_prefix_len; |
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
48 unsigned int path_idx, sql_fields_start_idx, next_map_idx; |
19316
467695fee373
dict-sql: Fixed async iteration with MySQL and SQLite
Timo Sirainen <tss@iki.fi>
parents:
19303
diff
changeset
|
49 bool synchronous_result; |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
50 bool failed; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
51 bool iter_query_sent; |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
52 bool allow_null_map; /* allow next map to be NULL */ |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 }; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
55 struct sql_dict_inc_row { |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
56 struct sql_dict_inc_row *prev; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
57 unsigned int rows; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
58 }; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
59 |
3787
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
60 struct sql_dict_transaction_context { |
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
61 struct dict_transaction_context ctx; |
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
62 |
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
63 struct sql_transaction_context *sql_ctx; |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
64 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
65 const struct dict_sql_map *prev_inc_map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
66 char *prev_inc_key; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
67 long long prev_inc_diff; |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
68 pool_t inc_row_pool; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
69 struct sql_dict_inc_row *inc_row; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
70 |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
71 const struct dict_sql_map *prev_set_map; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
72 char *prev_set_key; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
73 char *prev_set_value; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
74 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
75 dict_transaction_commit_callback_t *async_callback; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
76 void *async_context; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
77 |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
78 unsigned int failed:1; |
3787
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
79 }; |
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
80 |
11252
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
81 static struct sql_db_cache *dict_sql_db_cache; |
6480 | 82 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
83 static void sql_dict_prev_inc_flush(struct sql_dict_transaction_context *ctx); |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
84 static void sql_dict_prev_set_flush(struct sql_dict_transaction_context *ctx); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
85 |
15067
9bff478f81fd
dict_init() API changed to return int.
Timo Sirainen <tss@iki.fi>
parents:
14920
diff
changeset
|
86 static int |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
87 sql_dict_init(struct dict *driver, const char *uri, |
17751
77e71a45a475
lib-dict: Changed dict.init() API to take struct dict_settings and added dict_init_full().
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
88 const struct dict_settings *set, |
15218
22c22d704422
lib-dict: dict_init() now returns error string instead of logging it
Timo Sirainen <tss@iki.fi>
parents:
15067
diff
changeset
|
89 struct dict **dict_r, const char **error_r) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
90 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
91 struct sql_dict *dict; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
92 pool_t pool; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
93 |
8685
6ee78e18d026
Increased some initial memory pool sizes.
Timo Sirainen <tss@iki.fi>
parents:
8660
diff
changeset
|
94 pool = pool_alloconly_create("sql dict", 2048); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
95 dict = p_new(pool, struct sql_dict, 1); |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
96 dict->pool = pool; |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
97 dict->dict = *driver; |
17751
77e71a45a475
lib-dict: Changed dict.init() API to take struct dict_settings and added dict_init_full().
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
98 dict->username = p_strdup(pool, set->username); |
20165
87d2e33aef64
dict-sql: Cache reading settings files.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
99 dict->set = dict_sql_settings_read(uri, error_r); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
100 if (dict->set == NULL) { |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
101 pool_unref(&pool); |
15067
9bff478f81fd
dict_init() API changed to return int.
Timo Sirainen <tss@iki.fi>
parents:
14920
diff
changeset
|
102 return -1; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
103 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
104 |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
105 /* currently pgsql and sqlite don't support "ON DUPLICATE KEY" */ |
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
106 dict->has_on_duplicate_key = strcmp(driver->name, "mysql") == 0; |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
107 /* only Cassandra CQL supports "USING TIMESTAMP" */ |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
108 dict->has_using_timestamp = strcmp(driver->name, "cassandra") == 0; |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
109 |
11252
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
110 dict->db = sql_db_cache_new(dict_sql_db_cache, driver->name, |
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
111 dict->set->connect); |
15067
9bff478f81fd
dict_init() API changed to return int.
Timo Sirainen <tss@iki.fi>
parents:
14920
diff
changeset
|
112 *dict_r = &dict->dict; |
9bff478f81fd
dict_init() API changed to return int.
Timo Sirainen <tss@iki.fi>
parents:
14920
diff
changeset
|
113 return 0; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
114 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
115 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
116 static void sql_dict_deinit(struct dict *_dict) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
117 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
118 struct sql_dict *dict = (struct sql_dict *)_dict; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
119 |
3879
928229f8b3e6
deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
120 sql_deinit(&dict->db); |
6428
7cad076906eb
pool_unref() now takes ** pointer.
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
121 pool_unref(&dict->pool); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
122 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
123 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
124 static int sql_dict_wait(struct dict *dict ATTR_UNUSED) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
125 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
126 /* FIXME: lib-sql doesn't support this yet */ |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
127 return 0; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
128 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
129 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
130 static bool |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
131 dict_sql_map_match(const struct dict_sql_map *map, const char *path, |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
132 ARRAY_TYPE(const_string) *values, size_t *pat_len_r, |
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
133 size_t *path_len_r, bool partial_ok, bool recurse) |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
134 { |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
135 const char *path_start = path; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
136 const char *pat, *field, *p; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
137 size_t len; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
138 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
139 array_clear(values); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
140 pat = map->pattern; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
141 while (*pat != '\0' && *path != '\0') { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
142 if (*pat == '$') { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
143 /* variable */ |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
144 pat++; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
145 if (*pat == '\0') { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
146 /* pattern ended with this variable, |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
147 it'll match the rest of the path */ |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
148 len = strlen(path); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
149 if (partial_ok) { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
150 /* iterating - the last field never |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
151 matches fully. if there's a trailing |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
152 '/', drop it. */ |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
153 pat--; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
154 if (path[len-1] == '/') { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
155 field = t_strndup(path, len-1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
156 array_append(values, &field, 1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
157 } else { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
158 array_append(values, &path, 1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
159 } |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
160 } else { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
161 array_append(values, &path, 1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
162 path += len; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
163 } |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
164 *path_len_r = path - path_start; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
165 *pat_len_r = pat - map->pattern; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
166 return TRUE; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
167 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
168 /* pattern matches until the next '/' in path */ |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
169 p = strchr(path, '/'); |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
170 if (p != NULL) { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
171 field = t_strdup_until(path, p); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
172 array_append(values, &field, 1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
173 path = p; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
174 } else { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
175 /* no '/' anymore, but it'll still match a |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
176 partial */ |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
177 array_append(values, &path, 1); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
178 path += strlen(path); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
179 pat++; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
180 } |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
181 } else if (*pat == *path) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
182 pat++; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
183 path++; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
184 } else { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
185 return FALSE; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
186 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
187 } |
14391
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
188 |
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
189 *path_len_r = path - path_start; |
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
190 *pat_len_r = pat - map->pattern; |
ba1fc76e3a2c
Changes to make static analyzer happier.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
191 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
192 if (*pat == '\0') |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
193 return *path == '\0'; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
194 else if (!partial_ok) |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
195 return FALSE; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
196 else { |
18653
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
197 /* partial matches must end with '/'. */ |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
198 if (pat != map->pattern && pat[-1] != '/') |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
199 return FALSE; |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
200 /* if we're not recursing, there should be only one $variable |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
201 left. */ |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
202 if (recurse) |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
203 return TRUE; |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
204 return pat[0] == '$' && strchr(pat, '/') == NULL; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
205 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
206 } |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
207 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
208 static const struct dict_sql_map * |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
209 sql_dict_find_map(struct sql_dict *dict, const char *path, |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
210 ARRAY_TYPE(const_string) *values) |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
211 { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
212 const struct dict_sql_map *maps; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
213 unsigned int i, count; |
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
214 size_t len; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
215 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
216 t_array_init(values, dict->set->max_field_count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
217 maps = array_get(&dict->set->maps, &count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
218 for (i = 0; i < count; i++) { |
18832
1a778838d3e1
dict-sql: Don't try to optimize finding a matching map by using the previous match.
Timo Sirainen <tss@iki.fi>
parents:
18653
diff
changeset
|
219 if (dict_sql_map_match(&maps[i], path, values, |
1a778838d3e1
dict-sql: Don't try to optimize finding a matching map by using the previous match.
Timo Sirainen <tss@iki.fi>
parents:
18653
diff
changeset
|
220 &len, &len, FALSE, FALSE)) |
1a778838d3e1
dict-sql: Don't try to optimize finding a matching map by using the previous match.
Timo Sirainen <tss@iki.fi>
parents:
18653
diff
changeset
|
221 return &maps[i]; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
222 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
223 return NULL; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
224 } |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
225 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
226 static int |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
227 sql_dict_value_escape(string_t *str, struct sql_dict *dict, |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
228 const struct dict_sql_map *map, |
19256
6785752f1e25
dict-sql: Added support for extensible field types.
Timo Sirainen <tss@iki.fi>
parents:
19220
diff
changeset
|
229 enum dict_sql_type value_type, const char *field_name, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
230 const char *value, const char *value_suffix, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
231 const char **error_r) |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
232 { |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
233 buffer_t *buf; |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
234 unsigned int num; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
235 |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
236 switch (value_type) { |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
237 case DICT_SQL_TYPE_STRING: |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
238 str_printfa(str, "'%s%s'", sql_escape_string(dict->db, value), |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
239 value_suffix); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
240 return 0; |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
241 case DICT_SQL_TYPE_UINT: |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
242 if (value_suffix[0] != '\0' || str_to_uint(value, &num) < 0) { |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
243 *error_r = t_strdup_printf( |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
244 "%s field's value isn't unsigned integer: %s%s (in pattern: %s)", |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
245 field_name, value, value_suffix, map->pattern); |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
246 return -1; |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
247 } |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
248 str_printfa(str, "%u", num); |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
249 return 0; |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
250 case DICT_SQL_TYPE_HEXBLOB: |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
251 break; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
252 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
253 |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
254 buf = buffer_create_dynamic(pool_datastack_create(), strlen(value)/2); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
255 if (hex_to_binary(value, buf) < 0) { |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
256 /* we shouldn't get untrusted input here. it's also a bit |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
257 annoying to handle this error. */ |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
258 *error_r = t_strdup_printf("%s field's value isn't hexblob: %s (in pattern: %s)", |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
259 field_name, value, map->pattern); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
260 return -1; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
261 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
262 str_append(buf, value_suffix); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
263 str_append(str, sql_escape_blob(dict->db, buf->data, buf->used)); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
264 return 0; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
265 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
266 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
267 static int |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
268 sql_dict_field_escape_value(string_t *str, struct sql_dict *dict, |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
269 const struct dict_sql_map *map, |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
270 const struct dict_sql_field *field, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
271 const char *value, const char *value_suffix, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
272 const char **error_r) |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
273 { |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
274 return sql_dict_value_escape(str, dict, map, field->value_type, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
275 field->name, value, value_suffix, error_r); |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
276 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
277 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
278 static int |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
279 sql_dict_where_build(struct sql_dict *dict, const struct dict_sql_map *map, |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
280 const ARRAY_TYPE(const_string) *values_arr, |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
281 char key1, enum sql_recurse_type recurse_type, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
282 string_t *query, const char **error_r) |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
283 { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
284 const struct dict_sql_field *sql_fields; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
285 const char *const *values; |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
286 unsigned int i, count, count2, exact_count; |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
287 bool priv = key1 == DICT_PATH_PRIVATE[0]; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
288 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
289 sql_fields = array_get(&map->sql_fields, &count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
290 values = array_get(values_arr, &count2); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
291 /* if we came here from iteration code there may be less values */ |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
292 i_assert(count2 <= count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
293 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
294 if (count2 == 0 && !priv) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
295 /* we want everything */ |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
296 return 0; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
297 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
298 |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
299 str_append(query, " WHERE"); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
300 exact_count = count == count2 && recurse_type != SQL_DICT_RECURSE_NONE ? |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
301 count2-1 : count2; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
302 for (i = 0; i < exact_count; i++) { |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
303 if (i > 0) |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
304 str_append(query, " AND"); |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
305 str_printfa(query, " %s = ", sql_fields[i].name); |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
306 if (sql_dict_field_escape_value(query, dict, map, &sql_fields[i], |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
307 values[i], "", error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
308 return -1; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
309 } |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
310 switch (recurse_type) { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
311 case SQL_DICT_RECURSE_NONE: |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
312 break; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
313 case SQL_DICT_RECURSE_ONE: |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
314 if (i > 0) |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
315 str_append(query, " AND"); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
316 if (i < count2) { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
317 str_printfa(query, " %s LIKE ", sql_fields[i].name); |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
318 if (sql_dict_field_escape_value(query, dict, map, &sql_fields[i], |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
319 values[i], "/%", error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
320 return -1; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
321 str_printfa(query, " AND %s NOT LIKE ", sql_fields[i].name); |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
322 if (sql_dict_field_escape_value(query, dict, map, &sql_fields[i], |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
323 values[i], "/%/%", error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
324 return -1; |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
325 } else { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
326 str_printfa(query, " %s LIKE '%%' AND " |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
327 "%s NOT LIKE '%%/%%'", |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
328 sql_fields[i].name, sql_fields[i].name); |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
329 } |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
330 break; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
331 case SQL_DICT_RECURSE_FULL: |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
332 if (i < count2) { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
333 if (i > 0) |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
334 str_append(query, " AND"); |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
335 str_printfa(query, " %s LIKE ", |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
336 sql_fields[i].name); |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
337 if (sql_dict_field_escape_value(query, dict, map, &sql_fields[i], |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
338 values[i], "/%", error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
339 return -1; |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
340 } |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
341 break; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
342 } |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
343 if (priv) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
344 if (count2 > 0) |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
345 str_append(query, " AND"); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
346 str_printfa(query, " %s = '%s'", map->username_field, |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
347 sql_escape_string(dict->db, dict->username)); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
348 } |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
349 return 0; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
350 } |
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
351 |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
352 static int |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
353 sql_lookup_get_query(struct sql_dict *dict, const char *key, |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
354 string_t *query, const struct dict_sql_map **map_r, |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
355 const char **error_r) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
356 { |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
357 const struct dict_sql_map *map; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
358 ARRAY_TYPE(const_string) values; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
359 const char *error; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
360 |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
361 map = *map_r = sql_dict_find_map(dict, key, &values); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
362 if (map == NULL) { |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
363 *error_r = t_strdup_printf( |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
364 "sql dict lookup: Invalid/unmapped key: %s", key); |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
365 return -1; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
366 } |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
367 str_printfa(query, "SELECT %s FROM %s", |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
368 map->value_field, map->table); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
369 if (sql_dict_where_build(dict, map, &values, key[0], |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
370 SQL_DICT_RECURSE_NONE, query, &error) < 0) { |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
371 *error_r = t_strdup_printf( |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
372 "sql dict lookup: Failed to lookup key %s: %s", key, error); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
373 return -1; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
374 } |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
375 return 0; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
376 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
377 |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
378 static const char * |
19256
6785752f1e25
dict-sql: Added support for extensible field types.
Timo Sirainen <tss@iki.fi>
parents:
19220
diff
changeset
|
379 sql_dict_result_unescape(enum dict_sql_type type, pool_t pool, |
6785752f1e25
dict-sql: Added support for extensible field types.
Timo Sirainen <tss@iki.fi>
parents:
19220
diff
changeset
|
380 struct sql_result *result, unsigned int result_idx) |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
381 { |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
382 const unsigned char *data; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
383 size_t size; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
384 string_t *str; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
385 |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
386 switch (type) { |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
387 case DICT_SQL_TYPE_STRING: |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
388 case DICT_SQL_TYPE_UINT: |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
389 return p_strdup(pool, sql_result_get_field_value(result, result_idx)); |
19257
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
390 case DICT_SQL_TYPE_HEXBLOB: |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
391 break; |
22015b472147
dict-sql: Added support for "uint" field type.
Timo Sirainen <tss@iki.fi>
parents:
19256
diff
changeset
|
392 } |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
393 |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
394 data = sql_result_get_field_value_binary(result, result_idx, &size); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
395 str = str_new(pool, size*2 + 1); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
396 binary_to_hex_append(str, data, size); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
397 return str_c(str); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
398 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
399 |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
400 static const char * |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
401 sql_dict_result_unescape_value(const struct dict_sql_map *map, pool_t pool, |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
402 struct sql_result *result) |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
403 { |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
404 return sql_dict_result_unescape(map->value_types[0], pool, result, 0); |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
405 } |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
406 |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
407 static const char *const * |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
408 sql_dict_result_unescape_values(const struct dict_sql_map *map, pool_t pool, |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
409 struct sql_result *result) |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
410 { |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
411 const char **values; |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
412 unsigned int i; |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
413 |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
414 values = p_new(pool, const char *, map->values_count + 1); |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
415 for (i = 0; i < map->values_count; i++) { |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
416 values[i] = sql_dict_result_unescape(map->value_types[i], |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
417 pool, result, i); |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
418 } |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
419 return values; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
420 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
421 |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
422 static const char * |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
423 sql_dict_result_unescape_field(const struct dict_sql_map *map, pool_t pool, |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
424 struct sql_result *result, unsigned int result_idx, |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
425 unsigned int sql_field_idx) |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
426 { |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
427 const struct dict_sql_field *sql_field; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
428 |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
429 sql_field = array_idx(&map->sql_fields, sql_field_idx); |
19256
6785752f1e25
dict-sql: Added support for extensible field types.
Timo Sirainen <tss@iki.fi>
parents:
19220
diff
changeset
|
430 return sql_dict_result_unescape(sql_field->value_type, pool, |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
431 result, result_idx); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
432 } |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
433 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
434 static int sql_dict_lookup(struct dict *_dict, pool_t pool, |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
435 const char *key, const char **value_r) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
436 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
437 struct sql_dict *dict = (struct sql_dict *)_dict; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
438 const struct dict_sql_map *map; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
439 struct sql_result *result = NULL; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
440 int ret; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
441 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
442 T_BEGIN { |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
443 string_t *query = t_str_new(256); |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
444 const char *error; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
445 |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
446 ret = sql_lookup_get_query(dict, key, query, &map, &error); |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
447 if (ret < 0) |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
448 i_error("%s", error); |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
449 else |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
450 result = sql_query_s(dict->db, str_c(query)); |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
451 } T_END; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
452 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
453 if (ret < 0) { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
454 *value_r = NULL; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
455 return -1; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
456 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
457 |
3787
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
458 ret = sql_result_next_row(result); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
459 if (ret <= 0) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
460 if (ret < 0) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
461 i_error("dict sql lookup failed: %s", |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
462 sql_result_get_error(result)); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
463 } |
3787
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
464 *value_r = NULL; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
465 } else { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
466 *value_r = sql_dict_result_unescape_value(map, pool, result); |
3787
40600601502e
Added dictinary lookup server and a "proxy" dict implementation to talk to
Timo Sirainen <tss@iki.fi>
parents:
3737
diff
changeset
|
467 } |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
468 |
9270
f9ebd72a73e8
SQL API change: SQL results can be now refed/unrefed.
Timo Sirainen <tss@iki.fi>
parents:
9056
diff
changeset
|
469 sql_result_unref(result); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
470 return ret; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
471 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
472 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
473 struct sql_dict_lookup_context { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
474 const struct dict_sql_map *map; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
475 dict_lookup_callback_t *callback; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
476 void *context; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
477 }; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
478 |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
479 static void |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
480 sql_dict_lookup_async_callback(struct sql_result *sql_result, |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
481 struct sql_dict_lookup_context *ctx) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
482 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
483 struct dict_lookup_result result; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
484 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21381
diff
changeset
|
485 i_zero(&result); |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
486 result.ret = sql_result_next_row(sql_result); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
487 if (result.ret < 0) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
488 result.error = sql_result_get_error(sql_result); |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
489 else if (result.ret > 0) { |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
490 result.values = sql_dict_result_unescape_values(ctx->map, |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
491 pool_datastack_create(), sql_result); |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
492 result.value = result.values[0]; |
20454
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
493 if (result.value == NULL) { |
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
494 /* NULL value returned. we'll treat this as |
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
495 "not found", which is probably what is usually |
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
496 wanted. */ |
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
497 result.ret = 0; |
fee25dcb4d03
dict-sql: Treat NULL value the same as "key not found"
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20386
diff
changeset
|
498 } |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
499 } |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
500 ctx->callback(&result, ctx->context); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
501 |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
502 i_free(ctx); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
503 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
504 |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
505 static void |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
506 sql_dict_lookup_async(struct dict *_dict, const char *key, |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
507 dict_lookup_callback_t *callback, void *context) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
508 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
509 struct sql_dict *dict = (struct sql_dict *)_dict; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
510 const struct dict_sql_map *map; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
511 struct sql_dict_lookup_context *ctx; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
512 |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
513 T_BEGIN { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
514 string_t *query = t_str_new(256); |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
515 const char *error; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
516 |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
517 if (sql_lookup_get_query(dict, key, query, &map, &error) < 0) { |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
518 struct dict_lookup_result result; |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
519 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21381
diff
changeset
|
520 i_zero(&result); |
19303
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
521 result.ret = -1; |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
522 result.error = error; |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
523 callback(&result, context); |
a5e47d9637c3
dict-sql: dict_lookup_async() didn't call callback on query build failures.
Timo Sirainen <tss@iki.fi>
parents:
19298
diff
changeset
|
524 } else { |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
525 ctx = i_new(struct sql_dict_lookup_context, 1); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
526 ctx->callback = callback; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
527 ctx->context = context; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
528 ctx->map = map; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
529 sql_query(dict->db, str_c(query), |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
530 sql_dict_lookup_async_callback, ctx); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
531 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
532 } T_END; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
533 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
534 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
535 static const struct dict_sql_map * |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
536 sql_dict_iterate_find_next_map(struct sql_dict_iterate_context *ctx, |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
537 ARRAY_TYPE(const_string) *values) |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
538 { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
539 struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
540 const struct dict_sql_map *maps; |
21322
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
541 unsigned int i, count; |
5ab8dc1a4a6f
global: Change string position/length from unsigned int to size_t
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20885
diff
changeset
|
542 size_t pat_len, path_len; |
18653
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
543 bool recurse = (ctx->flags & DICT_ITERATE_FLAG_RECURSE) != 0; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
544 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
545 t_array_init(values, dict->set->max_field_count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
546 maps = array_get(&dict->set->maps, &count); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
547 for (i = ctx->next_map_idx; i < count; i++) { |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
548 if (dict_sql_map_match(&maps[i], ctx->paths[ctx->path_idx], |
18653
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
549 values, &pat_len, &path_len, TRUE, recurse) && |
fce3e3eef9f0
dict-sql: Fixed non-recursive iteration.
Timo Sirainen <tss@iki.fi>
parents:
18639
diff
changeset
|
550 (recurse || |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
551 array_count(values)+1 >= array_count(&maps[i].sql_fields))) { |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
552 ctx->key_prefix_len = path_len; |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
553 ctx->pattern_prefix_len = pat_len; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
554 ctx->next_map_idx = i + 1; |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
555 |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
556 str_truncate(ctx->key, 0); |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
557 str_append(ctx->key, ctx->paths[ctx->path_idx]); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
558 return &maps[i]; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
559 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
560 } |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
561 |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
562 /* try the next path, if there is any */ |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
563 ctx->path_idx++; |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
564 if (ctx->paths[ctx->path_idx] != NULL) |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
565 return sql_dict_iterate_find_next_map(ctx, values); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
566 return NULL; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
567 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
568 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
569 static int |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
570 sql_dict_iterate_build_next_query(struct sql_dict_iterate_context *ctx, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
571 string_t *query, const char **error_r) |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
572 { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
573 struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
574 const struct dict_sql_map *map; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
575 ARRAY_TYPE(const_string) values; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
576 const struct dict_sql_field *sql_fields; |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
577 enum sql_recurse_type recurse_type; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
578 unsigned int i, count; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
579 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
580 map = sql_dict_iterate_find_next_map(ctx, &values); |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
581 /* NULL map is allowed if we have already done some lookups */ |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
582 if (map == NULL) { |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
583 if (!ctx->allow_null_map) { |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
584 *error_r = "Invalid/unmapped path"; |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
585 return -1; |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
586 } |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
587 return 0; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
588 } |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
589 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
590 if (ctx->result != NULL) { |
15947
0e0fd4b5a582
dict-sql: Iterating with multiple SQL queries didn't free all the queries.
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
591 sql_result_unref(ctx->result); |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
592 ctx->result = NULL; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
593 } |
15947
0e0fd4b5a582
dict-sql: Iterating with multiple SQL queries didn't free all the queries.
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
594 |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
595 str_append(query, "SELECT "); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
596 if ((ctx->flags & DICT_ITERATE_FLAG_NO_VALUE) == 0) |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
597 str_printfa(query, "%s,", map->value_field); |
15222
e7f10acf65e3
lib-dict: Added DICT_ITERATE_FLAG_NO_VALUE
Timo Sirainen <tss@iki.fi>
parents:
15218
diff
changeset
|
598 |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
599 /* get all missing fields */ |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
600 sql_fields = array_get(&map->sql_fields, &count); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
601 i = array_count(&values); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
602 if (i == count) { |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
603 /* we always want to know the last field since we're |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
604 iterating its children */ |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
605 i_assert(i > 0); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
606 i--; |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
607 } |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
608 ctx->sql_fields_start_idx = i; |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
609 for (; i < count; i++) |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
610 str_printfa(query, "%s,", sql_fields[i].name); |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
611 str_truncate(query, str_len(query)-1); |
15222
e7f10acf65e3
lib-dict: Added DICT_ITERATE_FLAG_NO_VALUE
Timo Sirainen <tss@iki.fi>
parents:
15218
diff
changeset
|
612 |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
613 str_printfa(query, " FROM %s", map->table); |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
614 |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
615 if ((ctx->flags & DICT_ITERATE_FLAG_RECURSE) != 0) |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
616 recurse_type = SQL_DICT_RECURSE_FULL; |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
617 else if ((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0) |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
618 recurse_type = SQL_DICT_RECURSE_NONE; |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
619 else |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
620 recurse_type = SQL_DICT_RECURSE_ONE; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
621 if (sql_dict_where_build(dict, map, &values, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
622 ctx->paths[ctx->path_idx][0], |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
623 recurse_type, query, error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
624 return -1; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
625 |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
626 if ((ctx->flags & DICT_ITERATE_FLAG_SORT_BY_KEY) != 0) { |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
627 str_append(query, " ORDER BY "); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
628 for (i = 0; i < count; i++) { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
629 str_printfa(query, "%s", sql_fields[i].name); |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
630 if (i < count-1) |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
631 str_append_c(query, ','); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
632 } |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
633 } else if ((ctx->flags & DICT_ITERATE_FLAG_SORT_BY_VALUE) != 0) |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
634 str_printfa(query, " ORDER BY %s", map->value_field); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
635 |
20885
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
636 if (ctx->ctx.max_rows > 0) { |
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
637 i_assert(ctx->ctx.row_count < ctx->ctx.max_rows); |
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
638 str_printfa(query, " LIMIT %llu", |
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
639 (unsigned long long)(ctx->ctx.max_rows - ctx->ctx.row_count)); |
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
640 } |
3f713b46d3b3
dict-sql: Use LIMIT in SQL queries with dict_iterate_set_limit()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20530
diff
changeset
|
641 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
642 ctx->map = map; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
643 return 1; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
644 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
645 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
646 static void sql_dict_iterate_callback(struct sql_result *result, |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
647 struct sql_dict_iterate_context *ctx) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
648 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
649 sql_result_ref(result); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
650 ctx->result = result; |
19316
467695fee373
dict-sql: Fixed async iteration with MySQL and SQLite
Timo Sirainen <tss@iki.fi>
parents:
19303
diff
changeset
|
651 if (ctx->ctx.async_callback != NULL && !ctx->synchronous_result) |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
652 ctx->ctx.async_callback(ctx->ctx.async_context); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
653 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
654 |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
655 static int sql_dict_iterate_next_query(struct sql_dict_iterate_context *ctx) |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
656 { |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
657 struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
658 const char *error; |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
659 unsigned int path_idx = ctx->path_idx; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
660 int ret; |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
661 |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
662 T_BEGIN { |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
663 string_t *query = t_str_new(256); |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
664 |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
665 ret = sql_dict_iterate_build_next_query(ctx, query, &error); |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
666 if (ret < 0) { |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
667 /* failed */ |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
668 i_error("sql dict iterate failed for %s: %s", |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
669 ctx->paths[path_idx], error); |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
670 } else if (ret == 0) { |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
671 /* this is expected error */ |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
672 } else if ((ctx->flags & DICT_ITERATE_FLAG_ASYNC) == 0) { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
673 ctx->result = sql_query_s(dict->db, str_c(query)); |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
674 } else { |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
675 i_assert(ctx->result == NULL); |
19316
467695fee373
dict-sql: Fixed async iteration with MySQL and SQLite
Timo Sirainen <tss@iki.fi>
parents:
19303
diff
changeset
|
676 ctx->synchronous_result = TRUE; |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
677 sql_query(dict->db, str_c(query), |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
678 sql_dict_iterate_callback, ctx); |
19316
467695fee373
dict-sql: Fixed async iteration with MySQL and SQLite
Timo Sirainen <tss@iki.fi>
parents:
19303
diff
changeset
|
679 ctx->synchronous_result = FALSE; |
19060
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
680 } |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
681 } T_END; |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
682 return ret; |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
683 } |
46eb03d95feb
dict-sql: Code cleanup - separated SQL query building function from sending it.
Timo Sirainen <tss@iki.fi>
parents:
18832
diff
changeset
|
684 |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
685 static struct dict_iterate_context * |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
686 sql_dict_iterate_init(struct dict *_dict, const char *const *paths, |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
687 enum dict_iterate_flags flags) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
688 { |
7819
59ec9752c3dd
Fixed dict iteration with SQL backend.
Timo Sirainen <tss@iki.fi>
parents:
7226
diff
changeset
|
689 struct sql_dict_iterate_context *ctx; |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
690 unsigned int i, path_count; |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
691 pool_t pool; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
692 |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
693 pool = pool_alloconly_create("sql dict iterate", 512); |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
694 ctx = p_new(pool, struct sql_dict_iterate_context, 1); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
695 ctx->ctx.dict = _dict; |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
696 ctx->pool = pool; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
697 ctx->flags = flags; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
698 |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
699 for (path_count = 0; paths[path_count] != NULL; path_count++) ; |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
700 ctx->paths = p_new(pool, const char *, path_count + 1); |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
701 for (i = 0; i < path_count; i++) |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
702 ctx->paths[i] = p_strdup(pool, paths[i]); |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
703 |
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
704 ctx->key = str_new(pool, 256); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
705 return &ctx->ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
706 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
707 |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
708 static bool sql_dict_iterate(struct dict_iterate_context *_ctx, |
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
709 const char **key_r, const char **value_r) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
710 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
711 struct sql_dict_iterate_context *ctx = |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
712 (struct sql_dict_iterate_context *)_ctx; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
713 const char *p, *value; |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
714 unsigned int i, sql_field_i, count; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
715 int ret; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
716 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
717 _ctx->has_more = FALSE; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
718 if (ctx->failed) |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
719 return FALSE; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
720 if (!ctx->iter_query_sent) { |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
721 ctx->iter_query_sent = TRUE; |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
722 if (sql_dict_iterate_next_query(ctx) <= 0) |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
723 return FALSE; |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
724 } |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
725 |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
726 if (ctx->result == NULL) { |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
727 /* wait for async lookup to finish */ |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
728 i_assert((ctx->flags & DICT_ITERATE_FLAG_ASYNC) != 0); |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
729 _ctx->has_more = TRUE; |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
730 return FALSE; |
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
731 } |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
732 |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
733 ret = sql_result_next_row(ctx->result); |
22355
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
734 while (ret == SQL_RESULT_NEXT_MORE) { |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
735 if ((ctx->flags & DICT_ITERATE_FLAG_ASYNC) == 0) |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
736 sql_result_more_s(&ctx->result); |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
737 else { |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
738 /* get more results asynchronously */ |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
739 ctx->synchronous_result = TRUE; |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
740 sql_result_more(&ctx->result, sql_dict_iterate_callback, ctx); |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
741 ctx->synchronous_result = FALSE; |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
742 if (ctx->result == NULL) { |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
743 _ctx->has_more = TRUE; |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
744 return FALSE; |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
745 } |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
746 } |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
747 ret = sql_result_next_row(ctx->result); |
d950ffd7cbe0
dict-sql: Add support for sql_result_more()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21942
diff
changeset
|
748 } |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
749 if (ret == 0) { |
18639
7abdcf42b63b
dict-sql: If DICT_ITERATE_FLAG_EXACT_KEY is used, use only the first found map.
Timo Sirainen <tss@iki.fi>
parents:
18638
diff
changeset
|
750 /* see if there are more results in the next map. |
7abdcf42b63b
dict-sql: If DICT_ITERATE_FLAG_EXACT_KEY is used, use only the first found map.
Timo Sirainen <tss@iki.fi>
parents:
18638
diff
changeset
|
751 don't do it if we're looking for an exact match, since we |
7abdcf42b63b
dict-sql: If DICT_ITERATE_FLAG_EXACT_KEY is used, use only the first found map.
Timo Sirainen <tss@iki.fi>
parents:
18638
diff
changeset
|
752 already should have handled it. */ |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
753 if ((ctx->flags & DICT_ITERATE_FLAG_EXACT_KEY) != 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
754 return FALSE; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
755 ctx->iter_query_sent = FALSE; |
21441
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
756 /* we have gotten *SOME* results, so can allow |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
757 unmapped next key now. */ |
fb5fc804a16c
dict-sql: Fix iterator to not fail
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21440
diff
changeset
|
758 ctx->allow_null_map = TRUE; |
21440
32f08dc1c1f7
dict-sql: Error handling fix & code cleanup
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21415
diff
changeset
|
759 return sql_dict_iterate(_ctx, key_r, value_r); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
760 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
761 if (ret < 0) { |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
762 ctx->failed = TRUE; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
763 i_error("dict sql iterate failed: %s", |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
764 sql_result_get_error(ctx->result)); |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
765 return FALSE; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
766 } |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
767 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
768 /* convert fetched row to dict key */ |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
769 str_truncate(ctx->key, ctx->key_prefix_len); |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
770 if (ctx->key_prefix_len > 0 && |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
771 str_c(ctx->key)[ctx->key_prefix_len-1] != '/') |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
772 str_append_c(ctx->key, '/'); |
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
773 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
774 count = sql_result_get_fields_count(ctx->result); |
21912
7a87ed7c9f88
dict-sql: Fix setting $variables to iteration key when map had multiple values
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21475
diff
changeset
|
775 i = (ctx->flags & DICT_ITERATE_FLAG_NO_VALUE) != 0 ? 0 : |
7a87ed7c9f88
dict-sql: Fix setting $variables to iteration key when map had multiple values
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21475
diff
changeset
|
776 ctx->map->values_count; |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
777 sql_field_i = ctx->sql_fields_start_idx; |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
778 for (p = ctx->map->pattern + ctx->pattern_prefix_len; *p != '\0'; p++) { |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
779 if (*p != '$') |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
780 str_append_c(ctx->key, *p); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
781 else { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
782 i_assert(i < count); |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
783 value = sql_dict_result_unescape_field(ctx->map, |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
784 pool_datastack_create(), ctx->result, i, sql_field_i); |
9056
40b5b098817b
dict-sql: Don't crash if iteration sees a NULL value in path.
Timo Sirainen <tss@iki.fi>
parents:
8685
diff
changeset
|
785 if (value != NULL) |
40b5b098817b
dict-sql: Don't crash if iteration sees a NULL value in path.
Timo Sirainen <tss@iki.fi>
parents:
8685
diff
changeset
|
786 str_append(ctx->key, value); |
19074
da570231062f
dict-sql: Fixed iteration with blob fields.
Timo Sirainen <tss@iki.fi>
parents:
19073
diff
changeset
|
787 i++; sql_field_i++; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
788 } |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
789 } |
7821
654cd4d966be
dict sql: Iterated keys need to contain private/shared prefix.
Timo Sirainen <tss@iki.fi>
parents:
7819
diff
changeset
|
790 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
791 *key_r = str_c(ctx->key); |
19069
7ccff6d5dd1b
dict-sql: Fixed iteration with DICT_ITERATE_FLAG_NO_VALUE
Timo Sirainen <tss@iki.fi>
parents:
19066
diff
changeset
|
792 if ((ctx->flags & DICT_ITERATE_FLAG_NO_VALUE) != 0) |
7ccff6d5dd1b
dict-sql: Fixed iteration with DICT_ITERATE_FLAG_NO_VALUE
Timo Sirainen <tss@iki.fi>
parents:
19066
diff
changeset
|
793 *value_r = ""; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
794 else { |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
795 *value_r = sql_dict_result_unescape_value(ctx->map, |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
796 pool_datastack_create(), ctx->result); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
797 } |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
798 return TRUE; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
799 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
800 |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
801 static int sql_dict_iterate_deinit(struct dict_iterate_context *_ctx) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
802 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
803 struct sql_dict_iterate_context *ctx = |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
804 (struct sql_dict_iterate_context *)_ctx; |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
805 int ret = ctx->failed ? -1 : 0; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
806 |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
807 if (ctx->result != NULL) |
9270
f9ebd72a73e8
SQL API change: SQL results can be now refed/unrefed.
Timo Sirainen <tss@iki.fi>
parents:
9056
diff
changeset
|
808 sql_result_unref(ctx->result); |
10752
6aa76d89195d
dict: Added dict_iterate_init_multiple().
Timo Sirainen <tss@iki.fi>
parents:
10659
diff
changeset
|
809 pool_unref(&ctx->pool); |
10659
7309b5c84ca9
lib-dict: Changed dict_iterate API.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
810 return ret; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
811 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
812 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
813 static struct dict_transaction_context * |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
814 sql_dict_transaction_init(struct dict *_dict) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
815 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
816 struct sql_dict *dict = (struct sql_dict *)_dict; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
817 struct sql_dict_transaction_context *ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
818 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
819 ctx = i_new(struct sql_dict_transaction_context, 1); |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
820 ctx->ctx.dict = _dict; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
821 ctx->sql_ctx = sql_transaction_begin(dict->db); |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
822 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
823 return &ctx->ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
824 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
825 |
19062
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
826 static void sql_dict_transaction_free(struct sql_dict_transaction_context *ctx) |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
827 { |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
828 if (ctx->inc_row_pool != NULL) |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
829 pool_unref(&ctx->inc_row_pool); |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
830 i_free(ctx->prev_inc_key); |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
831 i_free(ctx); |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
832 } |
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
833 |
19063
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
834 static bool |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
835 sql_dict_transaction_has_nonexistent(struct sql_dict_transaction_context *ctx) |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
836 { |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
837 struct sql_dict_inc_row *inc_row; |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
838 |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
839 for (inc_row = ctx->inc_row; inc_row != NULL; inc_row = inc_row->prev) { |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
840 i_assert(inc_row->rows != UINT_MAX); |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
841 if (inc_row->rows == 0) |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
842 return TRUE; |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
843 } |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
844 return FALSE; |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
845 } |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
846 |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
847 static void |
20530
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
848 sql_dict_transaction_commit_callback(const struct sql_commit_result *sql_result, |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
849 struct sql_dict_transaction_context *ctx) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
850 { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
851 int ret; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
852 |
20530
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
853 if (sql_result->error == NULL) |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
854 ret = sql_dict_transaction_has_nonexistent(ctx) ? 0 : 1; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
855 else { |
20530
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
856 i_error("sql dict: commit failed: %s", sql_result->error); |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
857 switch (sql_result->error_type) { |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
858 case SQL_RESULT_ERROR_TYPE_UNKNOWN: |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
859 default: |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
860 ret = DICT_COMMIT_RET_FAILED; |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
861 break; |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
862 case SQL_RESULT_ERROR_TYPE_WRITE_UNCERTAIN: |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
863 ret = DICT_COMMIT_RET_WRITE_UNCERTAIN; |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
864 break; |
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
865 } |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
866 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
867 |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
868 if (ctx->async_callback != NULL) |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
869 ctx->async_callback(ret, ctx->async_context); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
870 sql_dict_transaction_free(ctx); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
871 } |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
872 |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
873 static int |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
874 sql_dict_transaction_commit(struct dict_transaction_context *_ctx, bool async, |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
875 dict_transaction_commit_callback_t *callback, |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
876 void *context) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
877 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
878 struct sql_dict_transaction_context *ctx = |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
879 (struct sql_dict_transaction_context *)_ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
880 const char *error; |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
881 int ret = 1; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
882 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
883 if (ctx->prev_inc_map != NULL) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
884 sql_dict_prev_inc_flush(ctx); |
21397
08bcf7389337
dict-sql: Fix previous "merge sets" to flush at commit
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21394
diff
changeset
|
885 if (ctx->prev_set_map != NULL) |
08bcf7389337
dict-sql: Fix previous "merge sets" to flush at commit
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21394
diff
changeset
|
886 sql_dict_prev_set_flush(ctx); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
887 |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
888 if (ctx->failed) { |
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
889 sql_transaction_rollback(&ctx->sql_ctx); |
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
890 ret = -1; |
19061
5c444f10cef9
dict-sql: Fixed memory leak when committing/rollbacking unchanged transaction.
Timo Sirainen <tss@iki.fi>
parents:
19060
diff
changeset
|
891 } else if (!_ctx->changed) { |
5c444f10cef9
dict-sql: Fixed memory leak when committing/rollbacking unchanged transaction.
Timo Sirainen <tss@iki.fi>
parents:
19060
diff
changeset
|
892 /* nothing changed, no need to commit */ |
5c444f10cef9
dict-sql: Fixed memory leak when committing/rollbacking unchanged transaction.
Timo Sirainen <tss@iki.fi>
parents:
19060
diff
changeset
|
893 sql_transaction_rollback(&ctx->sql_ctx); |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
894 } else if (async) { |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
895 ctx->async_callback = callback; |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
896 ctx->async_context = context; |
20530
1d5136f4f27c
lib-dict: Added DICT_COMMIT_RET_WRITE_UNCERTAIN
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20454
diff
changeset
|
897 sql_transaction_commit2(&ctx->sql_ctx, |
19066
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
898 sql_dict_transaction_commit_callback, ctx); |
0d04ac4d43ca
dict-sql: Added support for async operations.
Timo Sirainen <tss@iki.fi>
parents:
19064
diff
changeset
|
899 return 1; |
19061
5c444f10cef9
dict-sql: Fixed memory leak when committing/rollbacking unchanged transaction.
Timo Sirainen <tss@iki.fi>
parents:
19060
diff
changeset
|
900 } else { |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
901 if (sql_transaction_commit_s(&ctx->sql_ctx, &error) < 0) { |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
902 i_error("sql dict: commit failed: %s", error); |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
903 ret = -1; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
904 } else { |
19063
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
905 if (sql_dict_transaction_has_nonexistent(ctx)) |
dd083fddda5e
dict-sql: Code cleanup - added sql_dict_transaction_has_nonexistent()
Timo Sirainen <tss@iki.fi>
parents:
19062
diff
changeset
|
906 ret = 0; |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
907 } |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
908 } |
19062
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
909 sql_dict_transaction_free(ctx); |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
910 |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
911 if (callback != NULL) |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
912 callback(ret, context); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
913 return ret; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
914 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
915 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
916 static void sql_dict_transaction_rollback(struct dict_transaction_context *_ctx) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
917 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
918 struct sql_dict_transaction_context *ctx = |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
919 (struct sql_dict_transaction_context *)_ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
920 |
19061
5c444f10cef9
dict-sql: Fixed memory leak when committing/rollbacking unchanged transaction.
Timo Sirainen <tss@iki.fi>
parents:
19060
diff
changeset
|
921 sql_transaction_rollback(&ctx->sql_ctx); |
19062
cb6d6d861ffa
dict-sql: Code cleanup - use a common sql_dict_transaction_free()
Timo Sirainen <tss@iki.fi>
parents:
19061
diff
changeset
|
922 sql_dict_transaction_free(ctx); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
923 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
924 |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
925 static void |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
926 sql_dict_transaction_add_timestamp(struct sql_dict_transaction_context *ctx, |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
927 string_t *query) |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
928 { |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
929 struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict; |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
930 unsigned long long timestamp_usecs; |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
931 |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
932 if (ctx->ctx.timestamp.tv_sec == 0 || !dict->has_using_timestamp) |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
933 return; |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
934 |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
935 timestamp_usecs = ctx->ctx.timestamp.tv_sec * 1000000ULL + |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
936 ctx->ctx.timestamp.tv_nsec / 1000; |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
937 str_printfa(query, " USING TIMESTAMP %llu", timestamp_usecs); |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
938 } |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
939 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
940 struct dict_sql_build_query_field { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
941 const struct dict_sql_map *map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
942 const char *value; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
943 }; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
944 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
945 struct dict_sql_build_query { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
946 struct sql_dict *dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
947 |
14920
a097ef0a9d6d
Array API changed: ARRAY_DEFINE(name, type) -> ARRAY(type) name
Timo Sirainen <tss@iki.fi>
parents:
14900
diff
changeset
|
948 ARRAY(struct dict_sql_build_query_field) fields; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
949 const ARRAY_TYPE(const_string) *extra_values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
950 char key1; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
951 bool inc; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
952 }; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
953 |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
954 static int sql_dict_set_query(struct sql_dict_transaction_context *ctx, |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
955 const struct dict_sql_build_query *build, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
956 const char **query_r, const char **error_r) |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
957 { |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
958 struct sql_dict *dict = build->dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
959 const struct dict_sql_build_query_field *fields; |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
960 const struct dict_sql_field *sql_fields; |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
961 const char *const *extra_values; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
962 unsigned int i, field_count, count, count2; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
963 string_t *prefix, *suffix; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
964 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
965 fields = array_get(&build->fields, &field_count); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
966 i_assert(field_count > 0); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
967 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
968 prefix = t_str_new(64); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
969 suffix = t_str_new(256); |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
970 str_printfa(prefix, "INSERT INTO %s", fields[0].map->table); |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
971 str_append(prefix, " ("); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
972 str_append(suffix, ") VALUES ("); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
973 for (i = 0; i < field_count; i++) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
974 if (i > 0) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
975 str_append_c(prefix, ','); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
976 str_append_c(suffix, ','); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
977 } |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
978 str_append(prefix, t_strcut(fields[i].map->value_field, ',')); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
979 if (build->inc) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
980 str_append(suffix, fields[i].value); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
981 else { |
19298
6bb60a869c71
dict-sql: Added support for value_type field, which deprecates value_hexblob.
Timo Sirainen <tss@iki.fi>
parents:
19257
diff
changeset
|
982 enum dict_sql_type value_type = |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
983 fields[i].map->value_types[0]; |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
984 if (sql_dict_value_escape(suffix, dict, fields[i].map, |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
985 value_type, "value", fields[i].value, |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
986 "", error_r) < 0) |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
987 return -1; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
988 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
989 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
990 if (build->key1 == DICT_PATH_PRIVATE[0]) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
991 str_printfa(prefix, ",%s", fields[0].map->username_field); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
992 str_printfa(suffix, ",'%s'", |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
993 sql_escape_string(dict->db, dict->username)); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
994 } |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
995 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
996 /* add the other fields from the key */ |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
997 sql_fields = array_get(&fields[0].map->sql_fields, &count); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
998 extra_values = array_get(build->extra_values, &count2); |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
999 i_assert(count == count2); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1000 for (i = 0; i < count; i++) { |
19073
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
1001 str_printfa(prefix, ",%s", sql_fields[i].name); |
0453c466c601
dict-sql: Implemented support for binary fields.
Timo Sirainen <tss@iki.fi>
parents:
19069
diff
changeset
|
1002 str_append_c(suffix, ','); |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
1003 if (sql_dict_field_escape_value(suffix, dict, fields[0].map, &sql_fields[i], |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1004 extra_values[i], "", error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1005 return -1; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
1006 } |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1007 |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1008 str_append_str(prefix, suffix); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1009 str_append_c(prefix, ')'); |
21475
011fc620d6f6
dict-sql: Move USING TIMESTAMP to correct location for INSERTs
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21463
diff
changeset
|
1010 sql_dict_transaction_add_timestamp(ctx, prefix); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1011 if (!dict->has_on_duplicate_key) { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1012 *query_r = str_c(prefix); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1013 return 0; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1014 } |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1015 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1016 str_append(prefix, " ON DUPLICATE KEY UPDATE "); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1017 for (i = 0; i < field_count; i++) { |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1018 const char *first_value_field = |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1019 t_strcut(fields[i].map->value_field, ','); |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1020 if (i > 0) |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1021 str_append_c(prefix, ','); |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1022 str_append(prefix, first_value_field); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1023 str_append_c(prefix, '='); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1024 if (build->inc) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1025 str_printfa(prefix, "%s+%s", |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1026 first_value_field, |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1027 fields[i].value); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1028 } else { |
19298
6bb60a869c71
dict-sql: Added support for value_type field, which deprecates value_hexblob.
Timo Sirainen <tss@iki.fi>
parents:
19257
diff
changeset
|
1029 enum dict_sql_type value_type = |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1030 fields[i].map->value_types[0]; |
20270
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
1031 if (sql_dict_value_escape(prefix, dict, fields[i].map, |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
1032 value_type, "value", fields[i].value, |
bd3ac2e8e8a5
dict-sql: Improve error message for invalid value fields.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20165
diff
changeset
|
1033 "", error_r) < 0) |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1034 return -1; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1035 } |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
1036 } |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1037 *query_r = str_c(prefix); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1038 return 0; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1039 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1040 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1041 static int |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1042 sql_dict_update_query(struct sql_dict_transaction_context *ctx, |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1043 const struct dict_sql_build_query *build, |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1044 const char **query_r, const char **error_r) |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1045 { |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1046 struct sql_dict *dict = build->dict; |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1047 const struct dict_sql_build_query_field *fields; |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1048 unsigned int i, field_count; |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1049 string_t *query; |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1050 |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1051 i_assert(build->inc); |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1052 |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1053 fields = array_get(&build->fields, &field_count); |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1054 i_assert(field_count > 0); |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1055 |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1056 query = t_str_new(64); |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1057 str_printfa(query, "UPDATE %s", fields[0].map->table); |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1058 sql_dict_transaction_add_timestamp(ctx, query); |
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1059 str_append(query, " SET "); |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1060 for (i = 0; i < field_count; i++) { |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1061 const char *first_value_field = |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1062 t_strcut(fields[i].map->value_field, ','); |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1063 if (i > 0) |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1064 str_append_c(query, ','); |
21401
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1065 str_printfa(query, "%s=%s", first_value_field, |
c22acf3af571
dict-sql: Support multiple values for lookups
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21399
diff
changeset
|
1066 first_value_field); |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1067 if (fields[i].value[0] != '-') |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1068 str_append_c(query, '+'); |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1069 str_append(query, fields[i].value); |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1070 } |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1071 |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1072 if (sql_dict_where_build(dict, fields[0].map, build->extra_values, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1073 build->key1, SQL_DICT_RECURSE_NONE, query, error_r) < 0) |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1074 return -1; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1075 *query_r = str_c(query); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1076 return 0; |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1077 } |
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1078 |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1079 static void sql_dict_set_real(struct dict_transaction_context *_ctx, |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1080 const char *key, const char *value) |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1081 { |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1082 struct sql_dict_transaction_context *ctx = |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1083 (struct sql_dict_transaction_context *)_ctx; |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1084 struct sql_dict *dict = (struct sql_dict *)_ctx->dict; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1085 const struct dict_sql_map *map; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1086 ARRAY_TYPE(const_string) values; |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1087 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1088 map = sql_dict_find_map(dict, key, &values); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1089 if (map == NULL) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1090 i_error("sql dict set: Invalid/unmapped key: %s", key); |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1091 ctx->failed = TRUE; |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1092 return; |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1093 } |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1094 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1095 T_BEGIN { |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1096 struct dict_sql_build_query build; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1097 struct dict_sql_build_query_field field; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1098 const char *query, *error; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1099 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1100 field.map = map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1101 field.value = value; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1102 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21381
diff
changeset
|
1103 i_zero(&build); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1104 build.dict = dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1105 t_array_init(&build.fields, 1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1106 array_append(&build.fields, &field, 1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1107 build.extra_values = &values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1108 build.key1 = key[0]; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1109 |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1110 if (sql_dict_set_query(ctx, &build, &query, &error) < 0) { |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1111 i_error("dict-sql: Failed to set %s=%s: %s", |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1112 key, value, error); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1113 ctx->failed = TRUE; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1114 } else { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1115 sql_update(ctx->sql_ctx, query); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1116 } |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1117 } T_END; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1118 } |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1119 |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1120 static void sql_dict_unset(struct dict_transaction_context *_ctx, |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1121 const char *key) |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1122 { |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1123 struct sql_dict_transaction_context *ctx = |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1124 (struct sql_dict_transaction_context *)_ctx; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1125 struct sql_dict *dict = (struct sql_dict *)_ctx->dict; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1126 const struct dict_sql_map *map; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1127 ARRAY_TYPE(const_string) values; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1128 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1129 if (ctx->prev_inc_map != NULL) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1130 sql_dict_prev_inc_flush(ctx); |
21415 | 1131 if (ctx->prev_set_map != NULL) |
1132 sql_dict_prev_set_flush(ctx); | |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1133 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1134 map = sql_dict_find_map(dict, key, &values); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1135 if (map == NULL) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1136 i_error("sql dict unset: Invalid/unmapped key: %s", key); |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1137 ctx->failed = TRUE; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1138 return; |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1139 } |
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1140 |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1141 T_BEGIN { |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1142 string_t *query = t_str_new(256); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1143 const char *error; |
7966
54225d0b6e2b
dict-sql: Only MySQL supports "INSERT .. ON DUPLICATE KEY". With others just
Timo Sirainen <tss@iki.fi>
parents:
7821
diff
changeset
|
1144 |
8265
72b7277aefb3
dict-sql: Dictionary iteration fixes.
Timo Sirainen <tss@iki.fi>
parents:
8113
diff
changeset
|
1145 str_printfa(query, "DELETE FROM %s", map->table); |
21463
0924bd1d77b3
dict-sql: Add missing USING TIMESTAMP for DELETE queries
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21442
diff
changeset
|
1146 sql_dict_transaction_add_timestamp(ctx, query); |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1147 if (sql_dict_where_build(dict, map, &values, key[0], |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1148 SQL_DICT_RECURSE_NONE, query, &error) < 0) { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1149 i_error("dict-sql: Failed to delete %s: %s", key, error); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1150 ctx->failed = TRUE; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1151 } else { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1152 sql_update(ctx->sql_ctx, str_c(query)); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1153 } |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1154 } T_END; |
4516
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1155 } |
aa2f73a4df26
Dictionary changes: Added support for defining value's type. Key is still always a string. Added support for sorting the iteration replies. Added dict_unset(). Added Berkeley DB support. Most of the code written by Tianyan Liu.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4511
diff
changeset
|
1156 |
14900
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1157 static void |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1158 sql_dict_append(struct dict_transaction_context *_ctx, |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1159 const char *key ATTR_UNUSED, const char *value ATTR_UNUSED) |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1160 { |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1161 struct sql_dict_transaction_context *ctx = |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1162 (struct sql_dict_transaction_context *)_ctx; |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1163 |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1164 i_error("sql dict: Append command not implemented currently"); |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1165 ctx->failed = TRUE; |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1166 } |
87e14707d210
dict: Implemented dict_append()
Timo Sirainen <tss@iki.fi>
parents:
14391
diff
changeset
|
1167 |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1168 static unsigned int * |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1169 sql_dict_next_inc_row(struct sql_dict_transaction_context *ctx) |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1170 { |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1171 struct sql_dict_inc_row *row; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1172 |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1173 if (ctx->inc_row_pool == NULL) { |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1174 ctx->inc_row_pool = |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1175 pool_alloconly_create("sql dict inc rows", 128); |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1176 } |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1177 row = p_new(ctx->inc_row_pool, struct sql_dict_inc_row, 1); |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1178 row->prev = ctx->inc_row; |
15904
d3cf06639864
Replaced all -1U and (unsigned int)-1 with UINT_MAX.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1179 row->rows = UINT_MAX; |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1180 ctx->inc_row = row; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1181 return &row->rows; |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1182 } |
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1183 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1184 static void sql_dict_atomic_inc_real(struct sql_dict_transaction_context *ctx, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1185 const char *key, long long diff) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1186 { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1187 struct sql_dict *dict = (struct sql_dict *)ctx->ctx.dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1188 const struct dict_sql_map *map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1189 ARRAY_TYPE(const_string) values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1190 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1191 map = sql_dict_find_map(dict, key, &values); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1192 i_assert(map != NULL); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1193 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1194 T_BEGIN { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1195 struct dict_sql_build_query build; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1196 struct dict_sql_build_query_field field; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1197 const char *query, *error; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1198 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1199 field.map = map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1200 field.value = t_strdup_printf("%lld", diff); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1201 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21381
diff
changeset
|
1202 i_zero(&build); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1203 build.dict = dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1204 t_array_init(&build.fields, 1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1205 array_append(&build.fields, &field, 1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1206 build.extra_values = &values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1207 build.key1 = key[0]; |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1208 build.inc = TRUE; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1209 |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1210 if (sql_dict_update_query(ctx, &build, &query, &error) < 0) { |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1211 i_error("dict-sql: Failed to increase %s: %s", key, error); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1212 ctx->failed = TRUE; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1213 } else { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1214 sql_update_get_rows(ctx->sql_ctx, query, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1215 sql_dict_next_inc_row(ctx)); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1216 } |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1217 } T_END; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1218 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1219 |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1220 static void sql_dict_prev_set_flush(struct sql_dict_transaction_context *ctx) |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1221 { |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1222 sql_dict_set_real(&ctx->ctx, ctx->prev_set_key, ctx->prev_set_value); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1223 i_free_and_null(ctx->prev_set_value); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1224 i_free_and_null(ctx->prev_set_key); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1225 ctx->prev_set_map = NULL; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1226 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1227 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1228 static void sql_dict_prev_inc_flush(struct sql_dict_transaction_context *ctx) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1229 { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1230 sql_dict_atomic_inc_real(ctx, ctx->prev_inc_key, ctx->prev_inc_diff); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1231 i_free_and_null(ctx->prev_inc_key); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1232 ctx->prev_inc_map = NULL; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1233 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1234 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1235 static bool |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1236 sql_dict_maps_are_mergeable(struct sql_dict *dict, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1237 const struct dict_sql_map *map1, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1238 const struct dict_sql_map *map2, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1239 const char *map1_key, const char *map2_key, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1240 const ARRAY_TYPE(const_string) *map2_values) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1241 { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1242 const struct dict_sql_map *map3; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1243 ARRAY_TYPE(const_string) map1_values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1244 const char *const *v1, *const *v2; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1245 unsigned int i, count1, count2; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1246 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1247 if (strcmp(map1->table, map2->table) != 0) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1248 return FALSE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1249 if (map1_key[0] != map2_key[0]) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1250 return FALSE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1251 if (map1_key[0] == DICT_PATH_PRIVATE[0]) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1252 if (strcmp(map1->username_field, map2->username_field) != 0) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1253 return FALSE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1254 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1255 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1256 map3 = sql_dict_find_map(dict, map1_key, &map1_values); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1257 i_assert(map3 == map1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1258 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1259 v1 = array_get(&map1_values, &count1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1260 v2 = array_get(map2_values, &count2); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1261 if (count1 != count2) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1262 return FALSE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1263 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1264 for (i = 0; i < count1; i++) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1265 if (strcmp(v1[i], v2[i]) != 0) |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1266 return FALSE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1267 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1268 return TRUE; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1269 } |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1270 |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1271 static void sql_dict_set(struct dict_transaction_context *_ctx, |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1272 const char *key, const char *value) |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1273 { |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1274 struct sql_dict_transaction_context *ctx = |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1275 (struct sql_dict_transaction_context *)_ctx; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1276 struct sql_dict *dict = (struct sql_dict *)_ctx->dict; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1277 const struct dict_sql_map *map; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1278 ARRAY_TYPE(const_string) values; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1279 |
21394
3f108cb63412
lib-dict: Fix compability for 2.2
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21391
diff
changeset
|
1280 if (ctx->failed) |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1281 return; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1282 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1283 map = sql_dict_find_map(dict, key, &values); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1284 if (map == NULL) { |
21394
3f108cb63412
lib-dict: Fix compability for 2.2
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21391
diff
changeset
|
1285 ctx->failed = TRUE; |
3f108cb63412
lib-dict: Fix compability for 2.2
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21391
diff
changeset
|
1286 i_error("sql dict set: Invalid/unmapped key: %s", key); |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1287 return; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1288 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1289 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1290 if (ctx->prev_set_map == NULL) { |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1291 /* see if we can merge this increment SQL query with the |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1292 next one */ |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1293 ctx->prev_set_map = map; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1294 ctx->prev_set_key = i_strdup(key); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1295 ctx->prev_set_value = i_strdup(value); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1296 return; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1297 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1298 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1299 if (!sql_dict_maps_are_mergeable(dict, ctx->prev_set_map, map, |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1300 ctx->prev_set_key, key, &values)) { |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1301 sql_dict_prev_set_flush(ctx); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1302 sql_dict_set_real(&ctx->ctx, key, value); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1303 } else { |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1304 struct dict_sql_build_query build; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1305 struct dict_sql_build_query_field *field; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1306 const char *query, *error; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1307 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1308 i_zero(&build); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1309 build.dict = dict; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1310 t_array_init(&build.fields, 1); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1311 build.extra_values = &values; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1312 build.key1 = key[0]; |
21942
66a1a4338260
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21912
diff
changeset
|
1313 build.inc = TRUE; |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1314 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1315 field = array_append_space(&build.fields); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1316 field->map = ctx->prev_set_map; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1317 field->value = ctx->prev_set_value; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1318 field = array_append_space(&build.fields); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1319 field->map = map; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1320 field->value = value; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1321 |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1322 if (sql_dict_set_query(ctx, &build, &query, &error) < 0) { |
21394
3f108cb63412
lib-dict: Fix compability for 2.2
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21391
diff
changeset
|
1323 i_error("dict-sql: Failed to set %s: %s", key, error); |
3f108cb63412
lib-dict: Fix compability for 2.2
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21391
diff
changeset
|
1324 ctx->failed = TRUE; |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1325 } else { |
21398
d82cd557651b
dict-sql: Remove unnecessary "affected rows" tracking from dict_set()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21397
diff
changeset
|
1326 sql_update(ctx->sql_ctx, query); |
21391
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1327 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1328 i_free_and_null(ctx->prev_set_value); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1329 i_free_and_null(ctx->prev_set_key); |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1330 ctx->prev_set_map = NULL; |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1331 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1332 } |
609e20f25505
lib-dict-sql: Try merge sets to single update
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
1333 |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1334 static void sql_dict_atomic_inc(struct dict_transaction_context *_ctx, |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1335 const char *key, long long diff) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1336 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1337 struct sql_dict_transaction_context *ctx = |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1338 (struct sql_dict_transaction_context *)_ctx; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1339 struct sql_dict *dict = (struct sql_dict *)_ctx->dict; |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1340 const struct dict_sql_map *map; |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1341 ARRAY_TYPE(const_string) values; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
1342 |
8113
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1343 map = sql_dict_find_map(dict, key, &values); |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1344 if (map == NULL) { |
2d3e942a4cde
dict sql: Added configuration for mapping dict paths to SQL fields.
Timo Sirainen <tss@iki.fi>
parents:
7966
diff
changeset
|
1345 i_error("sql dict atomic inc: Invalid/unmapped key: %s", key); |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
1346 ctx->failed = TRUE; |
3967
6fabe878c46d
Dictionary takes now a username parameter, which is used for private
Timo Sirainen <tss@iki.fi>
parents:
3943
diff
changeset
|
1347 return; |
4511
3f808dd8f568
If invalid key is given to dict_set() or dict_atomic_inc() fail the whole
Timo Sirainen <tss@iki.fi>
parents:
4371
diff
changeset
|
1348 } |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1349 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1350 if (ctx->prev_inc_map == NULL) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1351 /* see if we can merge this increment SQL query with the |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1352 next one */ |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1353 ctx->prev_inc_map = map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1354 ctx->prev_inc_key = i_strdup(key); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1355 ctx->prev_inc_diff = diff; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1356 return; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1357 } |
9889
0f7b25f3e2ce
dict: Added support for async commits. Changed dict_atomic_inc() behavior.
Timo Sirainen <tss@iki.fi>
parents:
9545
diff
changeset
|
1358 |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1359 if (!sql_dict_maps_are_mergeable(dict, ctx->prev_inc_map, map, |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1360 ctx->prev_inc_key, key, &values)) { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1361 sql_dict_prev_inc_flush(ctx); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1362 sql_dict_atomic_inc_real(ctx, key, diff); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1363 } else T_BEGIN { |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1364 struct dict_sql_build_query build; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1365 struct dict_sql_build_query_field *field; |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1366 const char *query, *error; |
6940
414c9d631a81
Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents:
6480
diff
changeset
|
1367 |
21389
59437f8764c6
global: Replaced all instances of memset(p, 0, sizeof(*p)) with the new i_zero() macro.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21381
diff
changeset
|
1368 i_zero(&build); |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1369 build.dict = dict; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1370 t_array_init(&build.fields, 1); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1371 build.extra_values = &values; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1372 build.key1 = key[0]; |
8654
abb1a50a266d
pgsql: When doing negative increments, don't even try to use INSERT.
Timo Sirainen <tss@iki.fi>
parents:
8647
diff
changeset
|
1373 build.inc = TRUE; |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1374 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1375 field = array_append_space(&build.fields); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1376 field->map = ctx->prev_inc_map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1377 field->value = t_strdup_printf("%lld", ctx->prev_inc_diff); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1378 field = array_append_space(&build.fields); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1379 field->map = map; |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1380 field->value = t_strdup_printf("%lld", diff); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1381 |
21381
f22040e335f9
dict-sql: Support transaction timestamps with Cassandra driver
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21322
diff
changeset
|
1382 if (sql_dict_update_query(ctx, &build, &query, &error) < 0) { |
19220
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1383 i_error("dict-sql: Failed to increase %s: %s", key, error); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1384 ctx->failed = TRUE; |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1385 } else { |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1386 sql_update_get_rows(ctx->sql_ctx, query, |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1387 sql_dict_next_inc_row(ctx)); |
fbff1e801300
dict-sql: If value isn't a hexblob as expected, log an error instead of killing the whole process.
Timo Sirainen <tss@iki.fi>
parents:
19074
diff
changeset
|
1388 } |
8647
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1389 |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1390 i_free_and_null(ctx->prev_inc_key); |
cc46e5822b96
sql dict: Merge two INSERTs together whenever possible (for dict quota).
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
1391 ctx->prev_inc_map = NULL; |
7226
e6693a0ec8e1
Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
1392 } T_END; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1393 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1394 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1395 static struct dict sql_dict = { |
10410
b757dab45756
Removed MEMBER() macro. Require C99 style struct initializer.
Timo Sirainen <tss@iki.fi>
parents:
9920
diff
changeset
|
1396 .name = "sql", |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1397 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1398 { |
20386
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1399 .init = sql_dict_init, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1400 .deinit = sql_dict_deinit, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1401 .wait = sql_dict_wait, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1402 .lookup = sql_dict_lookup, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1403 .iterate_init = sql_dict_iterate_init, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1404 .iterate = sql_dict_iterate, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1405 .iterate_deinit = sql_dict_iterate_deinit, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1406 .transaction_init = sql_dict_transaction_init, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1407 .transaction_commit = sql_dict_transaction_commit, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1408 .transaction_rollback = sql_dict_transaction_rollback, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1409 .set = sql_dict_set, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1410 .unset = sql_dict_unset, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1411 .append = sql_dict_append, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1412 .atomic_inc = sql_dict_atomic_inc, |
564a32262929
lib-dict: Explicitly specify used dict_vfuncs methods for drivers.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20341
diff
changeset
|
1413 .lookup_async = sql_dict_lookup_async, |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1414 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1415 }; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1416 |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1417 static struct dict *dict_sql_drivers; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1418 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1419 void dict_sql_register(void) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1420 { |
3943
cbe5c6772e0d
Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
1421 const struct sql_db *const *drivers; |
cbe5c6772e0d
Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
1422 unsigned int i, count; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1423 |
11252
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
1424 dict_sql_db_cache = sql_db_cache_init(DICT_SQL_MAX_UNUSED_CONNECTIONS); |
6480 | 1425 |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1426 /* @UNSAFE */ |
3943
cbe5c6772e0d
Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
1427 drivers = array_get(&sql_drivers, &count); |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1428 dict_sql_drivers = i_new(struct dict, count + 1); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1429 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1430 for (i = 0; i < count; i++) { |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1431 dict_sql_drivers[i] = sql_dict; |
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1432 dict_sql_drivers[i].name = drivers[i]->name; |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1433 |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1434 dict_driver_register(&dict_sql_drivers[i]); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1435 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1436 } |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1437 |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1438 void dict_sql_unregister(void) |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1439 { |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1440 int i; |
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1441 |
4517
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1442 for (i = 0; dict_sql_drivers[i].name != NULL; i++) |
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1443 dict_driver_unregister(&dict_sql_drivers[i]); |
e661182eab75
Berkeley DB dict support is now enabled only when using --with-db configure option.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
4516
diff
changeset
|
1444 i_free(dict_sql_drivers); |
11252
e0d02ae9d50f
lib-sql: Renamed "sql pool" to "sql db cache", since that's what it is.
Timo Sirainen <tss@iki.fi>
parents:
10752
diff
changeset
|
1445 sql_db_cache_deinit(&dict_sql_db_cache); |
20165
87d2e33aef64
dict-sql: Cache reading settings files.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
1446 dict_sql_settings_deinit(); |
3737
d67092398377
Added dictionary API and implementation for SQL. It's meant for looking and
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
1447 } |