Skip to content

Commit

Permalink
emit runnable query in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 1, 2024
1 parent 7597211 commit 7481ffd
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 134 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Autoinsert trailing commas in embedded SQL blocks.
- BREAKING CHANGE: `Null.t` is no longer emitted, all `null` values are autoconverted to `option`. This gives a much more idiomatic ReScript experience.
- Emit actually runnable query in module comment for each query, instead of the original non-valid SQL query.

# 2.4.0

Expand Down
12 changes: 4 additions & 8 deletions packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,15 @@ export async function generateDeclarationFile(
if (typeDec.mode === 'ts') {
continue;
}
const queryPP = typeDec.query.ast.statement.body
.split('\n')
.map((s: string) => ' * ' + s)
.join('\n');
declarationFileContents += `%%private(let ${
typeDec.query.name
}IR: IR.t = %raw(\`${JSON.stringify(typeDec.query.ir)}\`))\n\n`;
declarationFileContents +=
`/**\n` +
` * Query generated from SQL:\n` +
` * \`\`\`\n` +
`${queryPP}\n` +
` * \`\`\`\n` +
` Runnable query:\n` +
` \`\`\`sql\n` +
`${processSQLQueryIR(typeDec.query.ir).query}\n` +
` \`\`\`\n\n` +
` */\n`;
declarationFileContents += `@gentype
module ${
Expand Down
22 changes: 12 additions & 10 deletions packages/example/src/books/BookService__sql.res
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ type findBookByIdQuery = {
%%private(let findBookByIdIR: IR.t = %raw(`{"usedParamSet":{"id":true},"params":[{"name":"id","required":false,"transform":{"type":"scalar"},"locs":[{"a":31,"b":33}]}],"statement":"SELECT * FROM books WHERE id = :id"}`))

/**
* Query generated from SQL:
* ```
* SELECT * FROM books WHERE id = :id
* ```
Runnable query:
```sql
SELECT * FROM books WHERE id = $1
```
*/
@gentype
module FindBookById: {
Expand Down Expand Up @@ -123,12 +124,13 @@ type booksByAuthorQuery = {
%%private(let booksByAuthorIR: IR.t = %raw(`{"usedParamSet":{"authorName":true},"params":[{"name":"authorName","required":true,"transform":{"type":"scalar"},"locs":[{"a":118,"b":129}]}],"statement":"SELECT b.* FROM books b\n INNER JOIN authors a ON a.id = b.author_id\n WHERE a.first_name || ' ' || a.last_name = :authorName!"}`))

/**
* Query generated from SQL:
* ```
* SELECT b.* FROM books b
* INNER JOIN authors a ON a.id = b.author_id
* WHERE a.first_name || ' ' || a.last_name = :authorName!
* ```
Runnable query:
```sql
SELECT b.* FROM books b
INNER JOIN authors a ON a.id = b.author_id
WHERE a.first_name || ' ' || a.last_name = $1
```
*/
@gentype
module BooksByAuthor: {
Expand Down
172 changes: 93 additions & 79 deletions packages/example/src/books/books__sql.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ type findBookByIdQuery = {
%%private(let findBookByIdIR: IR.t = %raw(`{"usedParamSet":{"id":true},"params":[{"name":"id","required":false,"transform":{"type":"scalar"},"locs":[{"a":31,"b":33}]}],"statement":"SELECT * FROM books WHERE id = :id"}`))

/**
* Query generated from SQL:
* ```
* SELECT * FROM books WHERE id = :id
* ```
Runnable query:
```sql
SELECT * FROM books WHERE id = $1
```
*/
@gentype
module FindBookById: {
Expand Down Expand Up @@ -132,10 +133,11 @@ type findBookByCategoryQuery = {
%%private(let findBookByCategoryIR: IR.t = %raw(`{"usedParamSet":{"category":true},"params":[{"name":"category","required":false,"transform":{"type":"scalar"},"locs":[{"a":26,"b":34}]}],"statement":"SELECT * FROM books WHERE :category = ANY(categories)"}`))

/**
* Query generated from SQL:
* ```
* SELECT * FROM books WHERE :category = ANY(categories)
* ```
Runnable query:
```sql
SELECT * FROM books WHERE $1 = ANY(categories)
```
*/
@gentype
module FindBookByCategory: {
Expand Down Expand Up @@ -212,12 +214,13 @@ type findBookNameOrRankQuery = {
%%private(let findBookNameOrRankIR: IR.t = %raw(`{"usedParamSet":{"name":true,"rank":true},"params":[{"name":"name","required":false,"transform":{"type":"scalar"},"locs":[{"a":41,"b":45}]},{"name":"rank","required":false,"transform":{"type":"scalar"},"locs":[{"a":57,"b":61}]}],"statement":"SELECT id, name\nFROM books\nWHERE (name = :name OR rank = :rank)"}`))

/**
* Query generated from SQL:
* ```
* SELECT id, name
* FROM books
* WHERE (name = :name OR rank = :rank)
* ```
Runnable query:
```sql
SELECT id, name
FROM books
WHERE (name = $1 OR rank = $2)
```
*/
@gentype
module FindBookNameOrRank: {
Expand Down Expand Up @@ -296,10 +299,11 @@ type findBookUnicodeQuery = {
%%private(let findBookUnicodeIR: IR.t = %raw(`{"usedParamSet":{},"params":[],"statement":"SELECT * FROM books WHERE name = 'שקל'"}`))

/**
* Query generated from SQL:
* ```
* SELECT * FROM books WHERE name = 'שקל'
* ```
Runnable query:
```sql
SELECT * FROM books WHERE name = 'שקל'
```
*/
@gentype
module FindBookUnicode: {
Expand Down Expand Up @@ -381,11 +385,12 @@ type insertBooksQuery = {
%%private(let insertBooksIR: IR.t = %raw(`{"usedParamSet":{"books":true},"params":[{"name":"books","required":false,"transform":{"type":"pick_array_spread","keys":[{"name":"rank","required":true},{"name":"name","required":true},{"name":"authorId","required":true},{"name":"categories","required":false}]},"locs":[{"a":61,"b":66}]}],"statement":"INSERT INTO books (rank, name, author_id, categories)\nVALUES :books RETURNING id as book_id"}`))

/**
* Query generated from SQL:
* ```
* INSERT INTO books (rank, name, author_id, categories)
* VALUES :books RETURNING id as book_id
* ```
Runnable query:
```sql
INSERT INTO books (rank, name, author_id, categories)
VALUES ($1,$2,$3,$4) RETURNING id as book_id
```
*/
@gentype
module InsertBooks: {
Expand Down Expand Up @@ -463,11 +468,12 @@ type insertBookQuery = {
%%private(let insertBookIR: IR.t = %raw(`{"usedParamSet":{"rank":true,"name":true,"author_id":true,"categories":true},"params":[{"name":"rank","required":true,"transform":{"type":"scalar"},"locs":[{"a":62,"b":67}]},{"name":"name","required":true,"transform":{"type":"scalar"},"locs":[{"a":70,"b":75}]},{"name":"author_id","required":true,"transform":{"type":"scalar"},"locs":[{"a":78,"b":88}]},{"name":"categories","required":false,"transform":{"type":"scalar"},"locs":[{"a":91,"b":101}]}],"statement":"INSERT INTO books (rank, name, author_id, categories)\nVALUES (:rank!, :name!, :author_id!, :categories) RETURNING id as book_id"}`))

/**
* Query generated from SQL:
* ```
* INSERT INTO books (rank, name, author_id, categories)
* VALUES (:rank!, :name!, :author_id!, :categories) RETURNING id as book_id
* ```
Runnable query:
```sql
INSERT INTO books (rank, name, author_id, categories)
VALUES ($1, $2, $3, $4) RETURNING id as book_id
```
*/
@gentype
module InsertBook: {
Expand Down Expand Up @@ -541,18 +547,19 @@ type updateBooksCustomQuery = {
%%private(let updateBooksCustomIR: IR.t = %raw(`{"usedParamSet":{"rank":true,"id":true},"params":[{"name":"rank","required":false,"transform":{"type":"scalar"},"locs":[{"a":49,"b":53},{"a":95,"b":99}]},{"name":"id","required":true,"transform":{"type":"scalar"},"locs":[{"a":161,"b":164}]}],"statement":"UPDATE books\nSET\n rank = (\n CASE WHEN (:rank::int IS NOT NULL)\n THEN :rank\n ELSE rank\n END\n )\nWHERE id = :id!"}`))

/**
* Query generated from SQL:
* ```
* UPDATE books
* SET
* rank = (
* CASE WHEN (:rank::int IS NOT NULL)
* THEN :rank
* ELSE rank
* END
* )
* WHERE id = :id!
* ```
Runnable query:
```sql
UPDATE books
SET
rank = (
CASE WHEN ($1::int IS NOT NULL)
THEN $1
ELSE rank
END
)
WHERE id = $2
```
*/
@gentype
module UpdateBooksCustom: {
Expand Down Expand Up @@ -627,15 +634,16 @@ type updateBooksQuery = {
%%private(let updateBooksIR: IR.t = %raw(`{"usedParamSet":{"name":true,"rank":true,"id":true},"params":[{"name":"name","required":false,"transform":{"type":"scalar"},"locs":[{"a":50,"b":54}]},{"name":"rank","required":false,"transform":{"type":"scalar"},"locs":[{"a":68,"b":72}]},{"name":"id","required":true,"transform":{"type":"scalar"},"locs":[{"a":85,"b":88}]}],"statement":"UPDATE books\n \nSET\n name = :name,\n rank = :rank\nWHERE id = :id!"}`))

/**
* Query generated from SQL:
* ```
* UPDATE books
*
* SET
* name = :name,
* rank = :rank
* WHERE id = :id!
* ```
Runnable query:
```sql
UPDATE books
SET
name = $1,
rank = $2
WHERE id = $3
```
*/
@gentype
module UpdateBooks: {
Expand Down Expand Up @@ -710,14 +718,15 @@ type updateBooksRankNotNullQuery = {
%%private(let updateBooksRankNotNullIR: IR.t = %raw(`{"usedParamSet":{"rank":true,"name":true,"id":true},"params":[{"name":"rank","required":true,"transform":{"type":"scalar"},"locs":[{"a":28,"b":33}]},{"name":"name","required":false,"transform":{"type":"scalar"},"locs":[{"a":47,"b":51}]},{"name":"id","required":true,"transform":{"type":"scalar"},"locs":[{"a":64,"b":67}]}],"statement":"UPDATE books\nSET\n rank = :rank!,\n name = :name\nWHERE id = :id!"}`))

/**
* Query generated from SQL:
* ```
* UPDATE books
* SET
* rank = :rank!,
* name = :name
* WHERE id = :id!
* ```
Runnable query:
```sql
UPDATE books
SET
rank = $1,
name = $2
WHERE id = $3
```
*/
@gentype
module UpdateBooksRankNotNull: {
Expand Down Expand Up @@ -798,12 +807,13 @@ type getBooksByAuthorNameQuery = {
%%private(let getBooksByAuthorNameIR: IR.t = %raw(`{"usedParamSet":{"authorName":true},"params":[{"name":"authorName","required":true,"transform":{"type":"scalar"},"locs":[{"a":110,"b":121}]}],"statement":"SELECT b.* FROM books b\nINNER JOIN authors a ON a.id = b.author_id\nWHERE a.first_name || ' ' || a.last_name = :authorName!"}`))

/**
* Query generated from SQL:
* ```
* SELECT b.* FROM books b
* INNER JOIN authors a ON a.id = b.author_id
* WHERE a.first_name || ' ' || a.last_name = :authorName!
* ```
Runnable query:
```sql
SELECT b.* FROM books b
INNER JOIN authors a ON a.id = b.author_id
WHERE a.first_name || ' ' || a.last_name = $1
```
*/
@gentype
module GetBooksByAuthorName: {
Expand Down Expand Up @@ -879,10 +889,11 @@ type aggregateEmailsAndTestQuery = {
%%private(let aggregateEmailsAndTestIR: IR.t = %raw(`{"usedParamSet":{"testAges":true},"params":[{"name":"testAges","required":false,"transform":{"type":"scalar"},"locs":[{"a":55,"b":63}]}],"statement":"SELECT array_agg(email) as \"emails!\", array_agg(age) = :testAges as ageTest FROM users"}`))

/**
* Query generated from SQL:
* ```
* SELECT array_agg(email) as "emails!", array_agg(age) = :testAges as ageTest FROM users
* ```
Runnable query:
```sql
SELECT array_agg(email) as "emails!", array_agg(age) = $1 as ageTest FROM users
```
*/
@gentype
module AggregateEmailsAndTest: {
Expand Down Expand Up @@ -956,10 +967,11 @@ type getBooksQuery = {
%%private(let getBooksIR: IR.t = %raw(`{"usedParamSet":{},"params":[],"statement":"SELECT id, name as \"name!\" FROM books"}`))

/**
* Query generated from SQL:
* ```
* SELECT id, name as "name!" FROM books
* ```
Runnable query:
```sql
SELECT id, name as "name!" FROM books
```
*/
@gentype
module GetBooks: {
Expand Down Expand Up @@ -1032,10 +1044,11 @@ type countBooksQuery = {
%%private(let countBooksIR: IR.t = %raw(`{"usedParamSet":{},"params":[],"statement":"SELECT count(*) as book_count FROM books"}`))

/**
* Query generated from SQL:
* ```
* SELECT count(*) as book_count FROM books
* ```
Runnable query:
```sql
SELECT count(*) as book_count FROM books
```
*/
@gentype
module CountBooks: {
Expand Down Expand Up @@ -1109,10 +1122,11 @@ type getBookCountriesQuery = {
%%private(let getBookCountriesIR: IR.t = %raw(`{"usedParamSet":{},"params":[],"statement":"SELECT * FROM book_country"}`))

/**
* Query generated from SQL:
* ```
* SELECT * FROM book_country
* ```
Runnable query:
```sql
SELECT * FROM book_country
```
*/
@gentype
module GetBookCountries: {
Expand Down
Loading

0 comments on commit 7481ffd

Please sign in to comment.