hsql 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hsql.gemspec +27 -0
- data/lib/hsql.rb +18 -0
- data/lib/hsql/file.rb +56 -0
- data/lib/hsql/template.rb +28 -0
- data/lib/hsql/version.rb +3 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b36d377513ad3f4e47233608a6c6a3d10abc5400
|
4
|
+
data.tar.gz: cfa80fcbd40a0f23e715d84274d6f60be9ae56b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ceda7a4890b22ce3a751b249d4d6a13b2db52093086ab6b5b7cdefd045e77c99779676a39954ba14b14ce2e64556110e4a39a89b71bcbfd097ad5f5438c4e1c
|
7
|
+
data.tar.gz: 46ff5428b17d28372906f0d92e7f04846337165e36a5a447c6b2036a2ef87133f62024e9e80aa38758d4d498fe5e3dc5cd7accfccb78f6cfa4001b13546e8a3e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jack Danger Canty
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# HSQL
|
2
|
+
|
3
|
+
".SQL ETL" is a library that parses `.sql` files with YAML front matter.
|
4
|
+
This allows analysts and other non-developers to write and develop ETLs
|
5
|
+
without having to write source code but still giving them the power of
|
6
|
+
specifying variables to interpolate into the SQL and other metadata that
|
7
|
+
the program executing the SQL can use.
|
8
|
+
|
9
|
+
## How to use this
|
10
|
+
|
11
|
+
Rather than specifying variables and metadata for a set of database
|
12
|
+
queries in a .rb or other programming language source file the queries
|
13
|
+
should be written to a .sql file directly.
|
14
|
+
|
15
|
+
# filename: daily_summary.sql
|
16
|
+
owner: jackdanger
|
17
|
+
schedule: hourly
|
18
|
+
data:
|
19
|
+
production:
|
20
|
+
output_table: summaries
|
21
|
+
update_condition:
|
22
|
+
development:
|
23
|
+
output_table: jackdanger_summaries
|
24
|
+
update_condition: WHERE 1 <> 1
|
25
|
+
---
|
26
|
+
USE some_database;
|
27
|
+
INSERT INTO {{{output_table}}} SELECT * FROM interesting_information;
|
28
|
+
UPDATE summaries_performed SET complete = 1 {{{update_condition}}};
|
29
|
+
|
30
|
+
The above is a SQL file and any text editor will allow analysts to use
|
31
|
+
code completion and syntax highlighting for their queries.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hsql"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/hsql.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hsql/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hsql"
|
8
|
+
spec.version = Setl::VERSION
|
9
|
+
spec.authors = ["Jack Danger Canty"]
|
10
|
+
spec.email = ["gems@jackcanty.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Store a hash of data with your SQL queries.}
|
13
|
+
spec.description = %q{Write SQL queries in a .sql format and ship them with metadata about how they should be executed}
|
14
|
+
spec.homepage = "https://github.com/JackDanger/hsql"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "mustache", '~> 0'
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", '~> 0'
|
25
|
+
spec.add_development_dependency "rspec", '~> 0'
|
26
|
+
spec.add_development_dependency "pry-byebug", '~> 0'
|
27
|
+
end
|
data/lib/hsql.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative "hsql/version"
|
2
|
+
require_relative "hsql/template"
|
3
|
+
require_relative "hsql/file"
|
4
|
+
require 'mustache'
|
5
|
+
|
6
|
+
module HSQL
|
7
|
+
# This is used to indicate when a source file is malformed.
|
8
|
+
class FormatError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
# Given the contents of a SQL file with YAML front matter (see README for an
|
12
|
+
# example) this will return a HSQL::File object providing access to the parts
|
13
|
+
# of that file.
|
14
|
+
def self.parse(string, environment)
|
15
|
+
raise ArgumentError, "The environment argument is required" unless environment
|
16
|
+
File.new(string, environment).parse!
|
17
|
+
end
|
18
|
+
end
|
data/lib/hsql/file.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# HSQL::File parses the input file and provides reader methods to the hash of
|
2
|
+
# YAML data from the front matter section and a list of the queries in the SQL
|
3
|
+
# portion.
|
4
|
+
module HSQL
|
5
|
+
class File < Struct.new(:string, :environment)
|
6
|
+
def yaml
|
7
|
+
@yaml ||= YAML.load(@front_matter)
|
8
|
+
end
|
9
|
+
|
10
|
+
def queries
|
11
|
+
# TODO: allow multi-line queries!
|
12
|
+
@queries ||= @rendered_sql.lines.map(&:chomp).reject(&:empty?)
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse!
|
16
|
+
split!
|
17
|
+
interpolate_data!
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def split!
|
24
|
+
@split ||= begin
|
25
|
+
@front_matter, divider, @sql = string.partition(/^---$/)
|
26
|
+
unless divider == '---'
|
27
|
+
raise FormatError, "The YAML front matter is required, otherwise this is just a SQL file"
|
28
|
+
end
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def data
|
34
|
+
@data ||= begin
|
35
|
+
if yaml['data']
|
36
|
+
unless yaml['data'].key?(environment)
|
37
|
+
raise ArgumentError, "The environment #{environment.inspect} is not specified"
|
38
|
+
end
|
39
|
+
yaml['data'][environment]
|
40
|
+
end || {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def interpolate_data!
|
45
|
+
template = Template.new(@sql)
|
46
|
+
template.variable_names.each do |name|
|
47
|
+
unless data.key?(name)
|
48
|
+
raise FormatError, "#{name.inspect} is not set in #{environment.inspect} environment"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Insert the `data:` section of YAML for the given environment into our SQL queries.
|
53
|
+
@rendered_sql = template.render(data)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mustache'
|
2
|
+
module HSQL
|
3
|
+
class Template < Struct.new(:input)
|
4
|
+
def variable_names
|
5
|
+
extract_variable_names(ast).uniq
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(hash)
|
9
|
+
Mustache.render(input, hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# See Mustache::Generator#compile! for reference code
|
15
|
+
def extract_variable_names(tree)
|
16
|
+
return unless tree.is_a?(Array)
|
17
|
+
if tree[1] == :fetch
|
18
|
+
tree.last.first
|
19
|
+
else
|
20
|
+
tree.map { |token| extract_variable_names(token) }.compact.flatten
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def ast
|
25
|
+
::Mustache::Parser.new.compile(input)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/hsql/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hsql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jack Danger Canty
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mustache
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Write SQL queries in a .sql format and ship them with metadata about
|
84
|
+
how they should be executed
|
85
|
+
email:
|
86
|
+
- gems@jackcanty.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".ruby-version"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- hsql.gemspec
|
103
|
+
- lib/hsql.rb
|
104
|
+
- lib/hsql/file.rb
|
105
|
+
- lib/hsql/template.rb
|
106
|
+
- lib/hsql/version.rb
|
107
|
+
homepage: https://github.com/JackDanger/hsql
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.4.5
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Store a hash of data with your SQL queries.
|
131
|
+
test_files: []
|
132
|
+
has_rdoc:
|