📄 dbgather.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <glib.h>/* Database abstration layer. This will incude the guts of the * dbgather code, like the stuff in dbpgsqlgather or whatever other database. This * header file will include other header files. */#include "dbwrapper.h"/* Other stuff which is more important */#include "dbclient.h"#include "dbgather.h"#include "dbsqlparse.h"#include "debug.h"#include "dbconstraint.h"#include "dbdefault.h"#include "dbpgsqlgather.h"/** * Build database definiations. Query the database for information to build * a list of tables defined in the database by using sql queries against the database * and using the results to make the structure it returns. Function returns NULL * on failure *//* global database information variable */DbDatabaseDef *globaldb;/** * db_builddef: * * This will probe the database your connected to for detailed information regarding everything * and anything. This is normally done on startup automatically by db_init(). This function * may take a while to execute as it has to do a fair few sql statements to the backend. * * Returns: Database structure */DbDatabaseDef *db_builddef() { DbDatabaseDef *db = NULL; if (globaldb != NULL) return globaldb; /* malloc away */ db = mem_alloc(sizeof(DbDatabaseDef)); db->constraints = NULL; db->name = mem_strdup("Postgresql"); /* basic data gathering, like tables, views, fields etc */ db_buildtabledef(db); db_buildviewdef(db); db_buildrelationdef(db); /* make it global so other stuff gets it */ globaldb = db; /* the more intensese but proper relationship information */ g_assert(db_constraints_init(db)); /* Francis: */ db->methods = NULL;/* { GList *wk; for (wk = g_list_first(db->constraints); wk; wk = wk->next) printf("data %p\n", ((DbConstraintDef*)wk->data)->table[0]); }*/ db_comment_callback_build_constraints(db); db_buildfieldinfo(db); return db; }/** * db_checkpgresult: * @result: Recordset from a query * * Check a query result from db_dbexec(). Will display an error message if the query failed. * * Returns: non-zero on failure */gintdb_checkpgresult(DbRecordSet * result) { return db_dbcheckresult(result); }/* Data Free and shutdown functions */voiddb_freefielddef(DbFieldDef * dbfield) { GList *walk; g_assert(dbfield); if (dbfield->name != NULL) mem_free(dbfield->name); if (dbfield->typename != NULL) mem_free(dbfield->typename); if (dbfield->comment != NULL) { walk = g_list_first(dbfield->comment); while (walk != NULL) { mem_free(walk->data); walk = walk->next; } g_list_free(dbfield->comment); } if (dbfield->defaultvalue != NULL) mem_free(dbfield->defaultvalue); }voiddb_freetabledef(DbTableDef * dbtable) { gint i; g_assert(dbtable); if (dbtable->name != NULL) mem_free(dbtable->name); if (dbtable->query != NULL) mem_free(dbtable->query); if (dbtable->uidfield != NULL) mem_free(dbtable->uidfield); if (dbtable->subtable != NULL) g_list_free(dbtable->subtable); if (dbtable->fromtable != NULL) g_list_free(dbtable->fromtable); for (i = 0; i < dbtable->num; i++) { db_freefielddef(dbtable->field[i]); mem_free(dbtable->field[i]); } g_list_free(dbtable->cref); mem_free(dbtable->field); }/* EOF */gintdb_showallfields() { gint i, j; DbDatabaseDef *db = globaldb; for (i = 0; i < db->numtable; i++) for (j = 0; j < db->table[i]->num; j++) printf("%s.%s\n", db->table[i]->name, db->table[i]->field[j]->name); return 0; }/* no no just kidding, not EOF */gintdb_freedatabasedef(DbDatabaseDef * db) { gint i; g_assert(db); mem_free(db->connstr); for (i = 0; i < db->numtable; i++) { db_freetabledef(db->table[i]); mem_free(db->table[i]); } mem_free(db->table); for (i = 0; i < db->numview; i++) { db_freetabledef(db->view[i]); mem_free(db->view[i]); } mem_free(db->view); mem_free(db->name); db_constraints_uninit(db); /* Francis: blame me if this has problems */ if (db->methods) { db_method_list_cleanup (db->methods); } mem_free(db); return 0; }gintdb_buildfieldinfo(DbDatabaseDef * db) { gint j, viewoffset; gchar *table; gchar *field; GList *walk; DbTableDef *dt; DbFieldDef *df; DbConstraintDef *c; g_assert(db); db_generalisetype(db); walk = g_list_first(db->constraints); /* meaniless comment placed here for your convence */ while (walk != NULL) { c = walk->data; if (c->casual == 0) for (j = 0; j < 1; j++) { /* find the table and field of the reference */ table = c->table[j]; field = c->column[j]; dt = db_findtable(table); if (dt == NULL) continue; df = db_findfield(dt, field); if (df != NULL) { /* mark the fieldtype as a reference type */ df->fieldtype = 1; /* see if a table view exists with this same field name */ viewoffset = db_viewforthis(df->name); if (viewoffset != -1) df->fieldtype = 2; } } walk = walk->next; } /* todo. go though views, and if field exists for exactly same name mark that field as fieldtype 4 */ return 0; }/* Go though the database and assign a numerical number to the type in the general form to represent the data. */gintdb_generalisetype(DbDatabaseDef * db) { int x, y; char *typename; int *datatype; for (y = 0; y < db->numtable; y++) /* for each table in the database ... */ { for (x = 0; x < db->table[y]->num; x++) /* for each field in the table ... */ { typename = db->table[y]->field[x]->typename; /* shortcut to typename of field */ g_strdown(typename); datatype = &db->table[y]->field[x]->datatype; /* shortcut to datatype of field */ *datatype = DBTEXT; /* Default to DBTEXT datatype */ if ((strstr(typename, "int") != NULL) || (strstr(typename, "integer") != NULL)) { *datatype = DBINT; } if ((strstr(typename, "text") != NULL)) { *datatype = DBTEXT; } if ((strstr(typename, "char") != NULL)) { if ((strstr(typename, "varchar") != NULL)) { *datatype = DBTEXT; } else { *datatype = DBCHAR; } } if ((strstr(typename, "float") != NULL) || (strstr(typename, "double")) || (strstr(typename, "precision"))) { *datatype = DBFLOAT; } if ((strstr(typename, "bool") != NULL) || (strstr(typename, "boolean") != NULL)) { *datatype = DBBOOL; } if ((strstr(typename, "date") != NULL)) { *datatype = DBDATE; } if ((strstr(typename, "time") != NULL)) { *datatype = DBTIME; } if ((strstr(typename, "datetime") != NULL)) { *datatype = DBDATETIME; } } } return 0; }GList *db_getalltablenames() { DbDatabaseDef *db; int i; GList *tables = NULL; db = globaldb; for (i = 0; i < db->numtable; i++) { g_assert(db->table[i]); tables = g_list_append(tables, db->table[i]->name); } return tables; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -