mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-05-22 18:59:22 +08:00
changed GArray to GPtrArray for MdbColumn list. completed mdb_bind_column() stuff
This commit is contained in:
parent
fa6c08a08d
commit
6e24e9cb02
@ -76,7 +76,7 @@ typedef struct {
|
|||||||
unsigned int kkd_rowid;
|
unsigned int kkd_rowid;
|
||||||
int num_props;
|
int num_props;
|
||||||
GArray *props;
|
GArray *props;
|
||||||
GArray *columns;
|
GPtrArray *columns;
|
||||||
} MdbCatalogEntry;
|
} MdbCatalogEntry;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -86,7 +86,7 @@ typedef struct {
|
|||||||
int num_rows;
|
int num_rows;
|
||||||
int num_pgs;
|
int num_pgs;
|
||||||
int first_data_pg;
|
int first_data_pg;
|
||||||
GArray *columns;
|
GPtrArray *columns;
|
||||||
} MdbTableDef;
|
} MdbTableDef;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -19,12 +19,18 @@
|
|||||||
|
|
||||||
#include "mdbtools.h"
|
#include "mdbtools.h"
|
||||||
|
|
||||||
#define MDB_DEBUG 1
|
#define MDB_DEBUG 0
|
||||||
|
|
||||||
char *mdb_col_to_string(MdbHandle *mdb, int start, int datatype, int size);
|
char *mdb_col_to_string(MdbHandle *mdb, int start, int datatype, int size);
|
||||||
|
|
||||||
void mdb_bind_col(MdbColumn *col, void *bind_ptr)
|
void mdb_bind_column(MdbTableDef *table, int col_num, void *bind_ptr)
|
||||||
{
|
{
|
||||||
|
MdbColumn *col;
|
||||||
|
|
||||||
|
/*
|
||||||
|
** the column arrary is 0 based, so decrement to get 1 based parameter
|
||||||
|
*/
|
||||||
|
col=g_ptr_array_index(table->columns, col_num - 1);
|
||||||
col->bind_ptr = bind_ptr;
|
col->bind_ptr = bind_ptr;
|
||||||
}
|
}
|
||||||
int mdb_find_end_of_row(MdbHandle *mdb, int row)
|
int mdb_find_end_of_row(MdbHandle *mdb, int row)
|
||||||
@ -42,7 +48,7 @@ int rows, row_end;
|
|||||||
int mdb_read_row(MdbTableDef *table, int pg_num, int row)
|
int mdb_read_row(MdbTableDef *table, int pg_num, int row)
|
||||||
{
|
{
|
||||||
MdbHandle *mdb = table->entry->mdb;
|
MdbHandle *mdb = table->entry->mdb;
|
||||||
MdbColumn col;
|
MdbColumn *col;
|
||||||
int j;
|
int j;
|
||||||
int num_cols, var_cols, fixed_cols;
|
int num_cols, var_cols, fixed_cols;
|
||||||
int row_start, row_end;
|
int row_start, row_end;
|
||||||
@ -90,33 +96,33 @@ int delflag, lookupflag;
|
|||||||
|
|
||||||
/* fixed columns */
|
/* fixed columns */
|
||||||
for (j=0;j<table->num_cols;j++) {
|
for (j=0;j<table->num_cols;j++) {
|
||||||
col = g_array_index(table->columns,MdbColumn,j);
|
col = g_ptr_array_index(table->columns,j);
|
||||||
if (mdb_is_fixed_col(&col) &&
|
if (mdb_is_fixed_col(col) &&
|
||||||
++fixed_cols_found <= fixed_cols) {
|
++fixed_cols_found <= fixed_cols) {
|
||||||
if (col.bind_ptr) {
|
if (col->bind_ptr) {
|
||||||
strcpy(col.bind_ptr,
|
strcpy(col->bind_ptr,
|
||||||
mdb_col_to_string(mdb,
|
mdb_col_to_string(mdb,
|
||||||
row_start + col_start,
|
row_start + col_start,
|
||||||
col.col_type,
|
col->col_type,
|
||||||
0)
|
0)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#if MDB_DEBUG
|
#if MDB_DEBUG
|
||||||
fprintf(stdout,"fixed col %s = %s\n",
|
fprintf(stdout,"fixed col %s = %s\n",
|
||||||
col.name,
|
col->name,
|
||||||
mdb_col_to_string(mdb,
|
mdb_col_to_string(mdb,
|
||||||
row_start + col_start,
|
row_start + col_start,
|
||||||
col.col_type,
|
col->col_type,
|
||||||
0));
|
0));
|
||||||
col_start += col.col_size;
|
|
||||||
#endif
|
#endif
|
||||||
|
col_start += col->col_size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* variable columns */
|
/* variable columns */
|
||||||
for (j=0;j<table->num_cols;j++) {
|
for (j=0;j<table->num_cols;j++) {
|
||||||
col = g_array_index(table->columns,MdbColumn,j);
|
col = g_ptr_array_index(table->columns,j);
|
||||||
if (!mdb_is_fixed_col(&col) &&
|
if (!mdb_is_fixed_col(col) &&
|
||||||
++var_cols_found <= var_cols) {
|
++var_cols_found <= var_cols) {
|
||||||
col_start = mdb->pg_buf[row_end-1-var_cols_found];
|
col_start = mdb->pg_buf[row_end-1-var_cols_found];
|
||||||
|
|
||||||
@ -127,24 +133,24 @@ int delflag, lookupflag;
|
|||||||
|
|
||||||
#if MDB_DEBUG
|
#if MDB_DEBUG
|
||||||
fprintf(stdout,"coltype %d colstart %d len %d\n",
|
fprintf(stdout,"coltype %d colstart %d len %d\n",
|
||||||
col.col_type,
|
col->col_type,
|
||||||
col_start,
|
col_start,
|
||||||
len);
|
len);
|
||||||
#endif
|
#endif
|
||||||
if (col.bind_ptr) {
|
if (col->bind_ptr) {
|
||||||
strcpy(col.bind_ptr,
|
strcpy(col->bind_ptr,
|
||||||
mdb_col_to_string(mdb,
|
mdb_col_to_string(mdb,
|
||||||
row_start + col_start,
|
row_start + col_start,
|
||||||
col.col_type,
|
col->col_type,
|
||||||
len)
|
len)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#if MDB_DEBUG
|
#if MDB_DEBUG
|
||||||
fprintf(stdout,"var col %s = %s\n",
|
fprintf(stdout,"var col %s = %s\n",
|
||||||
col.name,
|
col->name,
|
||||||
mdb_col_to_string(mdb,
|
mdb_col_to_string(mdb,
|
||||||
row_start + col_start,
|
row_start + col_start,
|
||||||
col.col_type,
|
col->col_type,
|
||||||
len));
|
len));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -158,9 +164,14 @@ int delflag, lookupflag;
|
|||||||
void mdb_data_dump(MdbTableDef *table)
|
void mdb_data_dump(MdbTableDef *table)
|
||||||
{
|
{
|
||||||
MdbHandle *mdb = table->entry->mdb;
|
MdbHandle *mdb = table->entry->mdb;
|
||||||
int i, pg_num;
|
int i, j, pg_num;
|
||||||
int rows;
|
int rows;
|
||||||
|
char *bound_values[256]; /* warning doesn't handle table > 256 columns. Can that happen? */
|
||||||
|
|
||||||
|
for (i=0;i<table->num_cols;i++) {
|
||||||
|
bound_values[i] = (char *) malloc(256);
|
||||||
|
mdb_bind_column(table, i+1, bound_values[i]);
|
||||||
|
}
|
||||||
for (pg_num=1;pg_num<=table->num_pgs;pg_num++) {
|
for (pg_num=1;pg_num<=table->num_pgs;pg_num++) {
|
||||||
mdb_read_pg(mdb,table->first_data_pg + pg_num);
|
mdb_read_pg(mdb,table->first_data_pg + pg_num);
|
||||||
rows = mdb_get_int16(mdb,8);
|
rows = mdb_get_int16(mdb,8);
|
||||||
@ -169,8 +180,14 @@ int rows;
|
|||||||
rows);
|
rows);
|
||||||
for (i=0;i<rows;i++) {
|
for (i=0;i<rows;i++) {
|
||||||
mdb_read_row(table, table->first_data_pg + pg_num, i);
|
mdb_read_row(table, table->first_data_pg + pg_num, i);
|
||||||
|
for (j=0;j<table->num_cols;j++) {
|
||||||
|
fprintf(stdout, "column %d is %s\n", j+1, bound_values[j]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (i=0;i<table->num_cols;i++) {
|
||||||
|
free(bound_values[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int mdb_is_fixed_col(MdbColumn *col)
|
int mdb_is_fixed_col(MdbColumn *col)
|
||||||
|
@ -57,3 +57,14 @@ void mdb_free_tabledef(MdbTableDef *table)
|
|||||||
{
|
{
|
||||||
if (table) free(table);
|
if (table) free(table);
|
||||||
}
|
}
|
||||||
|
mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
|
||||||
|
{
|
||||||
|
MdbColumn *col;
|
||||||
|
|
||||||
|
col = g_memdup(in_col,sizeof(MdbCatalogEntry));
|
||||||
|
g_ptr_array_add(columns, col);
|
||||||
|
}
|
||||||
|
mdb_free_columns(GPtrArray *columns)
|
||||||
|
{
|
||||||
|
g_ptr_array_free(columns, TRUE);
|
||||||
|
}
|
||||||
|
@ -77,11 +77,10 @@ int len, i;
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
GArray *mdb_read_columns(MdbTableDef *table)
|
GPtrArray *mdb_read_columns(MdbTableDef *table)
|
||||||
{
|
{
|
||||||
MdbHandle *mdb = table->entry->mdb;
|
MdbHandle *mdb = table->entry->mdb;
|
||||||
MdbColumn col;
|
MdbColumn col;
|
||||||
GArray *columns;
|
|
||||||
int len, i;
|
int len, i;
|
||||||
int cur_col, cur_name;
|
int cur_col, cur_name;
|
||||||
int col_type, col_size;
|
int col_type, col_size;
|
||||||
@ -89,7 +88,7 @@ int col_start, name_start;
|
|||||||
char name[MDB_MAX_OBJ_NAME+1];
|
char name[MDB_MAX_OBJ_NAME+1];
|
||||||
int name_sz;
|
int name_sz;
|
||||||
|
|
||||||
table->columns = g_array_new(FALSE,FALSE,sizeof(MdbColumn));
|
table->columns = g_ptr_array_new();
|
||||||
|
|
||||||
col_start = 43 + (table->num_pgs * 8);
|
col_start = 43 + (table->num_pgs * 8);
|
||||||
name_start = col_start + (table->num_cols * 18);
|
name_start = col_start + (table->num_cols * 18);
|
||||||
@ -109,7 +108,7 @@ int name_sz;
|
|||||||
|
|
||||||
cur_col += 18;
|
cur_col += 18;
|
||||||
cur_name += name_sz + 1;
|
cur_name += name_sz + 1;
|
||||||
g_array_append_val(table->columns, col);
|
mdb_append_column(table->columns, &col);
|
||||||
}
|
}
|
||||||
|
|
||||||
return table->columns;
|
return table->columns;
|
||||||
@ -118,7 +117,7 @@ int name_sz;
|
|||||||
void mdb_table_dump(MdbCatalogEntry *entry)
|
void mdb_table_dump(MdbCatalogEntry *entry)
|
||||||
{
|
{
|
||||||
MdbTableDef *table;
|
MdbTableDef *table;
|
||||||
MdbColumn col;
|
MdbColumn *col;
|
||||||
MdbHandle *mdb = entry->mdb;
|
MdbHandle *mdb = entry->mdb;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -130,11 +129,11 @@ int i;
|
|||||||
|
|
||||||
mdb_read_columns(table);
|
mdb_read_columns(table);
|
||||||
for (i=0;i<table->num_cols;i++) {
|
for (i=0;i<table->num_cols;i++) {
|
||||||
col = g_array_index(table->columns,MdbColumn,i);
|
col = g_ptr_array_index(table->columns,i);
|
||||||
|
|
||||||
fprintf(stdout,"column %d Name: %-20s Type: %s(%d)\n",
|
fprintf(stdout,"column %d Name: %-20s Type: %s(%d)\n",
|
||||||
i, col.name,
|
i, col->name,
|
||||||
mdb_get_coltype_string(col.col_type),
|
mdb_get_coltype_string(col->col_type),
|
||||||
col.col_size);
|
col->col_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ int i, j, k;
|
|||||||
MdbHandle *mdb;
|
MdbHandle *mdb;
|
||||||
MdbCatalogEntry entry;
|
MdbCatalogEntry entry;
|
||||||
MdbTableDef *table;
|
MdbTableDef *table;
|
||||||
MdbColumn col;
|
MdbColumn *col;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf (stderr, "Usage: schema <file>\n");
|
fprintf (stderr, "Usage: schema <file>\n");
|
||||||
@ -75,13 +75,13 @@ MdbColumn col;
|
|||||||
|
|
||||||
for (k = 0; k < table->num_cols; k++)
|
for (k = 0; k < table->num_cols; k++)
|
||||||
{
|
{
|
||||||
col = g_array_index (table->columns, MdbColumn, k);
|
col = g_ptr_array_index (table->columns, k);
|
||||||
|
|
||||||
fprintf (stdout, "\t%s\t\t\t%s", col.name,
|
fprintf (stdout, "\t%s\t\t\t%s", col->name,
|
||||||
mdb_get_coltype_string (col.col_type));
|
mdb_get_coltype_string (col->col_type));
|
||||||
|
|
||||||
if (col.col_size != 0)
|
if (col->col_size != 0)
|
||||||
fprintf (stdout, " (%d)", col.col_size);
|
fprintf (stdout, " (%d)", col->col_size);
|
||||||
|
|
||||||
if (k < table->num_cols - 1)
|
if (k < table->num_cols - 1)
|
||||||
fprintf (stdout, ", \n");
|
fprintf (stdout, ", \n");
|
||||||
|
Loading…
Reference in New Issue
Block a user