### Execute Query to Get All Members Source: https://github.com/seratch/kotliquery/blob/master/README.md Executes the query to retrieve all members as a list of Member objects. ```kotlin val allMembers: List = session.run(allMembersQuery) ``` -------------------------------- ### Execute Query to Get All IDs Source: https://github.com/seratch/kotliquery/blob/master/README.md Executes a previously created Query object using a session to retrieve a list of integers representing member IDs. ```kotlin val allIds: List = session.run(allIdsQuery) ``` -------------------------------- ### Use Session with Use Block Source: https://github.com/seratch/kotliquery/blob/master/README.md Demonstrates using a session within a `use` block for automatic resource management, with strict mode enabled. ```kotlin // an auto-closing code block for session sessionOf(HikariCP.dataSource(), strict = true).use { session -> } ``` -------------------------------- ### Initialize Session with Strict Mode Source: https://github.com/seratch/kotliquery/blob/master/README.md Initializes a Session object with strict mode enabled to throw an error if `asSingle` encounters multiple rows. ```kotlin // Session object constructor val session = Session(HikariCP.dataSource(), strict = true) ``` -------------------------------- ### Query with Helper for Typed Parameters Source: https://github.com/seratch/kotliquery/blob/master/README.md Uses a helper method to specify parameter types, providing a more concise syntax for typed parameters. ```kotlin queryOf( """ select id, name from members where ? is null or ? = name """, null.param(), null.param() ) ``` -------------------------------- ### Execute DDL Statement Source: https://github.com/seratch/kotliquery/blob/master/README.md Use the `asExecute` method on a query object to run Data Definition Language (DDL) statements like 'create table'. It returns a Boolean indicating success. ```kotlin session.run(queryOf(""" create table members ( id serial not null primary key, name varchar(64), created_at timestamp not null ) """).asExecute) // returns Boolean ``` -------------------------------- ### Gradle Build Configuration Source: https://github.com/seratch/kotliquery/blob/master/README.md Add KotliQuery and H2 database dependencies to your Gradle project. Ensure Kotlin plugin and repositories are configured. ```groovy apply plugin: 'kotlin' buildscript { ext.kotlin_version = '1.7.20' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { mavenCentral() } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'com.github.seratch:kotliquery:1.9.0' implementation 'com.h2database:h2:2.1.214' } ``` -------------------------------- ### Create DB Session Source: https://github.com/seratch/kotliquery/blob/master/README.md Instantiate a KotliQuery Session object using a JDBC URL, username, and password. This object wraps a java.sql.Connection. ```kotlin import kotliquery.* val session = sessionOf("jdbc:h2:mem:hello", "user", "pass") ``` -------------------------------- ### Create Query for All Members Source: https://github.com/seratch/kotliquery/blob/master/README.md Creates a Query object to fetch all members, mapping each row to a Member object. ```kotlin val allMembersQuery = queryOf("select id, name, created_at from members").map(toMember).asList ``` -------------------------------- ### Configure HikariCP Connection Pool Source: https://github.com/seratch/kotliquery/blob/master/README.md Set up HikariCP as the default data source for KotliQuery. This is recommended for production environments for better performance. ```kotlin HikariCP.default("jdbc:h2:mem:hello", "user", "pass") sessionOf(HikariCP.dataSource()).use { session -> // working with the session } ``` -------------------------------- ### Process Large Datasets with forEach Source: https://github.com/seratch/kotliquery/blob/master/README.md Iterates over query results row by row using `forEach` to minimize memory consumption when dealing with large datasets. ```kotlin session.forEach(queryOf("select id from members")) { row -> // working with large data set }) ``` -------------------------------- ### Create Query for All IDs Source: https://github.com/seratch/kotliquery/blob/master/README.md Creates a reusable Query object to fetch all IDs from the members table. The SQL is not executed at this stage. ```kotlin val allIdsQuery = queryOf("select id from members").map { row -> row.int("id") }.asList ``` -------------------------------- ### Run Queries in a Transaction (Commit) Source: https://github.com/seratch/kotliquery/blob/master/README.md Use `session.transaction` to execute a block of code within a transaction. The transaction is automatically committed upon successful completion of the block. ```kotlin session.transaction { tx -> // begin tx.run(queryOf("insert into members (name, created_at) values (?, ?)", "Alice", Date()).asUpdate) } // commit ``` -------------------------------- ### Query with Named Parameters Source: https://github.com/seratch/kotliquery/blob/master/README.md Constructs a query using named parameters for safe and readable SQL injection prevention. ```kotlin queryOf( """ select id, name, created_at from members where (name = :name) and (age = :age) """, mapOf("name" to "Alice", "age" to 20) ) ``` -------------------------------- ### Create Query for a Single Member by Name Source: https://github.com/seratch/kotliquery/blob/master/README.md Creates a Query object to fetch a single member by name, returning an optional Member object. ```kotlin val aliceQuery = queryOf("select id, name, created_at from members where name = ?", "Alice").map(toMember).asSingle ``` -------------------------------- ### Run Queries in a Transaction (Rollback) Source: https://github.com/seratch/kotliquery/blob/master/README.md If an exception is thrown within the `session.transaction` block, the transaction is automatically rolled back. This ensures data integrity by reverting any changes made during the transaction. ```kotlin session.transaction { tx -> // begin tx.run(queryOf("update members set name = ? where id = ?", "Chris", 1).asUpdate) throw RuntimeException() // rollback } ``` -------------------------------- ### Query with Explicit Typed Parameters Source: https://github.com/seratch/kotliquery/blob/master/README.md Binds parameters to a query by explicitly specifying their Java types using the `Parameter` class. ```kotlin val param = Parameter(param, String::class.java) queryOf( """ select id, name from members where ? is null or ? = name """, param, param ) ``` -------------------------------- ### Define Member Data Class and Mapper Source: https://github.com/seratch/kotliquery/blob/master/README.md Defines a data class for Member and a mapping function to convert a database row into a Member object. ```kotlin data class Member( val id: Int, val name: String?, val createdAt: java.time.ZonedDateTime) val toMember: (Row) -> Member = { row -> Member( row.int("id"), row.stringOrNull("name"), row.zonedDateTime("created_at") ) } ``` -------------------------------- ### Execute Query for a Single Member Source: https://github.com/seratch/kotliquery/blob/master/README.md Executes the query to retrieve a single member, which may be null if no matching record is found. ```kotlin val alice: Member? = session.run(aliceQuery) ``` -------------------------------- ### Execute Update Operations Source: https://github.com/seratch/kotliquery/blob/master/README.md Perform insert, update, or delete operations using the `asUpdate` method. This method sets the underlying JDBC Statement method to `executeUpdate` and returns the number of affected rows. ```kotlin val insertQuery: String = "insert into members (name, created_at) values (?, ?)" session.run(queryOf(insertQuery, "Alice", Date()).asUpdate) // returns effected row count session.run(queryOf(insertQuery, "Bob", Date()).asUpdate) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.