mirror of
https://github.com/mdbtools/mdbtools.git
synced 2026-01-22 02:52:05 +08:00
First cut of sarg (search argument) support.
This commit is contained in:
6
TODO
6
TODO
@@ -9,6 +9,8 @@ Things to Do
|
||||
. Get automake working
|
||||
. Check out text file unixODBC driver to see if it can be adapted to use
|
||||
libmdb (it already has a SQL parser).
|
||||
. Straighten out which functions in libmdb are meant to be used and which ones
|
||||
should be static.
|
||||
. Straighten out which functions in libmdb are meant to be used and which ones should be static.
|
||||
. Create an API reference for libmdb (maybe some man pages).
|
||||
. Sargs need to support all datatypes
|
||||
. Need a way to express logical relationships between sargs (tree)
|
||||
. Add support for index scanning when using sargs
|
||||
|
||||
@@ -69,6 +69,17 @@ enum {
|
||||
MDB_REPID = 0x0f
|
||||
};
|
||||
|
||||
/* SARG operators */
|
||||
enum {
|
||||
MDB_EQUAL = 1,
|
||||
MDB_GT,
|
||||
MDB_LT,
|
||||
MDB_GTEQ,
|
||||
MDB_LTEQ,
|
||||
MDB_ISNULL,
|
||||
MDB_NOTNULL
|
||||
};
|
||||
|
||||
/* hash to store registered backends */
|
||||
GHashTable *mdb_backends;
|
||||
|
||||
@@ -129,9 +140,22 @@ typedef struct {
|
||||
int col_size;
|
||||
void *bind_ptr;
|
||||
GHashTable *properties;
|
||||
int num_sargs;
|
||||
GPtrArray *sargs;
|
||||
unsigned char is_fixed;
|
||||
} MdbColumn;
|
||||
|
||||
typedef union {
|
||||
int i;
|
||||
double d;
|
||||
char s[256];
|
||||
} MdbAny;
|
||||
|
||||
typedef struct {
|
||||
int op;
|
||||
MdbAny value;
|
||||
} MdbSarg;
|
||||
|
||||
/* mem.c */
|
||||
extern void mdb_init();
|
||||
extern void mdb_exit();
|
||||
|
||||
@@ -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
|
||||
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o money.o sargs.o
|
||||
|
||||
all: libmdb
|
||||
|
||||
@@ -53,5 +53,6 @@ file.o: file.c ../include/mdbtools.h
|
||||
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
|
||||
money.o: money.c
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "mdbtools.h"
|
||||
|
||||
char *mdb_money_to_string(MdbHandle *mdb, int start, char *s);
|
||||
static int _mdb_attempt_bind(MdbHandle *mdb,
|
||||
MdbColumn *col, unsigned char isnull, int offset, int len);
|
||||
|
||||
#define MDB_DEBUG 0
|
||||
|
||||
@@ -92,7 +94,7 @@ int mdb_read_row(MdbTableDef *table, int row)
|
||||
{
|
||||
MdbHandle *mdb = table->entry->mdb;
|
||||
MdbColumn *col;
|
||||
int i, j;
|
||||
int i, j, rc;
|
||||
int num_cols, var_cols, fixed_cols;
|
||||
int row_start, row_end;
|
||||
int fixed_cols_found, var_cols_found;
|
||||
@@ -102,6 +104,7 @@ int eod; /* end of data */
|
||||
int delflag, lookupflag;
|
||||
int bitmask_sz;
|
||||
unsigned char null_mask[33]; /* 256 columns max / 8 bits per byte */
|
||||
unsigned char isnull;
|
||||
|
||||
row_start = mdb_get_int16(mdb, 10+(row*2));
|
||||
row_end = mdb_find_end_of_row(mdb, row);
|
||||
@@ -157,13 +160,10 @@ unsigned char null_mask[33]; /* 256 columns max / 8 bits per byte */
|
||||
col = g_ptr_array_index(table->columns,j);
|
||||
if (mdb_is_fixed_col(col) &&
|
||||
++fixed_cols_found <= fixed_cols) {
|
||||
if (col->col_type == MDB_BOOL) {
|
||||
mdb_xfer_bound_bool(mdb, col, mdb_is_null(null_mask, j+1));
|
||||
} else if (mdb_is_null(null_mask, j+1)) {
|
||||
mdb_xfer_bound_data(mdb, 0, col, 0);
|
||||
} else {
|
||||
mdb_xfer_bound_data(mdb,row_start + col_start, col, col->col_size);
|
||||
}
|
||||
isnull = mdb_is_null(null_mask, j+1);
|
||||
rc = _mdb_attempt_bind(mdb, col, isnull,
|
||||
row_start + col_start, col->col_size);
|
||||
if (!rc) return 0;
|
||||
col_start += col->col_size;
|
||||
}
|
||||
}
|
||||
@@ -208,19 +208,34 @@ unsigned char null_mask[33]; /* 256 columns max / 8 bits per byte */
|
||||
- var_cols_found
|
||||
- 1 - num_of_jumps ] - col_start;
|
||||
|
||||
if (col->col_type == MDB_BOOL) {
|
||||
mdb_xfer_bound_bool(mdb, col, mdb_is_null(null_mask, j+1));
|
||||
} else if (mdb_is_null(null_mask, j+1)) {
|
||||
mdb_xfer_bound_data(mdb, 0, col, 0);
|
||||
} else {
|
||||
mdb_xfer_bound_data(mdb,row_start + col_start, col, len);
|
||||
}
|
||||
isnull = mdb_is_null(null_mask, j+1);
|
||||
rc = _mdb_attempt_bind(mdb, col, isnull,
|
||||
row_start + col_start, len);
|
||||
if (!rc) return 0;
|
||||
col_start += len;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
static int _mdb_attempt_bind(MdbHandle *mdb,
|
||||
MdbColumn *col,
|
||||
unsigned char isnull,
|
||||
int offset,
|
||||
int len)
|
||||
{
|
||||
if (col->col_type == MDB_BOOL) {
|
||||
mdb_xfer_bound_bool(mdb, col, isnull);
|
||||
} else if (isnull) {
|
||||
mdb_xfer_bound_data(mdb, 0, col, 0);
|
||||
} else {
|
||||
if (!mdb_test_sargs(mdb, col, offset, len)) {
|
||||
return 0;
|
||||
}
|
||||
mdb_xfer_bound_data(mdb, offset, col, len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int mdb_read_next_dpg(MdbTableDef *table)
|
||||
{
|
||||
MdbCatalogEntry *entry = table->entry;
|
||||
@@ -242,6 +257,7 @@ int mdb_fetch_row(MdbTableDef *table)
|
||||
{
|
||||
MdbHandle *mdb = table->entry->mdb;
|
||||
int rows;
|
||||
int rc;
|
||||
|
||||
if (table->num_rows==0)
|
||||
return 0;
|
||||
@@ -253,18 +269,19 @@ int rows;
|
||||
mdb_read_next_dpg(table);
|
||||
}
|
||||
|
||||
rows = mdb_get_int16(mdb,8);
|
||||
do {
|
||||
rows = mdb_get_int16(mdb,8);
|
||||
|
||||
/* if at end of page, find a new page */
|
||||
if (table->cur_row >= rows) {
|
||||
table->cur_row=0;
|
||||
if (!mdb_read_next_dpg(table)) return 0;
|
||||
}
|
||||
/* if at end of page, find a new page */
|
||||
if (table->cur_row >= rows) {
|
||||
table->cur_row=0;
|
||||
if (!mdb_read_next_dpg(table)) return 0;
|
||||
}
|
||||
|
||||
mdb_read_row(table,
|
||||
table->cur_row);
|
||||
rc = mdb_read_row(table, table->cur_row);
|
||||
table->cur_row++;
|
||||
} while (!rc);
|
||||
|
||||
table->cur_row++;
|
||||
return 1;
|
||||
}
|
||||
void mdb_data_dump(MdbTableDef *table)
|
||||
|
||||
101
src/libmdb/sargs.c
Normal file
101
src/libmdb/sargs.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/* 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"
|
||||
|
||||
int mdb_test_int(MdbSarg *sarg, gint32 i)
|
||||
{
|
||||
switch (sarg->op) {
|
||||
case MDB_EQUAL:
|
||||
if (sarg->value.i == i) return 1;
|
||||
break;
|
||||
case MDB_GT:
|
||||
if (sarg->value.i < i) return 1;
|
||||
break;
|
||||
case MDB_LT:
|
||||
if (sarg->value.i > i) return 1;
|
||||
break;
|
||||
case MDB_GTEQ:
|
||||
if (sarg->value.i <= i) return 1;
|
||||
break;
|
||||
case MDB_LTEQ:
|
||||
if (sarg->value.i >= i) return 1;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Calling mdb_test_sarg on unknown operator. Add code to mdb_test_int() for operator %d\n",sarg->op);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int mdb_test_sarg(MdbHandle *mdb, MdbColumn *col, MdbSarg *sarg, int offset, int len)
|
||||
{
|
||||
switch (col->col_type) {
|
||||
case MDB_INT:
|
||||
return mdb_test_int(sarg, mdb_get_int16(mdb, offset));
|
||||
break;
|
||||
case MDB_LONGINT:
|
||||
return mdb_test_int(sarg, mdb_get_int32(mdb, offset));
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Calling mdb_test_sarg on unknown type. Add code to mdb_test_sarg() for type %d\n",col->col_type);
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int mdb_test_sargs(MdbHandle *mdb, MdbColumn *col, int offset, int len)
|
||||
{
|
||||
MdbSarg *sarg;
|
||||
int i;
|
||||
|
||||
for (i=0;i<col->num_sargs;i++) {
|
||||
sarg = g_ptr_array_index (col->sargs, i);
|
||||
if (!mdb_test_sarg(mdb, col, sarg, offset, len)) {
|
||||
/* sarg didn't match, no sense going on */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
int mdb_add_sarg(MdbColumn *col, MdbSarg *in_sarg)
|
||||
{
|
||||
MdbSarg *sarg;
|
||||
if (!col->sargs) {
|
||||
col->sargs = g_ptr_array_new();
|
||||
}
|
||||
sarg = g_memdup(in_sarg,sizeof(MdbSarg));
|
||||
g_ptr_array_add(col->sargs, sarg);
|
||||
col->num_sargs++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
int mdb_add_sarg_by_name(MdbTableDef *table, char *colname, MdbSarg *in_sarg)
|
||||
{
|
||||
MdbColumn *col;
|
||||
int i;
|
||||
|
||||
for (i=0;i<table->num_cols;i++) {
|
||||
col = g_ptr_array_index (table->columns, i);
|
||||
if (!strcmp(col->name,colname)) {
|
||||
return mdb_add_sarg(col, in_sarg);
|
||||
}
|
||||
}
|
||||
/* else didn't find the column return 0! */
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user