////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

[[querying-hql]]
== HQL Queries

GORM supports querying using http://docs.jboss.org/hibernate/orm/current/querylanguage/html_single/Hibernate_Query_Language_Guide.html[Hibernate Query Language (HQL)], which is an object-oriented query language similar to SQL but operating on domain class names and properties rather than table and column names.

=== Basic HQL

The following static methods accept HQL strings:

* `find(CharSequence)` — returns the first matching instance
* `findAll(CharSequence)` — returns all matching instances
* `executeQuery(CharSequence)` — returns a list (supports projections)
* `executeUpdate(CharSequence)` — executes a bulk update/delete, returns the count

=== Safe Parameterization with GString

GORM automatically converts Groovy http://docs.groovy-lang.org/latest/html/documentation/#_string_interpolation[GString] interpolations into safe named parameters before the query reaches Hibernate. This is the **preferred** way to pass user-supplied values.

[source,groovy]
----
String title = params.title  // user input

// ✅ SAFE — ${title} is extracted and bound as :p0
List results = Book.findAll("from Book b where b.title = ${title}")

// ✅ SAFE — multiple interpolations become :p0, :p1
List results = Book.findAll(
    "from Book b where b.title like ${title} and b.genre = ${genre}")
----

NOTE: The single-argument overloads accept either a plain `String` (executed as written, exactly as on Hibernate 5) or a Groovy `GString`. A `GString` is never interpolated into the query text — every `${value}` is extracted and bound as a named parameter — so the idiomatic interpolated form above is injection-safe by binding rather than escaping.

[source,groovy]
----
// ✅ SAFE — a static plain String contains no untrusted input
List results = Book.findAll("from Book where active = true")

// ✅ SAFE — the GString value is bound as :p0, never interpolated into the query
List results = Book.findAll("from Book where title = ${userInput}")

// ⚠️ UNSAFE — manually concatenating untrusted input into a plain String is an
// injection risk in any ORM; never build queries this way
String hql = "from Book where title = '" + userInput + "'"
Book.findAll(hql)

// ✅ Instead use the GString form above, or the parameterized overload
Book.findAll("from Book where title = :title", [title: userInput])
----

=== Named Parameters

Use the `(CharSequence, Map)` overload to pass named parameters explicitly. This also accepts a plain `String`, making it safe for dynamically constructed queries where GString syntax is inconvenient.

[source,groovy]
----
// Named parameters — safe with plain String
List results = Book.findAll(
    "from Book b where b.title = :title and b.author = :author",
    [title: params.title, author: params.author])

Book.executeUpdate(
    "update Book set active = :flag where genre = :genre",
    [flag: false, genre: 'Horror'])
----

=== Positional Parameters

[source,groovy]
----
// Positional parameters (?1, ?2, ...)
List results = Book.executeQuery(
    "from Book b where b.title like ?1 and b.genre = ?2",
    ['%Groovy%', 'Tech'])
----

=== Query Settings

`find`, `findAll`, and `executeQuery` overloads accept a settings map as the last argument. Use it for pagination and Hibernate query settings such as `cache`, `readOnly`, `fetchSize`, `timeout`, `flushMode`, and `lock`:

[source,groovy]
----
List results = Book.findAll(
    "from Book b where b.genre = ${genre} order by b.title",
    [max: 10, offset: 20])

List results = Book.executeQuery(
    "from Book b where b.genre = :genre",
    [genre: 'Tech'],
    [max: 5, offset: 0, cache: true])

List locked = Book.findAll(
    "from Book b where b.genre = :genre",
    [genre: 'Tech'],
    [lock: true])
----

When `lock: true` is used, GORM requests a pessimistic write lock and disables query caching for that query.

=== Bulk Updates and Deletes

[source,groovy]
----
// Bulk update with named params
int count = Book.executeUpdate(
    "update Book set active = :flag where publishedYear < :year",
    [flag: false, year: 2000])

// Bulk delete
int count = Book.executeUpdate(
    "delete Book where active = false")
----
