Moved the creation of the meta-extended table to the private implementation
This commit is contained in:
40
src/modb.c
40
src/modb.c
@@ -4,10 +4,10 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "modb.h"
|
#include "modb.h"
|
||||||
#include "modb_p.h"
|
|
||||||
#include "db_query.h"
|
|
||||||
#include "strext.h"
|
#include "strext.h"
|
||||||
|
|
||||||
|
#include "modb_p.h"
|
||||||
|
|
||||||
|
|
||||||
int modbCreate(struct stored_conn_t *sconn, struct modb_t *modb)
|
int modbCreate(struct stored_conn_t *sconn, struct modb_t *modb)
|
||||||
{
|
{
|
||||||
@@ -79,39 +79,9 @@ int modbAccountingDestroy(struct stored_conn_t *sconn, struct modb_t *modb)
|
|||||||
int modbMetaExtCreate(struct stored_conn_t *sconn, struct modb_t *modb,
|
int modbMetaExtCreate(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
struct column_data_t **col_data, size_t cols)
|
struct column_data_t **col_data, size_t cols)
|
||||||
{
|
{
|
||||||
char *qry;
|
uint64_t err = 0
|
||||||
uint64_t res;
|
| createMetaExtTable(sconn, modb, col_data, cols);
|
||||||
size_t qry_len;
|
return err == 0;
|
||||||
str_builder *sb;
|
|
||||||
char *colstr;
|
|
||||||
|
|
||||||
if ((sb = strbld_create()) == 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
strbld_str(sb, "CREATE TABLE `", 0);
|
|
||||||
strbld_str(sb, modb->name, modb->name_len);
|
|
||||||
strbld_str(sb, META_EXT_TABLE, STR_LEN(META_EXT_TABLE));
|
|
||||||
strbld_str(sb, "` ", 2);
|
|
||||||
strbld_str(sb, "(", 1);
|
|
||||||
strbld_str(sb, "`mdo_id` INT UNSIGNED NOT NULL", 0);
|
|
||||||
for (size_t c = 0; c < cols; c++) {
|
|
||||||
struct column_data_t *col = *(col_data + c);
|
|
||||||
if ((colstr = createColString(col)) == 0) {
|
|
||||||
strbld_destroy(&sb);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
strbld_str(sb, colstr, 0);
|
|
||||||
free(colstr);
|
|
||||||
}
|
|
||||||
strbld_str(sb, ", INDEX (`mdo_id`))", 0);
|
|
||||||
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = simpleQuery(sconn, qry, qry_len);
|
|
||||||
free(qry);
|
|
||||||
|
|
||||||
return (res == (uint64_t)-1) ? -1 : (int)res;
|
|
||||||
}
|
}
|
||||||
int modbMetaExtExists(struct stored_conn_t *sconn, struct modb_t *modb)
|
int modbMetaExtExists(struct stored_conn_t *sconn, struct modb_t *modb)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
#ifndef H__MODB__
|
#ifndef H__MODB__
|
||||||
#define H__MODB__
|
#define H__MODB__
|
||||||
|
|
||||||
#include "db_connection.h"
|
#include "database.h"
|
||||||
#include "db_column.h"
|
|
||||||
#include "modb_types.h"
|
#include "modb_types.h"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
157
src/modb_p.c
157
src/modb_p.c
@@ -4,7 +4,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "modb_p.h"
|
#include "modb_p.h"
|
||||||
#include "db_query.h"
|
|
||||||
#include "strext.h"
|
#include "strext.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -219,64 +218,6 @@ uint64_t createUserGroupsTable(struct stored_conn_t *sconn, struct modb_t *modb)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tableExists(struct stored_conn_t *sconn, struct modb_t *modb,
|
|
||||||
const char *suffix, size_t suffix_len)
|
|
||||||
{
|
|
||||||
char *qry, *res;
|
|
||||||
size_t qry_len;
|
|
||||||
str_builder *sb;
|
|
||||||
int retval = 0;
|
|
||||||
|
|
||||||
if ((sb = strbld_create()) == 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
retval += strbld_str(sb, "SHOW TABLES LIKE '", 0);
|
|
||||||
retval += strbld_str(sb, modb->name, modb->name_len);
|
|
||||||
retval += strbld_str(sb, suffix, suffix_len);
|
|
||||||
retval += strbld_char(sb, '\'');
|
|
||||||
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = scalarString(sconn, qry, qry_len, "Z");
|
|
||||||
if (res == 0 || strlen(res) != (modb->name_len + suffix_len)) {
|
|
||||||
retval = -1;
|
|
||||||
} else {
|
|
||||||
retval = strncmp(res, modb->name, modb->name_len) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res != 0) {
|
|
||||||
free(res);
|
|
||||||
}
|
|
||||||
free(qry);
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t destroyTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
|
||||||
const char *suffix, size_t suffix_len)
|
|
||||||
{
|
|
||||||
char *qry;
|
|
||||||
uint64_t res;
|
|
||||||
size_t qry_len;
|
|
||||||
str_builder *sb;
|
|
||||||
|
|
||||||
if ((sb = strbld_create()) == 0) {
|
|
||||||
return (uint64_t)-1;
|
|
||||||
}
|
|
||||||
strbld_str(sb, "DROP TABLE `", 0);
|
|
||||||
strbld_str(sb, modb->name, modb->name_len);
|
|
||||||
strbld_str(sb, suffix, suffix_len);
|
|
||||||
strbld_str(sb, "` ", 2);
|
|
||||||
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
|
||||||
return (uint64_t)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = simpleQuery(sconn, qry, qry_len);
|
|
||||||
free(qry);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *createColString(struct column_data_t *col)
|
char *createColString(struct column_data_t *col)
|
||||||
{
|
{
|
||||||
@@ -346,4 +287,102 @@ char *createColString(struct column_data_t *col)
|
|||||||
|
|
||||||
return colstr;
|
return colstr;
|
||||||
}
|
}
|
||||||
|
uint64_t createMetaExtTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
|
struct column_data_t **col_data, size_t cols)
|
||||||
|
{
|
||||||
|
char *qry;
|
||||||
|
uint64_t res;
|
||||||
|
size_t qry_len;
|
||||||
|
str_builder *sb;
|
||||||
|
char *colstr;
|
||||||
|
|
||||||
|
if ((sb = strbld_create()) == 0) {
|
||||||
|
return (uint64_t)-1;
|
||||||
|
}
|
||||||
|
strbld_str(sb, "CREATE TABLE `", 0);
|
||||||
|
strbld_str(sb, modb->name, modb->name_len);
|
||||||
|
strbld_str(sb, META_EXT_TABLE, STR_LEN(META_EXT_TABLE));
|
||||||
|
strbld_str(sb, "` ", 2);
|
||||||
|
strbld_str(sb, "(", 1);
|
||||||
|
strbld_str(sb, "`mdo_id` INT UNSIGNED NOT NULL", 0);
|
||||||
|
for (size_t c = 0; c < cols; c++) {
|
||||||
|
struct column_data_t *col = *(col_data + c);
|
||||||
|
if ((colstr = createColString(col)) == 0) {
|
||||||
|
strbld_destroy(&sb);
|
||||||
|
return (uint64_t)-1;
|
||||||
|
}
|
||||||
|
strbld_str(sb, colstr, 0);
|
||||||
|
free(colstr);
|
||||||
|
}
|
||||||
|
strbld_str(sb, ", INDEX (`mdo_id`))", 0);
|
||||||
|
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
||||||
|
return (uint64_t)-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = simpleQuery(sconn, qry, qry_len);
|
||||||
|
free(qry);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int tableExists(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
|
const char *suffix, size_t suffix_len)
|
||||||
|
{
|
||||||
|
char *qry, *res;
|
||||||
|
size_t qry_len;
|
||||||
|
str_builder *sb;
|
||||||
|
int retval = 0;
|
||||||
|
|
||||||
|
if ((sb = strbld_create()) == 0) {
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
retval += strbld_str(sb, "SHOW TABLES LIKE '", 0);
|
||||||
|
retval += strbld_str(sb, modb->name, modb->name_len);
|
||||||
|
retval += strbld_str(sb, suffix, suffix_len);
|
||||||
|
retval += strbld_char(sb, '\'');
|
||||||
|
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = scalarString(sconn, qry, qry_len, "Z");
|
||||||
|
if (res == 0 || strlen(res) != (modb->name_len + suffix_len)) {
|
||||||
|
retval = -1;
|
||||||
|
} else {
|
||||||
|
retval = strncmp(res, modb->name, modb->name_len) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res != 0) {
|
||||||
|
free(res);
|
||||||
|
}
|
||||||
|
free(qry);
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint64_t destroyTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
|
const char *suffix, size_t suffix_len)
|
||||||
|
{
|
||||||
|
char *qry;
|
||||||
|
uint64_t res;
|
||||||
|
size_t qry_len;
|
||||||
|
str_builder *sb;
|
||||||
|
|
||||||
|
if ((sb = strbld_create()) == 0) {
|
||||||
|
return (uint64_t)-1;
|
||||||
|
}
|
||||||
|
strbld_str(sb, "DROP TABLE `", 0);
|
||||||
|
strbld_str(sb, modb->name, modb->name_len);
|
||||||
|
strbld_str(sb, suffix, suffix_len);
|
||||||
|
strbld_str(sb, "` ", 2);
|
||||||
|
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
||||||
|
return (uint64_t)-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = simpleQuery(sconn, qry, qry_len);
|
||||||
|
free(qry);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,8 +4,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "db_connection.h"
|
#include "database.h"
|
||||||
#include "db_column.h"
|
|
||||||
#include "modb_types.h"
|
#include "modb_types.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -30,12 +29,14 @@ uint64_t createUsersTable(struct stored_conn_t *sconn, struct modb_t *modb);
|
|||||||
uint64_t createGroupsTable(struct stored_conn_t *sconn, struct modb_t *modb);
|
uint64_t createGroupsTable(struct stored_conn_t *sconn, struct modb_t *modb);
|
||||||
uint64_t createUserGroupsTable(struct stored_conn_t *sconn, struct modb_t *modb);
|
uint64_t createUserGroupsTable(struct stored_conn_t *sconn, struct modb_t *modb);
|
||||||
|
|
||||||
|
uint64_t createMetaExtTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
|
struct column_data_t **col_data, size_t cols);
|
||||||
|
|
||||||
int tableExists(struct stored_conn_t *sconn, struct modb_t *modb,
|
int tableExists(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
const char *suffix, size_t suffix_len);
|
const char *suffix, size_t suffix_len);
|
||||||
|
|
||||||
uint64_t destroyTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
uint64_t destroyTable(struct stored_conn_t *sconn, struct modb_t *modb,
|
||||||
const char *suffix, size_t suffix_len);
|
const char *suffix, size_t suffix_len);
|
||||||
|
|
||||||
char *createColString(struct column_data_t *col);
|
|
||||||
|
|
||||||
#endif // H__MODB_P__
|
#endif // H__MODB_P__
|
||||||
|
|||||||
Reference in New Issue
Block a user