2003-01-05 23:00:23 +08:00
|
|
|
/* 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"
|
2003-01-10 04:24:19 +08:00
|
|
|
extern char idx_to_text[];
|
2003-01-05 23:00:23 +08:00
|
|
|
|
2003-01-10 04:24:19 +08:00
|
|
|
void walk_index(MdbHandle *mdb, MdbIndex *idx);
|
2003-01-21 00:04:24 +08:00
|
|
|
|
|
|
|
int
|
2003-01-05 23:00:23 +08:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2004-07-09 20:47:04 +08:00
|
|
|
unsigned int j;
|
2004-07-02 20:29:09 +08:00
|
|
|
MdbHandle *mdb;
|
|
|
|
MdbTableDef *table;
|
|
|
|
MdbIndex *idx;
|
2003-01-05 23:00:23 +08:00
|
|
|
|
|
|
|
if (argc<4) {
|
|
|
|
fprintf(stderr,"Usage: %s <file> <table> <index>\n",argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb_init();
|
2004-04-14 04:06:04 +08:00
|
|
|
if (!(mdb = mdb_open(argv[1], MDB_NOFLAGS))) {
|
2003-01-10 04:24:19 +08:00
|
|
|
fprintf(stderr,"Unable to open database.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-01-05 23:00:23 +08:00
|
|
|
|
2004-07-02 20:29:09 +08:00
|
|
|
table = mdb_read_table_by_name(mdb, argv[2], MDB_TABLE);
|
2003-01-05 23:00:23 +08:00
|
|
|
|
2004-07-02 20:29:09 +08:00
|
|
|
if (table) {
|
|
|
|
mdb_read_columns(table);
|
|
|
|
mdb_read_indices(table);
|
|
|
|
for (j=0;j<table->num_idxs;j++) {
|
|
|
|
idx = g_ptr_array_index (table->indices, j);
|
|
|
|
if (!strcmp(idx->name, argv[3])) {
|
|
|
|
walk_index(mdb, idx);
|
|
|
|
}
|
2003-01-05 23:00:23 +08:00
|
|
|
}
|
2004-07-02 20:29:09 +08:00
|
|
|
mdb_free_tabledef(table);
|
|
|
|
} else {
|
2003-01-05 23:00:23 +08:00
|
|
|
fprintf(stderr,"No table named %s found.\n", argv[2]);
|
|
|
|
}
|
2004-07-02 20:29:09 +08:00
|
|
|
|
2004-04-14 13:58:52 +08:00
|
|
|
mdb_close(mdb);
|
2003-01-05 23:00:23 +08:00
|
|
|
mdb_exit();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
2003-01-10 04:24:19 +08:00
|
|
|
char *
|
|
|
|
page_name(int page_type)
|
|
|
|
{
|
|
|
|
switch(page_type) {
|
|
|
|
case 0x00: return "Database Properties"; break;
|
|
|
|
case 0x01: return "Data"; break;
|
|
|
|
case 0x02: return "Table Definition"; break;
|
|
|
|
case 0x03: return "Index"; break;
|
|
|
|
case 0x04: return "Index Leaf"; break;
|
|
|
|
case 0x05: return "Page Usage"; break;
|
2003-01-21 00:04:24 +08:00
|
|
|
default: return "Unknown";
|
2003-01-10 04:24:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void check_row(MdbHandle *mdb, MdbIndex *idx, guint32 pg, int row, unsigned char *idxrow, int len)
|
|
|
|
{
|
|
|
|
MdbField fields[256];
|
2004-07-17 22:21:43 +08:00
|
|
|
unsigned int num_fields, i, j;
|
2005-06-28 12:53:37 +08:00
|
|
|
int row_start;
|
|
|
|
size_t row_size;
|
2003-01-10 04:24:19 +08:00
|
|
|
MdbColumn *col;
|
2005-05-02 20:46:33 +08:00
|
|
|
gchar buf[256], key[256];
|
2003-01-21 00:04:24 +08:00
|
|
|
int elem;
|
2003-01-10 04:24:19 +08:00
|
|
|
MdbTableDef *table = idx->table;
|
|
|
|
|
|
|
|
mdb_read_pg(mdb, pg);
|
2005-03-07 12:28:12 +08:00
|
|
|
mdb_find_row(mdb, row, &row_start, &row_size);
|
2003-01-10 04:24:19 +08:00
|
|
|
|
2005-03-07 12:28:12 +08:00
|
|
|
num_fields = mdb_crack_row(table, row_start, row_start + row_size - 1,
|
|
|
|
fields);
|
2003-01-10 04:24:19 +08:00
|
|
|
for (i=0;i<idx->num_keys;i++) {
|
|
|
|
col=g_ptr_array_index(table->columns,idx->key_col_num[i]-1);
|
|
|
|
if (col->col_type==MDB_TEXT) {
|
|
|
|
mdb_index_hash_text(buf, key);
|
|
|
|
}
|
|
|
|
for (j=0;j<num_fields;j++) {
|
|
|
|
if (fields[j].colnum+1==idx->key_col_num[i]) {
|
|
|
|
elem = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//j = idx->key_col_num[i];
|
|
|
|
strncpy(buf, fields[elem].value, fields[elem].siz);
|
|
|
|
buf[fields[elem].siz]=0;
|
2003-01-13 06:59:41 +08:00
|
|
|
//fprintf(stdout, "elem %d %d column %d %s %s\n",elem, fields[elem].colnum, idx->key_col_num[i], col->name, buf);
|
2003-01-10 04:24:19 +08:00
|
|
|
if (col->col_type == MDB_TEXT) {
|
2003-01-13 06:59:41 +08:00
|
|
|
// fprintf(stdout, "%s = %s \n", buf, key);
|
2003-01-10 04:24:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-05 23:00:23 +08:00
|
|
|
void
|
|
|
|
walk_index(MdbHandle *mdb, MdbIndex *idx)
|
|
|
|
{
|
2003-01-13 06:59:41 +08:00
|
|
|
int start, len;
|
2003-01-05 23:00:23 +08:00
|
|
|
guint32 pg;
|
2003-01-10 04:24:19 +08:00
|
|
|
guint16 row;
|
|
|
|
MdbHandle *mdbidx;
|
|
|
|
MdbIndexChain chain;
|
2003-01-13 06:59:41 +08:00
|
|
|
|
|
|
|
#if 0
|
2003-01-10 04:24:19 +08:00
|
|
|
MdbTableDef *table = idx->table;
|
|
|
|
MdbSarg sarg;
|
|
|
|
|
|
|
|
sarg.op = MDB_EQUAL;
|
|
|
|
//strcpy(sarg.value.s, "SP");
|
|
|
|
sarg.value.i = 2;
|
|
|
|
mdb_add_sarg_by_name(table, "ShipperID", &sarg);
|
|
|
|
#endif
|
2003-01-05 23:00:23 +08:00
|
|
|
|
2003-01-10 04:24:19 +08:00
|
|
|
memset(&chain, 0, sizeof(MdbIndexChain));
|
2003-01-05 23:00:23 +08:00
|
|
|
printf("name %s\n", idx->name);
|
2004-01-10 09:52:56 +08:00
|
|
|
printf("root page %lu\n", (long unsigned) idx->first_pg);
|
2003-01-10 04:24:19 +08:00
|
|
|
/* clone the handle to search the index, and use the original to read
|
|
|
|
* the data */
|
|
|
|
mdbidx = mdb_clone_handle(mdb);
|
|
|
|
mdb_read_pg(mdbidx, idx->first_pg);
|
2003-01-13 06:59:41 +08:00
|
|
|
//printf("page type %02x %s\n", mdbidx->pg_buf[0], page_name(mdbidx->pg_buf[0]));
|
2003-01-10 04:24:19 +08:00
|
|
|
while (mdb_index_find_next(mdbidx, idx, &chain, &pg, &row)) {
|
2004-01-10 09:52:56 +08:00
|
|
|
printf("row = %d pg = %lu\n", row, (long unsigned) pg);
|
2003-01-10 04:24:19 +08:00
|
|
|
check_row(mdb, idx, pg, row, &mdbidx->pg_buf[start], len - 4);
|
|
|
|
}
|
|
|
|
mdb_close(mdbidx);
|
2003-01-05 23:00:23 +08:00
|
|
|
}
|