Skip to content

Commit

Permalink
rename DBX::QueryExecutor to DBX::Query
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolab committed Dec 31, 2020
1 parent 25c22d1 commit 78bd537
Show file tree
Hide file tree
Showing 19 changed files with 610 additions and 603 deletions.
6 changes: 3 additions & 3 deletions guide/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DB_CONN = DBX.open("app", "postgres://...", strict: true)
DB_ADAPTER = DBX::Adapter::PostgreSQL.new(DB_CONN)
def new_query
DBX::QueryExecutor.new(DB_ADAPTER)
DBX::Query.new(DB_ADAPTER)
end
```

Expand Down Expand Up @@ -50,7 +50,7 @@ user = new_query
pp user
```

> Note: [to_o!](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#to_o!(astypes)-instance-method) is an alias of [query_one!](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#query_one!(astypes)-instance-method), see [querying](/guide/querying.md) for more details.
> Note: [to_o!](https://nicolab.github.io/crystal-dbx/DBX/Query.html#to_o!(astypes)-instance-method) is an alias of [query_one!](https://nicolab.github.io/crystal-dbx/DBX/Query.html#query_one!(astypes)-instance-method), see [querying](/guide/querying.md) for more details.
Or `create!` method (helper), insert one, returning the new record:

Expand Down Expand Up @@ -115,7 +115,7 @@ users = new_query
.to_a({id: String, username: String, email: String})
```

Find and get a [scalar](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#scalar-instance-method) value:
Find and get a [scalar](https://nicolab.github.io/crystal-dbx/DBX/Query.html#scalar-instance-method) value:

```crystal
id = new_query
Expand Down
26 changes: 13 additions & 13 deletions guide/querying.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Querying

DBX allows several ways to make queries, from low level (crystal-db),
to query builder (query executor) and ORM (model query).
to query builder and ORM (model query).

DBX provides a query builder that can be used directly (example: `query.find(:users).where(:username, "foo")`)
or through models (example: `User.find.where(:username, "foo")`).
Expand Down Expand Up @@ -55,7 +55,7 @@ pp user

For a more advanced model system, see [ORM: Model](/guide/orm/model.md).

## Query builder
## Build query only

To build queries only, then get the SQL string and its array of arguments.

Expand Down Expand Up @@ -85,7 +85,7 @@ pp args

> See [API: QueryBuilder](https://nicolab.github.io/crystal-dbx/DBX/QueryBuilder.html)
### Query executor
### Query

To build and execute the queries.

Expand All @@ -100,7 +100,7 @@ DB_CONN = DBX.open("app", "postgres://...", strict: true)
DB_ADAPTER = DBX::Adapter::PostgreSQL.new(DB_CONN)
def new_query
DBX::QueryExecutor.new(DB_ADAPTER)
DBX::Query.new(DB_ADAPTER)
end
```

Expand All @@ -119,7 +119,7 @@ users = new_query.find(:users).to_a
pp users
```

> See [API: QueryExecutor](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html)
> See [API: Query](https://nicolab.github.io/crystal-dbx/DBX/Query.html)
### ORM - Model query

Expand Down Expand Up @@ -186,30 +186,30 @@ Remember:
> `require "dbx/adapter/pg"` for PostgreSQL.
> `require "dbx/adapter/sqlite"` for SQLite.
💡 `dbx/query_builder` is the module that provides [DBX::QueryBuilder](https://nicolab.github.io/crystal-dbx/DBX/QueryBuilder.html) and [DBX::QueryExecutor](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html).
💡 `dbx/query_builder` is the module that provides [DBX::QueryBuilder](https://nicolab.github.io/crystal-dbx/DBX/QueryBuilder.html) and [DBX::Query](https://nicolab.github.io/crystal-dbx/DBX/Query.html).

> `DBX::QueryBuilder` only takes care of building the query intended to be executed to the database.
>
> `DBX::QueryExecutor` builds the query with `DBX::QueryBuilder` and executes it to the database.
> `DBX::Query` builds the query with `DBX::QueryBuilder` and executes it to the database.
💡 [to_o](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#to_o(astypes)-instance-method) is the same as [query_one](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#query_one(astypes)-instance-method).
💡 [to_o](https://nicolab.github.io/crystal-dbx/DBX/Query.html#to_o(astypes)-instance-method) is the same as [query_one](https://nicolab.github.io/crystal-dbx/DBX/Query.html#query_one(astypes)-instance-method).

> Mnemonic: `to_o` (`query_one`) - to one, to object.
> Performs the query (`#query_one`) and returns the data (one object).
> Returns [DB::ResultSet](https://crystal-lang.github.io/crystal-db/api/latest/DB/ResultSet.html), the response of a query performed on a [DB::Database](https://crystal-lang.github.io/crystal-db/api/latest/DB/Database.html).
💡 [to_a](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#to_a(astypes)-instance-method) is the same as [query_all](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#query_all(astypes)-instance-method).
💡 [to_a](https://nicolab.github.io/crystal-dbx/DBX/Query.html#to_a(astypes)-instance-method) is the same as [query_all](https://nicolab.github.io/crystal-dbx/DBX/Query.html#query_all(astypes)-instance-method).

> Mnemonic: `to_a` (`query_all`) - to all, to array.
> Performs the query (`#query_all`) and returns the data (array of object).
> Returns [DB::ResultSet](https://crystal-lang.github.io/crystal-db/api/latest/DB/ResultSet.html), the response of a query performed on a [DB::Database](https://crystal-lang.github.io/crystal-db/api/latest/DB/Database.html).
💡 [scalar](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#scalar-instance-method)
💡 [scalar](https://nicolab.github.io/crystal-dbx/DBX/Query.html#scalar-instance-method)

> Performs the [query](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#query-instance-method) and returns a single scalar value.
> Performs the [query](https://nicolab.github.io/crystal-dbx/DBX/Query.html#query-instance-method) and returns a single scalar value.
> Returns a single scalar value (`String` or `Int32` or `Int64` or another Crystal type).
💡 [exec](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html#exec-instance-method)
💡 [exec](https://nicolab.github.io/crystal-dbx/DBX/Query.html#exec-instance-method)

> Execution that does not wait for data (scalar or object or array) from the database.
> Returns [DB::ExecResult](https://crystal-lang.github.io/crystal-db/api/latest/DB/ExecResult.html), result of a `#exec` statement.
Expand All @@ -221,5 +221,5 @@ To go further, see:
* [Guide: ORM](/guide/orm/README.md)
* [API: ModelQuery](https://nicolab.github.io/crystal-dbx/DBX/ORM/ModelQuery.html)
* [API: QueryBuilder](https://nicolab.github.io/crystal-dbx/DBX/QueryBuilder.html)
* [API: QueryExecutor](https://nicolab.github.io/crystal-dbx/DBX/QueryExecutor.html)
* [API: Query](https://nicolab.github.io/crystal-dbx/DBX/Query.html)
* [Troubleshooting](/guide/troubleshooting.md)
4 changes: 2 additions & 2 deletions guide/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
The goal of DBX is to bring a practical abstraction to crystal-db and its drivers.
As a result, DBX leaves a lot of freedom to implement models and queries.
This can lead to errors.
For example at each end of query you have to choose the right executor
For example at each end of query you have to choose the right return of execution
(`to_o` / `to_o!` to force the return of a single resource,
`to_a` to get an array of one or more resources).
This requires some knowledge of _SQL_ and [crystal-db](https://crystal-lang.github.io/crystal-db/api/latest/DB/QueryMethods.html).
Expand All @@ -27,4 +27,4 @@ To help with this, the DBX query builder has the `returning` method
which accepts one or more fields (`*` (wildcard) for all SQL fields).

Also the `create!` method handles all this for you.
This method is accessible via `DBX::QueryExecutor` and the models.
This method is accessible via `DBX::Query` and the models.
4 changes: 2 additions & 2 deletions spec/adapter/pg_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
alias DBAdapter = DBX::Adapter::PostgreSQL
ADAPTER_NAME = :pg

def new_query_executor
DBX::QueryExecutor.new(DBAdapter.new(db_open))
def new_query
DBX::Query.new(DBAdapter.new(db_open))
end

# Include all common DB (adapter) tests
Expand Down
4 changes: 2 additions & 2 deletions spec/adapter/sqlite_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
alias DBAdapter = DBX::Adapter::SQLite
ADAPTER_NAME = :sqlite

def new_query_executor
DBX::QueryExecutor.new(DBAdapter.new(db_open))
def new_query
DBX::Query.new(DBAdapter.new(db_open))
end

# Include all common DB (adapter) tests
Expand Down
6 changes: 3 additions & 3 deletions spec/adapter_tests/orm/model/model_query_tests.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

require "../spec_helper"

describe "DBX::QueryExecutor executors" do
describe "DBX::Query executors" do
before_each do
db_open
create_table_test
Expand Down Expand Up @@ -96,10 +96,10 @@ describe "DBX::QueryExecutor executors" do
end

describe "ModelMixin" do
it "query : DBX::QueryExecutor" do
it "query : DBX::Query" do
insert_table_test(name: "DBX", age: 1)

Test.query.should be_a DBX::QueryExecutor
Test.query.should be_a DBX::Query

# Should be a new instance
Test.query.should_not eq Test.query
Expand Down
2 changes: 1 addition & 1 deletion spec/adapter_tests/orm/model/model_tests.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe DBX::ORM::Model do

it "model instance" do
Test.new
Test::ModelQuery.should be_a DBX::QueryExecutor.class
Test::ModelQuery.should be_a DBX::Query.class
Test::Schema.should_not be_nil
Test::Error.should be_a DBX::Error.class
Test::Error.should be_a DB::Error.class
Expand Down
Loading

0 comments on commit 78bd537

Please sign in to comment.