Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dj/include query builder #290

Merged
merged 5 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/granite/query/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec"
require "db"
require "../../../src/query_builder"
require "../../../src/granite/query/builder"

class Model
def self.table_name
Expand Down
1 change: 0 additions & 1 deletion src/granite.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ module Granite
end

require "./granite/base"
require "./query_builder"
3 changes: 3 additions & 0 deletions src/granite/base.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require "./association_collection"
require "./associations"
require "./callbacks"
require "./fields"
require "./query/executors/base"
require "./query/**"
require "./querying"
require "./settings"
require "./table"
Expand All @@ -27,6 +29,7 @@ class Granite::Base
include Select

extend Querying
extend Query::BuilderMethods
extend Transactions::ClassMethods

macro inherited
Expand Down
15 changes: 8 additions & 7 deletions src/granite/query/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ class Granite::Query::Builder(Model)
assembler.count
end

def first : Model?
first(1).first?
end

def first(n : Int32) : Executor::List(Model)
assembler.first(n)
end
# TODO: replace `querying.first` with this
# def first : Model?
# first(1).first?
# end

# def first(n : Int32) : Executor::List(Model)
# assembler.first(n)
# end

def any? : Bool
!first.nil?
Expand Down
18 changes: 5 additions & 13 deletions src/granite/query/builder_methods.cr
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
# DSL to be extended into a model
# To activate, simply
#
# class Model < Granite::Base
# extend Query::BuilderMethods
# end
module Granite::Query::BuilderMethods
def __builder
db_type = case adapter.class
when Granite::Adapter::Pg
db_type = case adapter.class.to_s
when "Granite::Adapter::Pg"
Granite::Query::Builder::DbType::Pg
when Granite::Adapter::Mysql
when "Granite::Adapter::Mysql"
Granite::Query::Builder::DbType::Mysql
when Granite::Adapter::Sqlite
Granite::Query::Builder::DbType::Sqlite
else
raise "Adapter not supported #{adapter.class}"
Granite::Query::Builder::DbType::Sqlite
end

Builder(self).new(db_type)
end

delegate where, count, order, offset, limit, first, to: __builder
delegate where, order, offset, limit, to: __builder
end
6 changes: 0 additions & 6 deletions src/granite/querying.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ module Granite::Querying
end
end

# Clear is used to remove all rows from the table and reset the counter for
# the primary key.
def clear
@@adapter.clear @@table_name
end

def raw_all(clause = "", params = [] of DB::Any)
rows = [] of self
@@adapter.select(@@select, clause, params) do |results|
Expand Down
4 changes: 4 additions & 0 deletions src/granite/transactions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ require "./exceptions"

module Granite::Transactions
module ClassMethods
disable_granite_docs? def clear
@@adapter.clear @@table_name
end

disable_granite_docs? def create(**args)
create(args.to_h)
end
Expand Down
5 changes: 0 additions & 5 deletions src/query_builder.cr

This file was deleted.