sqload

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 7 Imported by: 3

README

sqload

Personally I don't like writing SQL code inside the Go source files, so I made this simple and fully tested module to load SQL queries from files.

How to use it?

Each SQL query must include a comment at the beginning, the comment must be something like:

-- query: NameOfYourQuery

This comment is mandatory so the loader can match the name of your query with the field of the struct where the SQL code of your query will be stored. In this case, the struct would look like this:

type Query struct {
    NameOfYourQuery string `query:"NameOfYourQuery"`
}

The following are some examples of how you can use this library:

Load SQL code from strings
package main

import (
	"fmt"
	"os"

	"github.com/midir99/sqload"
)

type UserQuery struct {
	FindUserById            string `query:"FindUserById"`
	UpdateUserFirstNameById string `query:"UpdateUserFirstNameById"`
}

func main() {
	sql := `
	-- query: FindUserById
	SELECT * FROM user WHERE id = :id;

	-- query: UpdateUserFirstNameById
	UPDATE user SET first_name = :first_name WHERE id = :id;
	`
	userQuery := UserQuery{}
	err := sqload.FromString(sql, &userQuery)
	if err != nil {
		fmt.Printf("error loading user queries: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("FindUserById: %s\n", userQuery.FindUserById)
	fmt.Printf("UpdateUserFirstNameById: %s\n", userQuery.UpdateUserFirstNameById)
}
Load SQL code from files

file: cat-queries.sql

-- query: CreateCatTable
CREATE TABLE Cat (
    id SERIAL,
    name VARCHAR(150),
    color VARCHAR(50),
    PRIMARY KEY (id)
);

-- query: CreatePsychoCat
INSERT INTO Cat (name, color) VALUES ('Puca', 'Orange');

-- query: CreateNormalCat
INSERT INTO Cat (name, color) VALUES (:name, :color);

-- query: UpdateColorById
UPDATE Cat
   SET color = :color
 WHERE id = :id;

file: main.go

package main

import (
	"fmt"
	"os"

	"github.com/midir99/sqload"
)

type CatQuery struct {
	CreateCatTable  string `query:"CreateCatTable"`
	CreatePsychoCat string `query:"CreatePsychoCat"`
	CreateNormalCat string `query:"CreateNormalCat"`
	UpdateColorById string `query:"UpdateColorById"`
}

func main() {
	catQuery := CatQuery{}
	err := sqload.FromFile("cat-queries.sql", &catQuery)
	if err != nil {
		fmt.Printf("error loading cat queries: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("CreateCatTable: %s\n", catQuery.CreateCatTable)
	fmt.Printf("CreatePsychoCat: %s\n", catQuery.CreatePsychoCat)
	fmt.Printf("CreateNormalCat: %s\n", catQuery.CreateNormalCat)
	fmt.Printf("UpdateColorById: %s\n", catQuery.UpdateColorById)
}
Load SQL code from directories containing .sql files

Lets say you have a directory containing your SQL files:

sql/
├── cat-queries.sql
└── user-queries.sql

file: cat-queries.sql

-- query: CreateCatTable
CREATE TABLE Cat (
    id SERIAL,
    name VARCHAR(150),
    color VARCHAR(50),
    PRIMARY KEY (id)
);

-- query: CreatePsychoCat
INSERT INTO Cat (name, color) VALUES ('Puca', 'Orange');

-- query: CreateNormalCat
INSERT INTO Cat (name, color) VALUES (:name, :color);

-- query: UpdateColorById
UPDATE Cat
   SET color = :color
 WHERE id = :id;

file: user-queries.sql

-- query: FindUserById
SELECT * FROM user WHERE id = :id;

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

file: main.go

package main

import (
	"fmt"
	"os"

	"github.com/midir99/sqload"
)

type Query struct {
	CreateCatTable  string `query:"CreateCatTable"`
	CreatePsychoCat string `query:"CreatePsychoCat"`
	CreateNormalCat string `query:"CreateNormalCat"`
	UpdateColorById string `query:"UpdateColorById"`

	FindUserById            string `query:"FindUserById"`
	UpdateUserFirstNameById string `query:"UpdateUserFirstNameById"`
}

func main() {
	query := Query{}
	err := sqload.FromDir("sql/", &query)
	if err != nil {
		fmt.Printf("error loading queries: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("CreateCatTable: %s\n", query.CreateCatTable)
	fmt.Printf("CreatePsychoCat: %s\n", query.CreatePsychoCat)
	fmt.Printf("CreateNormalCat: %s\n", query.CreateNormalCat)
	fmt.Printf("UpdateColorById: %s\n", query.UpdateColorById)

	fmt.Printf("FindUserById: %s\n", query.FindUserById)
	fmt.Printf("UpdateUserFirstNameById: %s\n", query.UpdateUserFirstNameById)
}

Documentation

Overview

Package sqload provides functions to load SQL code from strings or .sql files into tagged struct fields.

package main

import (
	"fmt"
	"os"

	"github.com/midir99/sqload"
)

type UserQuery struct {
	FindUserById            string `query:"FindUserById"`
	UpdateUserFirstNameById string `query:"UpdateUserFirstNameById"`
}

func main() {
	sql := `
	-- query: FindUserById
	SELECT * FROM user WHERE id = :id;

	-- query: UpdateUserFirstNameById
	UPDATE user SET first_name = :first_name WHERE id = :id;
	`
	userQuery := UserQuery{}
	err := sqload.FromString(sql, &userQuery)
	if err != nil {
		fmt.Printf("error loading user queries: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("FindUserById: %s\n", userQuery.FindUserById)
	fmt.Printf("UpdateUserFirstNameById: %s\n", userQuery.UpdateUserFirstNameById)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromDir

func FromDir(dirname string, v any) error

FromDir loads the SQL code from all the .sql files in the directory dirname (recursively) and stores the queries in the struct pointed to by v, v must be a pointer to a struct with tags, and each tag indicates what query will be stored in what field.

func FromFile

func FromFile(filename string, v any) error

FromFile loads the SQL code from the file filename and stores the queries in the struct pointed to by v, v must be a pointer to a struct with tags, and each tag indicates what query will be stored in what field.

func FromString

func FromString(s string, v any) error

FromString loads the SQL code from the string passed and stores the queries in the struct pointed to by v, v must be a pointer to a struct with tags, and each tag indicates what query will be stored in what field.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL