sqload

package module
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromDir

func FromDir(name string, v any) error

func FromFile

func FromFile(name string, v any) error

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. Example:

package main

import (
	"fmt"
	"os"
	"github.com/midir99/sqload"
)

type UserQueries 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;
	`
	userQueries := UserQueries{}
	err := sqload.FromString(sql, &userQueries)
	if err != nil {
		fmt.Printf("error loading user queries: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("FindUserById: %s", userQueries.FindUserById)
	fmt.Printf("UpdateUserFirstNameById: %s", userQueries.UpdateUserFirstNameById)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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