Convenience functions in db_query (2 of 3)

This commit is contained in:
2020-10-08 16:08:39 +01:00
parent f3a15fddc8
commit 0fc012815b
2 changed files with 164 additions and 119 deletions

View File

@@ -33,9 +33,9 @@ uint64_t simpleQuery(struct stored_conn_t *sconn, const char *qry, size_t qry_le
if (mysql_real_query(SQCONN(sconn), qry, qry_len) != 0) {
fprintf(
stderr, "[%d]mysql_real_query: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
stderr, "[%d]mysql_real_query: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
return (uint64_t)-1;
}
@@ -77,10 +77,10 @@ uint64_t tableQuery(struct stored_conn_t *sconn, const char *qry, size_t qry_len
*n_cols = mysql_field_count(SQCONN(sconn));
if (scalar_result && (*n_cols) > 1) {
fprintf(
stderr,
"[%d]tableQuery: Scalar result expected, got %ul columns. Using first column value\n",
__LINE__, (unsigned int)*n_cols
);
stderr,
"[%d]tableQuery: Scalar result expected, got %ul columns. Using first column value\n",
__LINE__, (unsigned int)*n_cols
);
(*n_cols) = 1;
}
if (*n_cols == 0) {
@@ -90,9 +90,9 @@ uint64_t tableQuery(struct stored_conn_t *sconn, const char *qry, size_t qry_len
result = mysql_store_result(SQCONN(sconn));
if (result == NULL) {
fprintf(
stderr, "[%d]mysql_real_query: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
stderr, "[%d]mysql_real_query: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
return (uint64_t)-1;
}
@@ -137,9 +137,9 @@ uint64_t tableQuery(struct stored_conn_t *sconn, const char *qry, size_t qry_len
if (row == NULL) {
fprintf(
stderr, "[%d]mysql_fetch_row: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
stderr, "[%d]mysql_fetch_row: (%d) %s\n",
__LINE__, mysql_errno(SQCONN(sconn)), mysql_error(SQCONN(sconn))
);
return insertId;
}
@@ -283,7 +283,8 @@ char *scalarString(struct stored_conn_t *sconn, const char *qry, size_t qry_len,
}
int64_t countQuery(struct stored_conn_t *sconn, const char *table, where_builder *wb)
int64_t countQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len, where_builder *wb)
{
char *qry;
size_t qry_len;
@@ -293,10 +294,11 @@ int64_t countQuery(struct stored_conn_t *sconn, const char *table, where_builder
if ((sb = strbld_create()) == 0) {
return -1;
}
strbld_str(sb, "SELECT COUNT(*) AS `C` FROM `", 0);
strbld_str(sb, table, 0);
strbld_str(sb, "` WHERE ", 0);
compileWhereBuilder_sb(wb, sb, 1);
strbld_str(sb, "SELECT COUNT(*) AS `C` FROM ", 0);
escapeTableName_sb(sb, table, table_len);
compileWhereBuilder_sb(sb, wb, 1);
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return -1;
}
@@ -306,41 +308,9 @@ int64_t countQuery(struct stored_conn_t *sconn, const char *table, where_builder
return qry_ret;
}
int softDeleteByIdQuery(struct stored_conn_t *sconn,
const char *table, const char *col, unsigned int id)
{
str_builder *sb;
char *qry;
size_t qry_len;
uint64_t qry_ret;
if ((sb = strbld_create()) == 0) {
return 0;
}
strbld_str(sb, "UPDATE `", 0);
strbld_str(sb, table, 0);
strbld_str(sb, "` SET `deleted` = CURRENT_TIMESTAMP() WHERE ", 0);
compileWhereBuilder_sb(where(0, col, EQ, TYPE_ID, 1, id), sb, 1);
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return 0;
}
qry_ret = simpleQuery(sconn, qry, qry_len);
free(qry);
// Query failed
if (qry_ret == (uint64_t)-1) {
return 0;
}
return 1;
}
int deleteByIdQuery(struct stored_conn_t *sconn,
const char *table, const char *col, unsigned int id)
{
return deleteQuery(sconn, table, where(0, col, EQ, TYPE_ID, 1, id));
}
int deleteQuery(struct stored_conn_t *sconn, const char *table, where_builder *wb)
int64_t updateQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len,
const char *set, size_t set_len, where_builder *wb)
{
str_builder *sb;
char *qry;
@@ -350,10 +320,43 @@ int deleteQuery(struct stored_conn_t *sconn, const char *table, where_builder *w
if ((sb = strbld_create()) == 0) {
return -1;
}
strbld_str(sb, "DELETE FROM `", 0);
strbld_str(sb, table, 0);
strbld_str(sb, "` WHERE ", 0);
compileWhereBuilder_sb(wb, sb, 1);
strbld_str(sb, "UPDATE ", 7);
escapeTableName_sb(sb, table, table_len);
strbld_str(sb, " SET ", 4);
strbld_str(sb, set, set_len);
compileWhereBuilder_sb(sb, wb, 1);
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return -1;
}
qry_ret = simpleQuery(sconn, qry, qry_len);
free(qry);
// Query failure
if (qry_ret == (uint64_t)-1) {
return -1;
}
return qry_ret == 0;
}
int64_t deleteQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len, where_builder *wb)
{
str_builder *sb;
char *qry;
size_t qry_len;
uint64_t qry_ret;
if ((sb = strbld_create()) == 0) {
return -1;
}
strbld_str(sb, "DELETE FROM ", 12);
escapeTableName_sb(sb, table, table_len);
compileWhereBuilder_sb(sb, wb, 1);
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return -1;
}
@@ -369,17 +372,44 @@ int deleteQuery(struct stored_conn_t *sconn, const char *table, where_builder *w
}
int syncIdMap(struct stored_conn_t *sconn, const char *table,
const char *primary_col, const char *map_col,
unsigned int primary_id, size_t n_maps, unsigned int *map_ids)
int64_t updateByIdQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len, const char *set, size_t set_len,
const char *id_col, unsigned int id)
{
return updateQuery(sconn, table, table_len, set, set_len, where(0, id_col, EQ, TYPE_ID, 1, id));
}
int64_t deleteByIdQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len, const char *id_col, unsigned int id)
{
return deleteQuery(sconn, table, table_len, where(0, id_col, EQ, TYPE_ID, 1, id));
}
int64_t softDeleteByIdQuery(struct stored_conn_t *sconn,
const char *table, size_t table_len,
const char *id_col, unsigned int id)
{
char *set;
size_t set_len;
int64_t qry_ret;
columnSetValueStr(&set, &set_len, "deleted", TYPE_RAW, 2, "CURRENT_TIMESTAMP()", 19);
qry_ret = updateByIdQuery(sconn, table, table_len, set, set_len, id_col, id);
free(set);
return qry_ret;
}
int64_t syncIdMap(struct stored_conn_t *sconn, const char *table, size_t table_len,
const char *primary_col, const char *map_col,
unsigned int primary_id, size_t n_maps, unsigned int *map_ids)
{
str_builder *sb;
char *qry;
size_t qry_len;
int qry_ret;
int64_t qry_ret;
size_t idx;
qry_ret = deleteByIdQuery(sconn, table, primary_col, primary_id);
qry_ret = deleteByIdQuery(sconn, table, table_len, primary_col, primary_id);
if (qry_ret != 1) {
return qry_ret;
}
@@ -387,13 +417,14 @@ int syncIdMap(struct stored_conn_t *sconn, const char *table,
if ((sb = strbld_create()) == 0) {
return -1;
}
strbld_str(sb, "INSERT INTO `", 0);
strbld_str(sb, table, 0);
strbld_str(sb, "` (`", 0);
strbld_str(sb, primary_col, 0);
strbld_str(sb, "`, `", 0);
strbld_str(sb, map_col, 0);
strbld_str(sb, "`) VALUES ", 0);
strbld_str(sb, "INSERT INTO ", 12);
escapeTableName_sb(sb, table, table_len);
strbld_str(sb, " (", 2);
escapeColumnName_sb(sb, 0, 0, primary_col, 0);
strbld_char(sb, ',');
escapeColumnName_sb(sb, 0, 0, map_col, 0);
strbld_str(sb, ") VALUES ", 9);
for (idx = 0; idx < n_maps; idx++) {
strbld_char(sb, '(');
db_value_sb(sb, TYPE_ID, 1, primary_id);
@@ -404,6 +435,7 @@ int syncIdMap(struct stored_conn_t *sconn, const char *table,
strbld_char(sb, ',');
}
}
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return -1;
}
@@ -417,13 +449,13 @@ int syncIdMap(struct stored_conn_t *sconn, const char *table,
return 1;
}
int syncIdMap_va(struct stored_conn_t *sconn, const char *table,
const char *primary_col, const char *map_col,
unsigned int primary_id, size_t n_maps, va_list args)
int64_t syncIdMap_va(struct stored_conn_t *sconn, const char *table, size_t table_len,
const char *primary_col, const char *map_col,
unsigned int primary_id, size_t n_maps, va_list args)
{
unsigned int *map_ids;
size_t idx;
int ret_val;
int64_t ret_val;
if ((map_ids = (unsigned int *)malloc(sizeof(unsigned int) * n_maps)) == 0) {
return -1;
@@ -432,7 +464,7 @@ int syncIdMap_va(struct stored_conn_t *sconn, const char *table,
*(map_ids + idx) = va_arg(args, unsigned int);
}
ret_val = syncIdMap(sconn, table, primary_col, map_col, primary_id, n_maps, map_ids);
ret_val = syncIdMap(sconn, table, table_len, primary_col, map_col, primary_id, n_maps, map_ids);
free(map_ids);
@@ -440,14 +472,15 @@ int syncIdMap_va(struct stored_conn_t *sconn, const char *table,
}
int hasIdMap(struct stored_conn_t *sconn, const char *table,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
int64_t hasIdMap(struct stored_conn_t *sconn, const char *table, size_t table_len,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
{
int64_t qry_ret;
qry_ret = countQuery(
sconn, table,
sconn,
table, table_len,
whereAnd(
where(table, primary_col, EQ, TYPE_ID, 1, primary_id),
where(table, map_col, EQ, TYPE_ID, 1, map_id)
@@ -456,9 +489,9 @@ int hasIdMap(struct stored_conn_t *sconn, const char *table,
return qry_ret > 0;
}
int addIdMap(struct stored_conn_t *sconn, const char *table,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
int64_t addIdMap(struct stored_conn_t *sconn, const char *table, size_t table_len,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
{
str_builder *sb;
char *qry;
@@ -468,22 +501,21 @@ int addIdMap(struct stored_conn_t *sconn, const char *table,
if ((sb = strbld_create()) == 0) {
return -1;
}
strbld_str(sb, "INSERT INTO `", 0);
strbld_str(sb, "INSERT INTO ", 12);
escapeTableName_sb(sb, table, table_len);
strbld_str(sb, table, 0);
strbld_str(sb, "` (`", 0);
strbld_str(sb, primary_col, 0);
strbld_str(sb, "`, `", 0);
strbld_str(sb, map_col, 0);
strbld_str(sb, "`) VALUES ", 0);
strbld_char(sb, '(');
escapeColumnName_sb(sb, 0, 0, primary_col, 0);
strbld_char(sb, ',');
escapeColumnName_sb(sb, 0, 0, map_col, 0);
strbld_str(sb, ") VALUES (", 10);
db_value_sb(sb, TYPE_ID, 1, primary_id);
strbld_char(sb, ',');
db_value_sb(sb, TYPE_ID, 1, map_id);
strbld_char(sb, ')');
strbld_str(sb, " ON DUPLICATE KEY UPDATE `", 0);
strbld_str(sb, primary_col, 0);
strbld_str(sb, "` = ", 0);
db_value_sb(sb, TYPE_ID, 1, primary_id);
strbld_str(sb, ") ON DUPLICATE KEY UPDATE ", 0);
columnSetValueStr_sb(sb, primary_col, TYPE_ID, 1, primary_id);
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
return -1;
}
@@ -491,20 +523,22 @@ int addIdMap(struct stored_conn_t *sconn, const char *table,
qry_ret = simpleQuery(sconn, qry, qry_len);
free(qry);
// Query error
if (qry_ret == (uint64_t)-1) {
return -1;
}
return 1;
return (qry_ret > 0);
}
int removeIdMap(struct stored_conn_t *sconn, const char *table,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
int64_t removeIdMap(struct stored_conn_t *sconn, const char *table, size_t table_len,
const char *primary_col, const char *map_col,
unsigned int primary_id, unsigned int map_id)
{
int qry_ret;
int64_t qry_ret;
qry_ret = deleteQuery(
sconn, table,
sconn,
table, table_len,
whereAnd(
where(0, primary_col, EQ, TYPE_ID, 1, primary_id),
where(0, map_col, EQ, TYPE_ID, 1, map_id)