size_t Database::insert_and_get(const std::string &table, const std::string &key_name, const std::string &key) { std::string query = utils::sql::prepare_insert_start<std::string>(table, key_name); query+=utils::sql::prepare_insert_end(utils::str::make_dquote(key)); QSqlQuery q = db->exec(QString::fromStdString(query)); if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError()); return select_id(table, key_name, key); }
static int select_list(expression_type parent_type, struct selection *selection, const freesasa_structure *structure, const expression *expr) { int resr, resl; expression *left, *right; if (expr == NULL) return fail_msg("NULL expression"); left = expr->left; right = expr->right; switch(expr->type) { case E_PLUS: if (left == NULL || right == NULL) return fail_msg("NULL expression"); resl = select_list(parent_type, selection, structure, left); resr = select_list(parent_type, selection, structure, right); if (resl == FREESASA_WARN || resr == FREESASA_WARN) return FREESASA_WARN; break; case E_RANGE: if (left == NULL || right == NULL) return fail_msg("NULL expression"); return select_range(E_RANGE, parent_type, selection, structure, left, right); case E_RANGE_OPEN_L: if (left != NULL || right == NULL) return fail_msg("NULL expression"); return select_range(E_RANGE_OPEN_L, parent_type, selection, structure, left, right); case E_RANGE_OPEN_R: if (left == NULL || right != NULL) return fail_msg("NULL expression"); return select_range(E_RANGE_OPEN_R, parent_type, selection, structure, left, right); case E_ID: case E_NUMBER: if (is_valid_id(parent_type, expr) == FREESASA_SUCCESS) select_id(parent_type, selection, structure, expr->value); else return freesasa_warn("select: %s: '%s' invalid %s", e_str(parent_type), expr->value, e_str(expr->type)); break; default: return freesasa_fail("select: parse error (expression: '%s %s')", e_str(parent_type), e_str(expr->type)); } return FREESASA_SUCCESS; }
size_t Database::get_id(const std::string &table, const std::string &key_name, const std::string &key) { size_t res = 0; auto curr_cache = cache.emplace(table, cfg::db::cache_max).first; if (curr_cache->second.get(key, res)) return res; try { res = select_id(table, key_name, key); } catch (const NotExist &exc) { res = insert_and_get(table, key_name, key); } curr_cache->second.append(key, res); return res; }
//we dont' need to add anything to the list because we only need to assign and //id to this node. unsigned int select_id(){ #if 0 if(idl==NULL){ idl=create_list(); } #endif struct timeval tv; gettimeofday(&tv,NULL); srand(tv.tv_usec); int r=rand(); return r; #if 0 if(!item_in_list(idl, r)){ //add: add_to_list(idl,r); return r; } else{ return select_id(); } #endif }