Skip to content

Commit

Permalink
Add #count
Browse files Browse the repository at this point in the history
  • Loading branch information
maiha committed Feb 7, 2018
1 parent 29f7ea6 commit 2be7554
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/granite_orm/querying/count_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "../../spec_helper"

{% for adapter in GraniteExample::ADAPTERS %}
{% model_constant = "Parent#{adapter.camelcase.id}".id %}

describe "{{ adapter.id }} #count" do
it "returns 0 if no result" do
count = {{ model_constant }}.count
count.should eq 0
end

it "returns a number of the all records for the model" do
2.times do |i|
{{ model_constant }}.new(name: "model_#{i}").tap(&.save)
end

count = {{ model_constant }}.count
count.should eq 2
end
end

{% end %}
5 changes: 5 additions & 0 deletions src/granite_orm/querying.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ module Granite::ORM::Querying
end
end

# count returns a count of all the records
def count : Int64
scalar "select count(*) from #{@@table_name}", &.to_s.to_i64
end

def exec(clause = "")
@@adapter.open { |db| db.exec(clause) }
end
Expand Down

0 comments on commit 2be7554

Please sign in to comment.