185#define sqlite3_api_routines void
187#include <sqlite3ext.h>
188static SQLITE_EXTENSION_INIT1
198#define strcasecmp _stricmp
199#define strncasecmp _strnicmp
219#define ISSPACE(c) ((c) && (strchr(space_chars, (c)) != 0))
237 line = sqlite3_malloc(nline);
244 if (n + 256 > nline) {
245 nline = nline * 2 + 256;
246 tmp = sqlite3_realloc(line, nline);
253 if (!fgets(line + n, nline - n, fin)) {
265 if ((n > 0) && (line[n-1] ==
'\n')) {
271 tmp = sqlite3_realloc(line, n + 1);
288 while ((n > 0) &&
ISSPACE(str[n - 1])) {
291 return (n > 0) && (str[n - 1] ==
';');
303 for (; str[0]; str++) {
307 if ((str[0] ==
'/') && (str[1] ==
'*')) {
309 while (str[0] && ((str[0] !=
'*') || (str[1] !=
'/'))) {
318 if ((str[0] ==
'-') && (str[1] ==
'-')) {
320 while (str[0] && (str[0] !=
'\n')) {
359 for (i = 0; line[i] &&
ISSPACE(line[i]); i++) {
364 sql = sqlite3_malloc(nsql + 1);
372 int len = strlen(line);
375 tmp = sqlite3_realloc(sql, nsql + len + 2);
381 strcpy(sql + nsql,
"\n");
383 strcpy(sql + nsql, line);
389 rc = sqlite3_exec(db, sql, 0, 0, 0);
390 if (rc != SQLITE_OK) {
420quote_func(sqlite3_context *context,
int argc, sqlite3_value **argv)
428 mode = sqlite3_value_int(argv[1]);
430 switch (sqlite3_value_type(argv[0])) {
432 sqlite3_result_text(context,
"NULL", 4, SQLITE_STATIC);
437 sqlite3_result_value(context, argv[0]);
442 unsigned char *blob = (
unsigned char *) sqlite3_value_blob(argv[0]);
443 int nblob = sqlite3_value_bytes(argv[0]);
445 if (2 * nblob + 4 > 1000000000) {
446 sqlite3_result_error(context,
"value too large", -1);
449 text = (
char *) sqlite3_malloc((2 * nblob) + 4);
451 sqlite3_result_error(context,
"out of memory", -1);
454 static const char xdigits[] =
"0123456789ABCDEF";
459 }
else if (mode == 2) {
463 }
else if (mode == 3) {
472 for (i = 0; i < nblob; i++) {
473 text[k++] =
xdigits[(blob[i] >> 4 ) & 0x0F];
474 text[k++] =
xdigits[blob[i] & 0x0F];
479 }
else if (mode == 2) {
481 }
else if (mode == 3) {
489 sqlite3_result_text(context, text, k, SQLITE_TRANSIENT);
496 const unsigned char *arg = sqlite3_value_text(argv[0]);
502 for (i = 0, n = 0; arg[i]; i++) {
503 if (arg[i] ==
'\'') {
507 if (i + n + 3 > 1000000000) {
508 sqlite3_result_error(context,
"value too large", -1);
511 p = sqlite3_malloc(i + n + 3);
513 sqlite3_result_error(context,
"out of memory", -1);
517 for (i = 0, n = 1; arg[i]; i++) {
519 if (arg[i] ==
'\'') {
525 sqlite3_result_text(context, p, n, SQLITE_TRANSIENT);
545 switch (sqlite3_value_type(argv[0])) {
547 sqlite3_result_text(context,
"", 0, SQLITE_STATIC);
552 sqlite3_result_value(context, argv[0]);
557 unsigned char *blob = (
unsigned char *) sqlite3_value_blob(argv[0]);
558 int nblob = sqlite3_value_bytes(argv[0]);
560 if (2 * nblob + 4 > 1000000000) {
561 sqlite3_result_error(context,
"value too large", -1);
564 text = (
char *) sqlite3_malloc((2 * nblob) + 4);
566 sqlite3_result_error(context,
"out of memory", -1);
569 static const char xdigits[] =
"0123456789ABCDEF";
572 for (i = 0; i < nblob; i++) {
573 text[k++] =
xdigits[(blob[i] >> 4 ) & 0x0F];
574 text[k++] =
xdigits[blob[i] & 0x0F];
578 sqlite3_result_text(context, text, k, SQLITE_TRANSIENT);
585 const unsigned char *arg = sqlite3_value_text(argv[0]);
591 for (i = 0, n = 0; arg[i]; i++) {
596 if (i + n + 3 > 1000000000) {
597 sqlite3_result_error(context,
"value too large", -1);
600 p = sqlite3_malloc(i + n + 3);
602 sqlite3_result_error(context,
"out of memory", -1);
606 for (i = 0, n = 1; arg[i]; i++) {
614 sqlite3_result_text(context, p, n, SQLITE_TRANSIENT);
631 static const char spaces[] =
" ";
635 n = sqlite3_value_int(argv[0]);
642 sqlite3_result_text(context, spaces, n, SQLITE_STATIC);
655 static const char xdigits[] =
"0123456789ABCDEF";
656 int type, addtype = 0;
662 addtype = sqlite3_value_int(argv[1]);
664 type = sqlite3_value_type(argv[0]);
668 sqlite3_result_text(context,
" TYPE=\"NULL\">", -1, SQLITE_STATIC);
670 sqlite3_result_text(context,
"", 0, SQLITE_STATIC);
677 char *text = (
char *) sqlite3_malloc(128);
681 sqlite3_result_error(context,
"out of memory", -1);
684 strcpy(text, (type == SQLITE_FLOAT) ?
" TYPE=\"REAL\">" :
685 " TYPE=\"INTEGER\">");
687 strcpy(text + k, (
char *) sqlite3_value_text(argv[0]));
689 sqlite3_result_text(context, text, k, SQLITE_TRANSIENT);
692 sqlite3_result_value(context, argv[0]);
698 unsigned char *blob = (
unsigned char *) sqlite3_value_blob(argv[0]);
699 int nblob = sqlite3_value_bytes(argv[0]);
702 if (6 * nblob + 34 > 1000000000) {
703 sqlite3_result_error(context,
"value too large", -1);
706 text = (
char *) sqlite3_malloc((6 * nblob) + 34);
708 sqlite3_result_error(context,
"out of memory", -1);
712 strcpy(text,
" TYPE=\"BLOB\">");
715 for (i = 0; i < nblob; i++) {
719 text[k++] =
xdigits[(blob[i] >> 4 ) & 0x0F];
720 text[k++] =
xdigits[blob[i] & 0x0F];
724 sqlite3_result_text(context, text, k, SQLITE_TRANSIENT);
730 const unsigned char *arg = sqlite3_value_text(argv[0]);
736 for (i = 0, n = 0; arg[i]; i++) {
737 if ((arg[i] ==
'"') || (arg[i] ==
'\'') ||
738 (arg[i] ==
'<') || (arg[i] ==
'>') ||
739 (arg[i] ==
'&') || (arg[i] <
' ')) {
743 if (i + n + 32 > 1000000000) {
744 sqlite3_result_error(context,
"value too large", -1);
747 p = sqlite3_malloc(i + n + 32);
749 sqlite3_result_error(context,
"out of memory", -1);
754 strcpy(p,
" TYPE=\"TEXT\">");
757 for (i = 0; arg[i]; i++) {
765 }
else if (arg[i] ==
'\'') {
772 }
else if (arg[i] ==
'<') {
777 }
else if (arg[i] ==
'>') {
782 }
else if (arg[i] ==
'&') {
788 }
else if (arg[i] <
' ') {
792 p[n++] =
xdigits[(arg[i] >> 4 ) & 0x0F];
793 p[n++] =
xdigits[arg[i] & 0x0F];
795 }
else if (addtype < 0 && (arg[i] ==
' ')) {
799 p[n++] =
xdigits[(arg[i] >> 4 ) & 0x0F];
800 p[n++] =
xdigits[arg[i] & 0x0F];
807 sqlite3_result_text(context, p, n, SQLITE_TRANSIENT);
824 sqlite3 *db = (sqlite3 *) sqlite3_user_data(ctx);
825 int changes0 = sqlite3_changes(db);
829 char fnbuf[MAX_PATH];
833 if (sqlite3_value_type(args[0]) != SQLITE_NULL) {
834 filename = (
char *) sqlite3_value_text(args[0]);
841 memset(&ofn, 0,
sizeof (ofn));
842 memset(fnbuf, 0,
sizeof (fnbuf));
843 ofn.lStructSize =
sizeof (ofn);
844 ofn.lpstrFile = fnbuf;
845 ofn.nMaxFile = MAX_PATH;
846 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_FILEMUSTEXIST |
847 OFN_EXPLORER | OFN_PATHMUSTEXIST;
848 if (GetOpenFileName(&ofn)) {
856 fin = fopen(filename,
"r");
863 sqlite3_result_int(ctx, sqlite3_changes(db) - changes0);
874 char fnbuf[MAX_PATH];
880 changes0 = sqlite3_changes(db);
885 memset(&ofn, 0,
sizeof (ofn));
886 memset(fnbuf, 0,
sizeof (fnbuf));
887 ofn.lStructSize =
sizeof (ofn);
888 ofn.lpstrFile = fnbuf;
889 ofn.nMaxFile = MAX_PATH;
890 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_FILEMUSTEXIST |
891 OFN_EXPLORER | OFN_PATHMUSTEXIST;
892 if (GetOpenFileName(&ofn)) {
900 fin = fopen(filename,
"r");
907 return sqlite3_changes(db) - changes0;
936 for (i = 0; i < dd->
indent; i++) {
954 sqlite3_stmt *select = 0;
956 const char *rest, *q = query;
965 q = sqlite3_vmprintf(query, ap);
971#if defined(HAVE_SQLITE3PREPAREV2) && HAVE_SQLITE3PREPAREV2
972 rc = sqlite3_prepare_v2(dd->
db, q, -1, &select, &rest);
974 rc = sqlite3_prepare(dd->
db, q, -1, &select, &rest);
977 sqlite3_free((
char *) q);
979 if ((rc != SQLITE_OK) || !select) {
982 rc = sqlite3_step(select);
983 while (rc == SQLITE_ROW) {
984 if (fputs((
char *) sqlite3_column_text(select, 0), dd->
out) > 0) {
991 fputc(
'\r', dd->
out);
994 fputc(
'\n', dd->
out);
996 rc = sqlite3_step(select);
998 rc = sqlite3_finalize(select);
999 if (rc != SQLITE_OK) {
1001 *errp = sqlite3_mprintf(
"%s", sqlite3_errmsg(dd->
db));
1015 long *p = (
long *) *in;
1036 long *p = (
long *) *in;
1037 long len, maxlen, actlen;
1047 maxlen = actlen = 0;
1049 len = nappend + actlen;
1052 for (i = 0; i < nappend; i++) {
1053 if (
append[i] == quote) {
1057 }
else if (!nappend) {
1060 if (len >= maxlen - 1) {
1063 maxlen = (len + 0x03ff) & (~0x3ff);
1064 q = (
long *) sqlite3_realloc(p, maxlen + 1 + 2 *
sizeof (
long));
1073 *in = (
char *) (p + 2);
1078 for (i = 0; i < nappend; i++) {
1080 if (
append[i] == quote) {
1088 memcpy(pp,
append, nappend);
1106 static const char xdigits[] =
"0123456789ABCDEF";
1112 for (i = 0; str[i]; i++) {
1113 if (str[i] ==
'"') {
1114 fputs(
""", dd->
out);
1115 }
else if (str[i] ==
'\'') {
1116 fputs(
"'", dd->
out);
1117 }
else if (str[i] ==
'<') {
1118 fputs(
"<", dd->
out);
1119 }
else if (str[i] ==
'>') {
1120 fputs(
">", dd->
out);
1121 }
else if (str[i] ==
'&') {
1122 fputs(
"&", dd->
out);
1123 }
else if ((
unsigned char) str[i] <=
' ') {
1130 buf[4] =
xdigits[(str[i] >> 4 ) & 0x0F];
1131 buf[5] =
xdigits[str[i] & 0x0F];
1134 fputs(buf, dd->
out);
1136 fputc(str[i], dd->
out);
1151dump_cb(
void *udata,
int nargs,
char **args,
char **cols)
1154 const char *table, *type, *sql;
1157 if ((nargs != 3) || (args == NULL)) {
1163 if (strcmp(table,
"sqlite_sequence") == 0) {
1165 if (fputs(
"DELETE FROM sqlite_sequence;\n", dd->
out) >= 0) {
1169 }
else if (strcmp(table,
"sqlite_stat1") == 0) {
1171 if (fputs(
"ANALYZE sqlite_master;\n", dd->
out) >= 0) {
1175 }
else if (strncmp(table,
"sqlite_", 7) == 0) {
1177 }
else if (strncmp(sql,
"CREATE VIRTUAL TABLE", 20) == 0) {
1179 sqlite3_stmt *
stmt = 0;
1180 char *creat = 0, *table_info = 0;
1182 append(&table_info,
"PRAGMA table_info(", 0);
1183 append(&table_info, table,
'"');
1184 append(&table_info,
")", 0);
1185#if defined(HAVE_SQLITE3PREPAREV2) && HAVE_SQLITE3PREPAREV2
1186 rc = sqlite3_prepare_v2(dd->
db, table_info, -1, &
stmt, 0);
1188 rc = sqlite3_prepare(dd->
db, table_info, -1, &
stmt, 0);
1191 if ((rc != SQLITE_OK) || !
stmt) {
1194 sqlite3_finalize(
stmt);
1199 append(&creat, table,
'"');
1201 rc = sqlite3_step(
stmt);
1202 while (rc == SQLITE_ROW) {
1205 p = (
const char *) sqlite3_column_text(
stmt, 1);
1208 p = (
const char *) sqlite3_column_text(
stmt, 2);
1212 if (sqlite3_column_int(
stmt, 5)) {
1213 append(&creat,
" PRIMARY KEY", 0);
1215 if (sqlite3_column_int(
stmt, 3)) {
1216 append(&creat,
" NOT NULL", 0);
1218 p = (
const char *) sqlite3_column_text(
stmt, 4);
1220 append(&creat,
" DEFAULT ", 0);
1223 rc = sqlite3_step(
stmt);
1224 if (rc == SQLITE_ROW) {
1228 if (rc != SQLITE_DONE) {
1231 sqlite3_finalize(
stmt);
1233 if (creat && fprintf(dd->
out,
"CREATE TABLE %s;\n", creat) > 0) {
1240 if (fprintf(dd->
out,
"%s;\n", sql) > 0) {
1245 if ((strcmp(type,
"table") == 0) ||
1246 ((dd->
quote_mode < 0) && (strcmp(type,
"view") == 0))) {
1247 sqlite3_stmt *
stmt = 0;
1248 char *select = 0, *hdr = 0, *table_info = 0;
1251 append(&table_info,
"PRAGMA table_info(", 0);
1252 append(&table_info, table,
'"');
1253 append(&table_info,
")", 0);
1254#if defined(HAVE_SQLITE3PREPAREV2) && HAVE_SQLITE3PREPAREV2
1255 rc = sqlite3_prepare_v2(dd->
db, table_info, -1, &
stmt, 0);
1257 rc = sqlite3_prepare(dd->
db, table_info, -1, &
stmt, 0);
1260 if ((rc != SQLITE_OK) || !
stmt) {
1263 sqlite3_finalize(
stmt);
1271 append(&select,
"SELECT ", 0);
1272 sprintf(buffer,
"indent_xml(%d)", dd->
indent);
1273 append(&select, buffer, 0);
1274 append(&select,
" || '<' || quote_xml(", 0);
1276 append(&select,
",-1) || '>\n' || ", 0);
1278 append(&select,
"SELECT ", 0);
1282 append(&select,
"SELECT quote_csv(", 0);
1284 append(&select,
") || ',' || ", 0);
1286 append(&select,
"SELECT ", 0);
1295 append(&select,
"SELECT 'INSERT INTO ' || ", 0);
1297 append(&select,
"SELECT 'INSERT OR REPLACE INTO ' || ", 0);
1299 append(&tmp, table,
'"');
1301 append(&select, tmp,
'\'');
1308 append(&select,
" || ' (' || ", 0);
1309 rc = sqlite3_step(
stmt);
1310 while (rc == SQLITE_ROW) {
1311 const char *text = (
const char *) sqlite3_column_text(
stmt, 1);
1315 append(&select, tmp,
'\'');
1318 rc = sqlite3_step(
stmt);
1319 if (rc == SQLITE_ROW) {
1320 append(&select,
" || ',' || ", 0);
1323 if (rc != SQLITE_DONE) {
1326 sqlite3_reset(
stmt);
1327 append(&select,
"|| ')'", 0);
1330 rc = sqlite3_step(
stmt);
1331 while (rc == SQLITE_ROW) {
1332 const char *text = (
const char *) sqlite3_column_text(
stmt, 1);
1334 append(&hdr,
"quote_csv(", 0);
1336 rc = sqlite3_step(
stmt);
1337 if (rc == SQLITE_ROW) {
1338 append(&hdr,
") || ',' || ", 0);
1343 if (rc != SQLITE_DONE) {
1346 sqlite3_reset(
stmt);
1349 append(&select,
" || ' VALUES(' || ", 0);
1351 rc = sqlite3_step(
stmt);
1352 while (rc == SQLITE_ROW) {
1353 const char *text = (
const char *) sqlite3_column_text(
stmt, 1);
1354 const char *type = (
const char *) sqlite3_column_text(
stmt, 2);
1355 int tlen = strlen(type ? type :
"");
1358 sprintf(buffer,
"indent_xml(%d)", dd->
indent + 1);
1359 append(&select, buffer, 0);
1360 append(&select,
"|| '<' || quote_xml(", 0);
1361 append(&select, text,
'\'');
1362 append(&select,
",-1) || quote_xml(", 0);
1363 append(&select, text,
'"');
1364 append(&select,
",1) || '</' || quote_xml(", 0);
1365 append(&select, text,
'\'');
1366 append(&select,
",-1) || '>\n'", 0);
1369 if (((tlen >= 4) && (strncasecmp(type,
"BLOB", 4) == 0)) ||
1370 ((tlen >= 6) && (strncasecmp(type,
"BINARY", 6) == 0))) {
1371 rc = sqlite3_step(
stmt);
1372 if (rc != SQLITE_ROW) {
1373 tlen = strlen(select);
1375 select[tlen - 10] =
'\0';
1380 append(&select,
"quote_csv(", 0);
1381 append(&select, text,
'"');
1383 append(&select,
"quote_sql(", 0);
1384 append(&select, text,
'"');
1389 append(&select, mbuf, 0);
1392 rc = sqlite3_step(
stmt);
1393 if (rc == SQLITE_ROW) {
1395 append(&select,
") || ',' || ", 0);
1397 append(&select,
" || ", 0);
1401 append(&select,
") ", 0);
1407 if (rc != SQLITE_DONE) {
1410 sqlite3_finalize(
stmt);
1413 append(&select,
"|| ')' FROM ", 0);
1416 sprintf(buffer,
" || indent_xml(%d)", dd->
indent);
1417 append(&select, buffer, 0);
1418 append(&select,
" || '</' || quote_xml(", 0);
1420 append(&select,
",-1) || '>\n' FROM ", 0);
1422 append(&select,
"FROM ", 0);
1425 append(&select, table,
'"');
1436 if (rc == SQLITE_CORRUPT) {
1437 append(&select,
" ORDER BY rowid DESC", 0);
1462 sqlite3_free(*errp);
1465 va_start(ap, query);
1466 q = sqlite3_vmprintf(query, ap);
1469 return SQLITE_NOMEM;
1471 rc = sqlite3_exec(dd->
db, q,
dump_cb, dd, errp);
1472 if (rc == SQLITE_CORRUPT) {
1475 tmp = sqlite3_mprintf(
"%s ORDER BY rowid DESC", q);
1482 sqlite3_free(*errp);
1485 rc = sqlite3_exec(dd->
db, q,
dump_cb, dd, errp);
1502 sqlite3 *db = (sqlite3 *) sqlite3_user_data(ctx);
1506 char fnbuf[MAX_PATH];
1514 if (sqlite3_value_type(args[0]) != SQLITE_NULL) {
1515 filename = (
char *) sqlite3_value_text(args[0]);
1522 memset(&ofn, 0,
sizeof (ofn));
1523 memset(fnbuf, 0,
sizeof (fnbuf));
1524 ofn.lStructSize =
sizeof (ofn);
1525 ofn.lpstrFile = fnbuf;
1526 ofn.nMaxFile = MAX_PATH;
1527 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
1528 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
1529 if (GetSaveFileName(&ofn)) {
1537 dd->
out = fopen(filename,
"w");
1542 mode = sqlite3_value_int(args[1]);
1547 if (fputs(
"BEGIN TRANSACTION;\n", dd->
out) >= 0) {
1552 "SELECT name, type, sql FROM sqlite_master"
1553 " WHERE sql NOT NULL AND type = 'table'");
1556 "SELECT sql FROM sqlite_master WHERE"
1557 " sql NOT NULL AND type IN ('index','trigger','view')");
1560 for (i = 2; i < nargs; i += (mode & 2) ? 2 : 1) {
1562 if ((mode & 2) && (i + 1 < nargs)) {
1563 dd->
where = (
char *) sqlite3_value_text(args[i + 1]);
1566 "SELECT name, type, sql FROM sqlite_master"
1567 " WHERE tbl_name LIKE %Q AND type = 'table'"
1568 " AND sql NOT NULL",
1569 sqlite3_value_text(args[i]));
1572 "SELECT sql FROM sqlite_master"
1573 " WHERE sql NOT NULL"
1574 " AND type IN ('index','trigger','view')"
1575 " AND tbl_name LIKE %Q",
1576 sqlite3_value_text(args[i]));
1580 if (fputs(
"COMMIT;\n", dd->
out) >= 0) {
1585 sqlite3_result_int(ctx, dd->
nlines);
1599 sqlite3 *db = (sqlite3 *) sqlite3_user_data(ctx);
1603 char fnbuf[MAX_PATH];
1613 if (sqlite3_value_type(args[0]) != SQLITE_NULL) {
1614 filename = (
char *) sqlite3_value_text(args[0]);
1621 memset(&ofn, 0,
sizeof (ofn));
1622 memset(fnbuf, 0,
sizeof (fnbuf));
1623 ofn.lStructSize =
sizeof (ofn);
1624 ofn.lpstrFile = fnbuf;
1625 ofn.nMaxFile = MAX_PATH;
1626 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
1627 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
1628 if (GetSaveFileName(&ofn)) {
1637 dd->
out = fopen(filename,
"wb");
1639 dd->
out = fopen(filename,
"w");
1646 if (sqlite3_value_type(args[1]) != SQLITE_NULL) {
1647 if (sqlite3_value_int(args[1])) {
1652 for (i = 2; i <= nargs - 3; i += 3) {
1653 char *schema = 0, *sql;
1656 if (sqlite3_value_type(args[i]) != SQLITE_NULL) {
1657 dd->
where = (
char *) sqlite3_value_text(args[i]);
1662 if (sqlite3_value_type(args[i + 2]) != SQLITE_NULL) {
1663 schema = (
char *) sqlite3_value_text(args[i + 2]);
1665 if (!schema || (schema[0] ==
'\0')) {
1666 schema =
"sqlite_master";
1668 sql = sqlite3_mprintf(
"SELECT name, type, sql FROM %s"
1669 " WHERE tbl_name LIKE %%Q AND "
1670 " (type = 'table' OR type = 'view')"
1671 " AND sql NOT NULL", schema);
1673 schema_dump(dd, 0, sql, sqlite3_value_text(args[i + 1]));
1679 sqlite3_result_int(ctx, dd->
nlines);
1693 sqlite3 *db = (sqlite3 *) sqlite3_user_data(ctx);
1696 char *openmode =
"w";
1698 char fnbuf[MAX_PATH];
1708 if (sqlite3_value_type(args[0]) != SQLITE_NULL) {
1709 filename = (
char *) sqlite3_value_text(args[0]);
1716 memset(&ofn, 0,
sizeof (ofn));
1717 memset(fnbuf, 0,
sizeof (fnbuf));
1718 ofn.lStructSize =
sizeof (ofn);
1719 ofn.lpstrFile = fnbuf;
1720 ofn.nMaxFile = MAX_PATH;
1721 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
1722 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
1723 if (GetSaveFileName(&ofn)) {
1732 if (sqlite3_value_type(args[1]) != SQLITE_NULL) {
1733 if (sqlite3_value_int(args[1])) {
1739 if (sqlite3_value_type(args[2]) != SQLITE_NULL) {
1740 dd->
indent = sqlite3_value_int(args[2]);
1746 dd->
out = fopen(filename, openmode);
1751 for (i = 3; i <= nargs - 4; i += 4) {
1752 char *root = 0, *schema = 0, *sql;
1754 if (sqlite3_value_type(args[i]) != SQLITE_NULL) {
1755 root = (
char *) sqlite3_value_text(args[i]);
1756 if (root && !root[0]) {
1761 if (sqlite3_value_type(args[i + 1]) != SQLITE_NULL) {
1762 dd->
where = (
char *) sqlite3_value_text(args[i + 1]);
1770 fputs(
"<", dd->
out);
1772 fputs(
">\n", dd->
out);
1774 if (sqlite3_value_type(args[i + 3]) != SQLITE_NULL) {
1775 schema = (
char *) sqlite3_value_text(args[i + 3]);
1777 if (!schema || (schema[0] ==
'\0')) {
1778 schema =
"sqlite_master";
1780 sql = sqlite3_mprintf(
"SELECT name, type, sql FROM %s"
1781 " WHERE tbl_name LIKE %%Q AND"
1782 " (type = 'table' OR type = 'view')"
1783 " AND sql NOT NULL", schema);
1785 schema_dump(dd, 0, sql, sqlite3_value_text(args[i + 2]));
1791 fputs(
"</", dd->
out);
1793 fputs(
">\n", dd->
out);
1798 sqlite3_result_int(ctx, dd->
nlines);
1810 char fnbuf[MAX_PATH];
1823 memset(&ofn, 0,
sizeof (ofn));
1824 memset(fnbuf, 0,
sizeof (fnbuf));
1825 ofn.lStructSize =
sizeof (ofn);
1826 ofn.lpstrFile = fnbuf;
1827 ofn.nMaxFile = MAX_PATH;
1828 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
1829 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
1830 if (GetSaveFileName(&ofn)) {
1838 dd->
out = fopen(filename,
"w");
1844 if (fputs(
"BEGIN TRANSACTION;\n", dd->
out) >= 0) {
1848 table = va_arg(ap,
char *);
1851 "SELECT name, type, sql FROM sqlite_master"
1852 " WHERE sql NOT NULL AND type = 'table'");
1855 "SELECT sql FROM sqlite_master WHERE"
1856 " sql NOT NULL AND type IN ('index','trigger','view')");
1862 dd->
where = va_arg(ap,
char *);
1865 "SELECT name, type, sql FROM sqlite_master"
1866 " WHERE tbl_name LIKE %Q AND type = 'table'"
1867 " AND sql NOT NULL", table);
1870 "SELECT sql FROM sqlite_master"
1871 " WHERE sql NOT NULL"
1872 " AND type IN ('index','trigger','view')"
1873 " AND tbl_name LIKE %Q", table);
1875 table = va_arg(ap,
char *);
1879 if (fputs(
"COMMIT;\n", dd->
out) >= 0) {
1894 char *prefix, *table, *schema;
1896 char fnbuf[MAX_PATH];
1913 memset(&ofn, 0,
sizeof (ofn));
1914 memset(fnbuf, 0,
sizeof (fnbuf));
1915 ofn.lStructSize =
sizeof (ofn);
1916 ofn.lpstrFile = fnbuf;
1917 ofn.nMaxFile = MAX_PATH;
1918 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
1919 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
1920 if (GetSaveFileName(&ofn)) {
1929 dd->
out = fopen(filename,
"wb");
1931 if ((hdr < 0) && access(filename, W_OK) == 0) {
1932 dd->
out = fopen(filename,
"a");
1935 dd->
out = fopen(filename,
"w");
1943 prefix = va_arg(ap,
char *);
1944 table = va_arg(ap,
char *);
1945 schema = va_arg(ap,
char *);
1946 while (table != NULL) {
1949 dd->
where = (prefix && prefix[0]) ? prefix : 0;
1950 if (!schema || (schema[0] ==
'\0')) {
1951 schema =
"sqlite_master";
1953 sql = sqlite3_mprintf(
"SELECT name, type, sql FROM %s"
1954 " WHERE tbl_name LIKE %%Q AND"
1955 " (type = 'table' OR type = 'view')"
1956 " AND sql NOT NULL", schema);
1961 prefix = va_arg(ap,
char *);
1962 table = va_arg(ap,
char *);
1963 schema = va_arg(ap,
char *);
1975 char *root,
char *item,
char *tablename,
char *schema)
1980 char fnbuf[MAX_PATH];
1989 dd->
indent = (indnt > 0) ? indnt : 0;
1996 memset(&ofn, 0,
sizeof (ofn));
1997 memset(fnbuf, 0,
sizeof (fnbuf));
1998 ofn.lStructSize =
sizeof (ofn);
1999 ofn.lpstrFile = fnbuf;
2000 ofn.nMaxFile = MAX_PATH;
2001 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
2002 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
2003 if (GetSaveFileName(&ofn)) {
2011 dd->
out = fopen(filename,
append ?
"a" :
"w");
2019 fputs(
"<", dd->
out);
2021 fputs(
">\n", dd->
out);
2023 if (!schema || (schema[0] ==
'\0')) {
2024 schema =
"sqlite_master";
2026 sql = sqlite3_mprintf(
"SELECT name, type, sql FROM %s"
2027 " WHERE tbl_name LIKE %%Q AND"
2028 " (type = 'table' OR type = 'view')"
2029 " AND sql NOT NULL", schema);
2037 fputs(
"</", dd->
out);
2039 fputs(
">\n", dd->
out);
2071 void *parg = pfs->
parg;
2084 pfunc(*
string, parg);
2107 if (((*
string <
' ') && (*
string > 0)) || (*
string == 0x7f)) {
2108 sprintf(buf,
"\\u%04x", *
string);
2110 }
else if (*
string < 0) {
2111 unsigned char c =
string[0];
2112 unsigned long uc = 0;
2116 }
else if (c < 0xe0) {
2117 if ((
string[1] & 0xc0) == 0x80) {
2118 uc = ((c & 0x1f) << 6) | (
string[1] & 0x3f);
2123 }
else if (c < 0xf0) {
2124 if (((
string[1] & 0xc0) == 0x80) &&
2125 ((
string[2] & 0xc0) == 0x80)) {
2126 uc = ((c & 0x0f) << 12) |
2127 ((
string[1] & 0x3f) << 6) | (
string[2] & 0x3f);
2132 }
else if (c < 0xf8) {
2133 if (((
string[1] & 0xc0) == 0x80) &&
2134 ((
string[2] & 0xc0) == 0x80) &&
2135 ((
string[3] & 0xc0) == 0x80)) {
2136 uc = ((c & 0x03) << 18) |
2137 ((
string[1] & 0x3f) << 12) |
2138 ((
string[2] & 0x3f) << 6) |
2144 }
else if (c < 0xfc) {
2145 if (((
string[1] & 0xc0) == 0x80) &&
2146 ((
string[2] & 0xc0) == 0x80) &&
2147 ((
string[3] & 0xc0) == 0x80) &&
2148 ((
string[4] & 0xc0) == 0x80)) {
2149 uc = ((c & 0x01) << 24) |
2150 ((
string[1] & 0x3f) << 18) |
2151 ((
string[2] & 0x3f) << 12) |
2152 ((
string[4] & 0x3f) << 6) |
2163 sprintf(buf,
"\\u%04lx", uc);
2164 }
else if (uc < 0x100000) {
2167 sprintf(buf,
"\\u%04lx", 0xd800 | ((uc >> 10) & 0x3ff));
2169 sprintf(buf,
"\\u%04lx", 0xdc00 | (uc & 0x3ff));
2171 strcpy(buf,
"\\ufffd");
2175 pfunc(*
string, parg);
2193 if (*
string && strchr(
".0123456789-+", *
string)) {
2211 void *parg = pfs->
parg;
2214 static const char *b64 =
2215 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2223 for (i = 0; i < len; i += 3) {
2224 reg[1] = reg[2] = reg[3] = reg[4] = 0;
2227 reg[1] = blk[i + 1];
2231 reg[2] = blk[i + 2];
2234 buf[0] = b64[reg[0] >> 2];
2235 buf[1] = b64[((reg[0] << 4) & 0x30) | (reg[1] >> 4)];
2237 buf[2] = b64[((reg[1] << 2) & 0x3c) | (reg[2] >> 6)];
2242 buf[3] = b64[reg[2] & 0x3f];
2264 const char *tail = sql;
2265 int i, nresults = 0, result = SQLITE_ERROR;
2274 int firstrow = 1, nrows = 0;
2278 json_pstr((nresults == 1) ?
"{" :
",{", pfs);
2279 result = sqlite3_prepare(db, tail, -1, &
stmt, &tail);
2280 if (result != SQLITE_OK) {
2283 json_pstr(
"\"columns\":null,\"rows\":null,\"changes\":0,"
2284 "\"last_insert_rowid\":null,", pfs);
2291 result = sqlite3_step(
stmt);
2292 while ((result == SQLITE_ROW) || (result == SQLITE_DONE)) {
2294 for (i = 0; i < sqlite3_column_count(
stmt); i++) {
2297 json_pstr((i == 0) ?
"\"columns\":[" :
",", pfs);
2303 switch (sqlite3_column_type(
stmt, i)) {
2304 case SQLITE_INTEGER:
2331 if (result == SQLITE_DONE) {
2335 json_pstr((nrows == 1) ?
",\"rows\":[" :
",", pfs);
2336 for (i = 0; i < sqlite3_column_count(
stmt); i++) {
2337 pfunc((i == 0) ?
'[' :
',', parg);
2338 switch (sqlite3_column_type(
stmt, i)) {
2339 case SQLITE_INTEGER:
2347 sqlite3_column_bytes(
stmt, i), pfs);
2358 json_pstr((i == 0) ?
"null]" :
"]", pfs);
2359 result = sqlite3_step(
stmt);
2364 result = sqlite3_finalize(
stmt);
2365 if (result != SQLITE_OK) {
2369 ",\"changes\":%d,\"last_insert_rowid\":%I64d",
2371 ",\"changes\":%d,\"last_insert_rowid\":%lld",
2373 sqlite3_changes(db),
2374 sqlite3_last_insert_rowid(db));
2380 json_pstr(
"\"columns\":null,\"rows\":null", pfs);
2384 ",\"changes\":%d,\"last_insert_rowid\":%I64d",
2386 ",\"changes\":%d,\"last_insert_rowid\":%lld",
2388 sqlite3_changes(db),
2389 sqlite3_last_insert_rowid(db));
2392 }
while (tail && *tail);
2407 sqlite3 *db = (sqlite3 *) sqlite3_user_data(ctx);
2413 char fnbuf[MAX_PATH];
2417 if (sqlite3_value_type(args[0]) != SQLITE_NULL) {
2418 filename = (
char *) sqlite3_value_text(args[0]);
2425 memset(&ofn, 0,
sizeof (ofn));
2426 memset(fnbuf, 0,
sizeof (fnbuf));
2427 ofn.lStructSize =
sizeof (ofn);
2428 ofn.lpstrFile = fnbuf;
2429 ofn.nMaxFile = MAX_PATH;
2430 ofn.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_EXPLORER |
2431 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
2432 if (GetSaveFileName(&ofn)) {
2440 out = fopen(filename,
"w");
2445 sql = (
char *) sqlite3_value_text(args[1]);
2452 sqlite3_result_int(ctx, result);
2478 const sqlite3_api_routines *api)
2481 static const struct {
2483 void (*func)(sqlite3_context *, int, sqlite3_value **);
2487 {
"quote_sql",
quote_func, -1, SQLITE_UTF8 },
2500 SQLITE_EXTENSION_INIT2(api);
2504 for (i = 0; i <
sizeof (ftab) /
sizeof (ftab[0]); i++) {
2505 rc = sqlite3_create_function(db, ftab[i].name, ftab[i].nargs,
2506 ftab[i].textrep, db, ftab[i].func, 0, 0);
2507 if (rc != SQLITE_OK) {
2508 for (--i; i >= 0; --i) {
2509 sqlite3_create_function(db, ftab[i].name, ftab[i].nargs,
2510 ftab[i].textrep, 0, 0, 0, 0);
int impexp_export_xml(sqlite3 *db, char *filename, int append, int indnt, char *root, char *item, char *tablename, char *schema)
Writes a table as simple XML to provided filename.
int impexp_export_csv(sqlite3 *db, char *filename, int hdr,...)
Writes entire tables as CSV to provided filename.
static int process_input(sqlite3 *db, FILE *fin)
Process contents of FILE pointer as SQL commands.
static int json_output(sqlite3 *db, char *sql, impexp_putc pfunc, void *parg)
Execute SQL and write output as JSON.
static void json_pstr(const char *string, json_pfs *pfs)
Write string using JSON output function.
static char * one_input_line(FILE *fin)
Read one line of input into dynamically allocated buffer which the caller must free with sqlite3_free...
static void export_xml_func(sqlite3_context *ctx, int nargs, sqlite3_value **args)
SQLite function for XML output, see impexp_export_xml.
static int ends_with_semicolon(const char *str, int n)
Test if string ends with a semicolon.
static char * append(char **in, char const *append, char quote)
Append a string to dynamically allocated string buffer with optional quoting.
static void json_pb64(const unsigned char *blk, int len, json_pfs *pfs)
Write a blob as base64 string using JSON output function.
static int table_dump(DUMP_DATA *dd, char **errp, int fmt, const char *query,...)
Execute SQL to dump contents of one table.
static void quote_xml_str(DUMP_DATA *dd, char *str)
Quote string for XML output during dump.
int impexp_export_sql(sqlite3 *db, char *filename, int mode,...)
Writes SQL to filename similar to SQLite's shell ".dump" meta command.
static void json_pstrq(const char *string, json_pfs *pfs)
Quote and write string using JSON output function.
static void append_free(char **in)
Free dynamically allocated string buffer.
static void indent_xml_func(sqlite3_context *context, int argc, sqlite3_value **argv)
SQLite function to make XML indentation.
static int schema_dump(DUMP_DATA *dd, char **errp, const char *query,...)
Execute SQL on sqlite_master table in order to dump data.
static void export_func(sqlite3_context *ctx, int nargs, sqlite3_value **args)
SQLite function for SQL output, see impexp_export_sql.
int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api)
Initializer for SQLite extension load mechanism.
static void json_pstrc(const char *string, json_pfs *pfs)
Conditionally quote and write string using JSON output function.
static int all_whitespace(const char *str)
Test if string contains entirely whitespace or SQL comment.
static void indent(DUMP_DATA *dd)
Write indentation to dump.
static void quote_func(sqlite3_context *context, int argc, sqlite3_value **argv)
SQLite function to quote SQLite value depending on optional quote mode.
int impexp_init(sqlite3 *db)
Registers the SQLite functions.
int impexp_export_json(sqlite3 *db, char *sql, impexp_putc pfunc, void *parg)
Executes arbitrary SQL statements and formats the result in JavaScript Object Notation (JSON).
static int dump_cb(void *udata, int nargs, char **args, char **cols)
Callback for sqlite3_exec() to dump one data row.
static void import_func(sqlite3_context *ctx, int nargs, sqlite3_value **args)
SQLite function to read and process SQL commands from a file.
static void export_csv_func(sqlite3_context *ctx, int nargs, sqlite3_value **args)
SQLite function for CSV output, see impexp_export_csv.
static void export_json_func(sqlite3_context *ctx, int nargs, sqlite3_value **args)
SQLite function for JSON output, see impexp_export_json.
int impexp_import_sql(sqlite3 *db, char *filename)
Reads SQL commands from filename and executes them against the current database.
static const char space_chars[]
static void quote_csv_func(sqlite3_context *context, int argc, sqlite3_value **argv)
SQLite function to quote an SQLite value in CSV format.
static void quote_xml_func(sqlite3_context *context, int argc, sqlite3_value **argv)
SQLite function to quote a string for XML.
SQLite extension module for importing/exporting database information from/to SQL source text and expo...
void(* impexp_putc)(int c, void *arg)
The function pointer for the output function to "impexp_export_json" has a signature compatible with ...
static const char * xdigits
Structure for dump callback.
int with_schema
if true, output schema
int quote_mode
mode for quoting data
int nlines
counter for output lines
sqlite3 * db
SQLite database pointer.
char * where
optional where clause of dump
FILE * out
output file pointer
int indent
current indent level
JSON output helper structure.
impexp_putc pfunc
function like fputc()
void * parg
argument to function
Driver internal structure representing SQL statement (HSTMT).