This will call stmt_cache_requeue_old_entry() after it has allocated memory for * the new entry. On error will return non zero and caller will need to * finalize_stmt(). */
| 188 | * the new entry. On error will return non zero and caller will need to |
| 189 | * finalize_stmt(). */ |
| 190 | int stmt_cache_add_new_entry(stmt_cache_t *stmt_cache, const char *sql, |
| 191 | const char *actual_sql, sqlite3_stmt *stmt, |
| 192 | struct sqlclntstate *clnt) |
| 193 | { |
| 194 | if (!stmt_cache) { |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | if (strlen(sql) >= MAX_HASH_SQL_LENGTH) { |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | assert(stmt_cache->hash); |
| 203 | |
| 204 | /* stored procedure can call same stmt from a lua thread more than once so |
| 205 | * we should not add stmt that exists already */ |
| 206 | if (hash_find(stmt_cache->hash, sql) != NULL) { |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | void *list = GET_STMT_LIST(stmt_cache, stmt); |
| 211 | |
| 212 | /* remove older entries to make room for new ones */ |
| 213 | if (gbl_max_sqlcache <= listc_size(list)) { |
| 214 | stmt_cache_delete_last_entry(stmt_cache, list); |
| 215 | } |
| 216 | |
| 217 | stmt_cache_entry_t *entry = sqlite3_malloc(sizeof(stmt_cache_entry_t)); |
| 218 | strncpy(entry->sql, sql, MAX_HASH_SQL_LENGTH - 1); |
| 219 | entry->stmt = stmt; |
| 220 | |
| 221 | query_data_func(clnt, &entry->stmt_data, &entry->stmt_data_sz, |
| 222 | QUERY_STMT_DATA, QUERY_DATA_GET); |
| 223 | /* Take ownership of stmt data */ |
| 224 | query_data_func(clnt, NULL, NULL, QUERY_STMT_DATA, QUERY_DATA_SET); |
| 225 | entry->qd_func = clnt->plugin.query_data_func; |
| 226 | |
| 227 | if (actual_sql && gbl_debug_temptables) |
| 228 | entry->query = strdup(actual_sql); |
| 229 | else |
| 230 | entry->query = NULL; |
| 231 | |
| 232 | return stmt_cache_requeue_old_entry(stmt_cache, entry); |
| 233 | } |
| 234 | |
| 235 | int stmt_cache_find_and_remove_entry(stmt_cache_t *stmt_cache, const char *sql, stmt_cache_entry_t **entry) |
| 236 | { |
no test coverage detected