From c8fdb6cb2e40da9312af83298247a5cc6d402a42 Mon Sep 17 00:00:00 2001 From: Anton Davydov Date: Wed, 14 Jun 2017 03:17:21 +0300 Subject: [PATCH 1/4] Add information about relation orders --- data/guides.yml | 2 ++ source/guides/models/query-examples.md | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 source/guides/models/query-examples.md diff --git a/data/guides.yml b/data/guides.yml index 05cf9aeb9..a5a76a377 100644 --- a/data/guides.yml +++ b/data/guides.yml @@ -54,6 +54,8 @@ categories: - path: overview - path: database-configuration - path: repositories + - path: query-examples + title: Query Examples - path: entities - path: data-types - path: postgresql diff --git a/source/guides/models/query-examples.md b/source/guides/models/query-examples.md new file mode 100644 index 000000000..6d41ce195 --- /dev/null +++ b/source/guides/models/query-examples.md @@ -0,0 +1,18 @@ +--- +title: Guides - Query Examples +--- + +## Order + +For add `order` to your relation just call `#order` method with block: + +```ruby +repo = UserRepository.new + +repo.users.order { created_at.desc } +repo.users.order { created_at.asc } + +# you can set any field for order +repo.users.order { name.asc } +repo.users.order { last_name.asc } +``` From a01bd6d706bf734d0100fd9a69669c86e162f8af Mon Sep 17 00:00:00 2001 From: Anton Davydov Date: Wed, 14 Jun 2017 03:19:36 +0300 Subject: [PATCH 2/4] Add information about ilike/like --- source/guides/models/query-examples.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/guides/models/query-examples.md b/source/guides/models/query-examples.md index 6d41ce195..f8de4617b 100644 --- a/source/guides/models/query-examples.md +++ b/source/guides/models/query-examples.md @@ -16,3 +16,14 @@ repo.users.order { created_at.asc } repo.users.order { name.asc } repo.users.order { last_name.asc } ``` + +## SQL Functions + +You can use any SQL functions like `LIKE`, `ILIKE`, etc. For this call this functions in `where` block: + +```ruby +repo = UserRepository.new + +repo.users.where { name.ilike('%anton%') } +repo.users.where { name.like('%anton%') } +``` From 70b66fed6de8e642b967bb9c370278c1896fe2e4 Mon Sep 17 00:00:00 2001 From: Anton Davydov Date: Wed, 14 Jun 2017 03:22:36 +0300 Subject: [PATCH 3/4] Add information about IN and NOT --- source/guides/models/query-examples.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/guides/models/query-examples.md b/source/guides/models/query-examples.md index f8de4617b..fbe1e8d3e 100644 --- a/source/guides/models/query-examples.md +++ b/source/guides/models/query-examples.md @@ -26,4 +26,13 @@ repo = UserRepository.new repo.users.where { name.ilike('%anton%') } repo.users.where { name.like('%anton%') } + +# you can call IN functions too +repo.users.where { id.in(1) } +repo.users.where { id.in(1..100) } +repo.users.where { id > 1 || id < 100 } + +# you can call NOT functions too +repo.users.where { id.not(1) } +repo.users.where { id.not(1..100) } ``` From 2a976753a6b7f6d4a78a661e8a3965f1b610209f Mon Sep 17 00:00:00 2001 From: Anton Davydov Date: Thu, 27 Jul 2017 12:21:18 +0300 Subject: [PATCH 4/4] Add more examples --- source/guides/models/query-examples.md | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/source/guides/models/query-examples.md b/source/guides/models/query-examples.md index fbe1e8d3e..4a0d687c8 100644 --- a/source/guides/models/query-examples.md +++ b/source/guides/models/query-examples.md @@ -15,6 +15,9 @@ repo.users.order { created_at.asc } # you can set any field for order repo.users.order { name.asc } repo.users.order { last_name.asc } + +# you can use other relation for ordering +repo.users.order(repo.books[:title].qualified.asc) ``` ## SQL Functions @@ -36,3 +39,35 @@ repo.users.where { id > 1 || id < 100 } repo.users.where { id.not(1) } repo.users.where { id.not(1..100) } ``` + +```ruby +name = repo.user[:name].qualified +repo.where { length(name) > 10) } +``` + +```ruby +repo.users.where { id.in(1..100) }.limit(100) +``` + +```ruby +repo.users.where(id: 1) == repo.users.by_pk(1) +``` + +## Joins +https://stackoverflow.com/questions/43080678/join-query-in-hanami-model/43156257#43156257 + +```ruby + +repo.users.join(:posts) +repo.users.join(:posts, id: :user_id) +repo.users.join(repo.posts) +``` + +also you can use `#inner_join` method + +## Group by + +Examples with groyp you can find here: +https://github.com/rom-rb/rom-sql/blob/master/spec/unit/relation/group_spec.rb + +Also you can't use `#select` in hanami model. YOu need to use `#project` instead