new index code

This commit is contained in:
brianb
2001-04-01 22:10:15 +00:00
parent b40283a8f8
commit 5ceaff28ce
9 changed files with 129 additions and 15 deletions

View File

@@ -80,6 +80,11 @@ enum {
MDB_NOTNULL
};
enum {
MDB_ASC,
MDB_DESC
};
/* hash to store registered backends */
GHashTable *mdb_backends;
@@ -121,15 +126,26 @@ typedef struct {
MdbCatalogEntry *entry;
char name[MDB_MAX_OBJ_NAME+1];
int num_cols;
int num_rows;
int num_pgs;
int first_data_pg;
GPtrArray *columns;
int num_rows;
int index_start;
int num_real_idxs;
int num_idxs;
GPtrArray *indices;
int first_data_pg;
int cur_pg_num;
int cur_phys_pg;
int cur_row;
} MdbTableDef;
typedef struct {
int index_num;
char name[MDB_MAX_OBJ_NAME+1];
unsigned char primary_key;
int first_pg;
unsigned char sort_order;
} MdbIndex;
typedef struct {
char name[MDB_MAX_OBJ_NAME+1];
} MdbColumnProp;

View File

@@ -20,7 +20,7 @@ INSTALL= @INSTALL@
LIBLIST = libmdb.a
INC = -I ../include `glib-config --cflags`
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o money.o sargs.o
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o money.o sargs.o index.o
all: libmdb
@@ -54,5 +54,6 @@ kkd.o: kkd.c ../include/mdbtools.h
mem.o: mem.c ../include/mdbtools.h
table.o: table.c ../include/mdbtools.h
sargs.o: sargs.c ../include/mdbtools.h
index.o: sargs.c ../include/mdbtools.h
money.o: money.c

View File

@@ -245,6 +245,7 @@ MdbHandle *mdb = entry->mdb;
if (!mdb_read_pg(mdb, table->cur_phys_pg++))
return 0;
} while (mdb->pg_buf[0]!=0x01 || mdb_get_int32(mdb, 4)!=entry->table_pg);
/* fprintf(stderr,"returning new page %ld\n", table->cur_phys_pg); */
return table->cur_phys_pg;
}
int mdb_rewind_table(MdbTableDef *table)

69
src/libmdb/index.c Normal file
View File

@@ -0,0 +1,69 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mdbtools.h"
GPtrArray *mdb_read_indices(MdbTableDef *table)
{
MdbHandle *mdb = table->entry->mdb;
MdbIndex idx, *pidx;
int len, i;
int cur_pos;
int name_sz;
/* FIX ME -- doesn't handle multipage table headers */
table->indices = g_ptr_array_new();
cur_pos = table->index_start;
for (i=0;i<table->num_real_idxs;i++) {
memset(&idx, '\0', sizeof(MdbIndex));
idx.index_num = i;
cur_pos += 34;
idx.first_pg = mdb_get_int32(mdb, cur_pos);
cur_pos += 5;
mdb_append_index(table->indices, &idx);
}
for (i=0;i<table->num_idxs;i++) {
pidx = g_ptr_array_index (table->indices, i);
cur_pos += 19;
if (mdb->pg_buf[cur_pos++]==0x01)
pidx->primary_key=1;
}
for (i=0;i<table->num_idxs;i++) {
pidx = g_ptr_array_index (table->indices, i);
name_sz=mdb->pg_buf[cur_pos++];
memcpy(pidx->name, &mdb->pg_buf[cur_pos], name_sz);
pidx->name[name_sz]='\0';
//fprintf(stderr, "index name %s\n", pidx->name);
cur_pos += name_sz;
}
}
void mdb_index_dump(MdbIndex *idx)
{
int i;
fprintf(stdout,"index number %d\n", idx->index_num);
fprintf(stdout,"index name %s\n", idx->name);
fprintf(stdout,"index first page %d\n", idx->first_pg);
if (idx->primary_key) fprintf(stdout,"index is a primary key\n");
}

View File

@@ -75,10 +75,21 @@ mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
{
MdbColumn *col;
col = g_memdup(in_col,sizeof(MdbCatalogEntry));
col = g_memdup(in_col,sizeof(MdbColumn));
g_ptr_array_add(columns, col);
}
mdb_free_columns(GPtrArray *columns)
{
g_ptr_array_free(columns, TRUE);
}
mdb_append_index(GPtrArray *indices, MdbIndex *in_idx)
{
MdbIndex *idx;
idx = g_memdup(in_idx,sizeof(MdbIndex));
g_ptr_array_add(indices, idx);
}
mdb_free_indices(GPtrArray *indices)
{
g_ptr_array_free(indices, TRUE);
}

View File

@@ -46,6 +46,9 @@ int mdb_test_int(MdbSarg *sarg, gint32 i)
int mdb_test_sarg(MdbHandle *mdb, MdbColumn *col, MdbSarg *sarg, int offset, int len)
{
switch (col->col_type) {
case MDB_BYTE:
return mdb_test_int(sarg, mdb_get_byte(mdb, offset));
break;
case MDB_INT:
return mdb_test_int(sarg, mdb_get_int16(mdb, offset));
break;

View File

@@ -42,7 +42,8 @@ int len, i;
table->num_rows = mdb_get_int32(mdb,12);
table->num_cols = mdb_get_int16(mdb,25);
table->num_pgs = mdb_get_int32(mdb,31);
table->num_idxs = mdb_get_int32(mdb,27);
table->num_real_idxs = mdb_get_int32(mdb,31);
table->first_data_pg = mdb_get_int16(mdb,36);
return table;
@@ -75,7 +76,7 @@ int name_sz;
table->columns = g_ptr_array_new();
cur_col = 43 + (table->num_pgs * 8);
cur_col = 43 + (table->num_real_idxs * 8);
/* new code based on patch submitted by Tim Nelson 2000.09.27 */
@@ -134,6 +135,7 @@ int name_sz;
cur_name += name_sz + 1;
}
table->index_start = cur_name;
return table->columns;
}
@@ -141,16 +143,20 @@ void mdb_table_dump(MdbCatalogEntry *entry)
{
MdbTableDef *table;
MdbColumn *col;
MdbIndex *idx;
MdbHandle *mdb = entry->mdb;
int i;
table = mdb_read_table(entry);
fprintf(stdout,"definition page = %d\n",entry->table_pg);
fprintf(stdout,"number of datarows = %d\n",table->num_rows);
fprintf(stdout,"number of columns = %d\n",table->num_cols);
fprintf(stdout,"number of datapages = %d\n",table->num_pgs);
fprintf(stdout,"number of indices = %d\n",table->num_real_idxs);
fprintf(stdout,"first data page = %d\n",table->first_data_pg);
mdb_read_columns(table);
mdb_read_indices(table);
for (i=0;i<table->num_cols;i++) {
col = g_ptr_array_index(table->columns,i);
@@ -159,4 +165,10 @@ int i;
mdb_get_coltype_string(mdb->default_backend, col->col_type),
col->col_size);
}
for (i=0;i<table->num_idxs;i++) {
idx = g_ptr_array_index (table->indices, i);
mdb_index_dump(idx);
}
}