Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reset function to db backend #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions backend/db/sql-generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1953,4 +1953,157 @@ backend_iterate(gpointer _iterator, bson_t* metadata, GError** error)
/*something failed very hard*/
return FALSE;
}

static
gboolean
backend_reset(gpointer _batch, GError** error)
{
J_TRACE_FUNCTION(NULL);

JDBType type;
JThreadVariables* thread_variables = NULL;
void* stmt1;
void* stmt2;
JSqlBatch* batch = _batch;
gboolean found1 = FALSE;
gboolean found2 = FALSE;
g_autoptr(GArray) arr_types_out1 = NULL;
g_autoptr(GArray) arr_types_out2 = NULL;
JDBTypeValue value1;
JDBTypeValue value2;
char sql_strbuf[256];
char table_name[128];
g_autoptr(GString) error_string = NULL;

g_return_val_if_fail(_batch != NULL, FALSE);
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

if (G_UNLIKELY(!(thread_variables = thread_variables_get(error))))
{
goto _error3;
}
if (G_UNLIKELY(!_backend_batch_execute(batch, error)))
{
//no ddl in transaction - most databases wont support that - continue without any open transaction
goto _error3;
}
arr_types_out1 = g_array_new(FALSE, FALSE, sizeof(JDBType));
type = J_DB_TYPE_STRING;
g_array_append_val(arr_types_out1, type);
arr_types_out2 = g_array_new(FALSE, FALSE, sizeof(JDBType));
type = J_DB_TYPE_UINT32;
g_array_append_val(arr_types_out2, type);
do
{
snprintf(sql_strbuf, sizeof(sql_strbuf), sql_get_table_names, batch->namespace);
if (G_UNLIKELY(!j_sql_prepare(thread_variables->sql_backend, sql_strbuf, &stmt1, NULL, arr_types_out1, error)))
{
goto _error3;
}
if (G_UNLIKELY(!j_sql_step(thread_variables->sql_backend, stmt1, &found1, error)))
{
goto _error2;
}
if (!found1)
{
break;
}
value1.val_string = NULL;
if (G_UNLIKELY(!j_sql_column(thread_variables->sql_backend, stmt1, 0, J_DB_TYPE_STRING, &value1, error)))
{
goto _error2;
}
strncpy(table_name, value1.val_string, sizeof(table_name));
if (G_UNLIKELY(!j_sql_reset(thread_variables->sql_backend, stmt1, error)))
{
goto _error2;
}
if (G_UNLIKELY(!j_sql_finalize(thread_variables->sql_backend, stmt1, error)))
{
goto _error3;
}
snprintf(sql_strbuf, sizeof(sql_strbuf), "SELECT COUNT(*) FROM '%s'", table_name);
if (G_UNLIKELY(!j_sql_prepare(thread_variables->sql_backend, sql_strbuf, &stmt2, NULL, arr_types_out2, error)))
{
goto _error3;
}
if (G_UNLIKELY(!j_sql_step(thread_variables->sql_backend, stmt2, &found2, error)))
{
goto _error;
}
if (!found2)
{
g_set_error(error, J_BACKEND_DB_ERROR, 0, "reset %s count not found", table_name);
goto _error;
}
if (G_UNLIKELY(!j_sql_column(thread_variables->sql_backend, stmt2, 0, J_DB_TYPE_UINT32, &value2, error)))
{
goto _error;
}
if (value2.val_uint32 > 0)
{
if (!error_string)
error_string = g_string_new("reset -- ");
g_string_append_printf(error_string, "%s -> %d, ", table_name, value2.val_uint32);
}
if (G_UNLIKELY(!j_sql_reset(thread_variables->sql_backend, stmt2, error)))
{
goto _error;
}
if (G_UNLIKELY(!j_sql_finalize(thread_variables->sql_backend, stmt2, error)))
{
goto _error3;
}
snprintf(sql_strbuf, sizeof(sql_strbuf), "DROP TABLE '%s'", table_name);
if (G_UNLIKELY(!j_sql_exec(thread_variables->sql_backend, sql_strbuf, error)))
{
goto _error3;
}
} while (TRUE);
if (G_UNLIKELY(!j_sql_reset(thread_variables->sql_backend, stmt1, error)))
{
goto _error2;
}
if (G_UNLIKELY(!j_sql_finalize(thread_variables->sql_backend, stmt1, error)))
{
goto _error3;
}
if (error_string)
{
g_set_error(error, J_BACKEND_DB_ERROR, 0, "%s", error_string->str);
goto _error3;
}
if (G_UNLIKELY(!_backend_batch_start(batch, error)))
{
goto _error3;
}
return TRUE;
_error:
if (G_UNLIKELY(!j_sql_finalize(thread_variables->sql_backend, stmt2, NULL)))
{
goto _error2;
}
if (G_UNLIKELY(!_backend_batch_start(batch, NULL)))
{
goto _error;
}
return FALSE;
_error2:
if (G_UNLIKELY(!j_sql_finalize(thread_variables->sql_backend, stmt1, NULL)))
{
goto _error3;
}
if (G_UNLIKELY(!_backend_batch_start(batch, NULL)))
{
goto _error;
}
return FALSE;
_error3:
/*something failed very hard*/
if (G_UNLIKELY(!_backend_batch_start(batch, NULL)))
{
goto _error;
}
return FALSE;
}
#endif
Loading