Skip to content

Made the select pageable #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The module will reply to all requests. In the message, there will be either a `"
["Mr. Test", "[email protected]", true, 32, 123.45, "2014-04-04"],
["Mrs. Test", "[email protected]", false, 16, 543.21, "2022-02-22"]
]
}
}

* `rows` gives you the number of rows affected by the statement sent to the server. Bear in mind that MySQL only shows a row count on changed rows (DELETE, UPDATE, INSERT statements) whereas PostgreSQL also shows the number of SELECTed rows here.
* `message` is a status message from the server.
Expand Down Expand Up @@ -86,11 +86,14 @@ Use this action to insert new rows into a table. You need to specify a table, th
### select

The `select` action creates a `SELECT` statement to get a projection from a table. You can filter the columns by providing a `fields` array. If you omit the `fields` array, it selects every column available in the table.
A simple paging can be done with the optional parameters limit and offset.

{
"action" : "select",
"table" : "some_test",
"fields" : ["name", "email", "is_male", "age", "money", "wedding_date"], // Optional
"limit" : 10, // Optional
"offset" : 100 // Optional
}

### prepared
Expand Down Expand Up @@ -129,7 +132,7 @@ Takes several statements and wraps them into a single transaction for the server

### raw - Raw commands

Use this action to send arbitrary commands to the database. You should be able to do submit any query or insertion with this command.
Use this action to send arbitrary commands to the database. You should be able to do submit any query or insertion with this command.

Here is an example for creating a table in PostgreSQL:

Expand Down Expand Up @@ -157,7 +160,7 @@ And if you want to drop it again, you can send the following:

You can always use `raw` to do anything on the database. If the statement is a query, it will return its results just like a `select`.

The `select` and `insert` commands are just for you to be able to have a cross-database application in the end. If you do not use `raw`, these commands should create the needed statements for you.
The `select` and `insert` commands are just for you to be able to have a cross-database application in the end. If you do not use `raw`, these commands should create the needed statements for you.

* `update` - Updates rows of a table
* `delete` - Deletes rows from a table
Expand Down Expand Up @@ -202,7 +205,7 @@ These actions are currently not available, but they should be implemented in the
// email VARCHAR(255),
// age INTEGER
// );
{
{
"action" : "create",
"table" : "some_test",
"fields" : ["id PRIMARY KEY", "name VARCHAR(255)", "email VARCHAR(255)", "age INTEGER"]
Expand Down
15 changes: 12 additions & 3 deletions src/main/scala/io/vertx/asyncsql/database/ConnectionHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@ trait ConnectionHandler extends ScalaBusMod {

protected def selectCommand(json: JsonObject): String = {
val table = escapeField(json.getString("table"))
Option(json.getArray("fields")) match {
var offset = json.getInteger("offset")
var limit = json.getInteger("limit")
var stmt = Option(json.getArray("fields")) match {
case Some(fields) => fields.asScala.toStream.map(elem => escapeField(elem.toString)).mkString("SELECT ", ",", " FROM " + table)
case None => "SELECT * FROM " + table
}
if(limit != null) {
stmt += " LIMIT " + limit
}
if(offset != null) {
stmt += " OFFSET " + offset
}
stmt
}

protected def select(json: JsonObject): AsyncReply = AsyncReply(pool.withConnection({ c: Connection =>
Expand Down Expand Up @@ -109,7 +118,7 @@ trait ConnectionHandler extends ScalaBusMod {
}
}))


protected def sendWithPool(fn: Connection => Future[QueryResult]): Future[SyncReply] = pool.withConnection({ c: Connection =>
fn(c) map buildResults recover {
case x: GenericDatabaseException =>
Expand Down Expand Up @@ -159,4 +168,4 @@ trait ConnectionHandler extends ScalaBusMod {
}

private def rowDataToJsonArray(rowData: RowData): JsonArray = Json.arr(rowData.map(dataToJson).toList: _*)
}
}