· 7 years ago · Dec 28, 2018, 08:12 PM
1#!/usr/bin/env ruby
2
3# The goal here is a script that can be called like this:
4#
5# sqlite-now-mem users.tsv,users,id,login,email products.tsv,products,id,name
6#
7# For each comma-separated tuple passed are arguments, the input file
8# will be loaded into the table, and any following values are the
9# names of columns for which indexes will be created.
10#
11# The --file=FILE flag can also be specified. If FILE doesn't exist it
12# will be created. If it does, data will be imported into it,
13# including if when the table already exists, rows will be appended.
14#
15# sqlite-now-mem --file=website.sqlite users.tsv,users,id,login,email products.tsv,products,id,name
16
17require "optimist"
18
19opts = Optimist::options do
20 opt :file, "SQLite database filename. Optional. If not specified, db is in memory.", :type => :string
21end
22
23p opts
24
25sqlite_cmds = [
26 ".timer on",
27 ".mode tab",
28]
29
30ARGV.each do |definition|
31 file, table, *index_cols = definition.split(",")
32 p [file, table, index_cols]
33 sqlite_cmds << ".import #{file} #{table}"
34 index_cols.each do |index_col|
35 sqlite_cmds << "create index index_#{table}_#{index_col} on #{table} (#{index_col});"
36 end
37end
38
39# final_command = "sqlite3 -header" + " " + sqlite_cmds.map { |cmd| %[-cmd "#{cmd}"] }.join(" ") + " " + %{-cmd ".mode column"}
40
41final_system_cmd_elements = [
42 "sqlite3",
43 "-interactive",
44 "-header",
45 *sqlite_cmds.map { |cmd| %[-cmd "#{cmd}"] },
46 %{-cmd ".mode column"},
47 *opts[:file],
48]
49
50puts final_system_cmd_elements.join(" \\\n ")
51
52system final_system_cmd_elements.join(" \\\n ")