sqlgen

package
v0.0.0-...-f0512a5 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2012 License: BSD-2-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package sqlgen provides functions for SQL generation. Its functions generates insert's, update's, delete's and select's from struct instances. It does not generate ready-to-run SQLs, but prepared statements.

You can declare a struct as following:

type Person struct {
	Name string
	Age  int
}

And then use the sqlgen to generate your SQL instructions. For instance, a select statement without any filters could be generated this way:

var p Person
sql, err := sqlgen.Select(p)

This would generate:

select name, age from person

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(obj interface{}, filters []string) string

func Insert

func Insert(obj interface{}) (string, error)

Insert generates an INSERT statement, using all values from members of obj. If obj is not a struct nor a pointer to a struct, this function returns an empty string and an error. Otherwise, it returns the SQL instruction to be user in a prepared statement, and a nil error.

func Select

func Select(obj interface{}, fields ...string) (string, error)

Select generates a SELECT statement, selecting only "fields" (or all fields, if "fields" is not given) from the table given by the name of the struct "obj" lowercased. If the type of obj is not a struct, this method returns an empty string and an error.

If the type of obj is a struct, but one of the given fields is not a member of the struct (lowercased), it returns an empty string and another error.

Otherwise it returns the SQL instruction to be used in a prepared statement and a nil error.

func Update

func Update(obj interface{}, updateFields, filterFields []string) (string, error)

Update generates an UPDATE statement, using the given updateFields and filterFields. updateFields contains a list of fields to include in the update and filterFields contains a list of field to use in WHERE clause.

The following code:

p := Person{Id: 10, Name: "Batman", Age: 90}
sql, _ := Update(p, []string{"name"}, []string{"id"})

will generate the following SQL:

update person set name = ? where id = ?

Note that it generates just a string to build a prepared statement.

If the given object is not a struct, or one of the fields in the update or filter list is not member of the given struct, this function returns an empty string and an error. Otherwise, it returns the SQL to be used in a prepared statement and nil error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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