i_dig_sql 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3cf46c901868fcef273f122b0dde2d69aa3b45b
4
+ data.tar.gz: 0d06a069a7a363f35e0bb9192d01dea57a1d6412
5
+ SHA512:
6
+ metadata.gz: ea09f6f7ef88f34014ac5fe5b82a82a17f9f907ad510c42af41635a957398e16cc99edaf2ab18e44e5e1aa30efc508012b15714d0b97e7103de1cd5329258cf0
7
+ data.tar.gz: 58bd212f61780380a162fc28e4abd7207967e3fdfedcc486924004606ff3e68d8011ce9a82633d32d4bffb299938232c7074dc9715dc6ebc45d8ddcc5f3782b7
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i_dig_sql.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 da99
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 da99
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # I\_Dig\_Sql
2
+
3
+ My way of generating SQL using Ruby.
4
+
5
+ # Warning:
6
+
7
+ Don't use this. Instead, use [Arel](http://github.com/rails/arel).
8
+
9
+ # Another warning:
10
+
11
+ This is not yet usable.
12
+
13
+ ## Usage
14
+
15
+ Not coming anytime soon.
16
+
17
+
18
+
19
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/test ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+ # -*- bash -*-
3
+ #
4
+ #
5
+ set -u -e -o pipefail
6
+
7
+
8
+ bundle exec bacon specs/*
9
+
10
+
data/i_dig_sql.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'i_dig_sql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i_dig_sql"
8
+ spec.version = I_Dig_Sql::VERSION
9
+ spec.authors = ["da99"]
10
+ spec.email = ["i-hate-spam-1234567@mailinator.com"]
11
+ spec.summary = %q{Yet another way of generating Postgresql 9.2+ in Ruby.}
12
+ spec.description = %q{
13
+ You probably want another gem: arel. Use that
14
+ to generate SQL using Ruby.
15
+ This gem only generates SELECT and WITH (ie CTE) statements/expressions.
16
+ }
17
+ spec.homepage = "https://github.com/da99/i_dig_sql"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "rake", '~> 0'
27
+ spec.add_development_dependency "bacon", '~> 0'
28
+ end
@@ -0,0 +1,8 @@
1
+
2
+ class String
3
+
4
+ def i_dig_sql
5
+ I_Dig_Sql.new self
6
+ end
7
+
8
+ end # === class String ===
@@ -0,0 +1,3 @@
1
+ class I_Dig_Sql
2
+ VERSION = "0.0.2"
3
+ end
data/lib/i_dig_sql.rb ADDED
@@ -0,0 +1,103 @@
1
+
2
+ class I_Dig_Sql
3
+
4
+ class << self
5
+ end # === class self ===
6
+
7
+ def initialize sql = nil, args = nil
8
+ @withs = []
9
+ @select = nil
10
+ @as = nil
11
+ @unions = []
12
+ @sql = sql
13
+ @args = args || []
14
+ yield self if block_given?
15
+ end
16
+
17
+ def AS o = :return
18
+ if o == :return
19
+ return @as if @as
20
+ raise "@as not set"
21
+ end
22
+
23
+ @as = o
24
+ self
25
+ end
26
+
27
+ def WITH o
28
+ @withs << o
29
+
30
+ self
31
+ end
32
+
33
+ def comma o
34
+ WITH o
35
+ end
36
+
37
+ def SELECT str, *args
38
+ @select = {:select=>str, :args=>args, :from=>nil, :where=>nil}
39
+
40
+ self
41
+ end
42
+
43
+ def FROM o
44
+ @select[:from] = o
45
+
46
+ self
47
+ end
48
+
49
+ def WHERE o
50
+ @select[:where] = o
51
+
52
+ self
53
+ end
54
+
55
+ def UNION o
56
+ @unions << o
57
+
58
+ self
59
+ end
60
+
61
+ def to_sql
62
+
63
+ if @sql
64
+ s = "\n "
65
+ s << @sql
66
+ else
67
+
68
+ s = ""
69
+ unless @withs.empty?
70
+ s << "\n WITH"
71
+ s << @withs.map { |w|
72
+ case w
73
+ when String
74
+ " #{w} "
75
+ else
76
+ " #{w.AS} AS (#{w.to_sql[:sql]}) "
77
+ end
78
+ }.join("\n,\n")
79
+ end
80
+
81
+ s << "\n"
82
+
83
+ if @select
84
+ s << "\n SELECT #{@select[:select]}"
85
+ s << "\n FROM #{@select[:from]}" if @select[:from]
86
+ s << "\n WHERE #{@select[:where]}" if @select[:where]
87
+ end
88
+
89
+ end # === if @sql
90
+
91
+ if not @unions.empty?
92
+ s << "\n UNION #{@unions.map { |sql| sql.to_sql[:sql] }.join "\nUNION\n" }"
93
+ end
94
+
95
+ s << "\n"
96
+
97
+ {:sql=>s, :args=>[]}
98
+ end
99
+
100
+ end # === class I_Dig_Sql ===
101
+
102
+
103
+
@@ -0,0 +1,83 @@
1
+
2
+ require "i_dig_sql"
3
+ require "i_dig_sql/String"
4
+
5
+ def sql o
6
+ if o.is_a? String
7
+ return o.split.join(" ")
8
+ else
9
+ sql(o.to_sql[:sql])
10
+ end
11
+ end
12
+
13
+ describe ".new" do
14
+
15
+ it "returns a I_Dig_Sql if passed a String" do
16
+ o = I_Dig_Sql.new("SELECT * FROM some_table")
17
+ o.class.should == I_Dig_Sql
18
+ end
19
+
20
+ end # === describe it runs ===
21
+
22
+ describe "#WITH()" do
23
+
24
+ it "includes passed String" do
25
+ o = I_Dig_Sql.new
26
+ o.WITH("some_table AS (SELECT * FROM other_table)")
27
+ sql(o).should == sql("WITH some_table AS (SELECT * FROM other_table)")
28
+ end
29
+
30
+ it "accepts another I_Dig_Sql object" do
31
+ other = I_Dig_Sql.new("SELECT * FROM main_table")
32
+ .AS('cte1')
33
+
34
+ o = I_Dig_Sql.new
35
+ .WITH(other)
36
+ sql(o).should == sql(%^
37
+ WITH cte1 AS ( SELECT * FROM main_table )
38
+ ^)
39
+ end
40
+
41
+ end # === describe #WITH() ===
42
+
43
+ describe "#comma" do
44
+
45
+ it "saves arg as a WITH statement" do
46
+ o = I_Dig_Sql.new
47
+ .WITH('cte1 AS ( SELECT * FROM table_1 ) ')
48
+ .comma('cte2 AS ( SELECT * FROM table_2 )')
49
+
50
+ sql(o).should == sql(%^
51
+ WITH
52
+ cte1 AS ( SELECT * FROM table_1 )
53
+ , cte2 AS ( SELECT * FROM table_2 )
54
+ ^)
55
+ end
56
+
57
+ end # === describe #comma ===
58
+
59
+ describe "#to_sql" do
60
+
61
+ it "includes both WITH and SELECT statements" do
62
+ o = I_Dig_Sql.new
63
+ o.WITH("cte AS (SELECT * FROM other_table)")
64
+ o.SELECT(" parent_id ")
65
+ .FROM("main_table")
66
+ sql(o).should == sql(%^
67
+ WITH cte AS (SELECT * FROM other_table)
68
+ SELECT parent_id
69
+ FROM main_table
70
+ ^)
71
+ end
72
+
73
+ end # === describe #to_sql ===
74
+
75
+ describe "String#i_dig_sql" do
76
+
77
+ it "returns an I_Dig_Sql instance set to String" do
78
+ o = "SELECT id FROM my_table".i_dig_sql
79
+ sql(o.to_sql[:sql]).should == sql(%^SELECT id FROM my_table^)
80
+ end
81
+
82
+ end # === describe String ===
83
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i_dig_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - da99
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bacon
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
+ description: "\n You probably want another gem: arel. Use that\n to generate SQL
56
+ using Ruby.\n This gem only generates SELECT and WITH (ie CTE) statements/expressions.\n
57
+ \ "
58
+ email:
59
+ - i-hate-spam-1234567@mailinator.com
60
+ executables:
61
+ - test
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - Gemfile
67
+ - LICENSE
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/test
72
+ - i_dig_sql.gemspec
73
+ - lib/i_dig_sql.rb
74
+ - lib/i_dig_sql/String.rb
75
+ - lib/i_dig_sql/version.rb
76
+ - specs/i_dig_sql.rb
77
+ homepage: https://github.com/da99/i_dig_sql
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Yet another way of generating Postgresql 9.2+ in Ruby.
101
+ test_files: []