Browse files

MB-6445: Remove sqlite backend

Sqlite will be deprecated and not supported anymore as of 2.0
release.

Change-Id: I1a5c8025444ca264897958b4053ff7cfe9c97010
Reviewed-on: http://review.couchbase.org/20207
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
  • Loading branch information...
1 parent a95a3bb commit d34de537c9841bd2f7bc071da4079e6b55150b82 @mikewied mikewied committed with chiyoung Aug 26, 2012
View
12 .gitignore
@@ -50,9 +50,6 @@
/m4/lt~obsolete.m4
/m4/version.m4
/mc-kvstore-test
-/management/*bdbconvert
-/management/sqlite3
-/mbbackup-????.mbb
/sizes
/src/config.h
/src/config.h.in
@@ -61,17 +58,8 @@
/src/stats-info.[ch]
/src/testlogger_libify.cc
/tests/module_tests/*_test
-/wrapper/*badm-online-restore
-/wrapper/*badm-online-update
/wrapper/*badm-tap-registration
-/wrapper/*bbackup
-/wrapper/*bbackup-incremental
-/wrapper/*bbackup-merge-incremental
-/wrapper/*bdbconvert
-/wrapper/*bdbmaint
-/wrapper/*bdbupgrade
/wrapper/*bepctl
-/wrapper/*brestore
/wrapper/*bstats
/wrapper/*bvbucketctl
/wrapper/wrapper
View
87 Makefile.am
@@ -3,8 +3,6 @@ AM_CPPFLAGS += -I$(top_srcdir)/src -I${top_srcdir}/include
bin_SCRIPTS = management/cbanalyze-core \
wrapper/cbadm-tap-registration \
- wrapper/cbdbmaint \
- wrapper/cbdbupgrade \
wrapper/cbepctl \
wrapper/cbstats \
wrapper/cbvbucketctl
@@ -19,7 +17,6 @@ noinst_LTLIBRARIES = \
libconfiguration.la \
libkvstore.la \
libobjectregistry.la \
- libsqlite-kvstore.la \
libcouch-kvstore.la \
libdirutils.la
@@ -29,7 +26,6 @@ fixup_stats_json:
dist_doc_DATA = docs/stats.json
-bin_PROGRAMS = management/cbdbconvert
BUILT_SOURCES = src/generated_configuration.cc \
src/generated_configuration.hh \
src/stats-info.c src/stats-info.h
@@ -102,27 +98,7 @@ libkvstore_la_SOURCES = src/crc32.c src/crc32.h src/kvstore.cc src/kvstore.hh \
src/mutation_log.cc src/mutation_log.hh \
src/mutation_log_compactor.cc \
src/mutation_log_compactor.hh
-libkvstore_la_CPPFLAGS = -I$(top_srcdir)/src/sqlite-kvstore $(AM_CPPFLAGS)
-
-libsqlite_kvstore_la_SOURCES = src/kvstore.hh \
- src/sqlite-kvstore/factory.cc \
- src/sqlite-kvstore/pathexpand.hh \
- src/sqlite-kvstore/pathexpand.cc \
- src/sqlite-kvstore/sqlite-eval.cc \
- src/sqlite-kvstore/sqlite-eval.hh \
- src/sqlite-kvstore/sqlite-kvstore.cc \
- src/sqlite-kvstore/sqlite-kvstore.hh \
- src/sqlite-kvstore/sqlite-stats.hh \
- src/sqlite-kvstore/sqlite-pst.cc \
- src/sqlite-kvstore/sqlite-pst.hh \
- src/sqlite-kvstore/sqlite-strategies.cc \
- src/sqlite-kvstore/sqlite-strategies.hh \
- src/sqlite-kvstore/sqlite-vfs.c \
- src/sqlite-kvstore/sqlite-vfs.h
-libsqlite_kvstore_la_CPPFLAGS = -I$(top_srcdir)/src/sqlite-kvstore \
- $(AM_CPPFLAGS)
-libsqlite_kvstore_la_LIBADD =
-libsqlite_kvstore_la_DEPENDENCIES =
+libkvstore_la_CPPFLAGS = $(AM_CPPFLAGS)
libblackhole_kvstore_la_CPPFLAGS = -I$(top_srcdir)/src/blackhole-kvstore \
$(AM_CPPFLAGS)
@@ -171,46 +147,16 @@ libconfiguration_la_SOURCES = src/generated_configuration.hh \
src/configuration.hh \
src/configuration.cc
-ep_la_LIBADD = libkvstore.la libsqlite-kvstore.la \
+ep_la_LIBADD = libkvstore.la \
libblackhole-kvstore.la libcouch-kvstore.la \
libobjectregistry.la libconfiguration.la $(LTLIBEVENT)
-ep_la_DEPENDENCIES = libkvstore.la libsqlite-kvstore.la \
+ep_la_DEPENDENCIES = libkvstore.la \
libblackhole-kvstore.la \
libobjectregistry.la libconfiguration.la \
libcouch-kvstore.la
ep_testsuite_la_LIBADD =libobjectregistry.la $(LTLIBEVENT)
ep_testsuite_la_DEPENDENCIES = libobjectregistry.la
-management_cbdbconvert_SOURCES = src/atomic.cc src/mutex.cc \
- management/dbconvert.cc src/testlogger.cc \
- src/item.cc src/stored-value.cc src/ep_time.c \
- src/checkpoint.cc src/vbucketmap.cc
-management_cbdbconvert_LDADD = libkvstore.la libsqlite-kvstore.la \
- libblackhole-kvstore.la \
- libobjectregistry.la \
- libconfiguration.la libkvstore.la \
- libblackhole-kvstore.la \
- libcouch-kvstore.la \
- $(LTLIBEVENT)
-management_cbdbconvert_DEPENDENCIES = libkvstore.la libsqlite-kvstore.la \
- libcouch-kvstore.la libobjectregistry.la \
- libconfiguration.la
-
-if BUILD_EMBEDDED_LIBSQLITE3
-ep_la_LIBADD += libsqlite3.la
-ep_la_DEPENDENCIES += libsqlite3.la
-management_cbdbconvert_LDADD += libsqlite3.la
-management_cbdbconvert_DEPENDENCIES += libsqlite3.la
-noinst_LTLIBRARIES += libsqlite3.la
-bin_PROGRAMS += management/sqlite3
-else
-ep_la_LIBADD += $(LIBSQLITE3)
-management_cbdbconvert_LDADD += $(LIBSQLITE3)
-endif
-
-libsqlite3_la_SOURCES = src/embedded/sqlite3.h src/embedded/sqlite3.c
-libsqlite3_la_CFLAGS = $(AM_CFLAGS) ${NO_WERROR} -DSQLITE_THREADSAFE=2
-
check_PROGRAMS=\
atomic_ptr_test \
atomic_test \
@@ -223,7 +169,6 @@ check_PROGRAMS=\
misc_test \
mutation_log_test \
mutex_test \
- pathexpand_test \
priority_test \
ringbuffer_test \
vbucket_test
@@ -345,11 +290,6 @@ gen_config_LDFLAGS = -lm
gen_code_CPPFLAGS = -I$(top_srcdir)/tools $(AM_CPPFLAGS)
gen_code_SOURCES = tools/gencode.cc tools/cJSON.c tools/cJSON.h
-management_sqlite3_SOURCES = src/embedded/sqlite3-shell.c
-management_sqlite3_CFLAGS = $(AM_CFLAGS) ${NO_WERROR}
-management_sqlite3_DEPENDENCIES = libsqlite3.la
-management_sqlite3_LDADD = libsqlite3.la
-
vbucket_test_CXXFLAGS = $(AM_CPPFLAGS) $(AM_CXXFLAGS) ${NO_WERROR}
vbucket_test_SOURCES = tests/module_tests/vbucket_test.cc \
tests/module_tests/threadtests.hh src/vbucket.hh \
@@ -405,12 +345,6 @@ chunk_creation_test_CXXFLAGS = $(AM_CPPFLAGS) $(AM_CXXFLAGS) ${NO_WERROR}
chunk_creation_test_SOURCES = tests/module_tests/chunk_creation_test.cc \
src/common.hh
-pathexpand_test_CPPFLAGS = -I$(top_srcdir)/src/sqlite-kvstore \
- $(AM_CPPFLAGS) ${NO_WERROR}
-pathexpand_test_SOURCES = tests/module_tests/pathexpand_test.cc \
- src/sqlite-kvstore/pathexpand.hh \
- src/sqlite-kvstore/pathexpand.cc
-
ringbuffer_test_CXXFLAGS = $(AM_CPPFLAGS) $(AM_CXXFLAGS) ${NO_WERROR}
ringbuffer_test_SOURCES = tests/module_tests/ringbuffer_test.cc src/ringbuffer.hh
ringbuffer_test_DEPENDENCIES = src/ringbuffer.hh
@@ -421,7 +355,6 @@ hrtime_test_SOURCES += src/gethrtime.c
dispatcher_test_SOURCES += src/gethrtime.c
vbucket_test_SOURCES += src/gethrtime.c
checkpoint_test_SOURCES += src/gethrtime.c
-management_cbdbconvert_SOURCES += src/gethrtime.c
ep_testsuite_la_SOURCES += src/gethrtime.c
hash_table_test_SOURCES += src/gethrtime.c
mutation_log_test_SOURCES += src/gethrtime.c
@@ -430,12 +363,10 @@ endif
if BUILD_BYTEORDER
ep_la_SOURCES += src/byteorder.c
ep_testsuite_la_SOURCES += src/byteorder.c
-management_cbdbconvert_SOURCES += src/byteorder.c
endif
pythonlibdir=$(libdir)/python
pythonlib_DATA= \
- management/capture.py \
management/clitool.py \
management/mc_bin_client.py \
management/mc_bin_server.py \
@@ -445,16 +376,12 @@ pythonlib_DATA= \
pythonlib_SCRIPTS= \
management/cbadm-tap-registration \
- management/cbdbmaint \
- management/cbdbupgrade \
management/cbepctl \
management/cbstats \
management/cbvbucketctl
PYTHON_TOOLS= \
wrapper/cbadm-tap-registration \
- wrapper/cbdbmaint \
- wrapper/cbdbupgrade \
wrapper/cbepctl \
wrapper/cbstats \
wrapper/cbvbucketctl
@@ -498,8 +425,6 @@ ep_la_LIBADD += ep_la-probes.lo
ep_la_DEPENDENCIES += ep_la-probes.lo
ep_testsuite_la_LIBADD += ep_testsuite_la-probes.lo
ep_testsuite_la_DEPENDENCIES += ep_testsuite_la-probes.lo
-management_cbdbconvert_LDADD += .libs/cddbconvert-probes.o
-management_cbdbconvert_DEPENDENCIES += .libs/cddbconvert-probes.o
atomic_ptr_test_LDADD = .libs/atomic_ptr_test-probes.o
atomic_ptr_test_DEPENDENCIES += .libs/atomic_ptr_test-probes.o
atomic_test_LDADD = .libs/atomic_test-probes.o
@@ -550,12 +475,6 @@ ep_testsuite_la-probes.lo: $(ep_testsuite_la_OBJECTS) dtrace/probes.h
sed -e s,ep_la-item,ep_testsuite_la-probes,g ep_la-item.lo \
> ep_testsuite_la-probes.lo
-.libs/cddbconvert-probes.o: $(management_cbdbconvert_OBJECTS) dtrace/probes.h
- $(DTRACE) $(DTRACEFLAGS) -G \
- -o .libs/cddbconvert-probes.o \
- -s ${srcdir}/dtrace/probes.d \
- $(management_cbdbconvert_OBJECTS)
-
.libs/atomic_ptr_test-probes.o: $(atomic_ptr_test_OBJECTS) dtrace/probes.h
$(DTRACE) $(DTRACEFLAGS) -G \
-o .libs/atomic_ptr_test-probes.o \
View
18 configuration.json
@@ -40,15 +40,13 @@
}
},
"backend": {
- "default": "sqlite",
+ "default": "couchdb",
"dynamic": false,
"type": "std::string",
"validator": {
"enum": [
"blackhole",
- "couchdb",
- "sqlite",
- "mccouch"
+ "couchdb"
]
}
},
@@ -117,14 +115,6 @@
"descr": "True if we want to enable data traffic after warmup is complete",
"type": "bool"
},
- "db_shards": {
- "default": "4",
- "type": "size_t"
- },
- "db_strategy": {
- "default": "multiMTVBDB",
- "type": "std::string"
- },
"dbname": {
"default": "/tmp/test",
"descr": "Path to on-disk storage.",
@@ -327,10 +317,6 @@
}
}
},
- "shardpattern": {
- "default": "%d/%b-%i.sqlite",
- "type": "std::string"
- },
"stored_val_type": {
"default": "",
"type": "std::string"
View
12 configure.ac
@@ -40,18 +40,6 @@ AC_ARG_ENABLE([valgrind],
AS_IF([test "$ac_enable_valgrind" = "yes"],
[ AC_DEFINE(VALGRIND, 1, [Enable extra memset to help valgrind])])
-AC_ARG_ENABLE([system-libsqlite3],
- [AS_HELP_STRING([--enable-system-libsqlite3],
- [Build with system installed libsqlite3. @<:@default=off@:>@])],
- [ac_enable_system_libsqlite3="$enableval"],
- [ac_enable_system_libsqlite3="no"])
-
-AS_IF([test "$ac_enable_system_libsqlite3" = "yes"],
- [ PANDORA_REQUIRE_LIBSQLITE3
- AC_DEFINE(USE_SYSTEM_LIBSQLITE3, 1, "Set if you want to use a system installed sqlite3")
- ])
-AM_CONDITIONAL(BUILD_EMBEDDED_LIBSQLITE3, test "$ac_enable_system_libsqlite3" = "no")
-
AC_ARG_ENABLE([generated-tests],
[AS_HELP_STRING([--enable-generated-tests],
[Run generated test suite. @<:@default=off@:>@])],
View
24 docs/engine-params.org
@@ -28,14 +28,8 @@ memcached like this:
|------------------------+--------+--------------------------------------------|
| config_file | string | Path to additional parameters. |
| dbname | string | Path to on-disk storage. |
-| shardpattern | string | File pattern for shards (see below) |
| ht_locks | int | Number of locks per hash table. |
| ht_size | int | Number of buckets per hash table. |
-| initfile | string | Optional SQL script to run after |
-| | | opening DB |
-| postInitfile | string | Optional SQL script to run after |
-| | | all DB shards and statements have |
-| | | been initialized |
| max_item_size | int | Maximum number of bytes allowed for |
| | | an item. |
| max_size | int | Max cumulative item size in bytes. |
@@ -72,9 +66,6 @@ memcached like this:
| failpartialwarmup | bool | If false, continue running after failing |
| | | to load some records. |
| max_vbuckets | int | Maximum number of vbuckets expected (1024) |
-| db_shards | int | Number of shards for db store |
-| db_strategy | string | DB store strategy ("multiDB", "singleDB" |
-| | | or "singleMTDB") |
| concurrentDB | bool | True (default) if concurrent DB reads are |
| | | permitted where possible. |
| chk_remover_stime | int | Interval for the checkpoint remover that |
@@ -123,18 +114,3 @@ memcached like this:
| } | scanner will be scheduled to run. |
| pager_active_vb_pcnt | int | Percentage of active vbucket items among |
| | | all evicted items by item pager. |
-
-** Shard Patterns
-
-When using a multi-sharded database, =shardpattern= specifies how
-shards are named relative to the main database.
-
-=shardpattern= uses a printf-style string to expand the path with the
-given variables:
-
-- =%d= : The directory name containing the main database
- (This will be =.= for relative paths).
-- =%b= : The base name of the main database.
-- =%i= : The shard number.
-
-The default value of =shardpattern= is =%d/%b-%i.sqlite=
View
11 docs/stats.org
@@ -128,8 +128,6 @@ For introductory information on stats within membase, start with the
| ep_tap_keepalive | Tap keepalive time. |
| ep_dbname | DB path. |
| ep_dbinit | Number of seconds to initialize DB. |
-| ep_dbshards | Number of shards for db store |
-| ep_db_strategy | SQLite db strategy |
| ep_io_num_read | Number of io read operations |
| ep_io_num_write | Number of io write operations |
| ep_io_read_bytes | Number of bytes read (key + values) |
@@ -627,15 +625,6 @@ The following stats are available for all database engine:
| writeSize | Size of data in write operations |
| delete | Time spent in delete() calls |
-The following stats are available for the Sqlite database engine:
-
-| sector_size | Disk sector size (bytes) |
-| lock | Number of database lock operations |
-| truncate | Number of truncate operations |
-| sync | Time spent in sync() calls |
-| readSeek | Seek distance in read operations |
-| writeSeek | Seek distance in write operations |
-
The following stats are available for the CouchStore database engine:
| backend_type | Type of backend database engine |
View
46 m4/pandora_have_libpq.m4
@@ -1,46 +0,0 @@
-dnl Copyright (C) 2009 Sun Microsystems
-dnl This file is free software; Sun Microsystems
-dnl gives unlimited permission to copy and/or distribute it,
-dnl with or without modifications, as long as this notice is preserved.
-
-AC_DEFUN([_PANDORA_SEARCH_LIBPQ],[
- AC_REQUIRE([AC_LIB_PREFIX])
-
- dnl --------------------------------------------------------------------
- dnl Check for libpq
- dnl --------------------------------------------------------------------
-
- AC_ARG_ENABLE([libpq],
- [AS_HELP_STRING([--disable-libpq],
- [Build with libpq support @<:@default=on@:>@])],
- [ac_enable_libpq="$enableval"],
- [ac_enable_libpq="yes"])
-
- AS_IF([test "x$ac_enable_libpq" = "xyes"],[
- AC_CHECK_HEADERS([libpq-fe.h])
- AC_LIB_HAVE_LINKFLAGS(pq,,[
- #ifdef HAVE_LIBPQ_FE_H
- # include <libpq-fe.h>
- #else
- # include <postgresql/libpq-fe.h>
- #endif
- ], [
- PGconn *conn;
- conn = PQconnectdb(NULL);
- ])
- ],[
- ac_cv_libpq="no"
- ])
-
- AM_CONDITIONAL(HAVE_LIBPQ, [test "x${ac_cv_libpq}" = "xyes"])
-])
-
-AC_DEFUN([PANDORA_HAVE_LIBPQ],[
- AC_REQUIRE([_PANDORA_SEARCH_LIBPQ])
-])
-
-AC_DEFUN([PANDORA_REQUIRE_LIBPQ],[
- AC_REQUIRE([PANDORA_HAVE_LIBPQ])
- AS_IF([test "x${ac_cv_libpq}" = "xno"],
- AC_MSG_ERROR([libpq is required for ${PACKAGE}]))
-])
View
42 m4/pandora_have_libsqlite3.m4
@@ -1,42 +0,0 @@
-dnl Copyright (C) 2009 Sun Microsystems
-dnl This file is free software; Sun Microsystems
-dnl gives unlimited permission to copy and/or distribute it,
-dnl with or without modifications, as long as this notice is preserved.
-
-AC_DEFUN([_PANDORA_SEARCH_LIBSQLITE3],[
- AC_REQUIRE([AC_LIB_PREFIX])
-
- dnl --------------------------------------------------------------------
- dnl Check for libsqlite3
- dnl --------------------------------------------------------------------
-
- AC_ARG_ENABLE([libsqlite3],
- [AS_HELP_STRING([--disable-libsqlite3],
- [Build with libsqlite3 support @<:@default=on@:>@])],
- [ac_enable_libsqlite3="$enableval"],
- [ac_enable_libsqlite3="yes"])
-
- AS_IF([test "x$ac_enable_libsqlite3" = "xyes"],[
- AC_LIB_HAVE_LINKFLAGS(sqlite3,,[
- #include <stdio.h>
- #include <sqlite3.h>
- ],[
- sqlite3 *db;
- sqlite3_open(NULL, &db);
- ])
- ],[
- ac_cv_libsqlite3="no"
- ])
-
- AM_CONDITIONAL(HAVE_LIBSQLITE3, [test "x${ac_cv_libsqlite3}" = "xyes"])
-])
-
-AC_DEFUN([PANDORA_HAVE_LIBSQLITE3],[
- AC_REQUIRE([_PANDORA_SEARCH_LIBSQLITE3])
-])
-
-AC_DEFUN([PANDORA_REQUIRE_LIBSQLITE3],[
- AC_REQUIRE([_PANDORA_SEARCH_LIBSQLITE3])
- AS_IF([test "x${ac_cv_libsqlite3}" = "xno"],
- AC_MSG_ERROR([libsqlite3 is required for ${PACKAGE}]))
-])
View
69 management/capture.py
@@ -1,69 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import time
-
-from sqlite3 import dbapi2 as sqlite
-
-import mc_bin_client
-
-CREATE_TABLE = """
-create table if not exists samples (
- host varchar(64) not null,
- uptime integer not null,
- statname varchar(64) not null,
- value varchar(64) not null
-)
-"""
-
-INS = """insert into samples (host, uptime, statname, value) values (?, ?, ?, ?)"""
-
-dbpath = None
-
-def wants_db(orig):
- def f(*args):
- global dbpath
- db = sqlite.connect(dbpath)
- try:
- return orig(*args + (db,))
- finally:
- db.commit()
- db.close()
- return f
-
-@wants_db
-def setup(db):
- db.execute(CREATE_TABLE)
-
-@wants_db
-def sample(hp, db):
- cur = db.cursor()
-
- try:
- h, p = hp.split(':')
- p = int(p)
- except:
- h = hp
- port = 11211
-
- mc = mc_bin_client.MemcachedClient(h, p)
- stats = mc.stats()
- uptime = int(stats['uptime'])
- print "Sampling", h, "at uptime =", uptime
- for k,v in stats.iteritems():
- cur.execute(INS, (h, uptime, k, v))
-
-if __name__ == '__main__':
- dbpath = sys.argv[1]
- hosts = sys.argv[2:]
- assert hosts
-
- setup()
- while True:
- try:
- for h in hosts:
- sample(h)
- except:
- import traceback
- traceback.print_exc()
- time.sleep(60)
View
88 management/cbdbmaint
@@ -1,88 +0,0 @@
-#!/usr/bin/env python
-import sys
-import os
-import glob
-import shutil
-import getopt
-import subprocess
-import mc_bin_client
-
-DEFAULT_PORT="11210"
-
-def usage(err=None):
- print >> sys.stderr, """
-Usage: %s [--vacuum] [--backupto=<dest_dir>] [--port=%s]
-""" % (os.path.basename(sys.argv[0]), DEFAULT_PORT)
-
- if err:
- print >> sys.stderr, err
- sys.exit(1)
-
-def isADBFile(f):
- return f.endswith('.sqlite') or f.endswith('.mb')
-
-def run_sql(sqlite, fn, sql, more_args=[], logger=sys.stderr):
- args = ['-batch', '-bail']
- cmd = [sqlite] + args + more_args + [fn]
- p = subprocess.Popen(cmd,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- (o,e) = p.communicate(sql)
- if p.returncode != 0:
- logger.write("Error running query: %s\n" % sql)
- logger.write(e)
- sys.exit(1)
- return o
-
-def findCmd(cmdName):
- cmd_dir = os.path.dirname(sys.argv[0])
- possible = []
- for bin_dir in [cmd_dir, os.path.join(cmd_dir, "..", "..", "bin")]:
- possible = possible + [os.path.join(bin_dir, p) for p in [cmdName, cmdName + '.exe']]
- cmdbin = [p for p in possible if os.path.exists(p)][0]
- return cmdbin
-
-def main():
- try:
- opts, args = getopt.getopt(sys.argv[1:], '', ['vacuum', 'backupto=', 'port='])
- except getopt.GetoptError, e:
- usage(e.msg)
-
- if not opts:
- usage()
-
- flushctl = findCmd('cbflushctl')
- sqlitebin = findCmd('sqlite3')
-
- optdict = dict(opts)
- shouldVacuum = '--vacuum' in optdict
-
- dest_dir = optdict.get("--backupto")
- port = int(optdict.get("--port", DEFAULT_PORT))
-
- mc = mc_bin_client.MemcachedClient('127.0.0.1', port=port)
- db_path = mc.stats()['ep_dbname']
- db_files = glob.glob('%s*' % db_path)
-
- print 'Pausing persistence... ',
- os.system('"%s" 127.0.0.1:%d stop' % (flushctl, port))
- print 'paused.'
- try:
- for fn in db_files:
-
- if shouldVacuum and (fn == db_path or isADBFile(fn)):
- print "Vacuuming", fn
- run_sql(sqlitebin, fn, "vacuum")
-
- if dest_dir:
- dest_fn = os.path.join(dest_dir, os.path.basename(fn))
- print 'Copying %s to %s' % (fn, dest_fn)
- shutil.copyfile(fn, dest_fn)
- finally:
- print 'Unpausing persistence.'
- os.system('"%s" 127.0.0.1:%d start' % (flushctl, port))
-
-
-if __name__ == '__main__':
- main()
View
187 management/cbdbupgrade
@@ -1,187 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import os
-import glob
-import subprocess
-import shutil
-
-TARGET_VERSION=2
-
-# Backported any to older versions of python
-try:
- any
-except NameError:
- def any(a):
- for i in a:
- if i:
- return True
- return False
-
-try:
- all
-except NameError:
- def all(iterable):
- for element in iterable:
- if not element:
- return False
- return True
-
-def run_sql(sqlite, fn, sql, more_args=[], logger=sys.stderr):
- args = ['-batch', '-bail']
- cmd = [sqlite] + args + more_args + [fn]
- p = subprocess.Popen(cmd,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- (o,e) = p.communicate(sql)
- if p.returncode != 0:
- logger.write("Error running query: %s\n" % sql)
- logger.write(e)
- sys.exit(1)
- return o
-
-def version(sqlite, fn):
- return int(run_sql(sqlite, fn, "pragma user_version;").strip())
-
-def count_kv_vb_tables(sqlite, fn):
- return int(run_sql(sqlite, fn,
- 'select count(*) from sqlite_master '\
- 'where name like "kv_%";').strip())
-
-def findCmd(cmdName):
- cmd_dir = os.path.dirname(sys.argv[0])
- possible = []
- for bin_dir in [cmd_dir, os.path.join(cmd_dir, "..", "..", "bin")]:
- possible = possible + [os.path.join(bin_dir, p) for p in [cmdName, cmdName + '.exe']]
- cmdbin = [p for p in possible if os.path.exists(p)][0]
- return cmdbin
-
-def findSqlite():
- return findCmd('sqlite3')
-
-def usage(command, msg):
- e = """Error: %s
-
-Usage: %s /path/to/srcdb [...] /path/to/dest
-
-src is the path to the main database. You can specify more than one.
-dest can be either a new main database filename, or a directory
-(the old DB name will be used).
-
-If more than one src db is specified, the destination *must* be a
-directory.""" % (msg, command)
- sys.exit(e)
-
-class NullLogger(object):
-
- def write(self, x):
- pass
-
-def updateVBStatesTableSchema(sqlite, src):
- try:
- run_sql(sqlite, src, 'alter table vbucket_states add column vb_version;',
- logger=NullLogger())
- except SystemExit:
- pass # Already had the column
- try:
- run_sql(sqlite, src, 'alter table vbucket_states add column checkpoint_id;',
- logger=NullLogger())
- except SystemExit:
- pass # Already had the column
-
-def updateKVTableSchema(sqlite, src):
- for d in glob.glob(src + '*.mb') + glob.glob(src + '*.sqlite'):
- if os.path.exists(d):
- try:
- run_sql(sqlite, d, 'alter table kv add column vb_version;',
- logger=NullLogger())
- except SystemExit:
- pass # Already had the column
-
-def ensureNewColumns(sqlite, src):
- updateVBStatesTableSchema(sqlite, src)
- updateKVTableSchema(sqlite, src)
-
-def findTarget(src, dest):
- if os.path.isdir(dest):
- dest = os.path.join(dest, os.path.basename(src))
- return dest
-
-def findTargetCollisions(srcs, dest):
- rv = []
- for fn in (findTarget(s, dest) for s in srcs):
- if os.path.exists(fn):
- rv.append(fn)
- for shard in glob.glob(fn + '*.mb'):
- if os.path.exists(shard):
- rv.append(shard)
- return rv
-
-def doset(sqlite, src, dest):
- v = version(sqlite, src)
- print 'Source version from "%s" is %d' % (src, v)
-
- args = [findCmd('cbdbconvert')]
- if v < 2:
- args.append("--remove-crlf")
- if os.path.exists(src + '-0.mb'):
- args.append('--src-pattern=%d/%b-%i.mb')
- if count_kv_vb_tables(sqlite, src + "-0.mb") > 0:
- args.append("--src-strategy=multiMTVBDB")
- args.append(src)
- args.append(findTarget(src, dest))
- args.append("--report-every=2000")
- mydir = os.path.dirname(sys.argv[0])
- if os.name == 'nt':
- init_file = os.path.join(mydir, '..', 'etc', 'couchbase', 'init.sql')
- else:
- init_file = os.path.join(mydir, '..', '..', 'etc', 'couchbase', 'init.sql')
- args.append("--init-file=" + init_file)
-
- print " ".join(args)
-
- ensureNewColumns(sqlite, src)
-
- returncode = subprocess.call(args)
- if returncode != 0:
- sys.stderr.write("Error running convert, exit code %d (%s)\n" %
- (returncode, ' '.join(args)))
- sys.exit(1)
-
-def main():
- command = sys.argv[0]
- args = sys.argv[1:]
- try:
- dest = args.pop()
- srcs = args
- if not srcs:
- usage(command, "No destination specified")
- if not all(os.path.isfile(p) for p in srcs):
- usage(command, "Source must point to existing main DB files")
- if len(srcs) > 1 and not os.path.isdir(dest):
- usage(command,
- "Multiple sources, but destination is not a directory.")
- except IndexError:
- usage(command, "Too few arguments")
-
- if os.name == 'nt':
- mydir = os.path.dirname(sys.argv[0])
- memcache_dir = os.path.join(mydir, '..', '..', 'memcached')
- erlang_dir = os.path.join(mydir, 'erlang', 'bin')
- path = [mydir, memcache_dir, erlang_dir, os.environ['PATH']]
- os.environ['PATH'] = ';'.join(path)
-
- sqlite = findSqlite()
-
- collisions = findTargetCollisions(srcs, dest)
- if collisions:
- sys.stderr.write("Would overwrite the following files:\n\t")
- sys.stderr.write('\n\t'.join(collisions) + '\n')
- sys.exit(1)
-
- for src in srcs:
- doset(sqlite, src, dest)
-
-if __name__ == '__main__':
- main()
View
286 management/dbconvert.cc
@@ -1,286 +0,0 @@
-/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
-
-#include "config.h"
-
-#include <cassert>
-#include <iostream>
-#include <utility>
-#include <cstdlib>
-
-#ifdef HAVE_SYSEXITS_H
-#include <sysexits.h>
-#endif
-
-#include <memcached/engine.h>
-
-#include <getopt.h>
-#include <stats.hh>
-#include <kvstore.hh>
-#include <item.hh>
-#include <callbacks.hh>
-#include "sqlite-kvstore/sqlite-strategies.hh"
-#include "sqlite-kvstore/sqlite-kvstore.hh"
-
-#ifndef EX_USAGE
-#define EX_USAGE 64
-#endif
-
-/* getopt.h on Solaris defines the name member as "char*" and not
- * "const char*". This cause a compile error when you try to assign
- * it to a constant string. To aviod compile errors let's create
- * a macro to cast it to a char pointer.
- */
-#ifdef __sun
-#define OPTNAME(a) (char*)(a)
-#else
-#define OPTNAME(a) (const char*)(a)
-#endif
-
-using namespace std;
-
-static KVStore *getStore(EPStats &st,
- const char *path,
- const char *strategyName,
- const char *shardPattern,
- const char *initFile = NULL) {
- db_type dbStrategy = multi_db;
-
- if (!SqliteKVStoreFactory::stringToType(strategyName, dbStrategy)) {
- cerr << "Unable to parse strategy type: " << strategyName << endl;
- exit(EX_USAGE);
- }
-
- size_t nVBuckets(1024);
- size_t dbShards(4);
- SqliteStrategy *sqliteInstance = NULL;
-
- switch (dbStrategy) {
- case multi_db:
- sqliteInstance = new MultiDBSingleTableSqliteStrategy(path,
- shardPattern,
- initFile,
- NULL, dbShards);
- break;
- case single_db:
- sqliteInstance = new SingleTableSqliteStrategy(path,
- initFile,
- NULL);
- break;
- case single_mt_db:
- sqliteInstance = new MultiTableSqliteStrategy(path,
- initFile,
- NULL, nVBuckets);
- break;
- case multi_mt_db:
- sqliteInstance = new ShardedMultiTableSqliteStrategy(path,
- shardPattern,
- initFile,
- NULL, nVBuckets,
- dbShards);
- break;
- case multi_mt_vb_db:
- sqliteInstance = new ShardedByVBucketSqliteStrategy(path,
- shardPattern,
- initFile,
- NULL, nVBuckets,
- dbShards);
- break;
- }
-
- return new StrategicSqlite3(st,
- shared_ptr<SqliteStrategy>(sqliteInstance));
-}
-
-class MutationVerifier : public Callback<mutation_result> {
-public:
- void callback(mutation_result &mutation) {
- assert(mutation.first == 1);
- }
-};
-
-class Mover : public Callback<GetValue> {
-public:
-
- Mover(KVStore *d, size_t re, bool kc, size_t ts) : dest(d),
- transferred(0),
- txnSize(ts),
- reportEvery(re),
- killCrlf(kc),
- inTxn(false) {
- assert(dest);
- assert(txnSize != 0);
- assert(reportEvery != 0);
-
- if (!enterTransaction()) {
- cout << "Failed to start a transaction" << endl;
- abort();
- }
-
- startTime = gethrtime();
- }
-
- ~Mover() {
- nextTransaction(false);
- cout << "Elapsed time=" << (gethrtime() - startTime)/1000000000 << " seconds."<< std::endl;
- }
-
- void callback(GetValue &gv) {
- Item *i = gv.getValue();
- adjust(&i);
- dest->set(*i, mv);
- delete i;
-
- if ((++transferred % txnSize) == 0) {
- nextTransaction(true);
- }
- if ((transferred % reportEvery) == 0) {
- cout << "." << flush;
- }
- }
-
- size_t getTransferred() {
- return transferred;
- }
-
- bool enterTransaction() {
- if (!inTxn) {
- inTxn = dest->begin();
- }
- return inTxn;
- }
-
- bool nextTransaction(bool next) {
- if (inTxn) {
- while (!dest->commit()) {
- cout << "Failed to commit a transaction. Sleep a while." << endl;
- sleep(1);
- }
- inTxn = false;
- }
- return next ? enterTransaction() : true;
- }
-
-private:
-
- void adjust(Item **i) {
- Item *input(*i);
- if (killCrlf) {
- const char *data = input->getData();
- assert(data[input->getNBytes() - 2] == '\r');
- assert(data[input->getNBytes() - 1] == '\n');
- *i = new Item(input->getKey(), input->getFlags(),
- input->getExptime(),
- data, input->getNBytes() - 2,
- 0, -1, input->getVBucketId());
- delete input;
- } else {
- input->setId(-1);
- }
- }
-
- MutationVerifier mv;
- KVStore *dest;
- size_t transferred;
- size_t txnSize;
- size_t reportEvery;
- bool killCrlf;
- bool inTxn;
- hrtime_t startTime;
-};
-
-static void usage(const char *cmd) {
- cerr << "Usage: " << cmd << " [args] srcPath destPath" << endl
- << endl
- << "Optional arguments:" << endl
- << " --src-strategy=someStrategy (default=multiDB)" << endl
- << " --src-pattern=shardPattern (default=%d/%b-%i.sqlite)" << endl
- << " --dest-strategy=someStrategy (default=multiMTVBDB)" << endl
- << " --dest-pattern=somePattern (default=%d/%b-%i.mb)" << endl
- << " --remove-crlf" << endl
- << " --txn-size=someNumber (default=10000)" << endl
- << " --report-every=someNumber (default=10000)" << endl
- << " --init-file=filepath (default=NULL)" << endl;
- exit(EX_USAGE);
-}
-
-int main(int argc, char **argv) {
- putenv(strdup("ALLOW_NO_STATS_UPDATE=yeah"));
- const char *cmd(argv[0]);
- const char *srcPath(NULL), *srcStrategy("multiDB");
- const char *destPath(NULL), *destStrategy("multiMTVBDB");
- const char *srcShardPattern("%d/%b-%i.sqlite");
- const char *destShardPattern("%d/%b-%i.mb");
- const char *initFile(NULL);
- size_t txnSize(10000), reportEvery(10000);
- int killCrlf(0);
-
- /* options descriptor */
- static struct option longopts[] = {
- { OPTNAME("src-strategy"), required_argument, NULL, 's' },
- { OPTNAME("src-pattern"), required_argument, NULL, 'p' },
- { OPTNAME("dest-strategy"), required_argument, NULL, 'S' },
- { OPTNAME("dest-pattern"), required_argument, NULL, 'P' },
- { OPTNAME("remove-crlf"), no_argument, &killCrlf, 'x' },
- { OPTNAME("txn-size"), required_argument, NULL, 't' },
- { OPTNAME("report-every"), required_argument, NULL, 'r' },
- { OPTNAME("init-file"), required_argument, NULL, 'i' },
- { NULL, 0, NULL, 0 }
- };
-
- int ch(0);
- while ((ch = getopt_long(argc, argv, "s:S:x", longopts, NULL)) != -1) {
- switch (ch) {
- case 's':
- srcStrategy = optarg;
- break;
- case 'p':
- srcShardPattern = optarg;
- break;
- case 'S':
- destStrategy = optarg;
- break;
- case 'P':
- destShardPattern = optarg;
- break;
- case 'i':
- initFile = optarg;
- break;
- case 't':
- txnSize = static_cast<size_t>(atoi(optarg));
- break;
- case 'r':
- reportEvery = static_cast<size_t>(atoi(optarg));
- break;
- case 0: // Path for automatically handled cases (e.g. remove-crlf)
- break;
- default:
- usage(cmd);
- }
- }
- argc -= optind;
- argv += optind;
-
- if (argc != 2) {
- usage(cmd);
- }
- srcPath = argv[0];
- destPath = argv[1];
-
- cout << "src = " << srcStrategy << "@" << srcPath << endl;
- cout << "dest = " << destStrategy << "@" << destPath << endl;
- EPStats srcStats, destStats;
-
- SqliteStrategy::disableSchemaCheck();
-
- KVStore *src(getStore(srcStats, srcPath,
- srcStrategy, srcShardPattern));
- KVStore *dest(getStore(destStats, destPath,
- destStrategy, destShardPattern, initFile));
-
- shared_ptr<Mover> mover(new Mover(dest, txnSize, static_cast<bool>(killCrlf), reportEvery));
- cout << "Each . represents " << reportEvery << " items moved." << endl;
- src->dump(mover);
- cout << endl << "Moved " << mover->getTransferred() << " items." << endl;
-
- return 0;
-}
View
2,733 src/embedded/sqlite3-shell.c
@@ -1,2733 +0,0 @@
-/*
-** 2001 September 15
-**
-** The author disclaims copyright to this source code. In place of
-** a legal notice, here is a blessing:
-**
-** May you do good and not evil.
-** May you find forgiveness for yourself and forgive others.
-** May you share freely, never taking more than you give.
-**
-*************************************************************************
-** This file contains code to implement the "sqlite" command line
-** utility for accessing SQLite databases.
-*/
-#if defined(_WIN32) || defined(WIN32)
-/* This needs to come before any includes for MSVC compiler */
-#define _CRT_SECURE_NO_WARNINGS
-#endif
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <assert.h>
-#include "sqlite3.h"
-#include <ctype.h>
-#include <stdarg.h>
-
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
-# include <signal.h>
-# if !defined(__RTP__) && !defined(_WRS_KERNEL)
-# include <pwd.h>
-# endif
-# include <unistd.h>
-# include <sys/types.h>
-#endif
-
-#ifdef __OS2__
-# include <unistd.h>
-#endif
-
-#if defined(HAVE_READLINE) && HAVE_READLINE==1
-# include <readline/readline.h>
-# include <readline/history.h>
-#else
-# define readline(p) local_getline(p,stdin)
-# define add_history(X)
-# define read_history(X)
-# define write_history(X)
-# define stifle_history(X)
-#endif
-
-#if defined(_WIN32) || defined(WIN32)
-# include <io.h>
-#define isatty(h) _isatty(h)
-#define access(f,m) _access((f),(m))
-#else
-/* Make sure isatty() has a prototype.
-*/
-extern int isatty();
-#endif
-
-#if defined(_WIN32_WCE)
-/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
- * thus we always assume that we have a console. That can be
- * overridden with the -batch command line option.
- */
-#define isatty(x) 1
-#endif
-
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
-#include <sys/time.h>
-#include <sys/resource.h>
-
-/* Saved resource information for the beginning of an operation */
-static struct rusage sBegin;
-
-/* True if the timer is enabled */
-static int enableTimer = 0;
-
-/*
-** Begin timing an operation
-*/
-static void beginTimer(void){
- if( enableTimer ){
- getrusage(RUSAGE_SELF, &sBegin);
- }
-}
-
-/* Return the difference of two time_structs in seconds */
-static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
- return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
- (double)(pEnd->tv_sec - pStart->tv_sec);
-}
-
-/*
-** Print the timing results.
-*/
-static void endTimer(void){
- if( enableTimer ){
- struct rusage sEnd;
- getrusage(RUSAGE_SELF, &sEnd);
- printf("CPU Time: user %f sys %f\n",
- timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
- timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
- }
-}
-
-#define BEGIN_TIMER beginTimer()
-#define END_TIMER endTimer()
-#define HAS_TIMER 1
-
-#elif (defined(_WIN32) || defined(WIN32))
-
-#include <windows.h>
-
-/* Saved resource information for the beginning of an operation */
-static HANDLE hProcess;
-static FILETIME ftKernelBegin;
-static FILETIME ftUserBegin;
-typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME);
-static GETPROCTIMES getProcessTimesAddr = NULL;
-
-/* True if the timer is enabled */
-static int enableTimer = 0;
-
-/*
-** Check to see if we have timer support. Return 1 if necessary
-** support found (or found previously).
-*/
-static int hasTimer(void){
- if( getProcessTimesAddr ){
- return 1;
- } else {
- /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions.
- ** See if the version we are running on has it, and if it does, save off
- ** a pointer to it and the current process handle.
- */
- hProcess = GetCurrentProcess();
- if( hProcess ){
- HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
- if( NULL != hinstLib ){
- getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
- if( NULL != getProcessTimesAddr ){
- return 1;
- }
- FreeLibrary(hinstLib);
- }
- }
- }
- return 0;
-}
-
-/*
-** Begin timing an operation
-*/
-static void beginTimer(void){
- if( enableTimer && getProcessTimesAddr ){
- FILETIME ftCreation, ftExit;
- getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin);
- }
-}
-
-/* Return the difference of two FILETIME structs in seconds */
-static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
- sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
- sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
- return (double) ((i64End - i64Start) / 10000000.0);
-}
-
-/*
-** Print the timing results.
-*/
-static void endTimer(void){
- if( enableTimer && getProcessTimesAddr){
- FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
- getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd);
- printf("CPU Time: user %f sys %f\n",
- timeDiff(&ftUserBegin, &ftUserEnd),
- timeDiff(&ftKernelBegin, &ftKernelEnd));
- }
-}
-
-#define BEGIN_TIMER beginTimer()
-#define END_TIMER endTimer()
-#define HAS_TIMER hasTimer()
-
-#else
-#define BEGIN_TIMER
-#define END_TIMER
-#define HAS_TIMER 0
-#endif
-
-/*
-** Used to prevent warnings about unused parameters
-*/
-#define UNUSED_PARAMETER(x) (void)(x)
-
-/*
-** If the following flag is set, then command execution stops
-** at an error if we are not interactive.
-*/
-static int bail_on_error = 0;
-
-/*
-** Threat stdin as an interactive input if the following variable
-** is true. Otherwise, assume stdin is connected to a file or pipe.
-*/
-static int stdin_is_interactive = 1;
-
-/*
-** The following is the open SQLite database. We make a pointer
-** to this database a static variable so that it can be accessed
-** by the SIGINT handler to interrupt database processing.
-*/
-static sqlite3 *db = 0;
-
-/*
-** True if an interrupt (Control-C) has been received.
-*/
-static volatile int seenInterrupt = 0;
-
-/*
-** This is the name of our program. It is set in main(), used
-** in a number of other places, mostly for error messages.
-*/
-static char *Argv0;
-
-/*
-** Prompt strings. Initialized in main. Settable with
-** .prompt main continue
-*/
-static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
-static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
-
-/*
-** Write I/O traces to the following stream.
-*/
-#ifdef SQLITE_ENABLE_IOTRACE
-static FILE *iotrace = 0;
-#endif
-
-/*
-** This routine works like printf in that its first argument is a
-** format string and subsequent arguments are values to be substituted
-** in place of % fields. The result of formatting this string
-** is written to iotrace.
-*/
-#ifdef SQLITE_ENABLE_IOTRACE
-static void iotracePrintf(const char *zFormat, ...){
- va_list ap;
- char *z;
- if( iotrace==0 ) return;
- va_start(ap, zFormat);
- z = sqlite3_vmprintf(zFormat, ap);
- va_end(ap);
- fprintf(iotrace, "%s", z);
- sqlite3_free(z);
-}
-#endif
-
-
-/*
-** Determines if a string is a number of not.
-*/
-static int isNumber(const char *z, int *realnum){
- if( *z=='-' || *z=='+' ) z++;
- if( !isdigit(*z) ){
- return 0;
- }
- z++;
- if( realnum ) *realnum = 0;
- while( isdigit(*z) ){ z++; }
- if( *z=='.' ){
- z++;
- if( !isdigit(*z) ) return 0;
- while( isdigit(*z) ){ z++; }
- if( realnum ) *realnum = 1;
- }
- if( *z=='e' || *z=='E' ){
- z++;
- if( *z=='+' || *z=='-' ) z++;
- if( !isdigit(*z) ) return 0;
- while( isdigit(*z) ){ z++; }
- if( realnum ) *realnum = 1;
- }
- return *z==0;
-}
-
-/*
-** A global char* and an SQL function to access its current value
-** from within an SQL statement. This program used to use the
-** sqlite_exec_printf() API to substitue a string into an SQL statement.
-** The correct way to do this with sqlite3 is to use the bind API, but
-** since the shell is built around the callback paradigm it would be a lot
-** of work. Instead just use this hack, which is quite harmless.
-*/
-static const char *zShellStatic = 0;
-static void shellstaticFunc(
- sqlite3_context *context,
- int argc,
- sqlite3_value **argv
-){
- assert( 0==argc );
- assert( zShellStatic );
- UNUSED_PARAMETER(argc);
- UNUSED_PARAMETER(argv);
- sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
-}
-
-
-/*
-** This routine reads a line of text from FILE in, stores
-** the text in memory obtained from malloc() and returns a pointer
-** to the text. NULL is returned at end of file, or if malloc()
-** fails.
-**
-** The interface is like "readline" but no command-line editing
-** is done.
-*/
-static char *local_getline(char *zPrompt, FILE *in){
- char *zLine;
- int nLine;
- int n;
- int eol;
-
- if( zPrompt && *zPrompt ){
- printf("%s",zPrompt);
- fflush(stdout);
- }
- nLine = 100;
- zLine = malloc( nLine );
- if( zLine==0 ) return 0;
- n = 0;
- eol = 0;
- while( !eol ){
- if( n+100>nLine ){
- nLine = nLine*2 + 100;
- zLine = realloc(zLine, nLine);
- if( zLine==0 ) return 0;
- }
- if( fgets(&zLine[n], nLine - n, in)==0 ){
- if( n==0 ){
- free(zLine);
- return 0;
- }
- zLine[n] = 0;
- eol = 1;
- break;
- }
- while( zLine[n] ){ n++; }
- if( n>0 && zLine[n-1]=='\n' ){
- n--;
- if( n>0 && zLine[n-1]=='\r' ) n--;
- zLine[n] = 0;
- eol = 1;
- }
- }
- zLine = realloc( zLine, n+1 );
- return zLine;
-}
-
-/*
-** Retrieve a single line of input text.
-**
-** zPrior is a string of prior text retrieved. If not the empty
-** string, then issue a continuation prompt.
-*/
-static char *one_input_line(const char *zPrior, FILE *in){
- char *zPrompt;
- char *zResult;
- if( in!=0 ){
- return local_getline(0, in);
- }
- if( zPrior && zPrior[0] ){
- zPrompt = continuePrompt;
- }else{
- zPrompt = mainPrompt;
- }
- zResult = readline(zPrompt);
-#if defined(HAVE_READLINE) && HAVE_READLINE==1
- if( zResult && *zResult ) add_history(zResult);
-#endif
- return zResult;
-}
-
-struct previous_mode_data {
- int valid; /* Is there legit data in here? */
- int mode;
- int showHeader;
- int colWidth[100];
-};
-
-/*
-** An pointer to an instance of this structure is passed from
-** the main program to the callback. This is used to communicate
-** state and mode information.
-*/
-struct callback_data {
- sqlite3 *db; /* The database */
- int echoOn; /* True to echo input commands */
- int statsOn; /* True to display memory stats before each finalize */
- int cnt; /* Number of records displayed so far */
- FILE *out; /* Write results here */
- int mode; /* An output mode setting */
- int writableSchema; /* True if PRAGMA writable_schema=ON */
- int showHeader; /* True to show column names in List or Column mode */
- char *zDestTable; /* Name of destination table when MODE_Insert */
- char separator[20]; /* Separator character for MODE_List */
- int colWidth[100]; /* Requested width of each column when in column mode*/
- int actualWidth[100]; /* Actual width of each column */
- char nullvalue[20]; /* The text to print when a NULL comes back from
- ** the database */
- struct previous_mode_data explainPrev;
- /* Holds the mode information just before
- ** .explain ON */
- char outfile[FILENAME_MAX]; /* Filename for *out */
- const char *zDbFilename; /* name of the database file */
- sqlite3_stmt *pStmt; /* Current statement if any. */
- FILE *pLog; /* Write log output here */
-};
-
-/*
-** These are the allowed modes.
-*/
-#define MODE_Line 0 /* One column per line. Blank line between records */
-#define MODE_Column 1 /* One record per line in neat columns */
-#define MODE_List 2 /* One record per line with a separator */
-#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
-#define MODE_Html 4 /* Generate an XHTML table */
-#define MODE_Insert 5 /* Generate SQL "insert" statements */
-#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */
-#define MODE_Csv 7 /* Quote strings, numbers are plain */
-#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */
-
-static const char *modeDescr[] = {
- "line",
- "column",
- "list",
- "semi",
- "html",
- "insert",
- "tcl",
- "csv",
- "explain",
-};
-
-/*
-** Number of elements in an array
-*/
-#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
-
-/*
-** Compute a string length that is limited to what can be stored in
-** lower 30 bits of a 32-bit signed integer.
-*/
-static int strlen30(const char *z){
- const char *z2 = z;
- while( *z2 ){ z2++; }
- return 0x3fffffff & (int)(z2 - z);
-}
-
-/*
-** A callback for the sqlite3_log() interface.
-*/
-static void shellLog(void *pArg, int iErrCode, const char *zMsg){
- struct callback_data *p = (struct callback_data*)pArg;
- if( p->pLog==0 ) return;
- fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
- fflush(p->pLog);
-}
-
-/*
-** Output the given string as a hex-encoded blob (eg. X'1234' )
-*/
-static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
- int i;
- char *zBlob = (char *)pBlob;
- fprintf(out,"X'");
- for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]); }
- fprintf(out,"'");
-}
-
-/*
-** Output the given string as a quoted string using SQL quoting conventions.
-*/
-static void output_quoted_string(FILE *out, const char *z){
- int i;
- int nSingle = 0;
- for(i=0; z[i]; i++){
- if( z[i]=='\'' ) nSingle++;
- }
- if( nSingle==0 ){
- fprintf(out,"'%s'",z);
- }else{
- fprintf(out,"'");
- while( *z ){
- for(i=0; z[i] && z[i]!='\''; i++){}
- if( i==0 ){
- fprintf(out,"''");
- z++;
- }else if( z[i]=='\'' ){
- fprintf(out,"%.*s''",i,z);
- z += i+1;
- }else{
- fprintf(out,"%s",z);
- break;
- }
- }
- fprintf(out,"'");
- }
-}
-
-/*
-** Output the given string as a quoted according to C or TCL quoting rules.
-*/
-static void output_c_string(FILE *out, const char *z){
- unsigned int c;
- fputc('"', out);
- while( (c = *(z++))!=0 ){
- if( c=='\\' ){
- fputc(c, out);
- fputc(c, out);
- }else if( c=='\t' ){
- fputc('\\', out);
- fputc('t', out);
- }else if( c=='\n' ){
- fputc('\\', out);
- fputc('n', out);
- }else if( c=='\r' ){
- fputc('\\', out);
- fputc('r', out);
- }else if( !isprint(c) ){
- fprintf(out, "\\%03o", c&0xff);
- }else{
- fputc(c, out);
- }
- }
- fputc('"', out);
-}
-
-/*
-** Output the given string with characters that are special to
-** HTML escaped.
-*/
-static void output_html_string(FILE *out, const char *z){
- int i;
- while( *z ){
- for(i=0; z[i]
- && z[i]!='<'
- && z[i]!='&'
- && z[i]!='>'
- && z[i]!='\"'
- && z[i]!='\'';
- i++){}
- if( i>0 ){
- fprintf(out,"%.*s",i,z);
- }
- if( z[i]=='<' ){
- fprintf(out,"&lt;");
- }else if( z[i]=='&' ){
- fprintf(out,"&amp;");
- }else if( z[i]=='>' ){
- fprintf(out,"&gt;");
- }else if( z[i]=='\"' ){
- fprintf(out,"&quot;");
- }else if( z[i]=='\'' ){
- fprintf(out,"&#39;");
- }else{
- break;
- }
- z += i + 1;
- }
-}
-
-/*
-** If a field contains any character identified by a 1 in the following
-** array, then the string must be quoted for CSV.
-*/
-static const char needCsvQuote[] = {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-};
-
-/*
-** Output a single term of CSV. Actually, p->separator is used for
-** the separator, which may or may not be a comma. p->nullvalue is
-** the null value. Strings are quoted using ANSI-C rules. Numbers
-** appear outside of quotes.
-*/
-static void output_csv(struct callback_data *p, const char *z, int bSep){
- FILE *out = p->out;
- if( z==0 ){
- fprintf(out,"%s",p->nullvalue);
- }else{
- int i;
- int nSep = strlen30(p->separator);
- for(i=0; z[i]; i++){
- if( needCsvQuote[((unsigned char*)z)[i]]
- || (z[i]==p->separator[0] &&
- (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){
- i = 0;
- break;
- }
- }
- if( i==0 ){
- putc('"', out);
- for(i=0; z[i]; i++){
- if( z[i]=='"' ) putc('"', out);
- putc(z[i], out);
- }
- putc('"', out);
- }else{
- fprintf(out, "%s", z);
- }
- }
- if( bSep ){
- fprintf(p->out, "%s", p->separator);
- }
-}
-
-#ifdef SIGINT
-/*
-** This routine runs when the user presses Ctrl-C
-*/
-static void interrupt_handler(int NotUsed){
- UNUSED_PARAMETER(NotUsed);
- seenInterrupt = 1;
- if( db ) sqlite3_interrupt(db);
-}
-#endif
-
-/*
-** This is the callback routine that the shell
-** invokes for each row of a query result.
-*/
-static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){
- int i;
- struct callback_data *p = (struct callback_data*)pArg;
-
- switch( p->mode ){
- case MODE_Line: {
- int w = 5;
- if( azArg==0 ) break;
- for(i=0; i<nArg; i++){
- int len = strlen30(azCol[i] ? azCol[i] : "");
- if( len>w ) w = len;
- }
- if( p->cnt++>0 ) fprintf(p->out,"\n");
- for(i=0; i<nArg; i++){
- fprintf(p->out,"%*s = %s\n", w, azCol[i],
- azArg[i] ? azArg[i] : p->nullvalue);
- }
- break;
- }
- case MODE_Explain:
- case MODE_Column: {
- if( p->cnt++==0 ){
- for(i=0; i<nArg; i++){
- int w, n;
- if( i<ArraySize(p->colWidth) ){
- w = p->colWidth[i];
- }else{
- w = 0;
- }
- if( w<=0 ){
- w = strlen30(azCol[i] ? azCol[i] : "");
- if( w<10 ) w = 10;
- n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
- if( w<n ) w = n;
- }
- if( i<ArraySize(p->actualWidth) ){
- p->actualWidth[i] = w;
- }
- if( p->showHeader ){
- fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " ");
- }
- }
- if( p->showHeader ){
- for(i=0; i<nArg; i++){
- int w;
- if( i<ArraySize(p->actualWidth) ){
- w = p->actualWidth[i];
- }else{
- w = 10;
- }
- fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
- "----------------------------------------------------------",
- i==nArg-1 ? "\n": " ");
- }
- }
- }
- if( azArg==0 ) break;
- for(i=0; i<nArg; i++){
- int w;
- if( i<ArraySize(p->actualWidth) ){
- w = p->actualWidth[i];
- }else{
- w = 10;
- }
- if( p->mode==MODE_Explain && azArg[i] &&
- strlen30(azArg[i])>w ){
- w = strlen30(azArg[i]);
- }
- fprintf(p->out,"%-*.*s%s",w,w,
- azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
- }
- break;
- }
- case MODE_Semi:
- case MODE_List: {
- if( p->cnt++==0 && p->showHeader ){
- for(i=0; i<nArg; i++){
- fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);
- }
- }
- if( azArg==0 ) break;
- for(i=0; i<nArg; i++){
- char *z = azArg[i];
- if( z==0 ) z = p->nullvalue;
- fprintf(p->out, "%s", z);
- if( i<nArg-1 ){
- fprintf(p->out, "%s", p->separator);
- }else if( p->mode==MODE_Semi ){
- fprintf(p->out, ";\n");
- }else{
- fprintf(p->out, "\n");
- }
- }
- break;
- }
- case MODE_Html: {
- if( p->cnt++==0 && p->showHeader ){
- fprintf(p->out,"<TR>");
- for(i=0; i<nArg; i++){
- fprintf(p->out,"<TH>");
- output_html_string(p->out, azCol[i]);
- fprintf(p->out,"</TH>\n");
- }
- fprintf(p->out,"</TR>\n");
- }
- if( azArg==0 ) break;
- fprintf(p->out,"<TR>");
- for(i=0; i<nArg; i++){
- fprintf(p->out,"<TD>");
- output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
- fprintf(p->out,"</TD>\n");
- }
- fprintf(p->out,"</TR>\n");
- break;
- }
- case MODE_Tcl: {
- if( p->cnt++==0 && p->showHeader ){
- for(i=0; i<nArg; i++){
- output_c_string(p->out,azCol[i] ? azCol[i] : "");
- fprintf(p->out, "%s", p->separator);
- }
- fprintf(p->out,"\n");
- }
- if( azArg==0 ) break;
- for(i=0; i<nArg; i++){
- output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
- fprintf(p->out, "%s", p->separator);
- }
- fprintf(p->out,"\n");
- break;
- }
- case MODE_Csv: {
- if( p->cnt++==0 && p->showHeader ){
- for(i=0; i<nArg; i++){
- output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
- }
- fprintf(p->out,"\n");
- }
- if( azArg==0 ) break;
- for(i=0; i<nArg; i++){
- output_csv(p, azArg[i], i<nArg-1);
- }
- fprintf(p->out,"\n");
- break;
- }
- case MODE_Insert: {
- p->cnt++;
- if( azArg==0 ) break;
- fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);
- for(i=0; i<nArg; i++){
- char *zSep = i>0 ? ",": "";
- if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
- fprintf(p->out,"%sNULL",zSep);
- }else if( aiType && aiType[i]==SQLITE_TEXT ){
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
- output_quoted_string(p->out, azArg[i]);
- }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){
- fprintf(p->out,"%s%s",zSep, azArg[i]);
- }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
- const void *pBlob = sqlite3_column_blob(p->pStmt, i);
- int nBlob = sqlite3_column_bytes(p->pStmt, i);
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
- output_hex_blob(p->out, pBlob, nBlob);
- }else if( isNumber(azArg[i], 0) ){
- fprintf(p->out,"%s%s",zSep, azArg[i]);
- }else{
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
- output_quoted_string(p->out, azArg[i]);
- }
- }
- fprintf(p->out,");\n");
- break;
- }
- }
- return 0;
-}
-
-/*
-** This is the callback routine that the SQLite library
-** invokes for each row of a query result.
-*/
-static int callback(void *pArg, int nArg, char **azArg, char **azCol){
- /* since we don't have type info, call the shell_callback with a NULL value */
- return shell_callback(pArg, nArg, azArg, azCol, NULL);
-}
-
-/*
-** Set the destination table field of the callback_data structure to
-** the name of the table given. Escape any quote characters in the
-** table name.
-*/
-static void set_table_name(struct callback_data *p, const char *zName){
- int i, n;
- int needQuote;
- char *z;
-
- if( p->zDestTable ){
- free(p->zDestTable);
- p->zDestTable = 0;
- }
- if( zName==0 ) return;
- needQuote = !isalpha((unsigned char)*zName) && *zName!='_';
- for(i=n=0; zName[i]; i++, n++){
- if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){
- needQuote = 1;
- if( zName[i]=='\'' ) n++;
- }
- }
- if( needQuote ) n += 2;
- z = p->zDestTable = malloc( n+1 );
- if( z==0 ){
- fprintf(stderr,"Error: out of memory\n");
- exit(1);
- }
- n = 0;
- if( needQuote ) z[n++] = '\'';
- for(i=0; zName[i]; i++){
- z[n++] = zName[i];
- if( zName[i]=='\'' ) z[n++] = '\'';
- }
- if( needQuote ) z[n++] = '\'';
- z[n] = 0;
-}
-
-/* zIn is either a pointer to a NULL-terminated string in memory obtained
-** from malloc(), or a NULL pointer. The string pointed to by zAppend is
-** added to zIn, and the result returned in memory obtained from malloc().
-** zIn, if it was not NULL, is freed.
-**
-** If the third argument, quote, is not '\0', then it is used as a
-** quote character for zAppend.
-*/
-static char *appendText(char *zIn, char const *zAppend, char quote){
- int len;
- int i;
- int nAppend = strlen30(zAppend);
- int nIn = (zIn?strlen30(zIn):0);
-
- len = nAppend+nIn+1;
- if( quote ){
- len += 2;
- for(i=0; i<nAppend; i++){
- if( zAppend[i]==quote ) len++;
- }
- }
-
- zIn = (char *)realloc(zIn, len);
- if( !zIn ){
- return 0;
- }
-
- if( quote ){
- char *zCsr = &zIn[nIn];
- *zCsr++ = quote;
- for(i=0; i<nAppend; i++){
- *zCsr++ = zAppend[i];
- if( zAppend[i]==quote ) *zCsr++ = quote;
- }
- *zCsr++ = quote;
- *zCsr++ = '\0';
- assert( (zCsr-zIn)==len );
- }else{
- memcpy(&zIn[nIn], zAppend, nAppend);
- zIn[len-1] = '\0';
- }
-
- return zIn;
-}
-
-
-/*
-** Execute a query statement that has a single result column. Print
-** that result column on a line by itself with a semicolon terminator.
-**
-** This is used, for example, to show the schema of the database by
-** querying the SQLITE_MASTER table.
-*/
-static int run_table_dump_query(
- FILE *out, /* Send output here */
- sqlite3 *db, /* Database to query */
- const char *zSelect, /* SELECT statement to extract content */
- const char *zFirstRow /* Print before first row, if not NULL */
-){
- sqlite3_stmt *pSelect;
- int rc;
- rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
- if( rc!=SQLITE_OK || !pSelect ){
- return rc;
- }
- rc = sqlite3_step(pSelect);
- while( rc==SQLITE_ROW ){
- if( zFirstRow ){
- fprintf(out, "%s", zFirstRow);
- zFirstRow = 0;
- }
- fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
- rc = sqlite3_step(pSelect);
- }
- return sqlite3_finalize(pSelect);
-}
-
-/*
-** Allocate space and save off current error string.
-*/
-static char *save_err_msg(
- sqlite3 *db /* Database to query */
-){
- int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
- char *zErrMsg = sqlite3_malloc(nErrMsg);
- if( zErrMsg ){
- memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
- }
- return zErrMsg;
-}
-
-/*
-** Display memory stats.
-*/
-static int display_stats(
- sqlite3 *db, /* Database to query */
- struct callback_data *pArg, /* Pointer to struct callback_data */
- int bReset /* True to reset the stats */
-){
- int iCur;
- int iHiwtr;
-
- if( pArg && pArg->out ){
-
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr);
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Number of Allocations: %d (max %d)\n", iCur, iHiwtr);
-/*
-** Not currently used by the CLI.
-** iHiwtr = iCur = -1;
-** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
-** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr);
-*/
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
-/*
-** Not currently used by the CLI.
-** iHiwtr = iCur = -1;
-** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
-** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr);
-*/
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr);
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr);
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr);
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr);
-#ifdef YYTRACKMAXSTACKDEPTH
- iHiwtr = iCur = -1;
- sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr);
-#endif
- }
-
- if( pArg && pArg->out && db ){
- iHiwtr = iCur = -1;
- sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
- iHiwtr = iCur = -1;
- sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
- iHiwtr = iCur = -1;
- sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
- iHiwtr = iCur = -1;
- sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
- }
-
- if( pArg && pArg->out && db && pArg->pStmt ){
- iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset);
- fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
- iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
- fprintf(pArg->out, "Sort Operations: %d\n", iCur);
- iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset);
- fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
- }
-
- return 0;
-}
-
-/*
-** Execute a statement or set of statements. Print
-** any result rows/columns depending on the current mode
-** set via the supplied callback.
-**
-** This is very similar to SQLite's built-in sqlite3_exec()
-** function except it takes a slightly different callback
-** and callback data argument.
-*/
-static int shell_exec(
- sqlite3 *db, /* An open database */
- const char *zSql, /* SQL to be evaluated */
- int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
- /* (not the same as sqlite3_exec) */
- struct callback_data *pArg, /* Pointer to struct callback_data */
- char **pzErrMsg /* Error msg written here */
-){
- sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
- int rc = SQLITE_OK; /* Return Code */
- const char *zLeftover; /* Tail of unprocessed SQL */
-
- if( pzErrMsg ){
- *pzErrMsg = NULL;
- }
-
- while( zSql[0] && (SQLITE_OK == rc) ){
- rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
- if( SQLITE_OK != rc ){
- if( pzErrMsg ){
- *pzErrMsg = save_err_msg(db);
- }
- }else{
- if( !pStmt ){
- /* this happens for a comment or white-space */
- zSql = zLeftover;
- while( isspace(zSql[0]) ) zSql++;
- continue;
- }
-
- /* save off the prepared statment handle and reset row count */
- if( pArg ){
- pArg->pStmt = pStmt;
- pArg->cnt = 0;
- }
-
- /* echo the sql statement if echo on */
- if( pArg && pArg->echoOn ){
- const char *zStmtSql = sqlite3_sql(pStmt);
- fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
- }
-
- /* perform the first step. this will tell us if we
- ** have a result set or not and how wide it is.
- */
- rc = sqlite3_step(pStmt);
- /* if we have a result set... */
- if( SQLITE_ROW == rc ){
- /* if we have a callback... */
- if( xCallback ){
- /* allocate space for col name ptr, value ptr, and type */
- int nCol = sqlite3_column_count(pStmt);
- void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1);
- if( !pData ){
- rc = SQLITE_NOMEM;
- }else{
- char **azCols = (char **)pData; /* Names of result columns */
- char **azVals = &azCols[nCol]; /* Results */
- int *aiTypes = (int *)&azVals[nCol]; /* Result types */
- int i;
- assert(sizeof(int) <= sizeof(char *));
- /* save off ptrs to column names */
- for(i=0; i<nCol; i++){
- azCols[i] = (char *)sqlite3_column_name(pStmt, i);
- }
- do{
- /* extract the data and data types */
- for(i=0; i<nCol; i++){
- azVals[i] = (char *)sqlite3_column_text(pStmt, i);
- aiTypes[i] = sqlite3_column_type(pStmt, i);
- if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
- rc = SQLITE_NOMEM;
- break; /* from for */
- }
- } /* end for */
-
- /* if data and types extracted successfully... */
- if( SQLITE_ROW == rc ){
- /* call the supplied callback with the result row data */
- if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
- rc = SQLITE_ABORT;
- }else{
- rc = sqlite3_step(pStmt);
- }
- }
- } while( SQLITE_ROW == rc );
- sqlite3_free(pData);
- }
- }else{
- do{
- rc = sqlite3_step(pStmt);
- } while( rc == SQLITE_ROW );
- }
- }
-
- /* print usage stats if stats on */
- if( pArg && pArg->statsOn ){
- display_stats(db, pArg, 0);
- }
-
- /* Finalize the statement just executed. If this fails, save a
- ** copy of the error message. Otherwise, set zSql to point to the
- ** next statement to execute. */
- rc = sqlite3_finalize(pStmt);
- if( rc==SQLITE_OK ){
- zSql = zLeftover;
- while( isspace(zSql[0]) ) zSql++;
- }else if( pzErrMsg ){
- *pzErrMsg = save_err_msg(db);
- }
-
- /* clear saved stmt handle */
- if( pArg ){
- pArg->pStmt = NULL;
- }
- }
- } /* end while */
-
- return rc;
-}
-
-
-/*
-** This is a different callback routine used for dumping the database.
-** Each row received by this callback consists of a table name,
-** the table type ("index" or "table") and SQL to create the table.
-** This routine should print text sufficient to recreate the table.
-*/
-static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
- int rc;
- const char *zTable;
- const char *zType;
- const char *zSql;
- const char *zPrepStmt = 0;
- struct callback_data *p = (struct callback_data *)pArg;
-
- UNUSED_PARAMETER(azCol);
- if( nArg!=3 ) return 1;
- zTable = azArg[0];
- zType = azArg[1];
- zSql = azArg[2];
-
- if( strcmp(zTable, "sqlite_sequence")==0 ){
- zPrepStmt = "DELETE FROM sqlite_sequence;\n";
- }else if( strcmp(zTable, "sqlite_stat1")==0 ){
- fprintf(p->out, "ANALYZE sqlite_master;\n");
- }else if( strncmp(zTable, "sqlite_", 7)==0 ){
- return 0;
- }else