{"id":18414909,"url":"https://github.com/xerial/sbt-sql","last_synced_at":"2025-06-27T13:39:47.202Z","repository":{"id":37768260,"uuid":"79615219","full_name":"xerial/sbt-sql","owner":"xerial","description":"A sbt plugin for generating useful Scala case classes from SQL files","archived":false,"fork":false,"pushed_at":"2025-03-22T12:07:07.000Z","size":492,"stargazers_count":29,"open_issues_count":3,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T04:56:54.812Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xerial.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-01-21T01:27:55.000Z","updated_at":"2025-03-22T12:07:09.000Z","dependencies_parsed_at":"2023-11-30T05:24:32.540Z","dependency_job_id":"6b9f16fe-f8a4-45ec-822d-73af59219714","html_url":"https://github.com/xerial/sbt-sql","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xerial%2Fsbt-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xerial%2Fsbt-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xerial%2Fsbt-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xerial%2Fsbt-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xerial","download_url":"https://codeload.github.com/xerial/sbt-sql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247645199,"owners_count":20972423,"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-06T03:52:39.839Z","updated_at":"2025-04-07T11:32:58.985Z","avatar_url":"https://github.com/xerial.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"sbt-sql\n====\n\nA sbt plugin for generating model classes from SQL query files in `src/main/sql`.\n\n## Why you need sbt-sql?\n\n - Integrate the power of SQL and Scala\n     - If you write an SQL, it creates a Scala class to read the SQL result.\n - Type safety\n     - No longer need to write a code like `ResultSet.getColumn(\"id\")` etc.\n     - Editors such as IntelliJ can show the SQL result parameter names and types.\n     - For example, if you rename a column name in SQL from `id` to `ID`, the code using `id` will be shown as compilation error. Without sbt-sql, it will be a run-time exception, such as `Unknown column \"id\"`!.\n - Reuse your SQL as a template\n     - You can embed parameters in your SQL with automatically generated Scala functions.\n\n## Usage\n\nsbt-sql version: [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.xerial.sbt/sbt-sql/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.xerial.sbt/sbt-sql) Airframe version: [![wvlet/airframe](https://maven-badges.herokuapp.com/maven-central/org.wvlet.airframe/airframe-codec_2.13/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.wvlet.airframe/airframe-codec_2.13)\n\nsbt-sql supports only sbt 1.8.x or higher.\n\n**project/plugins.sbt**\n```scala\n// For Trino\naddSbtPlugin(\"org.xerial.sbt\" % \"sbt-sql-trino\" % \"(version)\")\n\n// For DuckDB\naddSbtPlugin(\"org.xerial.sbt\" % \"sbt-sql-duckdb\" % \"(version)\")\n\n// For SQLite (available since 0.7.0)\naddSbtPlugin(\"org.xerial.sbt\" % \"sbt-sql-sqlite\" % \"(version)\")\n\n// For Treasure Data Presto\naddSbtPlugin(\"org.xerial.sbt\" % \"sbt-sql-td\" % \"(version)\")\n\n// For Generic JDBC drivers\naddSbtPlugin(\"org.xerial.sbt\" % \"sbt-sql\" % \"(version)\")\n// Add your jdbc driver dependency for checking the result schema\nlibraryDependencies ++= Seq(\n   // Add airframe-codec for mapping JDBC data to Scala objects\n   \"org.wvlet.airframe\" %% \"airframe-codec\" % \"(airframe version)\"\n   // Add your jdbc driver here\n)\n```\n\n**build.sbt**\n\nThis is an example of using a custom JDBC driver:\n\n```scala\nenablePlugins(SbtSQLJDBC)\n\n// Add your JDBC driver to the dependency\n// For using trino-jdbc\nlibraryDependencies ++= Seq(\n  \"org.wvlet.airframe\" %% \"airframe-codec\" % \"(airframe version)\", // Necessary for mapping JDBC ResultSets to model classes\n  \"io.trino\" % \"trino-jdbc\" % \"332\"\n )\n\n// You can change SQL file folder. The default is src/main/sql\n// sqlDir := (sourceDirectory in Compile).value / \"sql\"\n\n// Configure your JDBC driver (e.g., using Trino JDBC)\njdbcDriver := \"io.trino.jdbc.TrinoDriver\"\njdbcURL := \"(jdbc url e.g., jdbc:trino://.... )\"\njdbcUser := \"(jdbc user name)\"\njdbcPassword := \"(jdbc password)\"\n```\n\n### sbt-sql-sqlite\n\n`sbt-sql-sqlite` plugin uses `src/main/sql/sqlite` as the SQL file directory. Configure `jdbcURL` and `jdbcUser` properties:\n```scala\nenablePlugins(SbtSQLSQLite)\n\njdbcURL := \"jdbc:sqlite:(sqlite db file path)\"\n```\n\n### sbt-sql-duckdb\n\n`sbt-sql-duckdb` plugin uses `src/main/sql/duckdb` as the SQL file directory. \n\n```scala\nenablePlugins(SbtSQLDuckDB)\n\n// [optional]\njdbcURL := \"jdbc:duckdb:(duckdb file path)\"\n```\n\n\n### sbt-sql-trino\n\n`sbt-sql-trino` plugin uses `src/main/sql/trino` as the SQL file directory. Configure `jdbcURL` and `jdbcUser` properties:\n\n```scala\nenablePlugins(SbtSQLTrino)\n\njdbcURL := \"jdbc:trino://(your trino server address):443/(catalog name)\"\njdbcUser := \"trino user name\"\n```\n\n### sbt-sql-td (Treasure Data)\n\nTo use [Treasure Data](http://www.treasuredata.com/), set TD_API_KEY environment variable. `jdbcUser` will be set to this value. `src/main/sql/trino` will be the SQL file directory.\n\nAlternatively you can set TD_API_KEY in your sbt credential:\n\n**$HOME/.sbt/1.0/td.sbt**\n```\ncredentials +=\n  Credentials(\"Treasure Data\", \"api-presto.treasuredata.com\", \"(your TD API KEY)\", \"\")\n```\n\n```scala\nenablePlugins(SbtSQLTreasureData)\n```\n\n## Writing SQL\n\n**src/main/sql/trino/sample/nasdaq.sql**\n```sql\n@(start:Long, end:Long)\nselect * from sample_datasets.nasdaq\nwhere time between ${start} and ${end}\n```\n\nFrom this SQL file, sbt-sql generates Scala model classes and several utility methods.\n\n* SQL file can contain template variables `${(Scala expression)}`.\nTo define user input variables, use `@(name:type, ...)`. sbt-sql generates a function to populate them, such as `Nasdaq.select(start = xxxxx, end = yyyyy)`. The variable can have a default value, e.g., `@(x:String=\"hello\")`.\n\n### Template Variable Examples\n\n- Embed a String value\n```sql\n@(symbol:String)\nselect * from sample_datasets.nasdaq\nwhere symbol = '${symbol}'\n```\n\n- Embed an input table name as a variable with the default value `sample_datasets.nasdaq`:\n```sql\n@(table:SQL=\"sample_datasets.nasdaq\")\nselect * from ${table}\n```\nSQL type can be used for embedding an SQL expression as a String.\n\n### Import statement\n\nYou can use your own type for populating SQL templates by importing your class as follows:\n```\n@import your.own.class\n```\n\n### Using Option[X] types\n\nTo generate case classes with Option[X] parameters, add `__optional` suffix to the target column names.  \n\n```\nselect a as a__optional // Option[X] will be used  \nfrom ...\n```\n\n```scala\n@optional(a, b)\nselect a, b, c // Option[A], Option[B], C \n```\n\n### Generated Files\n**target/src_managed/main/sample/nasdaq.scala**\n```scala\npackage sample\n\nobject nasdaq {\n  def path : String = \"/sample/nasdaq.sql\"\n\n  def sql(start:Long, end:Long) : String = {\n    s\"\"\"\"select * from sample_dataest.nasdaq\nwhere time between ${start} and ${end}\n    \"\"\"\n  }\n\n  def select(start:Long, end:Long)(implicit conn:java.sql.Connection): Seq[nasdaq] = ...\n  def selectWith(sql:String)(implicit conn:java.sql.Connection): Seq[nasdaq] = ...\n}\n\ncase class nasdaq(\n  symbol: String,\n  open: Double,\n  volume: Long,\n  high: Double,\n  low: Double,\n  close: Double,\n  time: Long\n)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxerial%2Fsbt-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxerial%2Fsbt-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxerial%2Fsbt-sql/lists"}