From b92f4cc8825a2234ce88adbaa7d6d6f47a22e3ce Mon Sep 17 00:00:00 2001 From: kodonnell Date: Sun, 5 Feb 2017 08:56:24 +1300 Subject: [PATCH] added (trivial) mdb-count utility --- AUTHORS | 2 ++ README | 2 ++ src/util/Makefile.am | 2 +- src/util/mdb-count.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/util/mdb-count.c diff --git a/AUTHORS b/AUTHORS index 391ad01..1841e64 100644 --- a/AUTHORS +++ b/AUTHORS @@ -40,3 +40,5 @@ Tim Retout Nirgal Vourgère postgres fixes and adds, bug fixes +@kodonnell + added (trivial) mdb-count utility diff --git a/README b/README index 9e44cfb..bcb61da 100644 --- a/README +++ b/README @@ -12,6 +12,8 @@ pieces are: mdb-export -- export table to CSV format. mdb-tables -- a simple dump of table names to be used with shell scripts + mdb-count -- a simple count of number of rows in a table, to be + used in shell scripts and ETL pipelines mdb-header -- generates a C header to be used in exporting mdb data to a C prog. mdb-parsecsv -- generates a C program given a CSV file made with diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 634810c..fbb5321 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = bash-completion -bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop +bin_PROGRAMS = mdb-export mdb-array mdb-schema mdb-tables mdb-parsecsv mdb-header mdb-sql mdb-ver mdb-prop mdb-count noinst_PROGRAMS = mdb-import prtable prcat prdata prkkd prdump prole updrow prindex LIBS = $(GLIB_LIBS) @LIBS@ @LEXLIB@ DEFS = @DEFS@ -DLOCALEDIR=\"$(localedir)\" diff --git a/src/util/mdb-count.c b/src/util/mdb-count.c new file mode 100644 index 0000000..61ab8fc --- /dev/null +++ b/src/util/mdb-count.c @@ -0,0 +1,55 @@ +/* MDB Tools - A library for reading MS Access database file + * Copyright (C) 2000 Brian Bruns + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "mdbtools.h" + +int main(int argc, char **argv) { + + unsigned int i; + MdbHandle *mdb; + MdbCatalogEntry *entry; + MdbTableDef *table; + int found = 0; + + if (argc < 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + + // open db and try to read table: + mdb = mdb_open(argv[1], MDB_NOFLAGS); + mdb_read_catalog(mdb, MDB_TABLE); + for (i = 0; i < mdb->num_catalog; i++) { + entry = g_ptr_array_index(mdb->catalog, i); + if (entry->object_type == MDB_TABLE && !strcmp(entry->object_name, argv[2])) { + table = mdb_read_table(entry); + fprintf(stdout, "%d\n", table->num_rows); + found = 1; + break; + } + } + + // check was found: + if (!found) { + fprintf(stderr, "No table named %s found.\n", argv[2]); + exit(1); + } + + mdb_close(mdb); + return 0; +}