{"id":21451374,"url":"https://github.com/anizoptera/backt_sql","last_synced_at":"2025-03-17T02:41:39.605Z","repository":{"id":152286689,"uuid":"91980243","full_name":"Anizoptera/BacKT_SQL","owner":"Anizoptera","description":"Convenience Library for SQL Databases written in Kotlin","archived":false,"fork":false,"pushed_at":"2017-09-19T09:18:34.000Z","size":28,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-23T12:27:23.034Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Anizoptera.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-21T18:55:43.000Z","updated_at":"2017-09-18T21:14:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"26da10e2-55fe-4268-baf2-5dd6ab9b1100","html_url":"https://github.com/Anizoptera/BacKT_SQL","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FBacKT_SQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FBacKT_SQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FBacKT_SQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FBacKT_SQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anizoptera","download_url":"https://codeload.github.com/Anizoptera/BacKT_SQL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243963980,"owners_count":20375691,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T04:20:55.314Z","updated_at":"2025-03-17T02:41:39.600Z","avatar_url":"https://github.com/Anizoptera.png","language":"Kotlin","readme":"# BacKT SQL\n\nThis library provides a convenient way to connect with SQL-databases via JDBC, and easily make SQL-requests.\n\n**BacKT SQL** is a part of «**Back-end Kotlin Tool Set**» that consists of:\n\n- [BacKT SQL](https://github.com/Anizoptera/BacKT_SQL) (this one)\n- [BacKT WebServer](https://github.com/Anizoptera/BacKT_WebServer) (a Netty-based web-server)\n\nIt also uses [KotLog](https://github.com/Anizoptera/Kotlin-Logging-Facade) for logging.\n\n## Installation\n\n```gradle\nrepositories {\n\tmaven { url \"http://dl.bintray.com/azadev/maven\" }\n}\n\ndependencies {\n\tcompile \"azadev.backt:backt_sql:0.7.1\"\n}\n```\n\nAnd of course you need a JDBC database connector. For example, MySQL Connector/J:\n\n```gradle\ndependencies {\n\t// ...\n\n\tcompile \"mysql:mysql-connector-java:5.1.42\"\n}\n```\n\n## Usage\n\nBacKT SQL consists of 4 main parts:\n\n1. `Database`\n2. `ConnectionPool`\n3. `QueryBuilder`\n4. Small utilities\n\n### Database\n\nThe main character of the library is the `Database` class. It holds a connection to the database and allows to perform queries and transactions.\n\n```kotlin\n// Must be called once, as your application starts:\nDatabase.loadDriver(\"com.mysql.jdbc.Driver\")\n\nval db = Database.connect(\"jdbc:mysql://localhost:3306/dbname\", \"user\", \"pass\")\nval resultSet = db.executeQuery(\"SELECT * FROM t WHERE id = ?\", 234)\nval count = db.executeUpdate(\"UPDATE t SET id = ?\", 234)\n```\n\n`Database` provides methods to perform transactions: `disableAutoCommit`, `enableAutoCommit`, `commit`, `rollback`. Here is an example usage:\n\n```kotlin\ndb.disableAutoCommit()\n\nval rows = db.executeUpdate(\"UPDATE t SET id = ?\", 234)\n\nif (rows == 0) {\n\t// log error\n\tdb.rollback()\n}\nelse {\n\t// do something else\n\tdb.commit()\n}\n```\n\nRead more about transactions on [JDBC Docs](https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html).\n\n### ConnectionPool\n\nAnother must-have tool is the `ConnectionPool`. It caches database connections so they can be reused for future requests:\n\n```kotlin\nval pool = ConnectionPool(maxSize = 50) { Database.connect(url, user, pass) }\n\n// Obtain a connection:\nval db = pool.obtain()\n\n// ... do some work ...\n\n// Then release:\npool.release(db)\n```\n\nThere is a couple of convenience methods named `use` and `useAndGet`. `useAndGet` returns the result of its lambda, `use` doesn't:\n\n```kotlin\npool.use { db -\u003e\n\tdb.executeUpdate(...)\n}\n\nval res = pool.useAndGet { db -\u003e\n\tdb.executeQuery(...)\n}\n```\n\n### QueryBuilder\n\n`QueryBuilder` helps to build and execute SQL-statements. For example, the methods listed below are performing exacly the same request:\n\n```kotlin\nfun getUser(db: Database, id: Int, email: String): ResultSet {\n\treturn db.executeQuery(\n\t\t\t\"SELECT `name`, `surname` FROM `user` WHERE `id`=$id AND `email`=? LIMIT 1\",\n\t\t\temail\n\t)\n}\n\nfun getUser(db: Database, id: Int, email: String): ResultSet {\n\treturn QueryBuilder()\n\t\t\t.select(\"name\", \"surname\")\n\t\t\t.from(\"user\")\n\t\t\t.where(\"id\", id)\n\t\t\t.wherep(\"email\", email)\n\t\t\t.limit(1)\n\t\t\t.executeQuery(db)\n}\n```\n\n`QueryBuilder` is an extremely useful helper when it comes to build SQL-quesries that depend on some conditions:\n\n```kotlin\nfun getUser(db: Database, id: Int? = null, email: String? = null, order: String? = null, desc: Boolean = false): ResultSet {\n\tval q = QueryBuilder().select().from(\"user\")\n\n\tif (id != null)\n\t\tq.where(\"id\", id)\n\n\tif (email != null)\n\t\tq.wherep(\"email\", email)\n\n\tif (order != null)\n\t\tq.orderBy(order, desc)\n\n\treturn q.executeQuery(db)\n}\n```\n\n### Utilities\n\n`count`, `countByte`, `countLong`, `countFloat`, `countDouble`:\n\n```kotlin\nval countInt = db.executeUpdate(\"UPDATE ...\").count\nval idLong = db.executeQuery(\"SELECT id FROM ...\").countLong\nval numFloat = db.executeQuery(\"SELECT rating FROM ...\").countFloat\n```\n\n`single`, `toList`:\n\n```kotlin\nclass User(res: ResultSet) { ... }\n\nval user = db.executeQuery(\"SELECT * FROM user LIMIT 1\").single(::User)\nval userList = db.executeQuery(\"SELECT * FROM user\").toList(::User)\n```\n\n... and others: `escapeSqlLiteral`, `escapeSqlIdentifier`, `makeValueSet`, `asParameterized`\n\n## License\n\nThis software is released under the MIT License.\nSee [LICENSE.md](LICENSE.md) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fbackt_sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanizoptera%2Fbackt_sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fbackt_sql/lists"}