-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8ba7f1b
Showing
16 changed files
with
4,710 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: "26.0.2" | ||
gleam-version: "1.4.0-rc1" | ||
rebar3-version: "3" | ||
# elixir-version: "1.15.4" | ||
- run: gleam deps download | ||
- run: gleam test | ||
- run: gleam format --check src test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.beam | ||
*.ez | ||
/build | ||
erl_crash.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# squirrel | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/squirrel)](https://hex.pm/packages/squirrel) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/squirrel/) | ||
|
||
```gleam | ||
import squirrel | ||
pub fn main() { | ||
// TODO: An example of the project in use | ||
} | ||
``` | ||
|
||
Further documentation can be found at <https://hexdocs.pm/squirrel>. | ||
|
||
## Development | ||
|
||
```sh | ||
gleam run # Run the project | ||
gleam test # Run the tests | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name = "squirrel" | ||
version = "0.1.0" | ||
description = "🐿️ Type safe SQL in Gleam" | ||
licences = ["Apache-2.0"] | ||
repository = { type = "github", user = "giacomocavalieri", repo = "squirrel" } | ||
|
||
[dependencies] | ||
gleam_stdlib = ">= 0.34.0 and < 2.0.0" | ||
simplifile = ">= 2.0.1 and < 3.0.0" | ||
eval = ">= 1.0.0 and < 2.0.0" | ||
gleam_json = ">= 1.0.0 and < 2.0.0" | ||
mug = ">= 1.1.0 and < 2.0.0" | ||
glam = ">= 2.0.1 and < 3.0.0" | ||
justin = ">= 1.0.1 and < 2.0.0" | ||
filepath = ">= 1.0.0 and < 2.0.0" | ||
gleam_community_ansi = ">= 1.4.0 and < 2.0.0" | ||
term_size = ">= 1.0.1 and < 2.0.0" | ||
gleam_community_colour = ">= 1.4.0 and < 2.0.0" | ||
argv = ">= 1.0.2 and < 2.0.0" | ||
|
||
[dev-dependencies] | ||
gleeunit = ">= 1.0.0 and < 2.0.0" | ||
birdie = ">= 1.1.8 and < 2.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This file was generated by Gleam | ||
# You typically do not need to edit this file | ||
|
||
packages = [ | ||
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" }, | ||
{ name = "birdie", version = "1.1.8", build_tools = ["gleam"], requirements = ["argv", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "D225C0A3035FCD73A88402925A903AAD3567A1515C9EAE8364F11C17AD1805BB" }, | ||
{ name = "eval", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "eval", source = "hex", outer_checksum = "264DAF4B49DF807F303CA4A4E4EBC012070429E40BE384C58FE094C4958F9BDA" }, | ||
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, | ||
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" }, | ||
{ name = "glance", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "8F3314D27773B7C3B9FB58D8C02C634290422CE531988C0394FA0DF8676B964D" }, | ||
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" }, | ||
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" }, | ||
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, | ||
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" }, | ||
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" }, | ||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, | ||
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" }, | ||
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" }, | ||
{ name = "mug", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "mug", source = "hex", outer_checksum = "85A61E67A7A8C25F4460D9CBEF1C09C68FC06ABBC6FF893B0A1F42AE01CBB546" }, | ||
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" }, | ||
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" }, | ||
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" }, | ||
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, | ||
{ name = "trie_again", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "5B19176F52B1BD98831B57FDC97BD1F88C8A403D6D8C63471407E78598E27184" }, | ||
] | ||
|
||
[requirements] | ||
argv = { version = ">= 1.0.2 and < 2.0.0" } | ||
birdie = { version = ">= 1.1.8 and < 2.0.0" } | ||
eval = { version = ">= 1.0.0 and < 2.0.0" } | ||
filepath = { version = ">= 1.0.0 and < 2.0.0" } | ||
glam = { version = ">= 2.0.1 and < 3.0.0" } | ||
gleam_community_ansi = { version = ">= 1.4.0 and < 2.0.0" } | ||
gleam_community_colour = { version = ">= 1.4.0 and < 2.0.0" } | ||
gleam_json = { version = ">= 1.0.0 and < 2.0.0" } | ||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } | ||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" } | ||
justin = { version = ">= 1.0.1 and < 2.0.0" } | ||
mug = { version = ">= 1.1.0 and < 2.0.0" } | ||
simplifile = { version = ">= 2.0.1 and < 3.0.0" } | ||
term_size = { version = ">= 1.0.1 and < 2.0.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import gleam/pgo | ||
import decode | ||
|
||
/// A row you get from running the `prova` query | ||
/// defined in `prova.sql`. | ||
/// | ||
/// > 🐿️ This type definition was generated automatically using v1.0.0 of the | ||
/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). | ||
/// | ||
pub type ProvaRow { | ||
ProvaRow(user_id: String, username: String, password: String) | ||
} | ||
|
||
/// Runs the `prova` query | ||
/// defined in `prova.sql`. | ||
/// | ||
/// > 🐿️ This function was generated automatically using v1.0.0 of the | ||
/// > [squirrel package](https://github.com/giacomocavalieri/squirrel). | ||
/// | ||
pub fn prova(db) { | ||
let decoder = | ||
decode.into({ | ||
use user_id <- decode.parameter | ||
use username <- decode.parameter | ||
use password <- decode.parameter | ||
ProvaRow(user_id: user_id, username: username, password: password) | ||
}) | ||
|> decode.field(0, decode.string) | ||
|> decode.field(1, decode.string) | ||
|> decode.field(2, decode.string) | ||
|
||
"-- name: prova | ||
select * from kira_user; | ||
" | ||
|> pgo.execute(db, [], decode.from(decoder, _)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: prova | ||
select * from kira_user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
import argv | ||
import filepath | ||
import glam/doc | ||
import gleam/int | ||
import gleam/io | ||
import gleam/list | ||
import gleam/result | ||
import gleam_community/ansi | ||
import simplifile | ||
import squirrel/internal/cli | ||
import squirrel/internal/database/postgres | ||
import squirrel/internal/error.{type Error, CannotWriteToFile, FileWithNoQueries} | ||
import squirrel/internal/query.{type TypedQuery} | ||
import term_size | ||
|
||
// TODO LIST: | ||
// - [ ] How does one decide where the query goes? | ||
// - [ ] How do I deal with duplicate names then? | ||
// - [ ] Somehow it treats int[][] as int[] | ||
|
||
const squirrel_version = "v1.0.0" | ||
|
||
// const clear_screen = "\u{001B}[H\u{001B}[J" | ||
|
||
/// Entry point for the `squirrel` CLI. | ||
/// | ||
pub fn main() { | ||
let width = term_size.columns() |> result.unwrap(80) | ||
use option <- cli.run(squirrel_cli(), argv.load().arguments) | ||
case option { | ||
Base(version: False) -> cli.PrintFullHelp | ||
Base(version: True) -> { | ||
io.println(squirrel_version) | ||
cli.Done | ||
} | ||
|
||
Postgres(input: input, output: output, connection: connection) -> { | ||
run(input, output, postgres.main(_, connection)) | ||
|> pretty_result | ||
|> doc.to_string(width) | ||
|> io.println | ||
|
||
cli.Done | ||
} | ||
} | ||
} | ||
|
||
fn run(input_file: String, output_file: String, fun: _) { | ||
use queries <- result.try(query.from_file(input_file)) | ||
use queries <- result.try(fun(queries)) | ||
use _ <- result.try(case queries { | ||
[] -> Error(FileWithNoQueries(input_file)) | ||
_ -> Ok(Nil) | ||
}) | ||
write_queries(queries, to: output_file) | ||
} | ||
|
||
fn write_queries( | ||
queries: List(TypedQuery), | ||
to file: String, | ||
) -> Result(Int, Error) { | ||
let directory = filepath.directory_name(file) | ||
let _ = simplifile.create_directory_all(directory) | ||
|
||
let imports = "import gleam/pgo\nimport decode\n" | ||
let #(count, code) = { | ||
use #(count, code), query <- list.fold(queries, #(0, imports)) | ||
#(count + 1, code <> "\n" <> query.generate_code(squirrel_version, query)) | ||
} | ||
|
||
let try_write = | ||
simplifile.write(code, to: file) | ||
|> result.map_error(CannotWriteToFile(file, _)) | ||
|
||
use _ <- result.try(try_write) | ||
Ok(count) | ||
} | ||
|
||
fn pretty_result(result: Result(Int, Error)) -> doc.Document { | ||
case result { | ||
Error(error) -> error.to_doc(error) | ||
Ok(n) -> | ||
{ | ||
"🐿️ Generated code for " | ||
<> int.to_string(n) | ||
<> " " | ||
<> pluralise(n, "query", "queries") | ||
} | ||
|> ansi.green | ||
|> doc.from_string | ||
} | ||
} | ||
|
||
fn pluralise(count: Int, singular: String, plural: String) -> String { | ||
case count { | ||
1 -> singular | ||
_ -> plural | ||
} | ||
} | ||
|
||
// --- CLI --------------------------------------------------------------------- | ||
|
||
type SquirrelArgs { | ||
Base(version: Bool) | ||
Postgres( | ||
input: String, | ||
output: String, | ||
connection: postgres.ConnectionOptions, | ||
) | ||
} | ||
|
||
fn squirrel_cli() -> cli.Cli(SquirrelArgs) { | ||
cli.new("gleam run -m squirrel", base()) | ||
|> cli.explain("🐿️ squirrel - " <> squirrel_version) | ||
|> cli.add_subcommand( | ||
cli.new("postgres", postgres()) | ||
|> cli.summarise("generate code for Postgres") | ||
|> cli.explain( | ||
"Perform code generation for a Postgres database.\n" | ||
<> "The generated code will use the `gleam_pgo` package to connect to a " | ||
<> "Postgres database and the `decode` package to decode the read rows.", | ||
), | ||
) | ||
} | ||
|
||
fn base() -> cli.Command(SquirrelArgs) { | ||
cli.command({ | ||
use version <- cli.next | ||
Base(version: version) | ||
}) | ||
|> cli.add(cli.flag("version", "print version")) | ||
} | ||
|
||
fn postgres() -> cli.Command(SquirrelArgs) { | ||
cli.command({ | ||
use input <- cli.next | ||
use output <- cli.next | ||
use database <- cli.next | ||
use port <- cli.next | ||
use user <- cli.next | ||
Postgres( | ||
input: input, | ||
output: output, | ||
connection: postgres.ConnectionOptions( | ||
..postgres.default_connection(), | ||
port: port, | ||
database: database, | ||
user: user, | ||
), | ||
) | ||
}) | ||
|> cli.add(input_arg()) | ||
|> cli.add(output_arg()) | ||
|> cli.add(database_arg()) | ||
|> cli.add(port_option(5432)) | ||
|> cli.add(user_option()) | ||
} | ||
|
||
fn input_arg() { | ||
cli.positional( | ||
name: "input", | ||
description: "the input file to read SQL queries from", | ||
) | ||
} | ||
|
||
fn output_arg() { | ||
cli.positional( | ||
name: "output", | ||
description: "the output file to write generated Gleam to", | ||
) | ||
} | ||
|
||
fn database_arg() { | ||
cli.positional( | ||
name: "database", | ||
description: "the name of the database to connect to", | ||
) | ||
} | ||
|
||
fn port_option(default: Int) { | ||
cli.labelled( | ||
long: "port", | ||
description: "the port used to connect to the database", | ||
) | ||
|> cli.validate_if_present(fn(n) { | ||
int.parse(n) | ||
|> result.replace_error("port should be a number") | ||
}) | ||
|> cli.default(default) | ||
} | ||
|
||
fn user_option() { | ||
cli.labelled( | ||
long: "user", | ||
description: "the username used to connect to the database", | ||
) | ||
|> cli.default("root") | ||
} |
Oops, something went wrong.