Modifications to metadata and meta_ext handling
This commit is contained in:
@@ -14,10 +14,12 @@
|
||||
int tableRowsToMetadataList(column_data **col_data, size_t n_cols,
|
||||
struct metadata_t ***metadata_list, size_t *n_metadatas)
|
||||
{
|
||||
column_data *col_id, *col_type, *col_title, *col_owner_id,
|
||||
column_data *col_id, *col_title, *col_owner_id,
|
||||
*col_created, *col_updated, *col_deleted;
|
||||
size_t n_rows, idx;
|
||||
struct metadata_t *metadata;
|
||||
column_data **ext_cols;
|
||||
column_data *col;
|
||||
|
||||
n_rows = (*col_data)->n_values;
|
||||
if (n_rows == 0) {
|
||||
@@ -25,16 +27,32 @@ int tableRowsToMetadataList(column_data **col_data, size_t n_cols,
|
||||
}
|
||||
|
||||
col_id = findColumnByName(col_data, n_cols, "mdo_id");
|
||||
col_type = findColumnByName(col_data, n_cols, "type");
|
||||
//col_type = findColumnByName(col_data, n_cols, "type");
|
||||
col_title = findColumnByName(col_data, n_cols, "title");
|
||||
col_owner_id = findColumnByName(col_data, n_cols, "owner_id");
|
||||
col_created = findColumnByName(col_data, n_cols, "created");
|
||||
col_updated = findColumnByName(col_data, n_cols, "updated");
|
||||
col_deleted = findColumnByName(col_data, n_cols, "deleted");
|
||||
|
||||
size_t n_ext_cols = 0;
|
||||
ext_cols = malloc(n_cols * sizeof(column_data *));
|
||||
if (ext_cols == 0) {
|
||||
fprintf(stderr, "[%d]malloc: (%d) %s\n", __LINE__, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
size_t tbl_len = STR_LEN(META_EXT_TABLE);
|
||||
for (idx = 0; idx < n_cols; idx++) {
|
||||
col = *(col_data + idx);
|
||||
if (strncmp(col->table + (col->table_len - tbl_len), META_EXT_TABLE, tbl_len) == 0) {
|
||||
*(ext_cols + n_ext_cols) = col;
|
||||
n_ext_cols++;
|
||||
}
|
||||
}
|
||||
|
||||
*metadata_list = (struct metadata_t **)malloc(sizeof(struct metadata_t *) * n_rows);
|
||||
if (*metadata_list == 0) {
|
||||
fprintf(stderr, "[%d]malloc: (%d) %s\n", __LINE__, errno, strerror(errno));
|
||||
free(ext_cols);
|
||||
return -errno;
|
||||
}
|
||||
memset(*metadata_list, 0, sizeof(struct metadata_t *) * n_rows);
|
||||
@@ -43,19 +61,21 @@ int tableRowsToMetadataList(column_data **col_data, size_t n_cols,
|
||||
metadata = allocMetadata();
|
||||
if (metadata == 0) {
|
||||
freeMetadataList(metadata_list, idx - 1);
|
||||
free(ext_cols);
|
||||
return -1;
|
||||
}
|
||||
|
||||
metadata->id = *(col_id->data.ptr_uint32 + idx);
|
||||
|
||||
if (!moveColumnStrPointer(col_type, idx, 1, &metadata->type, &metadata->type_len)) {
|
||||
freeMetadataList(metadata_list, idx);
|
||||
return -1;
|
||||
}
|
||||
metadata->type_c = metadata->type;
|
||||
// if (!moveColumnStrPointer(col_type, idx, 1, &metadata->type, &metadata->type_len)) {
|
||||
// freeMetadataList(metadata_list, idx);
|
||||
// return -1;
|
||||
// }
|
||||
// metadata->type_c = metadata->type;
|
||||
|
||||
if (!moveColumnStrPointer(col_title, idx, 1, &metadata->title, &metadata->title_len)) {
|
||||
freeMetadataList(metadata_list, idx);
|
||||
free(ext_cols);
|
||||
return -1;
|
||||
}
|
||||
metadata->title_c = metadata->title;
|
||||
@@ -70,8 +90,15 @@ int tableRowsToMetadataList(column_data **col_data, size_t n_cols,
|
||||
metadata->deleted_on = *(col_deleted->data.ptr_int64 + idx);
|
||||
}
|
||||
|
||||
if (tableRowToMetaExt(ext_cols, n_ext_cols, idx, &metadata->ext) != 1) {
|
||||
freeMetadataList(metadata_list, idx);
|
||||
free(ext_cols);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*(*metadata_list + idx) = metadata;
|
||||
}
|
||||
free(ext_cols);
|
||||
|
||||
*n_metadatas = n_rows;
|
||||
|
||||
@@ -80,8 +107,8 @@ int tableRowsToMetadataList(column_data **col_data, size_t n_cols,
|
||||
int doMetadataListQuery(stored_conn *sconn, modb_ref *modb, where_builder *wb,
|
||||
struct metadata_t ***metadata_list, size_t *n_metadatas)
|
||||
{
|
||||
char *table;
|
||||
size_t table_len;
|
||||
char *table, *ext_table;
|
||||
size_t table_len, ext_len;
|
||||
char *qry;
|
||||
size_t qry_len;
|
||||
uint64_t qry_ret;
|
||||
@@ -97,14 +124,17 @@ int doMetadataListQuery(stored_conn *sconn, modb_ref *modb, where_builder *wb,
|
||||
return -1;
|
||||
}
|
||||
modbTableName(&table, &table_len, modb, METADATA_TABLE, strlen(METADATA_TABLE));
|
||||
modbTableName(&ext_table, &ext_len, modb, META_EXT_TABLE, strlen(META_EXT_TABLE));
|
||||
|
||||
strbld_str(sb, "SELECT * FROM ", 0);
|
||||
escapeTableName_sb(sb, table, table_len);
|
||||
joinStr_sb(sb, " LEFT", 5, 1, ext_table, ext_len, "mdo_id", 6, table, table_len, "mdo_id", 6);
|
||||
if (wb != 0) {
|
||||
compileWhereBuilder_sb(sb, wb, 0);
|
||||
}
|
||||
strbld_str(sb, " ORDER BY `updated` DESC, `created` DESC", 0);
|
||||
|
||||
modbFreeTableName(&ext_table);
|
||||
modbFreeTableName(&table);
|
||||
if (strbld_finalize_or_destroy(&sb, &qry, &qry_len) != 0) {
|
||||
return -1;
|
||||
@@ -238,9 +268,13 @@ int modbMetadataById(stored_conn *sconn, modb_ref *modb, unsigned int id,
|
||||
{
|
||||
where_builder *wb = 0;
|
||||
int res;
|
||||
char *table;
|
||||
size_t table_len;
|
||||
|
||||
wb = where(0, "mdo_id", EQ, TYPE_ID, 1, id);
|
||||
modbTableName(&table, &table_len, modb, METADATA_TABLE, strlen(METADATA_TABLE));
|
||||
wb = where(table, "mdo_id", EQ, TYPE_ID, 1, id);
|
||||
res = doScalarMetadataListQuery(sconn, modb, wb, metadata);
|
||||
free(table);
|
||||
freeWhereBuilder(&wb);
|
||||
|
||||
return res;
|
||||
@@ -296,6 +330,12 @@ int modbMetadataListByGroupId(stored_conn *sconn, modb_ref *modb, unsigned int g
|
||||
return res;
|
||||
}
|
||||
|
||||
int modbMetadataQuery(stored_conn *sconn, modb_ref *modb, where_builder *wb,
|
||||
struct metadata_t ***metadata_list, size_t *n_metadatas)
|
||||
{
|
||||
return doMetadataListQuery(sconn, modb, wb, metadata_list, n_metadatas);
|
||||
}
|
||||
|
||||
int modbMetadataList(stored_conn *sconn, modb_ref *modb, int with_deleted,
|
||||
struct metadata_t ***metadata_list, size_t *n_metadatas)
|
||||
{
|
||||
@@ -323,9 +363,9 @@ unsigned int modbMetadataCreate(stored_conn *sconn, modb_ref *modb,
|
||||
size_t qry_len;
|
||||
uint64_t qry_ret;
|
||||
|
||||
if (metadata->type_c == 0) {
|
||||
metadata->type_c = metadata->type;
|
||||
}
|
||||
// if (metadata->type_c == 0) {
|
||||
// metadata->type_c = metadata->type;
|
||||
// }
|
||||
if (metadata->title_c == 0) {
|
||||
metadata->title_c = metadata->title;
|
||||
}
|
||||
@@ -337,10 +377,11 @@ unsigned int modbMetadataCreate(stored_conn *sconn, modb_ref *modb,
|
||||
|
||||
strbld_str(sb, "INSERT INTO ", 0);
|
||||
escapeTableName_sb(sb, table, table_len);
|
||||
strbld_str(sb, " (`mdo_id`, `type`, `title`, `owner_id`) VALUES (", 0);
|
||||
// strbld_str(sb, " (`mdo_id`, `type`, `title`, `owner_id`) VALUES (", 0);
|
||||
strbld_str(sb, " (`mdo_id`, `title`, `owner_id`) VALUES (", 0);
|
||||
db_value_sb(sb, TYPE_ID, 1, metadata->id);
|
||||
strbld_char(sb, ',');
|
||||
db_value_sb(sb, TYPE_STRING, 2, metadata->type_c, metadata->type_len);
|
||||
// strbld_char(sb, ',');
|
||||
// db_value_sb(sb, TYPE_STRING, 2, metadata->type_c, metadata->type_len);
|
||||
strbld_char(sb, ',');
|
||||
db_value_sb(sb, TYPE_STRING, 2, metadata->title_c, metadata->title_len);
|
||||
strbld_char(sb, ',');
|
||||
@@ -370,9 +411,9 @@ int64_t modbMetadataReplace(stored_conn *sconn, modb_ref *modb, unsigned int id,
|
||||
size_t table_len, set_len;
|
||||
int64_t qry_ret;
|
||||
|
||||
if (metadata->type_c == 0) {
|
||||
metadata->type_c = metadata->type;
|
||||
}
|
||||
// if (metadata->type_c == 0) {
|
||||
// metadata->type_c = metadata->type;
|
||||
// }
|
||||
if (metadata->title_c == 0) {
|
||||
metadata->title_c = metadata->title;
|
||||
}
|
||||
@@ -380,18 +421,31 @@ int64_t modbMetadataReplace(stored_conn *sconn, modb_ref *modb, unsigned int id,
|
||||
if ((sb = strbld_create()) == 0) {
|
||||
return 0;
|
||||
}
|
||||
columnSetValueStr_sb(sb, "mdo_id", TYPE_ID, 1, id);
|
||||
if (metadata->id != 0 && metadata->id != id) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "mdo_id", TYPE_ID, 1, metadata->id);
|
||||
}
|
||||
if (metadata->type_c != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "type", TYPE_STRING, 2, metadata->type_c, metadata->type_len);
|
||||
}
|
||||
// if (metadata->type_c != 0) {
|
||||
// strbld_char(sb, ',');
|
||||
// columnSetValueStr_sb(sb, "type", TYPE_STRING, 2, metadata->type_c, metadata->type_len);
|
||||
// }
|
||||
if (metadata->title_c != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "title", TYPE_STRING, 2, metadata->title_c, metadata->title_len);
|
||||
}
|
||||
if (metadata->created_on != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "created", TYPE_TIMESTAMP, 1, metadata->created_on);
|
||||
}
|
||||
if (metadata->updated_on != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "updated", TYPE_TIMESTAMP, 1, metadata->updated_on);
|
||||
}
|
||||
if (metadata->deleted_on != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "deleted", TYPE_TIMESTAMP, 1, metadata->deleted_on);
|
||||
}
|
||||
if (metadata->owner != 0) {
|
||||
strbld_char(sb, ',');
|
||||
columnSetValueStr_sb(sb, "owner_id", TYPE_ID, 1, metadata->owner->id);
|
||||
|
||||
Reference in New Issue
Block a user