Skip to content

Instantly share code, notes, and snippets.

@joenoon
Last active August 29, 2015 14:23
Show Gist options
  • Save joenoon/c36819d7c03710b4e931 to your computer and use it in GitHub Desktop.
Save joenoon/c36819d7c03710b4e931 to your computer and use it in GitHub Desktop.
CLI wrapper to simplify outputting a doc or all docs from a cblite db.
#!/bin/bash
#
# CLI wrapper to simplify outputting a doc or all docs from a cblite db.
#
# The MIT License (MIT)
#
# Copyright (c) 2015, Joe Noon
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
usage() {
echo "Usage: $0 FILENAME [OPTIONS]"
echo "FILENAME is the name of a .cblite database (SQLite)."
echo "OPTIONS include:"
echo " -id DOCID get a specific doc id"
echo " -a get all revisions, not just current"
echo " -c only count"
}
if [ -z "$1" ]; then
usage
exit 1
fi
file="$1"
shift
while true
do
case $1 in
-id) # get version
docid=$2
shift; shift
;;
-a) # use verbose mode
alldocs=true
shift
;;
-c) # use verbose mode
count=true
shift
;;
-*)
usage
exit 1
;;
*)
break
;;
esac
done
if [ -n "$docid" ]; then
docsql="AND docid = '${docid}'"
fi
if [ -z "$alldocs" ]; then
currentsql="AND current = 1"
fi
criteria="$docsql $currentsql"
sql="DROP TABLE IF EXISTS tmp_cbldocs_sh; CREATE TEMP TABLE tmp_cbldocs_sh AS SELECT json as json, docs.docid COLLATE NOCASE as docid, revid COLLATE NOCASE as revid, current, deleted FROM revs INNER JOIN docs WHERE docs.doc_id = revs.doc_id COLLATE NOCASE $criteria ;"
if [ -n "$count" ]; then
sql="$sql select count(*) count from tmp_cbldocs_sh;"
else
sql="$sql select * from tmp_cbldocs_sh;"
fi
sqlite3 -line "$file" "$sql"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment