sql-composer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE/bug-report.md +26 -0
- data/.github/ISSUE_TEMPLATE/config.yml +5 -0
- data/.github/SUPPORT.md +3 -0
- data/.github/workflows/ci.yml +50 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/.rubocop.yml +109 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +18 -0
- data/LICENSE +21 -0
- data/README.md +3 -0
- data/Rakefile +8 -0
- data/bin/console +7 -0
- data/lib/sql/composer.rb +38 -0
- data/lib/sql/composer/compiler.rb +75 -0
- data/lib/sql/composer/dsl.rb +47 -0
- data/lib/sql/composer/node_helpers.rb +13 -0
- data/lib/sql/composer/nodes.rb +12 -0
- data/lib/sql/composer/nodes/core.rb +34 -0
- data/lib/sql/composer/nodes/from.rb +19 -0
- data/lib/sql/composer/nodes/identifier.rb +39 -0
- data/lib/sql/composer/nodes/literal.rb +19 -0
- data/lib/sql/composer/nodes/operations/eql.rb +30 -0
- data/lib/sql/composer/nodes/operations/or.rb +25 -0
- data/lib/sql/composer/nodes/operations/order_direction.rb +33 -0
- data/lib/sql/composer/nodes/operators.rb +32 -0
- data/lib/sql/composer/nodes/order.rb +19 -0
- data/lib/sql/composer/nodes/select.rb +19 -0
- data/lib/sql/composer/nodes/value.rb +19 -0
- data/lib/sql/composer/nodes/where.rb +19 -0
- data/lib/sql/composer/statement.rb +41 -0
- data/lib/sql/composer/tokens.rb +40 -0
- data/lib/sql/composer/version.rb +7 -0
- data/sql-builder.gemspec +26 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d4ddd6a9d7d02d90066790a780220957b9f77c27fdda2e6c72f53ffaa19d76e1
|
4
|
+
data.tar.gz: bfc55fe0f6fe3b1304a918f41631532f64cc922c4cdbba86ededd4cb851ab899
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2fe18c8cc104fad93498f4000fa3f0d3b116d6c18f26cdd8f7a7d37903570b2729e731135c729f388258e338b040681bb779402f105ea131f08361b0990c5b88
|
7
|
+
data.tar.gz: a440c6b5e11bfd9ca96fe66b8830e13ac718b15dd843d70deceeb47641421f00d5234c8f9afb3d68fd035a60aa6cd1ecec39c4304ed988d22619b4d4e609aac7
|
@@ -0,0 +1,26 @@
|
|
1
|
+
---
|
2
|
+
name: "\U0001F41B Bug report"
|
3
|
+
about: See CONTRIBUTING.md for more information
|
4
|
+
title: ''
|
5
|
+
labels: bug, help wanted
|
6
|
+
assignees: ''
|
7
|
+
|
8
|
+
---
|
9
|
+
|
10
|
+
## Describe the bug
|
11
|
+
|
12
|
+
A clear and concise description of what the bug is.
|
13
|
+
|
14
|
+
## To Reproduce
|
15
|
+
|
16
|
+
Provide detailed steps to reproduce, **an executable script would be best**.
|
17
|
+
|
18
|
+
## Expected behavior
|
19
|
+
|
20
|
+
A clear and concise description of what you expected to happen.
|
21
|
+
|
22
|
+
## My environment
|
23
|
+
|
24
|
+
- Affects my production application: **YES/NO**
|
25
|
+
- Ruby version: ...
|
26
|
+
- OS: ...
|
data/.github/SUPPORT.md
ADDED
@@ -0,0 +1,3 @@
|
|
1
|
+
## Support
|
2
|
+
|
3
|
+
If you need help with any of the dry-rb libraries, feel free to ask questions on our [discussion forum](https://discourse.dry-rb.org/). This is the best place to seek help. Make sure to search for a potential solution in past threads before posting your question. Thanks! :heart:
|
@@ -0,0 +1,50 @@
|
|
1
|
+
---
|
2
|
+
name: ci
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
paths:
|
6
|
+
- ".github/workflows/ci.yml"
|
7
|
+
- lib/**
|
8
|
+
- "*.gemspec"
|
9
|
+
- spec/**
|
10
|
+
- Rakefile
|
11
|
+
- Gemfile
|
12
|
+
- Gemfile.devtools
|
13
|
+
- ".rubocop.yml"
|
14
|
+
- project.yml
|
15
|
+
pull_request:
|
16
|
+
branches:
|
17
|
+
- master
|
18
|
+
create:
|
19
|
+
jobs:
|
20
|
+
tests:
|
21
|
+
runs-on: ubuntu-latest
|
22
|
+
strategy:
|
23
|
+
fail-fast: false
|
24
|
+
matrix:
|
25
|
+
ruby:
|
26
|
+
- head
|
27
|
+
- '2.7'
|
28
|
+
- '2.6'
|
29
|
+
- '2.5'
|
30
|
+
- jruby
|
31
|
+
include:
|
32
|
+
- ruby: '2.7'
|
33
|
+
coverage: 'true'
|
34
|
+
env:
|
35
|
+
COVERAGE: "${{matrix.coverage}}"
|
36
|
+
COVERAGE_TOKEN: "${{secrets.CODACY_PROJECT_TOKEN}}"
|
37
|
+
steps:
|
38
|
+
- uses: actions/checkout@v1
|
39
|
+
- name: Set up Ruby
|
40
|
+
uses: ruby/setup-ruby@v1
|
41
|
+
with:
|
42
|
+
ruby-version: "${{matrix.ruby}}"
|
43
|
+
- name: Install latest bundler
|
44
|
+
run: |
|
45
|
+
gem install bundler --no-document
|
46
|
+
bundle config set without 'tools benchmarks docs'
|
47
|
+
- name: Bundle install
|
48
|
+
run: bundle install --jobs 4 --retry 3
|
49
|
+
- name: Run all tests
|
50
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# this file is managed by dry-rb/devtools project
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.7
|
5
|
+
NewCops: disable
|
6
|
+
|
7
|
+
Style/StringLiterals:
|
8
|
+
Enabled: true
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Style/AccessModifierDeclarations:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/BlockDelimiters:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/SymbolArray:
|
18
|
+
Exclude:
|
19
|
+
- "spec/**/*_spec.rb"
|
20
|
+
|
21
|
+
Style/ConditionalAssignment:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/ClassAndModuleChildren:
|
25
|
+
Exclude:
|
26
|
+
- "spec/**/*_spec.rb"
|
27
|
+
|
28
|
+
Style/ParallelAssignment:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/Alias:
|
32
|
+
Enabled: true
|
33
|
+
EnforcedStyle: prefer_alias_method
|
34
|
+
|
35
|
+
Style/LambdaCall:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/StabbyLambdaParentheses:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Style/FormatString:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/Documentation:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Style/AsciiComments:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/DateTime:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/IfUnlessModifier:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
Style/EachWithObject:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
Lint/SuppressedException:
|
60
|
+
Exclude:
|
61
|
+
- "spec/spec_helper.rb"
|
62
|
+
|
63
|
+
Lint/BooleanSymbol:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Layout/SpaceInLambdaLiteral:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Layout/MultilineMethodCallIndentation:
|
70
|
+
Enabled: true
|
71
|
+
EnforcedStyle: indented
|
72
|
+
|
73
|
+
Layout/FirstArrayElementIndentation:
|
74
|
+
EnforcedStyle: consistent
|
75
|
+
|
76
|
+
Naming/VariableNumber:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
Naming/PredicateName:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
Naming/FileName:
|
83
|
+
Exclude:
|
84
|
+
- "lib/dry-*.rb"
|
85
|
+
|
86
|
+
Naming/MethodName:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
Naming/MemoizedInstanceVariableName:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Layout/LineLength:
|
93
|
+
Max: 100
|
94
|
+
|
95
|
+
Metrics/MethodLength:
|
96
|
+
Enabled: false
|
97
|
+
|
98
|
+
Metrics/ClassLength:
|
99
|
+
Enabled: false
|
100
|
+
|
101
|
+
Metrics/BlockLength:
|
102
|
+
Enabled: false
|
103
|
+
|
104
|
+
Metrics/AbcSize:
|
105
|
+
Max: 25
|
106
|
+
|
107
|
+
Metrics/CyclomaticComplexity:
|
108
|
+
Enabled: true
|
109
|
+
Max: 12
|
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, 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
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in sql.rb.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake"
|
9
|
+
|
10
|
+
group :tools do
|
11
|
+
gem "pry", platforms: :mri
|
12
|
+
gem "byebug", platforms: :mri
|
13
|
+
gem "rubocop", "~> 1.6"
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "rspec", "~> 3.10"
|
18
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Piotr Solnica
|
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
data/Rakefile
ADDED
data/bin/console
ADDED
data/lib/sql/composer.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/dsl"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
def self.compose(options, &block)
|
7
|
+
backend = Composer.backends[options[:backend]]
|
8
|
+
args = options[:args]
|
9
|
+
Composer::DSL.new(args: args, backend: backend, &block).()
|
10
|
+
end
|
11
|
+
|
12
|
+
module Composer
|
13
|
+
def self.backends
|
14
|
+
@backends ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
module Backends
|
18
|
+
InputTypeNotSupported = Class.new(StandardError)
|
19
|
+
|
20
|
+
class Postgres
|
21
|
+
def quote(identifier)
|
22
|
+
%("#{identifier.to_s}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def escape(input)
|
26
|
+
case input
|
27
|
+
when String
|
28
|
+
%('#{input}')
|
29
|
+
else
|
30
|
+
raise InputTypeNotSupported, input.class
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
backends[:postgres] = Backends::Postgres.new
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/statement"
|
4
|
+
require "sql/composer/tokens"
|
5
|
+
require "sql/composer/nodes"
|
6
|
+
|
7
|
+
module SQL
|
8
|
+
module Composer
|
9
|
+
class Compiler
|
10
|
+
include ::Dry::Effects::Handler.State(:tokens)
|
11
|
+
|
12
|
+
attr_reader :backend
|
13
|
+
|
14
|
+
attr_reader :nodes
|
15
|
+
|
16
|
+
attr_reader :options
|
17
|
+
|
18
|
+
attr_reader :tokens
|
19
|
+
|
20
|
+
def initialize(backend, options)
|
21
|
+
@backend = backend
|
22
|
+
@options = options
|
23
|
+
@nodes = options[:nodes] || []
|
24
|
+
@tokens = options[:tokens]
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(ast)
|
28
|
+
with_tokens(tokens) { ast.map { |node| visit(node) } }
|
29
|
+
Statement.new(compiler: freeze)
|
30
|
+
end
|
31
|
+
|
32
|
+
def with(new_options)
|
33
|
+
self.class.new(backend, options.merge(new_options).merge(nodes: nodes))
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
with_tokens(tokens) { nodes.map(&:to_s).join("\n") }.last
|
38
|
+
end
|
39
|
+
|
40
|
+
def visit(node)
|
41
|
+
visitor, *args = node
|
42
|
+
__send__(:"visit_#{visitor}", args)
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def visit_literal(node)
|
47
|
+
add_node(Nodes::Literal, value: node[0])
|
48
|
+
end
|
49
|
+
|
50
|
+
def visit_from(node)
|
51
|
+
source, _ = node
|
52
|
+
add_node(Nodes::From, source: source)
|
53
|
+
end
|
54
|
+
|
55
|
+
def visit_select(nodes)
|
56
|
+
add_node(Nodes::Select, identifiers: nodes)
|
57
|
+
end
|
58
|
+
|
59
|
+
def visit_where(node)
|
60
|
+
add_node(Nodes::Where, operations: node)
|
61
|
+
end
|
62
|
+
|
63
|
+
def visit_order(node)
|
64
|
+
add_node(Nodes::Order, operations: node)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def add_node(klass, options)
|
70
|
+
nodes << klass.new(options.update(backend: backend))
|
71
|
+
self
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/effects"
|
4
|
+
|
5
|
+
require "sql/composer/compiler"
|
6
|
+
require "sql/composer/tokens"
|
7
|
+
require "sql/composer/nodes/literal"
|
8
|
+
|
9
|
+
module SQL
|
10
|
+
module Composer
|
11
|
+
class DSL < BasicObject
|
12
|
+
include ::Dry::Effects::Handler.State(:tokens)
|
13
|
+
|
14
|
+
attr_reader :options
|
15
|
+
|
16
|
+
attr_reader :ast
|
17
|
+
|
18
|
+
attr_reader :tokens
|
19
|
+
|
20
|
+
def initialize(options, &block)
|
21
|
+
@options = options
|
22
|
+
@ast = []
|
23
|
+
@tokens = options[:tokens] || Tokens.new
|
24
|
+
|
25
|
+
with_tokens(tokens) do
|
26
|
+
instance_exec(*options[:args], &block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def call
|
31
|
+
compiler = Compiler.new(options.fetch(:backend), tokens: tokens)
|
32
|
+
compiler.(ast)
|
33
|
+
end
|
34
|
+
|
35
|
+
def `(value)
|
36
|
+
Nodes::Literal.new(value: value, backend: options[:backend])
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def method_missing(name, *args)
|
42
|
+
ast << [name.to_s.downcase, *args]
|
43
|
+
self
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/literal"
|
4
|
+
require "sql/composer/nodes/identifier"
|
5
|
+
require "sql/composer/nodes/select"
|
6
|
+
require "sql/composer/nodes/from"
|
7
|
+
require "sql/composer/nodes/where"
|
8
|
+
require "sql/composer/nodes/order"
|
9
|
+
require "sql/composer/nodes/operations/eql"
|
10
|
+
require "sql/composer/nodes/operations/or"
|
11
|
+
require "sql/composer/nodes/operations/order_direction"
|
12
|
+
require "sql/composer/nodes/value"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/effects"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class Core
|
9
|
+
include Dry::Effects.State(:tokens)
|
10
|
+
|
11
|
+
attr_reader :id
|
12
|
+
|
13
|
+
attr_reader :options
|
14
|
+
|
15
|
+
def initialize(options)
|
16
|
+
@options = options
|
17
|
+
@id = tokens.next_id
|
18
|
+
end
|
19
|
+
|
20
|
+
def fetch(name, default = nil)
|
21
|
+
@options.fetch(name, default)
|
22
|
+
end
|
23
|
+
|
24
|
+
def backend
|
25
|
+
fetch(:backend)
|
26
|
+
end
|
27
|
+
|
28
|
+
def quote(identifier)
|
29
|
+
backend.quote(identifier)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class From < Core
|
9
|
+
def source
|
10
|
+
fetch(:source)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"FROM #{source.to_s}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
require "sql/composer/nodes/operators"
|
5
|
+
|
6
|
+
|
7
|
+
module SQL
|
8
|
+
module Composer
|
9
|
+
module Nodes
|
10
|
+
class Identifier < Core
|
11
|
+
include Operators
|
12
|
+
|
13
|
+
def name
|
14
|
+
fetch(:name).to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
qualify? ? [qualifier, quote(name)].map(&:to_s).join(".") : quote(name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def asc
|
22
|
+
Operations::Asc.new(self)
|
23
|
+
end
|
24
|
+
|
25
|
+
def desc
|
26
|
+
Operations::Desc.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def qualifier
|
30
|
+
fetch(:qualifier)
|
31
|
+
end
|
32
|
+
|
33
|
+
def qualify?
|
34
|
+
options.key?(:qualifier)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
require "sql/composer/nodes/operators"
|
5
|
+
|
6
|
+
module SQL
|
7
|
+
module Composer
|
8
|
+
module Nodes
|
9
|
+
class Literal < Core
|
10
|
+
include Operators
|
11
|
+
|
12
|
+
def value
|
13
|
+
fetch(:value)
|
14
|
+
end
|
15
|
+
alias_method :to_s, :value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
module Operations
|
9
|
+
class Eql < Core
|
10
|
+
def left
|
11
|
+
fetch(:left)
|
12
|
+
end
|
13
|
+
|
14
|
+
def right
|
15
|
+
fetch(:right)
|
16
|
+
end
|
17
|
+
|
18
|
+
def or(other)
|
19
|
+
Operations::Or.new(left: self, right: other)
|
20
|
+
end
|
21
|
+
alias_method :OR, :or
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"#{left.to_s} == #{right.to_s}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
module Operations
|
9
|
+
class Or < Core
|
10
|
+
def left
|
11
|
+
fetch(:left)
|
12
|
+
end
|
13
|
+
|
14
|
+
def right
|
15
|
+
fetch(:right)
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"(#{left.to_s}) OR (#{right.to_s})"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQL
|
4
|
+
module Composer
|
5
|
+
module Nodes
|
6
|
+
module Operations
|
7
|
+
class OrderDirection
|
8
|
+
attr_reader :source
|
9
|
+
|
10
|
+
def initialize(source)
|
11
|
+
@source = source
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"#{source.to_s} #{direction}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Asc < OrderDirection
|
20
|
+
def direction
|
21
|
+
"ASC"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Desc < OrderDirection
|
26
|
+
def direction
|
27
|
+
"DESC"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/operations/eql"
|
4
|
+
require "sql/composer/nodes/operations/order_direction"
|
5
|
+
|
6
|
+
module SQL
|
7
|
+
module Composer
|
8
|
+
module Nodes
|
9
|
+
module Operators
|
10
|
+
def ==(other)
|
11
|
+
value = Nodes::Value.new(input: other, backend: backend)
|
12
|
+
|
13
|
+
operation = Operations::Eql.new(left: self, right: value)
|
14
|
+
|
15
|
+
if other.start_with?("%") && other.end_with?("%")
|
16
|
+
tokens.add(other, value)
|
17
|
+
end
|
18
|
+
|
19
|
+
operation
|
20
|
+
end
|
21
|
+
|
22
|
+
def asc
|
23
|
+
Operations::Asc.new(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def desc
|
27
|
+
Operations::Desc.new(self)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class Order < Core
|
9
|
+
def operations
|
10
|
+
fetch(:operations)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"ORDER BY #{operations.map(&:to_s).join(', ')}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class Select < Core
|
9
|
+
def identifiers
|
10
|
+
fetch(:identifiers)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"SELECT #{identifiers.map(&:to_s).join(', ')}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class Value < Core
|
9
|
+
def input
|
10
|
+
tokens.value(id, fetch(:input))
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
backend.escape(input)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sql/composer/nodes/core"
|
4
|
+
|
5
|
+
module SQL
|
6
|
+
module Composer
|
7
|
+
module Nodes
|
8
|
+
class Where < Core
|
9
|
+
def operations
|
10
|
+
fetch(:operations)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"WHERE #{operations.map(&:to_s).join(' ')}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQL
|
4
|
+
module Composer
|
5
|
+
class Statement
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
def initialize(options)
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def each(&block)
|
15
|
+
nodes.each(&block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def set(values)
|
19
|
+
tokens = compiler.tokens.new
|
20
|
+
|
21
|
+
values.each do |key, value|
|
22
|
+
tokens.set(key, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
compiler.with(tokens: tokens)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
compiler.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def compiler
|
33
|
+
options.fetch(:compiler)
|
34
|
+
end
|
35
|
+
|
36
|
+
def nodes
|
37
|
+
compiler.nodes
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SQL
|
4
|
+
module Composer
|
5
|
+
class Tokens
|
6
|
+
include Dry::Core::Constants
|
7
|
+
|
8
|
+
attr_reader :data
|
9
|
+
|
10
|
+
def initialize(data: {}, counter: 0)
|
11
|
+
@data = data
|
12
|
+
@counter = counter
|
13
|
+
end
|
14
|
+
|
15
|
+
def add(key, node)
|
16
|
+
data[key[1..-2].to_sym] = [node.id, Undefined]
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def set(key, value)
|
21
|
+
tuple = [data[key][0], value]
|
22
|
+
data[key] = tuple
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def new
|
27
|
+
self.class.new(data: data.dup, counter: @counter)
|
28
|
+
end
|
29
|
+
|
30
|
+
def value(id, default)
|
31
|
+
entry = data.detect { |_, (node_id, value)| node_id.equal?(id) }
|
32
|
+
entry ? entry[1][1] : default
|
33
|
+
end
|
34
|
+
|
35
|
+
def next_id
|
36
|
+
@counter += 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/sql-builder.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "sql/composer/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "sql-composer"
|
9
|
+
spec.version = SQL::Composer::VERSION
|
10
|
+
spec.authors = ["Piotr Solnica"]
|
11
|
+
spec.email = ["piotr.solnica@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = ""
|
14
|
+
spec.description = ""
|
15
|
+
spec.homepage = ""
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
}
|
21
|
+
spec.bindir = "bin"
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "dry-effects", "~> 0.1"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sql-composer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Solnica
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-effects
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
description: ''
|
28
|
+
email:
|
29
|
+
- piotr.solnica@gmail.com
|
30
|
+
executables:
|
31
|
+
- console
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".github/ISSUE_TEMPLATE/bug-report.md"
|
36
|
+
- ".github/ISSUE_TEMPLATE/config.yml"
|
37
|
+
- ".github/SUPPORT.md"
|
38
|
+
- ".github/workflows/ci.yml"
|
39
|
+
- ".gitignore"
|
40
|
+
- ".rspec"
|
41
|
+
- ".rubocop.yml"
|
42
|
+
- CODE_OF_CONDUCT.md
|
43
|
+
- Gemfile
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- bin/console
|
48
|
+
- lib/sql/composer.rb
|
49
|
+
- lib/sql/composer/compiler.rb
|
50
|
+
- lib/sql/composer/dsl.rb
|
51
|
+
- lib/sql/composer/node_helpers.rb
|
52
|
+
- lib/sql/composer/nodes.rb
|
53
|
+
- lib/sql/composer/nodes/core.rb
|
54
|
+
- lib/sql/composer/nodes/from.rb
|
55
|
+
- lib/sql/composer/nodes/identifier.rb
|
56
|
+
- lib/sql/composer/nodes/literal.rb
|
57
|
+
- lib/sql/composer/nodes/operations/eql.rb
|
58
|
+
- lib/sql/composer/nodes/operations/or.rb
|
59
|
+
- lib/sql/composer/nodes/operations/order_direction.rb
|
60
|
+
- lib/sql/composer/nodes/operators.rb
|
61
|
+
- lib/sql/composer/nodes/order.rb
|
62
|
+
- lib/sql/composer/nodes/select.rb
|
63
|
+
- lib/sql/composer/nodes/value.rb
|
64
|
+
- lib/sql/composer/nodes/where.rb
|
65
|
+
- lib/sql/composer/statement.rb
|
66
|
+
- lib/sql/composer/tokens.rb
|
67
|
+
- lib/sql/composer/version.rb
|
68
|
+
- sql-builder.gemspec
|
69
|
+
homepage: ''
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.1.4
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: ''
|
92
|
+
test_files: []
|