hsql 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Rakefile +1 -1
- data/bin/hsql +8 -69
- data/examples/simple.sql +2 -1
- data/lib/hsql/command_line.rb +94 -0
- data/lib/hsql/file.rb +15 -2
- data/lib/hsql/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82602edd7ada1606c4d57b9295082549d459f219
|
4
|
+
data.tar.gz: 127987efbe6ea8b8b35c29cff1ee19e79fcf3343
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0202059f7b57dba2b6fa56278cf534748f191c46454d43634619e8e6653dd19fa518caf1c11d929bfc8f435804763f77f4f21539f4ea6ebc20ed7e36322137b7
|
7
|
+
data.tar.gz: 5902bb40c3f183f693434ee7c0789812456ec19ce40e08b82d76f9010be2b4ab432e0e042ef4d2f805bac3c3a077da9494ae5c9b89a87d3ffbc348e4bdcf5c2f
|
data/.rubocop.yml
CHANGED
data/Rakefile
CHANGED
data/bin/hsql
CHANGED
@@ -1,75 +1,12 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
require_relative '../lib/hsql/command_line'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'active_support/time'
|
7
|
-
|
8
|
-
options = {}
|
9
|
-
option_parser = OptionParser.new do |opts|
|
10
|
-
opts.banner = <<-BANNER
|
11
|
-
Usage: #{File.basename(__FILE__)} file [environment]
|
12
|
-
|
13
|
-
Arguments
|
14
|
-
file: Any *.sql file. If it has a YAML header (ending in three hyphens) the metadata will be processed;
|
15
|
-
|
16
|
-
Options
|
17
|
-
BANNER
|
18
|
-
|
19
|
-
opts.on('-d DATE',
|
20
|
-
'--date DATE',
|
21
|
-
'--timestamp DATE', 'The time that the SQL will consider to be "{{{now}}}".') do |option|
|
22
|
-
options[:timestamp] = Time.parse(option) if option
|
23
|
-
end
|
24
|
-
opts.on('-e ENV',
|
25
|
-
'--env ENV',
|
26
|
-
'Which key of the YAML header "data:" key you want to interpolate.') do |option|
|
27
|
-
options[:environment] = option
|
28
|
-
end
|
29
|
-
opts.on('-y', '--yaml', 'Output just the metadata for this file as YAML') do |_option|
|
30
|
-
options[:meta_only] = 'yaml'
|
31
|
-
end
|
32
|
-
opts.on('-j', '--json', 'Output just the metadata for this file as JSON') do |_option|
|
33
|
-
options[:meta_only] = 'json'
|
34
|
-
end
|
35
|
-
opts.on_tail('-v', '--version', 'Show the current version of HSQL') do |_option|
|
36
|
-
require_relative '../lib/hsql/version'
|
37
|
-
puts HSQL::VERSION
|
38
|
-
exit 1
|
39
|
-
end
|
40
|
-
opts.on('-h', '--help', 'Show the full help documentation') do |_option|
|
41
|
-
require_relative '../lib/hsql/data'
|
42
|
-
<<-HELP
|
43
|
-
TEMPLATE
|
44
|
-
|
45
|
-
You can use the Mustache syntax (three curly braces) to interpolate any of your
|
46
|
-
'data' into your SQL. You specify the data in the YAML header like so:
|
47
|
-
|
48
|
-
data:
|
49
|
-
production: # whatever you want to be interpolated into your SQL
|
50
|
-
name: Alison # when you pass 'production' as the environment argument
|
51
|
-
development:
|
52
|
-
name: Kailey
|
53
|
-
---
|
54
|
-
SELECT * FROM users WHERE name = '{{{name}}}'
|
55
|
-
|
56
|
-
|
57
|
-
There are some common date values available to you at all times, without having
|
58
|
-
to be defined in the YAML header:
|
59
|
-
|
60
|
-
#{HSQL::Data.for_humans}
|
61
|
-
|
62
|
-
For more details, run:
|
63
|
-
open https://github.com/JackDanger/hsql
|
64
|
-
HELP
|
65
|
-
end
|
66
|
-
end
|
67
|
-
option_parser.parse!
|
68
|
-
|
69
|
-
filename = ARGV.first
|
4
|
+
command_line = HSQL::CommandLine.new
|
5
|
+
options = command_line.options
|
6
|
+
filename = command_line.filename
|
70
7
|
|
71
8
|
unless filename
|
72
|
-
puts option_parser
|
9
|
+
puts command_line.option_parser
|
73
10
|
exit 1
|
74
11
|
end
|
75
12
|
|
@@ -88,8 +25,10 @@ if 'yaml' == options[:meta_only]
|
|
88
25
|
elsif 'json' == options[:meta_only]
|
89
26
|
puts file.to_json
|
90
27
|
else
|
28
|
+
# Runs the whole SQL string through the parser to find the individual queries
|
29
|
+
# and then deparses it
|
91
30
|
file.queries.each do |query|
|
92
|
-
|
31
|
+
puts '-- Parsed and deparsed SQL:' if command_line.options[:verbose]
|
93
32
|
puts query
|
94
33
|
end
|
95
34
|
end
|
data/examples/simple.sql
CHANGED
@@ -14,5 +14,6 @@ data:
|
|
14
14
|
update_condition: WHERE 1 <> 1
|
15
15
|
---
|
16
16
|
INSERT INTO {{{output_table}}} -- this query is joined to one line
|
17
|
-
SELECT COUNT(*)
|
17
|
+
SELECT COUNT(*)
|
18
|
+
FROM interesting_information; -- and the comments get stripped
|
18
19
|
UPDATE summaries_performed SET complete = 1 {{{update_condition}}};
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
2
|
+
require 'optparse'
|
3
|
+
require 'active_support/time'
|
4
|
+
require_relative 'data'
|
5
|
+
require_relative 'version'
|
6
|
+
|
7
|
+
module HSQL
|
8
|
+
# Stuff that I'd rather not put directly into the executable goes here. This
|
9
|
+
# makes it much easier to test.
|
10
|
+
class CommandLine
|
11
|
+
def initialize
|
12
|
+
@options = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def options
|
16
|
+
option_parser.parse!
|
17
|
+
@options
|
18
|
+
end
|
19
|
+
|
20
|
+
def filename
|
21
|
+
option_parser.parse!
|
22
|
+
ARGV.first
|
23
|
+
end
|
24
|
+
|
25
|
+
def option_parser
|
26
|
+
@option_parser ||= OptionParser.new do |opts|
|
27
|
+
opts.banner = banner
|
28
|
+
opts.on('-d DATE',
|
29
|
+
'--date DATE',
|
30
|
+
'--timestamp DATE', 'The time that the SQL will consider to be "{{{now}}}".') do |option|
|
31
|
+
@options[:timestamp] = Time.parse(option) if option
|
32
|
+
end
|
33
|
+
opts.on('-e ENV',
|
34
|
+
'--env ENV',
|
35
|
+
'Which key of the YAML header "data:" key you want to interpolate.') do |option|
|
36
|
+
@options[:environment] = option
|
37
|
+
end
|
38
|
+
opts.on('-y', '--yaml', 'Output just the metadata for this file as YAML') do |_option|
|
39
|
+
@options[:meta_only] = 'yaml'
|
40
|
+
end
|
41
|
+
opts.on('-j', '--json', 'Output just the metadata for this file as JSON') do |_option|
|
42
|
+
@options[:meta_only] = 'json'
|
43
|
+
end
|
44
|
+
opts.on('-v', '--verbose', 'Output debug information') do |_option|
|
45
|
+
@options[:verbose] = true
|
46
|
+
end
|
47
|
+
opts.on_tail('-V', '--version', 'Show the current version of HSQL') do |_option|
|
48
|
+
puts HSQL::VERSION
|
49
|
+
exit 1
|
50
|
+
end
|
51
|
+
opts.on('-h', '--help', 'Show the full help documentation') do |_option|
|
52
|
+
help
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def banner
|
58
|
+
<<-BANNER
|
59
|
+
Usage: #{File.basename(__FILE__)} file [environment]
|
60
|
+
|
61
|
+
Arguments
|
62
|
+
file: Any *.sql file. If it has a YAML header (ending in three hyphens) the metadata will be processed;
|
63
|
+
|
64
|
+
Options
|
65
|
+
BANNER
|
66
|
+
end
|
67
|
+
|
68
|
+
def help
|
69
|
+
<<-HELP
|
70
|
+
TEMPLATE
|
71
|
+
|
72
|
+
You can use the Mustache syntax (three curly braces) to interpolate any of your
|
73
|
+
'data' into your SQL. You specify the data in the YAML header like so:
|
74
|
+
|
75
|
+
data:
|
76
|
+
production: # whatever you want to be interpolated into your SQL
|
77
|
+
name: Alison # when you pass 'production' as the environment argument
|
78
|
+
development:
|
79
|
+
name: Kailey
|
80
|
+
---
|
81
|
+
SELECT * FROM users WHERE name = '{{{name}}}'
|
82
|
+
|
83
|
+
|
84
|
+
There are some common date values available to you at all times, without having
|
85
|
+
to be defined in the YAML header:
|
86
|
+
|
87
|
+
#{HSQL::Data.for_humans}
|
88
|
+
|
89
|
+
For more details, run:
|
90
|
+
open https://github.com/JackDanger/hsql
|
91
|
+
HELP
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/hsql/file.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'json'
|
2
3
|
require_relative 'query'
|
3
4
|
require_relative 'data'
|
4
5
|
require_relative 'template'
|
@@ -17,6 +18,7 @@ module HSQL
|
|
17
18
|
@string = string
|
18
19
|
@timestamp = options.fetch(:timestamp, Time.current)
|
19
20
|
@environment = options[:environment]
|
21
|
+
@verbose = options[:verbose]
|
20
22
|
end
|
21
23
|
|
22
24
|
# Given the contents of a SQL file with YAML front matter (see README for an
|
@@ -54,6 +56,10 @@ module HSQL
|
|
54
56
|
|
55
57
|
private
|
56
58
|
|
59
|
+
def verbose?
|
60
|
+
@verbose
|
61
|
+
end
|
62
|
+
|
57
63
|
def split!
|
58
64
|
@split ||= begin
|
59
65
|
top_half, divider, rest = string.partition(/^---$/)
|
@@ -75,20 +81,27 @@ module HSQL
|
|
75
81
|
end
|
76
82
|
end
|
77
83
|
|
78
|
-
def
|
84
|
+
def template
|
79
85
|
template = Template.new(@sql)
|
80
86
|
template.variable_names.each do |name|
|
81
87
|
next if data.key?(name)
|
82
|
-
|
83
88
|
if environment
|
84
89
|
fail FormatError, "#{name.inspect} is not set in #{environment.inspect} environment"
|
85
90
|
else
|
86
91
|
fail FormatError, "#{name.inspect} is not set! Did you provide the right environment argument?"
|
87
92
|
end
|
88
93
|
end
|
94
|
+
template
|
95
|
+
end
|
89
96
|
|
97
|
+
def interpolate_data!
|
90
98
|
# Insert the `data:` section of YAML for the given environment into our SQL queries.
|
91
99
|
@rendered_sql = template.render(data)
|
100
|
+
if verbose?
|
101
|
+
STDERR.puts '-- Rendered SQL:'
|
102
|
+
STDERR.puts @rendered_sql
|
103
|
+
end
|
104
|
+
@rendered_sql
|
92
105
|
end
|
93
106
|
end
|
94
107
|
end
|
data/lib/hsql/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hsql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Danger Canty
|
@@ -161,6 +161,7 @@ files:
|
|
161
161
|
- examples/simple.sql
|
162
162
|
- hsql.gemspec
|
163
163
|
- lib/hsql.rb
|
164
|
+
- lib/hsql/command_line.rb
|
164
165
|
- lib/hsql/data.rb
|
165
166
|
- lib/hsql/file.rb
|
166
167
|
- lib/hsql/query.rb
|