/
s3bdload.c
99 lines (92 loc) · 2.18 KB
/
s3bdload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "s3bd.h"
static void usage(void)
{
fputs(
"Usage: s3bdload [ options ] dbfile [ pragma_override ... ]\n"
" options:\n"
" -i infile # default is stdin\n"
" -s # schema only\n"
" overrides:\n"
" name=value # replace\n"
" name # delete\n",
stderr);
exit(1);
}
int main(
int argc,
char **argv)
{
int status;
sqlite3 *connection;
char *errmsg=NULL;
char *inpath=NULL;
unsigned int flags=0;
char const * const *overrides;
FILE *infile;
for (;;) {
int c;
c=getopt(argc,argv,"si:");
if (c==-1)
break;
switch (c) {
case 'i':
inpath=optarg;
break;
case 's':
flags|=S3BD_LOAD_SCHEMA_ONLY;
break;
default:
usage();
}
}
argc-=optind;
argv+=optind;
if (argc<1)
usage();
if (inpath) {
infile=fopen(inpath,"r");
if (!infile) {
fprintf(stderr,"%s: fopen: %s\n",
inpath,strerror(errno));
return 1;
}
} else {
infile=stdin;
}
if (argc>1) {
overrides=(char const * const *)argv+1;
} else {
overrides=NULL;
}
status=sqlite3_open_v2(
argv[0],
&connection,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL);
if (status!=SQLITE_OK) {
if (connection) {
fprintf(stderr,"%s: sqlite3_open: %s\n",
argv[0],sqlite3_errmsg(connection));
} else {
fprintf(stderr,"%s: sqlite3_open: %s\n",
argv[0],sqlite3_errstr(status));
}
return 1;
}
status=s3bd_load(connection,infile,flags,overrides,&errmsg);
sqlite3_close(connection);
if (status!=SQLITE_OK) {
if (errmsg) {
fprintf(stderr,"s3bd_load: %s\n",errmsg);
sqlite3_free(errmsg);
} else {
fprintf(stderr,"s3bd_load: %s\n",sqlite3_errstr(status));
}
return 1;
}
return 0;
}