diff --git a/db/seeds.cr b/db/seeds.cr index ca7311a..4053cf4 100644 --- a/db/seeds.cr +++ b/db/seeds.cr @@ -1,6 +1,5 @@ require "granite" -require "../spec/granite/config" -require "../spec/models/*" +require "../spec/support/*" unless Test.first 10.times do |num| diff --git a/shard.yml b/shard.yml index 3ea12ab..3c136c7 100644 --- a/shard.yml +++ b/shard.yml @@ -15,7 +15,7 @@ targets: development_dependencies: granite: github: amberframework/granite - version: ~> 0.14.0 + branch: master pg: github: will/crystal-pg diff --git a/spec/granite/granite_spec.cr b/spec/granite/granite_spec.cr deleted file mode 100644 index 86d54a0..0000000 --- a/spec/granite/granite_spec.cr +++ /dev/null @@ -1,17 +0,0 @@ -require "./spec_helper" - -describe Granite::Base do - it "builds limit and offset" do - Test.page(10).per(8).raw_sql.chomp.split - .should eq %w(SELECT id, num, created_at, updated_at FROM tests ORDER BY id DESC LIMIT 8 OFFSET 72) - end - - it "selects with limit and offset" do - tests = Test.page(3).per(3).select - - tests.size.should eq(3) - tests.each_with_index do |test, i| - test.num.should eq(3 - i) - end - end -end diff --git a/spec/granite/spec_helper.cr b/spec/granite/spec_helper.cr deleted file mode 100644 index cba784c..0000000 --- a/spec/granite/spec_helper.cr +++ /dev/null @@ -1 +0,0 @@ -require "../spec_helper" diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index c5dc17a..1ba635b 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,6 +1,4 @@ require "spec" -require "granite" -require "./granite/config" require "../src/granite" -require "./models/*" require "../src/shale" +require "./support/*" diff --git a/spec/granite/config.cr b/spec/support/config.cr similarity index 100% rename from spec/granite/config.cr rename to spec/support/config.cr diff --git a/spec/models/test.cr b/spec/support/test.cr similarity index 100% rename from spec/models/test.cr rename to spec/support/test.cr diff --git a/src/granite.cr b/src/granite.cr index 1dab415..06a7b9e 100644 --- a/src/granite.cr +++ b/src/granite.cr @@ -1,9 +1,3 @@ require "granite" require "./shale" require "./granite/**" - -class Granite::Base - macro inherited - extend Shale::Granite::Query::BuilderMethods - end -end diff --git a/src/granite/adapter.cr b/src/granite/adapter.cr index c886009..3aea2c0 100644 --- a/src/granite/adapter.cr +++ b/src/granite/adapter.cr @@ -1,12 +1,12 @@ class Shale::Granite::Adapter < Shale::BaseAdapter def count(model) - model.count.run + model.count.to_i end def select(model) model - .page(page) - .per(per) + .offset((page - 1) * per) + .limit(per) .order({order => direction}) .select end diff --git a/src/granite/query/builder.cr b/src/granite/query/builder.cr deleted file mode 100644 index e616766..0000000 --- a/src/granite/query/builder.cr +++ /dev/null @@ -1,42 +0,0 @@ -class Granite::Query::Builder(Model) - getter page : Int64? - getter per_page : Int64? - - def assembler - Shale::Granite::Query::PGAssembler(Model).new self - end - - def page(num) - @page = num.to_i64 - - self - end - - def per(num) - @per_page = num.to_i64 - - self - end - - def where(matches : Hash(Symbol | String, Symbol | String)) - matches.each do |field, data| - @where_fields[field.to_s] = data - end - - self - end - - def order(dsl : Hash(Symbol | String, Symbol | String)) - dsl.each do |field, direction| - direction = if direction == "desc" || direction == :desc - Sort::Descending - else - Sort::Ascending - end - - @order_fields << {field: field.to_s, direction: direction} - end - - self - end -end diff --git a/src/granite/query/builder_methods.cr b/src/granite/query/builder_methods.cr deleted file mode 100644 index d8bd482..0000000 --- a/src/granite/query/builder_methods.cr +++ /dev/null @@ -1,9 +0,0 @@ -module Shale::Granite::Query::BuilderMethods - include ::Granite::Query::BuilderMethods - - def __builder - ::Granite::Query::Builder(self).new - end - - delegate page, per, where, order, to: __builder -end diff --git a/src/granite/query/pg_assembler.cr b/src/granite/query/pg_assembler.cr deleted file mode 100644 index d1930fb..0000000 --- a/src/granite/query/pg_assembler.cr +++ /dev/null @@ -1,40 +0,0 @@ -class Shale::Granite::Query::PGAssembler(Model) < Granite::Query::Assembler::Postgresql(Model) - @numbered_parameters = [] of DB::Any - @aggregate_fields = [] of String - - def initialize(@query : ::Granite::Query::Builder(Model)) - end - - def build_limit_offset - if @query.page && @query.per_page - "LIMIT #{@query.per_page} OFFSET #{@query.per_page.not_nil! * (@query.page.not_nil! - 1)}" - else - "" - end - end - - def count : ::Granite::Query::Executor::Value(Model, Int64) - sql = <<-SQL - SELECT COUNT(*) - FROM #{table_name} - #{build_where} - #{build_group_by} - #{build_order false} - #{build_limit_offset} - SQL - - ::Granite::Query::Executor::Value(Model, Int64).new sql, numbered_parameters, default: 0_i64 - end - - def select : ::Granite::Query::Executor::List(Model) - sql = <<-SQL - SELECT #{field_list} - FROM #{table_name} - #{build_where} - #{build_order} - #{build_limit_offset} - SQL - - ::Granite::Query::Executor::List(Model).new sql, numbered_parameters - end -end