diff --git a/spec/granite/query/spec_helper.cr b/spec/granite/query/spec_helper.cr index aa9646b7..d5fe938e 100644 --- a/spec/granite/query/spec_helper.cr +++ b/spec/granite/query/spec_helper.cr @@ -1,6 +1,6 @@ require "spec" require "db" -require "../../../src/query_builder" +require "../../../src/granite/query/builder" class Model def self.table_name diff --git a/src/granite.cr b/src/granite.cr index b9a58ada..65e7f271 100644 --- a/src/granite.cr +++ b/src/granite.cr @@ -6,4 +6,3 @@ module Granite end require "./granite/base" -require "./query_builder" diff --git a/src/granite/base.cr b/src/granite/base.cr index 00cdda1f..a6385b6e 100644 --- a/src/granite/base.cr +++ b/src/granite/base.cr @@ -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" @@ -27,6 +29,7 @@ class Granite::Base include Select extend Querying + extend Query::BuilderMethods extend Transactions::ClassMethods macro inherited diff --git a/src/granite/query/builder.cr b/src/granite/query/builder.cr index 35633d80..642957bf 100644 --- a/src/granite/query/builder.cr +++ b/src/granite/query/builder.cr @@ -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? diff --git a/src/granite/query/builder_methods.cr b/src/granite/query/builder_methods.cr index c302c062..e3d737e3 100644 --- a/src/granite/query/builder_methods.cr +++ b/src/granite/query/builder_methods.cr @@ -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 diff --git a/src/granite/querying.cr b/src/granite/querying.cr index 5186082c..1113e294 100644 --- a/src/granite/querying.cr +++ b/src/granite/querying.cr @@ -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| diff --git a/src/granite/transactions.cr b/src/granite/transactions.cr index 88772b80..bbd09ad1 100644 --- a/src/granite/transactions.cr +++ b/src/granite/transactions.cr @@ -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 diff --git a/src/query_builder.cr b/src/query_builder.cr deleted file mode 100644 index 19308cdf..00000000 --- a/src/query_builder.cr +++ /dev/null @@ -1,5 +0,0 @@ -module Granite::Query -end - -require "./granite/query/executors/base" -require "./granite/query/**"