sqlp

package module
v0.1.23 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2025 License: MIT Imports: 6 Imported by: 0

README

sql+

Go Reference

sql+ provides some convenience functions for using structs with go's database/sql package

it is fully based on kisielks package. I am using it for experimenting with orm-concepts and am using it for some private projects. Simple stuff, no external dependencies.

Stuff changes and breaks fast atm. Testing is limited.

Changes made in this fork

  1. Use generics instead of interface{}: In my opinion, using generics improves readability and allows for additional functionality.
  2. Keep Language Injections intact: Intellij IDEs offer language injections that, in this case, provide support for sql-queries if literals match sql query patterns. This was previously not possible, because for injecting columns dynamically with sqlp, a pattern like fmt.Sprintf("SELECT %s FROM ...", sqlstruct.Columns(mystruct{})) had to be used.
  3. Improved the package for my use-cases: While I would love for someone else to find use in this package, one of its main goals is to allow for the removal of boilerplate and redundant code in my private project by integrating patterns I often deploy. For example, some limited support for IN(*)-Select queries and INSERTs were added.

Usage

This package allows linking a struct and its database-counterpart, which means that SELECT-queries automatically reflect changes made to the datastructure by injecting the required columns into the query.

This works by extracting the exported fields of a struct, converting their names and inserting them into the given query. Just write the queries as normal using the autocomplete language injections provide and let your struct-definitions and sqlp take care of the columns.

Documentation

Overview

Package sqlp provides some convenience functions for using structs with the Go standard library's database/sql package.

The package matches struct field names to SQL query column names. A field can also specify a matching column with "sql" tag, if it's different from field name. Unexported fields or fields marked with `sql:"-"` are ignored, just like with "encoding/json" package.

For example: ToDo (See Readme)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete added in v0.1.2

func Delete[T any](pk any, table string) error

Delete Deprecated

func DeleteR added in v0.1.12

func DeleteR[T Repo](obj T) error

func GetR added in v0.1.21

func GetR[T Repo]() ([]T, error)

func GetSingleWhereR added in v0.1.21

func GetSingleWhereR[T Repo](where string, args ...any) (res T, err error)

func GetWhereR added in v0.1.21

func GetWhereR[T Repo](where string, args ...any) ([]T, error)

func Insert

func Insert[T any](obj T, table string) (int, error)

Insert inserts the given object into the table and returns the last inserted id. Autogenerated fields can be tagged with `sql-auto:""` (AutoGenTagName) in order for them to be ignored during insert. Deprecated

func InsertR added in v0.1.12

func InsertR[T Repo](obj T) (int, error)

func Query

func Query[T any](query string, args ...any) (results []T, err error)

func QueryBasic

func QueryBasic[T string | int | int64 | float32 | float64](query string, args ...any) (results []T, err error)

func QueryBasicRow added in v0.1.4

func QueryBasicRow[T string | int | int64 | float32 | float64](query string, args ...any) (result T, err error)

func QueryRow

func QueryRow[T any](query string, args ...any) (result T, err error)

func SetDatabase

func SetDatabase(sqldb *sql.DB)

SetDatabase sets the global database handle to be used by the Query function.

func ToSnakeCase

func ToSnakeCase(src string) string

ToSnakeCase converts a string to snake case, words separated with underscores. It's intended to be used with NameMapper to map struct field names to snake case database fields.

func Update

func Update[T any](obj T, table string) error

Update Deprecated

func UpdateR added in v0.1.12

func UpdateR[T Repo](obj T) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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