return: * 0 - succes * -1 - error * -2 - not found in sql db */
| 1083 | * -2 - not found in sql db |
| 1084 | */ |
| 1085 | static int load_key(cache_entry_t *c_entry, db_handlers_t *db_hdls, str key, |
| 1086 | db_val_t **values, db_res_t **sql_res, int rld_vers) |
| 1087 | { |
| 1088 | db_key_t key_col; |
| 1089 | db_row_t *row; |
| 1090 | db_val_t key_val; |
| 1091 | str src_key, null_val; |
| 1092 | |
| 1093 | src_key.len = c_entry->id.len + key.len; |
| 1094 | src_key.s = pkg_malloc(src_key.len); |
| 1095 | if (!src_key.s) { |
| 1096 | LM_ERR("No more shm memory\n"); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | memcpy(src_key.s, c_entry->id.s, c_entry->id.len); |
| 1100 | memcpy(src_key.s + c_entry->id.len, key.s, key.len); |
| 1101 | |
| 1102 | key_col = &(c_entry->key); |
| 1103 | |
| 1104 | VAL_NULL(&key_val) = 0; |
| 1105 | VAL_TYPE(&key_val) = c_entry->key_type; |
| 1106 | if (c_entry->key_type == DB_STR) |
| 1107 | VAL_STR(&key_val) = key; |
| 1108 | else if (str2sint(&key, &VAL_INT(&key_val)) < 0) { |
| 1109 | LM_ERR("Failed to convert key value to integer\n"); |
| 1110 | goto out_error; |
| 1111 | } |
| 1112 | |
| 1113 | if (db_hdls->db_funcs.use_table(db_hdls->db_con, &c_entry->table) < 0) { |
| 1114 | LM_ERR("Invalid table name: %.*s\n", c_entry->table.len, c_entry->table.s); |
| 1115 | db_hdls->db_funcs.close(db_hdls->db_con); |
| 1116 | db_hdls->db_con = 0; |
| 1117 | goto out_error; |
| 1118 | } |
| 1119 | |
| 1120 | CON_SET_CURR_PS(db_hdls->db_con, &db_hdls->query_ps); |
| 1121 | if (db_hdls->db_funcs.query(db_hdls->db_con, |
| 1122 | &key_col, 0, &key_val, c_entry->columns, 1, |
| 1123 | c_entry->nr_columns, 0, sql_res) != 0) { |
| 1124 | LM_ERR("Failure to issue query to SQL DB: %s\n", |
| 1125 | db_url_escape(&c_entry->db_url)); |
| 1126 | goto sql_error; |
| 1127 | } |
| 1128 | |
| 1129 | if (RES_ROW_N(*sql_res) == 0) { |
| 1130 | LM_DBG("key %.*s not found in SQL db\n", key.len, key.s); |
| 1131 | null_val.len = 0; |
| 1132 | null_val.s = NULL; |
| 1133 | if (db_hdls->cdbf.set(db_hdls->cdbcon, &src_key, &null_val, c_entry->expire) < 0) { |
| 1134 | LM_ERR("Failed to insert null in cachedb\n"); |
| 1135 | goto sql_error; |
| 1136 | } |
| 1137 | |
| 1138 | pkg_free(src_key.s); |
| 1139 | db_hdls->db_funcs.free_result(db_hdls->db_con, *sql_res); |
| 1140 | return -2; |
| 1141 | |
| 1142 | } else if (RES_ROW_N(*sql_res) > 1) { |
no test coverage detected