== Running queries

When you don't need a transaction or run single queries, you can run queries directly on the pool; the pool
will use one of its connection to run the query and return the result to you.

Here is how to run simple queries:

[source,groovy]
----
client.query("SELECT * FROM users WHERE id='julien'", { ar ->
  if (ar.succeeded()) {
    def result = ar.result()
    println("Got ${result.size()} rows ")
  } else {
    println("Failure: ${ar.cause().getMessage()}")
  }
})

----

You can do the same with prepared queries.

The SQL string can refer to parameters by position, using `$1`, `$2`, etc…​

[source,groovy]
----
client.preparedQuery("SELECT * FROM users WHERE id=?", Tuple.of("julien"), { ar ->
  if (ar.succeeded()) {
    def rows = ar.result()
    println("Got ${rows.size()} rows ")
  } else {
    println("Failure: ${ar.cause().getMessage()}")
  }
})

----

Query methods provides an asynchronous `link:../../apidocs/io/vertx/sqlclient/RowSet.html[RowSet]` instance that works for _SELECT_ queries

[source,groovy]
----
client.preparedQuery("SELECT first_name, last_name FROM users", { ar ->
  if (ar.succeeded()) {
    def rows = ar.result()
    rows.each { row ->
      println("User ${row.getString(0)} ${row.getString(1)}")
    }
  } else {
    println("Failure: ${ar.cause().getMessage()}")
  }
})

----

or _UPDATE_/_INSERT_ queries:

[source,groovy]
----
client.preparedQuery("INSERT INTO users (first_name, last_name) VALUES (?, ?)", Tuple.of("Julien", "Viet"), { ar ->
  if (ar.succeeded()) {
    def rows = ar.result()
    println(rows.rowCount())
  } else {
    println("Failure: ${ar.cause().getMessage()}")
  }
})

----

The `link:../../apidocs/io/vertx/sqlclient/Row.html[Row]` gives you access to your data by index

[source,groovy]
----
println("User ${row.getString(0)} ${row.getString(1)}")

----

or by name

[source,groovy]
----
println("User ${row.getString("first_name")} ${row.getString("last_name")}")

----

You can access a wide variety of of types

[source,groovy]
----

def firstName = row.getString("first_name")
def male = row.getBoolean("male")
def age = row.getInteger("age")

// ...


----

You can execute prepared batch

[source,groovy]
----
// TODO

----

You can cache prepared queries:

[source,groovy]
----
// TODO

----

You can fetch generated keys with a 'RETURNING' clause in your query:

[source,groovy]
----
// TODO

----