Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
DB wraps a sql.DB.
Example ¶
package main import ( "context" "database/sql" "github.com/rrgmc/litsql-db/lsql" "github.com/rrgmc/litsql/dialect/psql" "github.com/rrgmc/litsql/dialect/psql/sm" "github.com/rrgmc/litsql/sq" ) func main() { ctx := context.Background() db, err := sql.Open("test", ":memory:") if err != nil { panic(err) } // wrap *sql.DB instance ddb := lsql.NewDB(db) query := psql.Select( sm.Columns("film_id", "title", "length"), sm.From("film"), sm.WhereClause("length > ?", sq.NamedArg("length")), sm.Limit(10), ) // generate SQL string from litsql and execute it, replacing named parameters. rows, err := ddb.Query(ctx, query, sq.MapArgValues{ "length": 90, }) if err != nil { panic(err) } defer rows.Close() for rows.Next() { var film_id, length int var title string err = rows.Scan(&film_id, &title, &length) if err != nil { panic(err) } } if rows.Err() != nil { panic(err) } }
Output:
type Option ¶
func WithQueryHandler ¶
type QuerierStmt ¶
type QuerierStmt = lsqlt.QuerierStmt
type Stmt ¶
Stmt wraps a sql.Stmt.
Example ¶
package main import ( "context" "database/sql" "github.com/rrgmc/litsql-db/lsql" "github.com/rrgmc/litsql/dialect/psql" "github.com/rrgmc/litsql/dialect/psql/sm" "github.com/rrgmc/litsql/sq" ) func main() { ctx := context.Background() db, err := sql.Open("test", ":memory:") if err != nil { panic(err) } // wrap *sql.DB instance ddb := lsql.NewDB(db) query := psql.Select( sm.Columns("film_id", "title", "length"), sm.From("film"), sm.WhereClause("length > ?", sq.NamedArg("length")), sm.Limit(10), ) // generate SQL string from litsql and prepare it, storing the named parameters to be replaced later dstmt, err := ddb.Prepare(ctx, query) if err != nil { panic(err) } // execute prepared query, replacing named parameters rows, err := dstmt.Query(ctx, sq.MapArgValues{ "length": 90, }) if err != nil { panic(err) } defer rows.Close() for rows.Next() { var film_id, length int var title string err = rows.Scan(&film_id, &title, &length) if err != nil { panic(err) } } if rows.Err() != nil { panic(err) } }
Output:
Click to show internal directories.
Click to hide internal directories.