mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-09-19 02:27:55 +08:00
last commit failed, see ChangeLog for changes
This commit is contained in:
@@ -88,6 +88,7 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr," -H supress header row\n");
|
fprintf(stderr," -H supress header row\n");
|
||||||
fprintf(stderr," -Q don't wrap text-like fields in quotes\n");
|
fprintf(stderr," -Q don't wrap text-like fields in quotes\n");
|
||||||
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
|
fprintf(stderr," -d <delimiter> specify a column delimiter\n");
|
||||||
|
fprintf(stderr," -D <format> set the date format (see strftime(3) for details)\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,6 +41,7 @@ convert_field(MdbColumn *col, char *s, MdbField *field)
|
|||||||
char *c;
|
char *c;
|
||||||
MdbAny any;
|
MdbAny any;
|
||||||
|
|
||||||
|
field->colnum = col->col_num;
|
||||||
switch (col->col_type) {
|
switch (col->col_type) {
|
||||||
case MDB_TEXT:
|
case MDB_TEXT:
|
||||||
field->value = g_strdup(s);
|
field->value = g_strdup(s);
|
||||||
@@ -207,6 +208,10 @@ main(int argc, char **argv)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!table) {
|
||||||
|
fprintf(stderr,"Table %s not found in database\n", argv[argc-2]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* open the CSV file and read any header rows
|
* open the CSV file and read any header rows
|
||||||
*/
|
*/
|
||||||
|
@@ -16,6 +16,8 @@
|
|||||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
* Boston, MA 02111-1307, USA.
|
* Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#ifdef HAVE_READLINE
|
#ifdef HAVE_READLINE
|
||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
@@ -40,6 +42,8 @@ char *delimiter;
|
|||||||
int showplan = 0;
|
int showplan = 0;
|
||||||
int noexec = 0;
|
int noexec = 0;
|
||||||
|
|
||||||
|
#define HISTFILE ".mdbhistory"
|
||||||
|
|
||||||
#ifndef HAVE_READLINE
|
#ifndef HAVE_READLINE
|
||||||
char *readline(char *prompt)
|
char *readline(char *prompt)
|
||||||
{
|
{
|
||||||
@@ -88,6 +92,10 @@ do_set_cmd(MdbSQL *sql, char *s)
|
|||||||
level1 = strtok(s, " \t\n");
|
level1 = strtok(s, " \t\n");
|
||||||
if (!strcmp(level1,"stats")) {
|
if (!strcmp(level1,"stats")) {
|
||||||
level2 = strtok(NULL, " \t");
|
level2 = strtok(NULL, " \t");
|
||||||
|
if (!level2) {
|
||||||
|
printf("Usage: set stats [on|off]\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!strcmp(level2,"on")) {
|
if (!strcmp(level2,"on")) {
|
||||||
mdb_stats_on(sql->mdb);
|
mdb_stats_on(sql->mdb);
|
||||||
} else if (!strcmp(level2,"off")) {
|
} else if (!strcmp(level2,"off")) {
|
||||||
@@ -98,6 +106,10 @@ do_set_cmd(MdbSQL *sql, char *s)
|
|||||||
}
|
}
|
||||||
} else if (!strcmp(level1,"showplan")) {
|
} else if (!strcmp(level1,"showplan")) {
|
||||||
level2 = strtok(NULL, " \t");
|
level2 = strtok(NULL, " \t");
|
||||||
|
if (!level2) {
|
||||||
|
printf("Usage: set showplan [on|off]\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!strcmp(level2,"on")) {
|
if (!strcmp(level2,"on")) {
|
||||||
showplan=1;
|
showplan=1;
|
||||||
} else if (!strcmp(level2,"off")) {
|
} else if (!strcmp(level2,"off")) {
|
||||||
@@ -107,6 +119,10 @@ do_set_cmd(MdbSQL *sql, char *s)
|
|||||||
}
|
}
|
||||||
} else if (!strcmp(level1,"noexec")) {
|
} else if (!strcmp(level1,"noexec")) {
|
||||||
level2 = strtok(NULL, " \t");
|
level2 = strtok(NULL, " \t");
|
||||||
|
if (!level2) {
|
||||||
|
printf("Usage: set noexec [on|off]\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!strcmp(level2,"on")) {
|
if (!strcmp(level2,"on")) {
|
||||||
noexec=1;
|
noexec=1;
|
||||||
} else if (!strcmp(level2,"off")) {
|
} else if (!strcmp(level2,"off")) {
|
||||||
@@ -143,7 +159,8 @@ read_file(char *s, int line, int *bufsz, char *mybuf)
|
|||||||
mybuf = (char *) realloc(mybuf, *bufsz);
|
mybuf = (char *) realloc(mybuf, *bufsz);
|
||||||
}
|
}
|
||||||
strcat(mybuf, buf);
|
strcat(mybuf, buf);
|
||||||
add_history(buf);
|
/* don't record blank lines */
|
||||||
|
if (strlen(buf)) add_history(buf);
|
||||||
strcat(mybuf, "\n");
|
strcat(mybuf, "\n");
|
||||||
lines++;
|
lines++;
|
||||||
printf("%d => %s",line+lines, buf);
|
printf("%d => %s",line+lines, buf);
|
||||||
@@ -204,9 +221,12 @@ int i;
|
|||||||
void
|
void
|
||||||
dump_results(MdbSQL *sql)
|
dump_results(MdbSQL *sql)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
MdbSQLColumn *sqlcol;
|
MdbSQLColumn *sqlcol;
|
||||||
unsigned long row_count = 0;
|
unsigned long row_count = 0;
|
||||||
|
int rows, rc, i;
|
||||||
|
MdbHandle *mdb = sql->mdb;
|
||||||
|
MdbFormatConstants *fmt = mdb->fmt;
|
||||||
|
|
||||||
if (headers) {
|
if (headers) {
|
||||||
for (j=0;j<sql->num_columns-1;j++) {
|
for (j=0;j<sql->num_columns-1;j++) {
|
||||||
@@ -217,6 +237,21 @@ unsigned long row_count = 0;
|
|||||||
fprintf(stdout, "%s", sqlcol->name);
|
fprintf(stdout, "%s", sqlcol->name);
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout,"\n");
|
||||||
}
|
}
|
||||||
|
if (sql->kludge_ttable_pg) {
|
||||||
|
memcpy(mdb->pg_buf, sql->kludge_ttable_pg, fmt->pg_size);
|
||||||
|
rows = mdb_pg_get_int16(mdb,fmt->row_count_offset);
|
||||||
|
for (i = 0; i < rows; i++) {
|
||||||
|
rc = mdb_read_row(sql->cur_table, i);
|
||||||
|
row_count++;
|
||||||
|
for (j=0;j<sql->num_columns-1;j++) {
|
||||||
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
||||||
|
fprintf(stdout, "%s%s", sql->bound_values[j], delimiter);
|
||||||
|
}
|
||||||
|
sqlcol = g_ptr_array_index(sql->columns,sql->num_columns-1);
|
||||||
|
fprintf(stdout, "%s", sql->bound_values[sql->num_columns-1]);
|
||||||
|
fprintf(stdout,"\n");
|
||||||
|
}
|
||||||
|
}else {
|
||||||
while(mdb_fetch_row(sql->cur_table)) {
|
while(mdb_fetch_row(sql->cur_table)) {
|
||||||
row_count++;
|
row_count++;
|
||||||
for (j=0;j<sql->num_columns-1;j++) {
|
for (j=0;j<sql->num_columns-1;j++) {
|
||||||
@@ -227,6 +262,7 @@ unsigned long row_count = 0;
|
|||||||
fprintf(stdout, "%s", sql->bound_values[sql->num_columns-1]);
|
fprintf(stdout, "%s", sql->bound_values[sql->num_columns-1]);
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout,"\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (footers) {
|
if (footers) {
|
||||||
if (!row_count)
|
if (!row_count)
|
||||||
fprintf(stdout, "No Rows retrieved\n");
|
fprintf(stdout, "No Rows retrieved\n");
|
||||||
@@ -241,14 +277,19 @@ unsigned long row_count = 0;
|
|||||||
void
|
void
|
||||||
dump_results_pp(MdbSQL *sql)
|
dump_results_pp(MdbSQL *sql)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
MdbSQLColumn *sqlcol;
|
MdbSQLColumn *sqlcol;
|
||||||
unsigned long row_count = 0;
|
unsigned long row_count = 0;
|
||||||
|
int rows, rc, i;
|
||||||
|
MdbHandle *mdb = sql->mdb;
|
||||||
|
MdbFormatConstants *fmt = mdb->fmt;
|
||||||
|
|
||||||
/* print header */
|
/* print header */
|
||||||
if (headers) {
|
if (headers) {
|
||||||
for (j=0;j<sql->num_columns;j++) {
|
for (j=0;j<sql->num_columns;j++) {
|
||||||
sqlcol = g_ptr_array_index(sql->columns,j);
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
||||||
|
if (strlen(sqlcol->name)>sqlcol->disp_size)
|
||||||
|
sqlcol->disp_size = strlen(sqlcol->name);
|
||||||
print_break(sqlcol->disp_size, !j);
|
print_break(sqlcol->disp_size, !j);
|
||||||
}
|
}
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout,"\n");
|
||||||
@@ -266,6 +307,19 @@ unsigned long row_count = 0;
|
|||||||
fprintf(stdout,"\n");
|
fprintf(stdout,"\n");
|
||||||
|
|
||||||
/* print each row */
|
/* print each row */
|
||||||
|
if (sql->kludge_ttable_pg) {
|
||||||
|
memcpy(mdb->pg_buf, sql->kludge_ttable_pg, fmt->pg_size);
|
||||||
|
rows = mdb_pg_get_int16(mdb,fmt->row_count_offset);
|
||||||
|
for (i = 0; i < rows; i++) {
|
||||||
|
rc = mdb_read_row(sql->cur_table, i);
|
||||||
|
row_count++;
|
||||||
|
for (j=0;j<sql->num_columns;j++) {
|
||||||
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
||||||
|
print_value(sql->bound_values[j],sqlcol->disp_size,!j);
|
||||||
|
}
|
||||||
|
fprintf(stdout,"\n");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
while(mdb_fetch_row(sql->cur_table)) {
|
while(mdb_fetch_row(sql->cur_table)) {
|
||||||
row_count++;
|
row_count++;
|
||||||
for (j=0;j<sql->num_columns;j++) {
|
for (j=0;j<sql->num_columns;j++) {
|
||||||
@@ -274,6 +328,7 @@ unsigned long row_count = 0;
|
|||||||
}
|
}
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout,"\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* footer */
|
/* footer */
|
||||||
for (j=0;j<sql->num_columns;j++) {
|
for (j=0;j<sql->num_columns;j++) {
|
||||||
@@ -316,12 +371,20 @@ int done = 0;
|
|||||||
MdbSQL *sql;
|
MdbSQL *sql;
|
||||||
int opt;
|
int opt;
|
||||||
FILE *in = NULL, *out = NULL;
|
FILE *in = NULL, *out = NULL;
|
||||||
|
char *home = getenv("HOME");
|
||||||
|
char *histpath;
|
||||||
|
|
||||||
|
|
||||||
|
if (home) {
|
||||||
|
histpath = (char *)malloc(strlen(home) + strlen(HISTFILE) + 2);
|
||||||
|
sprintf(histpath,"%s/%s",home,HISTFILE);
|
||||||
|
read_history(histpath);
|
||||||
|
free(histpath);
|
||||||
|
}
|
||||||
if (!isatty(fileno(stdin))) {
|
if (!isatty(fileno(stdin))) {
|
||||||
in = stdin;
|
in = stdin;
|
||||||
}
|
}
|
||||||
while ((opt=getopt(argc, argv, "hfpd:i:o:"))!=-1) {
|
while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'd':
|
case 'd':
|
||||||
delimiter = malloc(strlen(optarg)+1);
|
delimiter = malloc(strlen(optarg)+1);
|
||||||
@@ -397,7 +460,8 @@ FILE *in = NULL, *out = NULL;
|
|||||||
bufsz *= 2;
|
bufsz *= 2;
|
||||||
mybuf = (char *) realloc(mybuf, bufsz);
|
mybuf = (char *) realloc(mybuf, bufsz);
|
||||||
}
|
}
|
||||||
add_history(s);
|
/* don't record blank lines */
|
||||||
|
if (strlen(s)) add_history(s);
|
||||||
strcat(mybuf,s);
|
strcat(mybuf,s);
|
||||||
/* preserve line numbering for the parser */
|
/* preserve line numbering for the parser */
|
||||||
strcat(mybuf,"\n");
|
strcat(mybuf,"\n");
|
||||||
@@ -425,6 +489,14 @@ FILE *in = NULL, *out = NULL;
|
|||||||
|
|
||||||
free(mybuf);
|
free(mybuf);
|
||||||
if (s) free(s);
|
if (s) free(s);
|
||||||
|
|
||||||
|
if (home) {
|
||||||
|
histpath = (char *)malloc(strlen(home) + strlen(HISTFILE) + 2);
|
||||||
|
sprintf(histpath,"%s/%s",home,HISTFILE);
|
||||||
|
write_history(histpath);
|
||||||
|
free(histpath);
|
||||||
|
}
|
||||||
|
|
||||||
myexit(0);
|
myexit(0);
|
||||||
|
|
||||||
return 0; /* make gcc -Wall happy */
|
return 0; /* make gcc -Wall happy */
|
||||||
|
Reference in New Issue
Block a user