Stop writing your SQL code inside your Go source files!

You are writing Go but you need to write some SQL, so you end up writing SQL inside your Go source code, this is not a problem as long as they are short queries, but when your queries take up more lines of code they become hard to read and to modify, b…

You are writing Go but you need to write some SQL, so you end up writing SQL inside your Go source code, this is not a problem as long as they are short queries, but when your queries take up more lines of code they become hard to read and to modify, besides the lack of indentation and coloring of the code make it worse.

So… What is the solution?

Keep your SQL code as SQL and your Go code as Go (don’t write SQL inside Go).

For this you can use sqload!

File users.sql

-- query: FindUserById
SELECT first_name,
       last_name,
       dob,
       email
  FROM user
 WHERE id = :id;

-- query: UpdateFirstNameById
UPDATE user
   SET first_name = :first_name
 WHERE id = :id;

-- query: DeleteUserById
DELETE FROM user WHERE id = :id;

File main.go

package main

import (
    _ "embed"
    "fmt"

    "github.com/midir99/sqload"
)

//go:embed users.sql
var sqlCode string

var Q = sqload.MustLoadFromString[struct {
    FindUserById        string `query:"FindUserById"`
    UpdateFirstNameById string `query:"UpdateFirstNameById"`
    DeleteUserById      string `query:"DeleteUserById"`
}](sqlCode)

func main() {
    fmt.Printf("- FindUserById\n%s\n\n", Q.FindUserById)
    fmt.Printf("- UpdateFirstNameById\n%s\n\n", Q.UpdateFirstNameById)
    fmt.Printf("- DeleteUserById\n%s\n\n", Q.DeleteUserById)
}

By keeping your SQL and Go separate you get the following advantages:

  • Better support by your IDE or code editor (because you are now handling SQL code in its own file with its .sql extension).
  • Your DBA will be able to easily find the code of each query that your project is running.
  • SQL reuse, simply copy the same .sql files from one project to another.
  • Keep your Go code free of SQL queries.

sqload is a library with 100% test coverage, free of dependencies
from third parties and their documentation has many examples.

sqload is inspired by Yesql (Clojure).

https://github.com/midir99/sqload
https://pkg.go.dev/github.com/midir99/sqload


Print Share Comment Cite Upload Translate
APA
midir99 | Sciencx (2024-03-29T14:28:56+00:00) » Stop writing your SQL code inside your Go source files!. Retrieved from https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/.
MLA
" » Stop writing your SQL code inside your Go source files!." midir99 | Sciencx - Monday November 7, 2022, https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/
HARVARD
midir99 | Sciencx Monday November 7, 2022 » Stop writing your SQL code inside your Go source files!., viewed 2024-03-29T14:28:56+00:00,<https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/>
VANCOUVER
midir99 | Sciencx - » Stop writing your SQL code inside your Go source files!. [Internet]. [Accessed 2024-03-29T14:28:56+00:00]. Available from: https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/
CHICAGO
" » Stop writing your SQL code inside your Go source files!." midir99 | Sciencx - Accessed 2024-03-29T14:28:56+00:00. https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/
IEEE
" » Stop writing your SQL code inside your Go source files!." midir99 | Sciencx [Online]. Available: https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/. [Accessed: 2024-03-29T14:28:56+00:00]
rf:citation
» Stop writing your SQL code inside your Go source files! | midir99 | Sciencx | https://www.scien.cx/2022/11/07/stop-writing-your-sql-code-inside-your-go-source-files/ | 2024-03-29T14:28:56+00:00
https://github.com/addpipe/simple-recorderjs-demo