2002-03-21 12:23:45 +08:00
|
|
|
/* MDB Tools - A library for reading MS Access database file
|
|
|
|
* Copyright (C) 2000 Brian Bruns
|
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* 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.
|
2002-03-21 12:23:45 +08:00
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2002-03-21 12:23:45 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-08-29 07:53:29 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-03-21 12:23:45 +08:00
|
|
|
*
|
2011-08-29 07:53:29 +08:00
|
|
|
* 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.
|
2002-03-21 12:23:45 +08:00
|
|
|
*/
|
2011-08-29 07:53:29 +08:00
|
|
|
|
2002-03-21 12:23:45 +08:00
|
|
|
#include <stdio.h>
|
2004-06-20 22:36:23 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBREADLINE
|
|
|
|
# if defined(HAVE_READLINE_READLINE_H)
|
|
|
|
# include <readline/readline.h>
|
|
|
|
# elif defined(HAVE_READLINE_H)
|
|
|
|
# include <readline.h>
|
2004-10-25 12:11:13 +08:00
|
|
|
# else
|
|
|
|
/* no readline.h */
|
2004-06-20 22:36:23 +08:00
|
|
|
extern char *readline ();
|
2004-10-25 12:11:13 +08:00
|
|
|
# endif
|
2004-06-20 22:36:23 +08:00
|
|
|
char *cmdline = NULL;
|
|
|
|
#endif /* HAVE_LIBREADLINE */
|
|
|
|
|
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
|
|
|
# if defined(HAVE_READLINE_HISTORY_H)
|
|
|
|
# include <readline/history.h>
|
|
|
|
# elif defined(HAVE_HISTORY_H)
|
|
|
|
# include <history.h>
|
2004-10-25 12:11:13 +08:00
|
|
|
# else
|
|
|
|
/* no history.h */
|
2004-06-20 22:36:23 +08:00
|
|
|
extern void add_history ();
|
|
|
|
extern int write_history ();
|
|
|
|
extern int read_history ();
|
2004-12-01 14:34:27 +08:00
|
|
|
extern void clear_history ();
|
2004-10-25 12:11:13 +08:00
|
|
|
# endif
|
2004-06-20 22:36:23 +08:00
|
|
|
#endif /* HAVE_READLINE_HISTORY */
|
|
|
|
|
2002-03-21 12:23:45 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include "mdbsql.h"
|
|
|
|
|
2003-01-29 07:51:06 +08:00
|
|
|
#ifdef DMALLOC
|
|
|
|
#include "dmalloc.h"
|
|
|
|
#endif
|
|
|
|
|
2005-09-08 07:27:43 +08:00
|
|
|
void dump_results(FILE *out, MdbSQL *sql, char *delimiter);
|
|
|
|
void dump_results_pp(FILE *out, MdbSQL *sql);
|
2003-01-05 22:57:50 +08:00
|
|
|
|
2002-03-21 12:23:45 +08:00
|
|
|
#if SQL
|
|
|
|
|
2003-01-05 22:57:50 +08:00
|
|
|
int headers = 1;
|
|
|
|
int footers = 1;
|
|
|
|
int pretty_print = 1;
|
2003-01-21 00:04:24 +08:00
|
|
|
int showplan = 0;
|
|
|
|
int noexec = 0;
|
2003-01-05 22:57:50 +08:00
|
|
|
|
2004-10-25 12:11:13 +08:00
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
2004-02-06 10:35:18 +08:00
|
|
|
#define HISTFILE ".mdbhistory"
|
2004-10-25 12:11:13 +08:00
|
|
|
#endif
|
2004-02-06 10:35:18 +08:00
|
|
|
|
2004-06-20 22:36:23 +08:00
|
|
|
#ifndef HAVE_LIBREADLINE
|
2002-03-21 12:23:45 +08:00
|
|
|
char *readline(char *prompt)
|
|
|
|
{
|
|
|
|
char *buf, line[1000];
|
|
|
|
int i = 0;
|
|
|
|
|
2011-08-29 07:49:54 +08:00
|
|
|
puts(prompt);
|
2004-01-06 08:42:07 +08:00
|
|
|
if (! fgets(line,1000,stdin)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-21 12:23:45 +08:00
|
|
|
for (i=strlen(line);i>0;i--) {
|
|
|
|
if (line[i]=='\n') {
|
|
|
|
line[i]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf = (char *) malloc(strlen(line)+1);
|
|
|
|
strcpy(buf,line);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-29 07:49:54 +08:00
|
|
|
static int strlen_utf(const char *s) {
|
|
|
|
int len = 0;
|
|
|
|
while (*s) {
|
|
|
|
if ((*s++ & 0xc0) != 0x80)
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2003-01-05 22:57:50 +08:00
|
|
|
void
|
|
|
|
do_set_cmd(MdbSQL *sql, char *s)
|
|
|
|
{
|
|
|
|
char *level1, *level2;
|
|
|
|
level1 = strtok(s, " \t\n");
|
2004-10-28 11:33:20 +08:00
|
|
|
if (!level1) {
|
|
|
|
printf("Usage: set [stats|showplan|noexec] [on|off]\n");
|
|
|
|
return;
|
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
if (!strcmp(level1,"stats")) {
|
|
|
|
level2 = strtok(NULL, " \t");
|
2004-02-06 10:35:18 +08:00
|
|
|
if (!level2) {
|
|
|
|
printf("Usage: set stats [on|off]\n");
|
|
|
|
return;
|
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
if (!strcmp(level2,"on")) {
|
|
|
|
mdb_stats_on(sql->mdb);
|
|
|
|
} else if (!strcmp(level2,"off")) {
|
|
|
|
mdb_stats_off(sql->mdb);
|
|
|
|
mdb_dump_stats(sql->mdb);
|
|
|
|
} else {
|
|
|
|
printf("Unknown stats option %s\n", level2);
|
2004-10-28 11:33:20 +08:00
|
|
|
printf("Usage: set stats [on|off]\n");
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2003-01-21 00:04:24 +08:00
|
|
|
} else if (!strcmp(level1,"showplan")) {
|
|
|
|
level2 = strtok(NULL, " \t");
|
2004-02-06 10:35:18 +08:00
|
|
|
if (!level2) {
|
|
|
|
printf("Usage: set showplan [on|off]\n");
|
|
|
|
return;
|
|
|
|
}
|
2003-01-21 00:04:24 +08:00
|
|
|
if (!strcmp(level2,"on")) {
|
|
|
|
showplan=1;
|
|
|
|
} else if (!strcmp(level2,"off")) {
|
|
|
|
showplan=0;
|
|
|
|
} else {
|
|
|
|
printf("Unknown showplan option %s\n", level2);
|
2004-10-28 11:33:20 +08:00
|
|
|
printf("Usage: set showplan [on|off]\n");
|
2003-01-21 00:04:24 +08:00
|
|
|
}
|
|
|
|
} else if (!strcmp(level1,"noexec")) {
|
|
|
|
level2 = strtok(NULL, " \t");
|
2004-02-06 10:35:18 +08:00
|
|
|
if (!level2) {
|
|
|
|
printf("Usage: set noexec [on|off]\n");
|
|
|
|
return;
|
|
|
|
}
|
2003-01-21 00:04:24 +08:00
|
|
|
if (!strcmp(level2,"on")) {
|
|
|
|
noexec=1;
|
|
|
|
} else if (!strcmp(level2,"off")) {
|
|
|
|
noexec=0;
|
|
|
|
} else {
|
2004-10-28 11:33:20 +08:00
|
|
|
printf("Unknown noexec option %s\n", level2);
|
|
|
|
printf("Usage: set noexec [on|off]\n");
|
2003-01-21 00:04:24 +08:00
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
} else {
|
|
|
|
printf("Unknown set command %s\n", level1);
|
2004-10-28 11:33:20 +08:00
|
|
|
printf("Usage: set [stats|showplan|noexec] [on|off]\n");
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-21 00:04:24 +08:00
|
|
|
int
|
2004-07-09 20:47:04 +08:00
|
|
|
read_file(char *s, int line, unsigned int *bufsz, char *mybuf)
|
2003-01-21 00:04:24 +08:00
|
|
|
{
|
|
|
|
char *fname;
|
|
|
|
FILE *in;
|
|
|
|
char buf[256];
|
2004-07-09 20:47:04 +08:00
|
|
|
unsigned int cursz = 0;
|
2003-01-21 00:04:24 +08:00
|
|
|
int lines = 0;
|
|
|
|
|
|
|
|
fname = s;
|
|
|
|
while (*fname && *fname==' ') fname++;
|
|
|
|
|
|
|
|
if (! (in = fopen(fname, "r"))) {
|
|
|
|
fprintf(stderr,"Unable to open file %s\n", fname);
|
|
|
|
mybuf[0]=0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (fgets(buf, 255, in)) {
|
|
|
|
cursz += strlen(buf) + 1;
|
|
|
|
if (cursz > (*bufsz)) {
|
|
|
|
(*bufsz) *= 2;
|
|
|
|
mybuf = (char *) realloc(mybuf, *bufsz);
|
|
|
|
}
|
|
|
|
strcat(mybuf, buf);
|
2004-10-25 12:11:13 +08:00
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
2004-02-06 10:35:18 +08:00
|
|
|
/* don't record blank lines */
|
|
|
|
if (strlen(buf)) add_history(buf);
|
2004-10-25 12:11:13 +08:00
|
|
|
#endif
|
2003-01-21 00:04:24 +08:00
|
|
|
strcat(mybuf, "\n");
|
|
|
|
lines++;
|
|
|
|
printf("%d => %s",line+lines, buf);
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
void
|
2005-09-08 07:27:43 +08:00
|
|
|
run_query(FILE *out, MdbSQL *sql, char *mybuf, char *delimiter)
|
2003-01-05 22:57:50 +08:00
|
|
|
{
|
2003-01-21 00:04:24 +08:00
|
|
|
MdbTableDef *table;
|
|
|
|
|
2011-08-11 06:02:51 +08:00
|
|
|
mdb_sql_run_query(sql, mybuf);
|
|
|
|
if (!mdb_sql_has_error(sql)) {
|
2003-01-21 00:04:24 +08:00
|
|
|
if (showplan) {
|
|
|
|
table = sql->cur_table;
|
2003-01-23 04:15:42 +08:00
|
|
|
if (table->sarg_tree) mdb_sql_dump_node(table->sarg_tree, 0);
|
2003-01-21 00:04:24 +08:00
|
|
|
if (sql->cur_table->strategy == MDB_TABLE_SCAN)
|
|
|
|
printf("Table scanning %s\n", table->name);
|
|
|
|
else
|
|
|
|
printf("Index scanning %s using %s\n", table->name, table->scan_idx->name);
|
|
|
|
}
|
|
|
|
if (noexec) {
|
|
|
|
mdb_sql_reset(sql);
|
|
|
|
return;
|
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
if (pretty_print)
|
2005-09-08 07:27:43 +08:00
|
|
|
dump_results_pp(out, sql);
|
2003-01-05 22:57:50 +08:00
|
|
|
else
|
2005-09-08 07:27:43 +08:00
|
|
|
dump_results(out, sql, delimiter);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-08 07:27:43 +08:00
|
|
|
void print_value(FILE *out, char *v, int sz, int first)
|
2003-01-05 22:57:50 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int vlen;
|
|
|
|
|
2011-08-29 07:49:54 +08:00
|
|
|
if (first)
|
|
|
|
fputc('|', out);
|
|
|
|
vlen = strlen_utf(v);
|
|
|
|
fputs(v, out);
|
|
|
|
for (i=vlen;i<sz;i++)
|
|
|
|
fputc(' ', out);
|
|
|
|
fputc('|', out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
static void print_break(FILE *out, int sz, int first)
|
2003-01-05 22:57:50 +08:00
|
|
|
{
|
|
|
|
int i;
|
2011-08-29 07:49:54 +08:00
|
|
|
if (first)
|
|
|
|
fputc('+', out);
|
|
|
|
for (i=0;i<sz;i++)
|
|
|
|
fputc('-', out);
|
|
|
|
fputc('+', out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
void print_rows_retrieved(FILE *out, unsigned long row_count)
|
2004-10-28 11:33:20 +08:00
|
|
|
{
|
|
|
|
if (!row_count)
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out, "No Rows retrieved\n");
|
2004-10-28 11:33:20 +08:00
|
|
|
else if (row_count==1)
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out, "1 Row retrieved\n");
|
2004-10-28 11:33:20 +08:00
|
|
|
else
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out, "%lu Rows retrieved\n", row_count);
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2004-10-28 11:33:20 +08:00
|
|
|
}
|
2003-01-05 22:57:50 +08:00
|
|
|
void
|
2005-09-08 07:27:43 +08:00
|
|
|
dump_results(FILE *out, MdbSQL *sql, char *delimiter)
|
2003-01-05 22:57:50 +08:00
|
|
|
{
|
2004-07-09 20:47:04 +08:00
|
|
|
unsigned int j;
|
2004-02-06 10:35:18 +08:00
|
|
|
MdbSQLColumn *sqlcol;
|
|
|
|
unsigned long row_count = 0;
|
2003-01-05 22:57:50 +08:00
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
for (j=0;j<sql->num_columns-1;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out, "%s%s", sqlcol->name,
|
2004-10-28 11:33:20 +08:00
|
|
|
delimiter ? delimiter : "\t");
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,sql->num_columns-1);
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out, "%s", sqlcol->name);
|
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
while(mdb_fetch_row(sql->cur_table)) {
|
|
|
|
row_count++;
|
|
|
|
for (j=0;j<sql->num_columns-1;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2011-08-11 06:00:18 +08:00
|
|
|
fprintf(out, "%s%s", (char*)(sql->bound_values[j]),
|
2004-10-28 11:33:20 +08:00
|
|
|
delimiter ? delimiter : "\t");
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,sql->num_columns-1);
|
2011-08-11 06:00:18 +08:00
|
|
|
fprintf(out, "%s", (char*)(sql->bound_values[sql->num_columns-1]));
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
if (footers) {
|
2005-09-08 07:27:43 +08:00
|
|
|
print_rows_retrieved(out, row_count);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2004-10-28 11:33:20 +08:00
|
|
|
|
2003-01-05 22:57:50 +08:00
|
|
|
mdb_sql_reset(sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-09-08 07:27:43 +08:00
|
|
|
dump_results_pp(FILE *out, MdbSQL *sql)
|
2003-01-05 22:57:50 +08:00
|
|
|
{
|
2004-07-09 20:47:04 +08:00
|
|
|
unsigned int j;
|
2004-02-06 10:35:18 +08:00
|
|
|
MdbSQLColumn *sqlcol;
|
|
|
|
unsigned long row_count = 0;
|
2003-01-05 22:57:50 +08:00
|
|
|
|
|
|
|
/* print header */
|
|
|
|
if (headers) {
|
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2004-02-06 10:35:18 +08:00
|
|
|
if (strlen(sqlcol->name)>sqlcol->disp_size)
|
|
|
|
sqlcol->disp_size = strlen(sqlcol->name);
|
2005-09-08 07:27:43 +08:00
|
|
|
print_break(out, sqlcol->disp_size, !j);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2005-09-08 07:27:43 +08:00
|
|
|
print_value(out, sqlcol->name,sqlcol->disp_size,!j);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2005-09-08 07:27:43 +08:00
|
|
|
print_break(out, sqlcol->disp_size, !j);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
|
|
|
|
/* print each row */
|
2004-09-09 11:44:35 +08:00
|
|
|
while(mdb_fetch_row(sql->cur_table)) {
|
2003-01-05 22:57:50 +08:00
|
|
|
row_count++;
|
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2005-09-08 07:27:43 +08:00
|
|
|
print_value(out, sql->bound_values[j],sqlcol->disp_size,!j);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* footer */
|
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
|
|
|
sqlcol = g_ptr_array_index(sql->columns,j);
|
2005-09-08 07:27:43 +08:00
|
|
|
print_break(out, sqlcol->disp_size, !j);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
2005-09-08 07:27:43 +08:00
|
|
|
fprintf(out,"\n");
|
2005-12-17 17:55:38 +08:00
|
|
|
fflush(out);
|
2003-01-05 22:57:50 +08:00
|
|
|
if (footers) {
|
2005-09-08 07:27:43 +08:00
|
|
|
print_rows_retrieved(out, row_count);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
for (j=0;j<sql->num_columns;j++) {
|
2004-05-30 15:19:22 +08:00
|
|
|
g_free(sql->bound_values[j]);
|
2003-01-05 22:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mdb_sql_reset(sql);
|
|
|
|
}
|
|
|
|
|
2003-01-21 00:04:24 +08:00
|
|
|
int
|
2002-03-21 12:23:45 +08:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2004-12-01 14:34:27 +08:00
|
|
|
char *s = NULL;
|
2002-03-21 12:23:45 +08:00
|
|
|
char prompt[20];
|
2004-12-01 14:34:27 +08:00
|
|
|
int line = 0;
|
2002-03-21 12:23:45 +08:00
|
|
|
char *mybuf;
|
2004-10-28 11:33:20 +08:00
|
|
|
unsigned int bufsz;
|
2002-03-21 12:23:45 +08:00
|
|
|
MdbSQL *sql;
|
2003-01-05 22:57:50 +08:00
|
|
|
int opt;
|
|
|
|
FILE *in = NULL, *out = NULL;
|
2004-02-06 10:35:18 +08:00
|
|
|
char *home = getenv("HOME");
|
|
|
|
char *histpath;
|
2004-10-28 11:33:20 +08:00
|
|
|
char *delimiter = NULL;
|
2002-03-21 12:23:45 +08:00
|
|
|
|
2003-01-05 22:57:50 +08:00
|
|
|
|
2004-10-25 12:11:13 +08:00
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
2004-02-06 10:35:18 +08:00
|
|
|
if (home) {
|
2004-05-30 15:19:22 +08:00
|
|
|
histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
|
2004-02-06 10:35:18 +08:00
|
|
|
read_history(histpath);
|
2004-05-30 15:19:22 +08:00
|
|
|
g_free(histpath);
|
2004-02-06 10:35:18 +08:00
|
|
|
}
|
2004-10-25 12:11:13 +08:00
|
|
|
#endif
|
2005-12-17 17:55:38 +08:00
|
|
|
/* If input is coming from a pipe */
|
2003-01-05 22:57:50 +08:00
|
|
|
if (!isatty(fileno(stdin))) {
|
|
|
|
in = stdin;
|
|
|
|
}
|
2005-12-17 17:55:38 +08:00
|
|
|
|
2004-02-06 10:35:18 +08:00
|
|
|
while ((opt=getopt(argc, argv, "HFpd:i:o:"))!=-1) {
|
2003-01-05 22:57:50 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
2004-05-30 15:19:22 +08:00
|
|
|
delimiter = (char *) g_strdup(optarg);
|
2003-01-05 22:57:50 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pretty_print=0;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
headers=0;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
footers=0;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (!strcmp(optarg, "stdin"))
|
|
|
|
in = stdin;
|
|
|
|
else if (!(in = fopen(optarg, "r"))) {
|
|
|
|
fprintf(stderr,"Unable to open file %s\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if (!(out = fopen(optarg, "w"))) {
|
|
|
|
fprintf(stderr,"Unable to open file %s\n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stdout,"Unknown option.\nUsage: %s [-HFp] [-d <delimiter>] [-i <file>] [-o <file>] [<database>]\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2002-03-21 12:23:45 +08:00
|
|
|
/* initialize the SQL engine */
|
|
|
|
sql = mdb_sql_init();
|
2003-01-05 22:57:50 +08:00
|
|
|
if (argc>optind) {
|
|
|
|
mdb_sql_open(sql, argv[optind]);
|
2002-03-21 12:23:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* give the buffer an initial size */
|
|
|
|
bufsz = 4096;
|
2004-05-30 15:19:22 +08:00
|
|
|
mybuf = (char *) g_malloc(bufsz);
|
2002-03-21 12:23:45 +08:00
|
|
|
mybuf[0]='\0';
|
|
|
|
|
2004-10-28 11:33:20 +08:00
|
|
|
while (1) {
|
|
|
|
line ++;
|
2004-12-01 14:34:27 +08:00
|
|
|
if (s) free(s);
|
|
|
|
|
2004-10-28 11:33:20 +08:00
|
|
|
if (in) {
|
|
|
|
s=malloc(256);
|
|
|
|
if ((!s) || (!fgets(s, 256, in))) {
|
|
|
|
/* if we have something in the buffer, run it */
|
|
|
|
if (strlen(mybuf))
|
2005-11-09 21:24:26 +08:00
|
|
|
run_query((out) ? out : stdout,
|
|
|
|
sql, mybuf, delimiter);
|
2004-10-28 11:33:20 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s[strlen(s)-1]=='\n')
|
|
|
|
s[strlen(s)-1]=0;
|
|
|
|
} else {
|
|
|
|
sprintf(prompt, "%d => ", line);
|
|
|
|
s=readline(prompt);
|
|
|
|
if (!s)
|
|
|
|
break;
|
2004-05-30 15:19:22 +08:00
|
|
|
}
|
2004-10-28 11:33:20 +08:00
|
|
|
|
|
|
|
if (!strcmp(s,"exit") || !strcmp(s,"quit") || !strcmp(s,"bye"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (line==1 && (!strncmp(s,"set ",4) || !strcmp(s,"set"))) {
|
|
|
|
do_set_cmd(sql, &s[3]);
|
2002-03-21 12:23:45 +08:00
|
|
|
line = 0;
|
2003-01-05 22:57:50 +08:00
|
|
|
} else if (!strcmp(s,"go")) {
|
|
|
|
line = 0;
|
2005-11-09 21:24:26 +08:00
|
|
|
run_query((out) ? out : stdout, sql, mybuf, delimiter);
|
2002-03-21 12:23:45 +08:00
|
|
|
mybuf[0]='\0';
|
|
|
|
} else if (!strcmp(s,"reset")) {
|
|
|
|
line = 0;
|
|
|
|
mybuf[0]='\0';
|
2003-01-21 00:04:24 +08:00
|
|
|
} else if (!strncmp(s,":r",2)) {
|
|
|
|
line += read_file(&s[2], line, &bufsz, mybuf);
|
2002-03-21 12:23:45 +08:00
|
|
|
} else {
|
|
|
|
while (strlen(mybuf) + strlen(s) > bufsz) {
|
|
|
|
bufsz *= 2;
|
2004-07-09 20:47:04 +08:00
|
|
|
mybuf = (char *) g_realloc(mybuf, bufsz);
|
2002-03-21 12:23:45 +08:00
|
|
|
}
|
2004-10-25 12:11:13 +08:00
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
2004-02-06 10:35:18 +08:00
|
|
|
/* don't record blank lines */
|
|
|
|
if (strlen(s)) add_history(s);
|
2004-10-25 12:11:13 +08:00
|
|
|
#endif
|
2002-03-21 12:23:45 +08:00
|
|
|
strcat(mybuf,s);
|
|
|
|
/* preserve line numbering for the parser */
|
|
|
|
strcat(mybuf,"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mdb_sql_exit(sql);
|
2002-08-13 01:37:04 +08:00
|
|
|
|
2004-05-30 15:19:22 +08:00
|
|
|
g_free(mybuf);
|
2004-10-28 11:33:20 +08:00
|
|
|
g_free(delimiter);
|
2004-01-06 08:42:07 +08:00
|
|
|
if (s) free(s);
|
2004-10-28 11:33:20 +08:00
|
|
|
if (out) fclose(out);
|
|
|
|
if ((in) && (in != stdin)) fclose(in);
|
2004-02-06 10:35:18 +08:00
|
|
|
|
2004-10-25 12:11:13 +08:00
|
|
|
#ifdef HAVE_READLINE_HISTORY
|
2004-02-06 10:35:18 +08:00
|
|
|
if (home) {
|
2004-05-30 15:19:22 +08:00
|
|
|
histpath = (char *) g_strconcat(home, "/", HISTFILE, NULL);
|
2004-02-06 10:35:18 +08:00
|
|
|
write_history(histpath);
|
2004-05-30 15:19:22 +08:00
|
|
|
g_free(histpath);
|
2004-12-01 14:34:27 +08:00
|
|
|
clear_history();
|
2004-02-06 10:35:18 +08:00
|
|
|
}
|
2004-10-25 12:11:13 +08:00
|
|
|
#endif
|
2004-02-06 10:35:18 +08:00
|
|
|
|
2012-07-22 08:29:04 +08:00
|
|
|
return 0;
|
2002-03-21 12:23:45 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"You must configure using --enable-sql to get SQL support\n");
|
2002-08-13 01:37:04 +08:00
|
|
|
|
2012-07-22 08:29:04 +08:00
|
|
|
return -1;
|
2002-03-21 12:23:45 +08:00
|
|
|
}
|
|
|
|
#endif
|