koine-sql_builder 0.1.0

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
+ SHA256:
3
+ metadata.gz: bdad4f9f780a9089c8cfcd601df18f647c456f5d913d79f5047465775dfe3d0f
4
+ data.tar.gz: f04b3201df4dc8811ca76ab2a583fa2a7744bb915d03edf87f13faa59d51dd59
5
+ SHA512:
6
+ metadata.gz: c51c789b8109037f53c915d27b5a17d14da39428edca18359c02079fee8d45031a186de37d791312fbba62b0483c233faf1aca96de9e7dc9fc87a4246827598c
7
+ data.tar.gz: 37210be03f958940ff058b1ba7ab5ebdca6d0c584de3221490da637f64612306411fad9c7754cf3a538623182bfa8973d1b556c88c727abdbf5993f28d4afdd2
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.reek ADDED
@@ -0,0 +1,12 @@
1
+ ---
2
+ Attribute:
3
+ enabled: false
4
+ IrresponsibleModule:
5
+ enabled: false
6
+ UtilityFunction:
7
+ enabled: false
8
+ DuplicateMethodCall:
9
+ enabled: false
10
+ TooManyStatements:
11
+ enabled: true
12
+ max_statements: 10
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,62 @@
1
+ Lint/AmbiguousBlockAssociation:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Max: 100
9
+
10
+ Metrics/ParameterLists:
11
+ Max: 5
12
+
13
+ Metrics/ClassLength:
14
+ CountComments: false
15
+ Max: 250
16
+
17
+ Metrics/MethodLength:
18
+ CountComments: false
19
+ Max: 30
20
+
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 7
23
+
24
+ Style/GuardClause:
25
+ Enabled: false
26
+
27
+ Style/IfUnlessModifier:
28
+ Enabled: false
29
+
30
+ Style/TrailingCommaInArrayLiteral:
31
+ Enabled: false
32
+
33
+ Style/RedundantReturn:
34
+ Enabled: false
35
+
36
+ Style/SafeNavigation:
37
+ Enabled: false
38
+
39
+ Style/TrailingCommaInHashLiteral:
40
+ Enabled: false
41
+
42
+ Naming/PredicateName:
43
+ NamePrefix:
44
+ - is_
45
+
46
+ Metrics/AbcSize:
47
+ Max: 45
48
+
49
+ Metrics/PerceivedComplexity:
50
+ Max: 10
51
+
52
+ Documentation:
53
+ Enabled: false
54
+
55
+ Layout/ExtraSpacing:
56
+ AllowForAlignment: true
57
+
58
+ AllCops:
59
+ Exclude:
60
+ - bin/**/*
61
+ TargetRubyVersion: 2.4
62
+
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ - 2.4.4
6
+ - 2.5
7
+ script:
8
+ - bundle exec rake test:coveralls
9
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at marcelo.jacobus@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in koine-sql_builder.gemspec
8
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Marcelo Jacobus
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,93 @@
1
+ # Koine::SqlBuilder
2
+
3
+ Simple SQL builder created with MySql in mind - although I will try to make it compatible with other SQL based databases.
4
+ A port to other databases can potentially be achieved by replacing the adapter.
5
+
6
+ **This is a WIP**
7
+
8
+ [![Build Status](https://travis-ci.org/mjacobus/koine-sql_builder.svg?branch=master)](https://travis-ci.org/mjacobus/koine-sql_builder)
9
+ [![Coverage Status](https://coveralls.io/repos/github/mjacobus/koine-sql_builder/badge.svg?branch=master)](https://coveralls.io/github/mjacobus/koine-sql_builder?branch=master)
10
+ [![Maintainability](https://api.codeclimate.com/v1/badges/eb8b4ea26c2110d67732/maintainability)](https://codeclimate.com/github/mjacobus/koine-sql_builder/maintainability)
11
+ [![Issue Count](https://codeclimate.com/github/mjacobus/koine-sql_builder/badges/issue_count.svg)](https://codeclimate.com/github/mjacobus/koine-sql_builder)
12
+
13
+ [![Gem Version](https://badge.fury.io/rb/koine-sql_builder.svg)](https://badge.fury.io/rb/koine-sql_builder)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'koine-sql_builder'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install koine-sql_builder
30
+
31
+ ## Usage
32
+
33
+ See [examples](https://github.com/mjacobus/koine-sql_builder/tree/master/examples)
34
+
35
+ ```ruby
36
+ builder = Koine::SqlBuilder::Builder.new
37
+
38
+ builder.select
39
+ .from(users: :u)
40
+ .where(lastname: 'Jacobus')
41
+ .where(age: 18)
42
+
43
+ # SELECT *
44
+ # FROM users u
45
+ # WHERE lastname = "Jacobus" AND age = 18
46
+
47
+ builder
48
+ .select(:id, last_name: :lastname)
49
+ .from(:users)
50
+ .where(lastname: 'Jacobus')
51
+
52
+ # SELECT id, last_name AS lastname
53
+ # FROM users
54
+ # WHERE lastname = "Jacobus"
55
+
56
+ builder
57
+ .select
58
+ .from(:users)
59
+ .where(age: nil)
60
+
61
+ # SELECT *
62
+ # FROM users
63
+ # WHERE age IS NULL
64
+
65
+ age_not_null = builder.adapter.equal(:age, nil).not
66
+
67
+ builder
68
+ .select
69
+ .from(:users)
70
+ .where(age_not_null)
71
+
72
+ # SELECT *
73
+ # FROM users
74
+ # WHERE age IS NOT NULL
75
+ ```
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
80
+
81
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
82
+
83
+ ## Contributing
84
+
85
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-sql_builder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
90
+
91
+ ## Code of Conduct
92
+
93
+ Everyone interacting in the Koine::SqlBuilder project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mjacobus/koine-sql_builder/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
9
+
10
+ namespace :test do
11
+ desc 'run tests and generate invoke coveralls'
12
+ task :coveralls do
13
+ ENV['COVERALLS'] = 'true'
14
+ Rake::Task['test:coverage'].invoke
15
+ end
16
+
17
+ desc 'run tests and generate code coverage'
18
+ task :coverage do
19
+ ENV['COVERAGE'] = 'true'
20
+ Rake::Task[:spec].invoke
21
+ end
22
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "koine/sql_builder"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'koine/sql_builder'
5
+
6
+ class Examples
7
+ include Enumerable
8
+
9
+ def initialize
10
+ adapter = Koine::SqlBuilder::Adapter.new(joiner: "\n")
11
+ @builder = Koine::SqlBuilder::Builder.new(adapter: adapter)
12
+ @examples = []
13
+ end
14
+
15
+ def add
16
+ @examples.push(yield(@builder))
17
+ end
18
+
19
+ def each(&block)
20
+ @examples.each(&block)
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config'
4
+
5
+ examples = Examples.new
6
+
7
+ examples.add do |builder|
8
+ builder
9
+ .select
10
+ .from(users: :u)
11
+ .where(lastname: 'Jacobus')
12
+ .where(age: 18)
13
+
14
+ # SELECT *
15
+ # FROM users u
16
+ # WHERE lastname = "Jacobus" AND age = 18
17
+ end
18
+
19
+ examples.add do |builder|
20
+ builder
21
+ .select(:id, last_name: :lastname)
22
+ .from(:users)
23
+ .where(lastname: 'Jacobus')
24
+
25
+ # SELECT id, last_name AS lastname
26
+ # FROM users
27
+ # WHERE lastname = "Jacobus"
28
+ end
29
+
30
+ examples.add do |builder|
31
+ builder
32
+ .select
33
+ .from(:users)
34
+ .where(age: nil)
35
+
36
+ # SELECT *
37
+ # FROM users
38
+ # WHERE age IS NULL
39
+ end
40
+
41
+ examples.add do |builder|
42
+ age_not_null = builder.adapter.equal(:age, nil).not
43
+
44
+ builder
45
+ .select
46
+ .from(:users)
47
+ .where(age_not_null)
48
+
49
+ # SELECT *
50
+ # FROM users
51
+ # WHERE age IS NOT NULL
52
+ end
53
+
54
+ puts examples.to_a.join("\n\n")
@@ -0,0 +1,42 @@
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 'koine/sql_builder/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'koine-sql_builder'
9
+ spec.version = Koine::SqlBuilder::VERSION
10
+ spec.authors = ['Marcelo Jacobus']
11
+ spec.email = ['marcelo.jacobus@gmail.com']
12
+
13
+ spec.summary = 'SQL builder for writing SQL queries'
14
+ spec.description = 'SQL builder for writing SQL queries'
15
+ spec.homepage = 'https://github.com/mjacobus/koine-sql_builder'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
24
+ 'public gem pushes.'
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.16'
35
+ spec.add_development_dependency 'coveralls'
36
+ spec.add_development_dependency 'object_comparator'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'reek', '~> 5.0.2'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rubocop', '~> 0.58.0'
41
+ spec.add_development_dependency 'simplecov'
42
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'koine/sql_builder/version'
4
+ require 'koine/sql_builder/builder'
5
+ require 'koine/sql_builder/select_builder'
6
+ require 'koine/sql_builder/select'
7
+ require 'koine/sql_builder/from'
8
+ require 'koine/sql_builder/where'
9
+ require 'koine/sql_builder/adapter'
10
+ require 'koine/sql_builder/condition'
11
+ require 'koine/sql_builder/conditions/comparison'
12
+ require 'koine/sql_builder/conditions/equal'
13
+ require 'koine/sql_builder/conditions/not_equal'
14
+
15
+ module Koine
16
+ module SqlBuilder
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class Adapter
6
+ ArgumentError = Class.new(::ArgumentError)
7
+
8
+ INTEGER_REGEXP = /^\d+$/
9
+
10
+ attr_reader :joiner
11
+
12
+ def initialize(joiner: "\s")
13
+ @joiner = joiner
14
+ end
15
+
16
+ def create_condition(item)
17
+ if item.is_a?(Array)
18
+ return condition_from_array(item)
19
+ end
20
+
21
+ if item.is_a?(Condition)
22
+ return item
23
+ end
24
+
25
+ raise ArgumentError, "Unrecognized condition #{item.inspect}"
26
+ end
27
+
28
+ def equal(field, value)
29
+ Conditions::Equal.new(field, value, adapter: self)
30
+ end
31
+
32
+ def quote(value)
33
+ if INTEGER_REGEXP.match?(value.to_s)
34
+ return value.to_s
35
+ end
36
+
37
+ "\"#{value}\""
38
+ end
39
+
40
+ private
41
+
42
+ def condition_from_array(array)
43
+ if array.length != 2
44
+ raise ArgumentError, 'Invalid argument'
45
+ end
46
+
47
+ equal(array.first, array.last)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class Builder
6
+ attr_reader :adapter
7
+
8
+ def initialize(adapter: Adapter.new)
9
+ @adapter = adapter
10
+ end
11
+
12
+ def select(*args)
13
+ SelectBuilder.new(
14
+ adapter: adapter,
15
+ select: Select.new(*args),
16
+ from: From.new,
17
+ where: Where.new(adapter: adapter)
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class Condition
6
+ def to_s
7
+ raise NotImplementedError
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ module Conditions
6
+ class Comparison < Condition
7
+ def initialize(field, value, adapter:)
8
+ @adapter = adapter
9
+ @field = field.dup
10
+ @value = value.dup
11
+ end
12
+
13
+ def not
14
+ not_class.new(field, value, adapter: adapter)
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :field
20
+ attr_reader :value
21
+ attr_reader :adapter
22
+
23
+ def nil?
24
+ @value.nil?
25
+ end
26
+
27
+ def not_class
28
+ parts = self.class.to_s.split('::')
29
+ last = parts.pop
30
+ last = "Not#{last}".sub('NotNot', '')
31
+ klass = parts.push(last).join('::')
32
+
33
+ if Object.const_defined?(klass)
34
+ return Object.const_get(klass)
35
+ end
36
+
37
+ raise NotImplementedError, "Comparison class #{klass} is not defined"
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ module Conditions
6
+ class Equal < Comparison
7
+ def to_s
8
+ if nil?
9
+ return "#{field} IS NULL"
10
+ end
11
+
12
+ "#{field} = #{adapter.quote(value)}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ module Conditions
6
+ class NotEqual < Comparison
7
+ def to_s
8
+ if nil?
9
+ return "#{field} IS NOT NULL"
10
+ end
11
+
12
+ "#{field} != #{adapter.quote(value)}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class From
6
+ def initialize(config = nil)
7
+ if config.is_a?(Hash)
8
+ config = config.map do |table, alias_name|
9
+ "#{table} #{alias_name}"
10
+ end.join(', ')
11
+ end
12
+
13
+ @as_string = config.to_s
14
+ end
15
+
16
+ def to_s
17
+ if present?
18
+ return "FROM #{@as_string}"
19
+ end
20
+
21
+ ''
22
+ end
23
+
24
+ private
25
+
26
+ def present?
27
+ !@as_string.strip.empty?
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class Select
6
+ def initialize(*args)
7
+ config = args.flatten.inject([]) do |sum, item|
8
+ if item.is_a?(Hash)
9
+ item = item.map { |k, v| "#{k} AS #{v}" }
10
+ end
11
+
12
+ [sum, item].flatten
13
+ end
14
+
15
+ if config.empty?
16
+ config = ['*']
17
+ end
18
+
19
+ @as_string = config.join(', ').to_s
20
+ end
21
+
22
+ def to_s
23
+ "SELECT #{@as_string}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class SelectBuilder
6
+ def initialize(from:, select:, where:, adapter:)
7
+ @adapter = adapter
8
+ @attributes = {
9
+ select: select,
10
+ from: from,
11
+ where: where,
12
+ }
13
+ end
14
+
15
+ def select(select = nil)
16
+ create(:select, Select.new(select))
17
+ end
18
+
19
+ def from(from)
20
+ create(:from, From.new(from))
21
+ end
22
+
23
+ def where(conditions = {})
24
+ create(:where, @attributes[:where].with_added_conditions(conditions))
25
+ end
26
+
27
+ def to_s
28
+ @attributes.values.map(&:to_s).map(&:strip).reject do |element|
29
+ element == ''
30
+ end.join(@adapter.joiner)
31
+ end
32
+
33
+ private
34
+
35
+ def create(attribute, value)
36
+ self.class.new(@attributes.merge(attribute => value, adapter: @adapter))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Koine
4
+ module SqlBuilder
5
+ class Where
6
+ include Enumerable
7
+
8
+ def initialize(items = [], adapter:)
9
+ @adapter = adapter
10
+ @items = items
11
+ end
12
+
13
+ def with_added_conditions(items = {})
14
+ items = Array(items).map do |item|
15
+ @adapter.create_condition(item)
16
+ end
17
+
18
+ items = [@items, items].flatten
19
+ self.class.new(items, adapter: @adapter)
20
+ end
21
+
22
+ def each(&block)
23
+ @items.each(&block)
24
+ end
25
+
26
+ def to_s
27
+ unless items.empty?
28
+ return "WHERE #{to_a.join(' AND ')}"
29
+ end
30
+
31
+ ''
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :items
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: koine-sql_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Jacobus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-25 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
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: object_comparator
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: reek
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 5.0.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.58.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.58.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: SQL builder for writing SQL queries
126
+ email:
127
+ - marcelo.jacobus@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".reek"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".ruby-version"
137
+ - ".travis.yml"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - bin/console
144
+ - bin/setup
145
+ - examples/config.rb
146
+ - examples/select.rb
147
+ - koine-sql_builder.gemspec
148
+ - lib/koine/sql_builder.rb
149
+ - lib/koine/sql_builder/adapter.rb
150
+ - lib/koine/sql_builder/builder.rb
151
+ - lib/koine/sql_builder/condition.rb
152
+ - lib/koine/sql_builder/conditions/comparison.rb
153
+ - lib/koine/sql_builder/conditions/equal.rb
154
+ - lib/koine/sql_builder/conditions/not_equal.rb
155
+ - lib/koine/sql_builder/from.rb
156
+ - lib/koine/sql_builder/select.rb
157
+ - lib/koine/sql_builder/select_builder.rb
158
+ - lib/koine/sql_builder/version.rb
159
+ - lib/koine/sql_builder/where.rb
160
+ homepage: https://github.com/mjacobus/koine-sql_builder
161
+ licenses:
162
+ - MIT
163
+ metadata:
164
+ allowed_push_host: https://rubygems.org
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.7.6
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: SQL builder for writing SQL queries
185
+ test_files: []