patches 00-14 from David Mansfield

This commit is contained in:
brianb
2004-01-06 00:42:07 +00:00
parent 8592cf5db5
commit 11b43644dc
12 changed files with 293 additions and 166 deletions

View File

@@ -46,19 +46,19 @@ print_col(gchar *col_val, int quote_text, int col_type)
int
main(int argc, char **argv)
{
int i, j;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
/* doesn't handle tables > 256 columns. Can that happen? */
char *bound_values[256];
char *delimiter = ",";
char header_row = 1;
char quote_text = 1;
int opt;
int i, j;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
/* doesn't handle tables > 256 columns. Can that happen? */
char *bound_values[256];
char *delimiter = ",";
char header_row = 1;
char quote_text = 1;
int opt;
while ((opt=getopt(argc, argv, "HQd:"))!=-1) {
while ((opt=getopt(argc, argv, "HQd:D:"))!=-1) {
switch (opt) {
case 'H':
header_row = 0;
@@ -70,6 +70,9 @@ int opt;
delimiter = (char *) malloc(strlen(optarg)+1);
strcpy(delimiter, optarg);
break;
case 'D':
mdb_set_date_fmt(optarg);
break;
default:
break;
}

View File

@@ -17,35 +17,47 @@
*/
/* this utility dumps the schema for an existing database */
#include <ctype.h>
#include "mdbtools.h"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
static char *santize_name(char *str, int santize);
int
main (int argc, char **argv)
{
int i, k;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
char *the_relation;
char *tabname = NULL;
int opt;
int i, k;
MdbHandle *mdb;
MdbCatalogEntry *entry;
MdbTableDef *table;
MdbColumn *col;
char *the_relation;
char *tabname = NULL;
char *namespace = "";
int s = 0;
int opt;
if (argc < 2) {
fprintf (stderr, "Usage: %s <file> [<backend>]\n",argv[0]);
exit (1);
}
if (argc < 2) {
fprintf (stderr, "Usage: %s <file> [<backend>]\n",argv[0]);
exit (1);
}
while ((opt=getopt(argc, argv, "T:"))!=-1) {
switch (opt) {
case 'T':
tabname = (char *) malloc(strlen(optarg)+1);
strcpy(tabname, optarg);
break;
while ((opt=getopt(argc, argv, "T:N:S:"))!=-1) {
switch (opt) {
case 'T':
tabname = (char *) malloc(strlen(optarg)+1);
strcpy(tabname, optarg);
break;
case 'N':
namespace = (char *) malloc(strlen(optarg)+1);
strcpy(namespace, optarg);
break;
case 'S':
s = 1;
break;
}
}
@@ -54,7 +66,7 @@ int opt;
/* open the database */
mdb = mdb_open (argv[optind]);
if (argc - optind >2) {
if (argc - optind >= 2) {
if (!mdb_set_default_backend(mdb, argv[optind + 1])) {
fprintf(stderr,"Invalid backend type\n");
mdb_exit();
@@ -86,10 +98,10 @@ int opt;
if (!strcmp (mdb_get_objtype_string (entry->object_type), "Table"))
{
/* drop the table if it exists */
fprintf (stdout, "DROP TABLE %s;\n", entry->object_name);
fprintf (stdout, "DROP TABLE %s%s;\n", namespace, sanitize_name(entry->object_name,s));
/* create the table */
fprintf (stdout, "CREATE TABLE %s\n", entry->object_name);
fprintf (stdout, "CREATE TABLE %s%s\n", sanitize_name(entry->object_name,s));
fprintf (stdout, " (\n");
table = mdb_read_table (entry);
@@ -99,15 +111,17 @@ int opt;
/* loop over the columns, dumping the names and types */
for (k = 0; k < table->num_cols; k++)
{
col = g_ptr_array_index (table->columns, k);
for (k = 0; k < table->num_cols; k++) {
col = g_ptr_array_index (table->columns, k);
fprintf (stdout, "\t%s\t\t\t%s", col->name,
mdb_get_coltype_string (mdb->default_backend, col->col_type));
fprintf (stdout, "\t%s\t\t\t%s", sanitize_name(col->name,s),
mdb_get_coltype_string (mdb->default_backend, col->col_type));
if (col->col_size != 0)
fprintf (stdout, " (%d)", col->col_size);
if (col->col_size != 0 &&
mdb_coltype_takes_length(mdb->default_backend, col->col_type)) {
fprintf (stdout, " (%d)", col->col_size);
}
if (k < table->num_cols - 1)
fprintf (stdout, ", \n");
@@ -115,7 +129,7 @@ int opt;
fprintf (stdout, "\n");
}
fprintf (stdout, "\n);\n");
fprintf (stdout, ");\n");
fprintf (stdout, "-- CREATE ANY INDEXES ...\n");
fprintf (stdout, "\n");
}
@@ -137,3 +151,22 @@ int opt;
exit(0);
}
static char *sanitize_name(char *str, int sanitize)
{
static char namebuf[256];
char *p = namebuf;
if (!sanitize)
return str;
while (*str) {
*p = isalnum(*str) ? *str : '_';
p++;
str++;
}
*p = 0;
return namebuf;
}

View File

@@ -29,6 +29,7 @@
void dump_results(MdbSQL *sql);
void dump_results_pp(MdbSQL *sql);
int yyparse(void);
#if SQL
@@ -46,7 +47,9 @@ char *buf, line[1000];
int i = 0;
printf("%s",prompt);
fgets(line,1000,stdin);
if (! fgets(line,1000,stdin)) {
return NULL;
}
for (i=strlen(line);i>0;i--) {
if (line[i]=='\n') {
line[i]='\0';
@@ -58,7 +61,7 @@ int i = 0;
return buf;
}
add_history(char *s)
void add_history(char *s)
{
}
#endif
@@ -230,7 +233,7 @@ unsigned long row_count = 0;
else if (row_count==1)
fprintf(stdout, "1 Row retrieved\n");
else
fprintf(stdout, "%d Rows retrieved\n", row_count);
fprintf(stdout, "%lu Rows retrieved\n", row_count);
}
mdb_sql_reset(sql);
}
@@ -284,7 +287,7 @@ unsigned long row_count = 0;
else if (row_count==1)
fprintf(stdout, "1 Row retrieved\n");
else
fprintf(stdout, "%d Rows retrieved\n", row_count);
fprintf(stdout, "%lu Rows retrieved\n", row_count);
}
/* clean up */
@@ -372,7 +375,8 @@ FILE *in = NULL, *out = NULL;
} else {
sprintf(prompt,"1 => ");
s=readline(prompt);
if (!strcmp(s,"exit") || !strcmp(s,"quit") || !strcmp(s,"bye"))
if (!s) done = 1;
if (s && (!strcmp(s,"exit") || !strcmp(s,"quit") || !strcmp(s,"bye")))
done = 1;
}
while (!done) {
@@ -398,7 +402,7 @@ FILE *in = NULL, *out = NULL;
/* preserve line numbering for the parser */
strcat(mybuf,"\n");
}
free(s);
if (s) free(s);
if (in) {
s=malloc(256);
if (!fgets(s, 256, in)) {
@@ -411,15 +415,16 @@ FILE *in = NULL, *out = NULL;
} else {
sprintf(prompt,"%d => ",++line);
s=readline(prompt);
if (!s) done = 1;
}
if (!strcmp(s,"exit") || !strcmp(s,"quit") || !strcmp(s,"bye")) {
if( s && (!strcmp(s,"exit") || !strcmp(s,"quit") || !strcmp(s,"bye"))) {
done = 1;
}
}
mdb_sql_exit(sql);
free(mybuf);
free(s);
if (s) free(s);
myexit(0);
return 0; /* make gcc -Wall happy */