The package facilitates execution of SQL scripts generated by prana. Also it provides a query builder and object relation mapper. Note that it is in BETA. We may introduce breaking changes until we reach version 1.0.
$ go get -u github.com/phogolabs/orm
Let's first import all required packages:
import (
"github.com/phogolabs/orm"
)
and then establish the connection:
gateway, err := orm.Open("sqlite3", "example.db", orm.WithRoutine(routine.Statement))
if err != nil {
return err
}
You can execute the migration generated by prana. For that you have to use either embed package or os package.
if err := gateway.Migrate(resource); err != nil {
return err
}
The package provides a way to work with embeddable SQL scripts. It understands predefined files with SQL Scripts.
It executes them as standard SQL queries. Let's define a SQL routines named insert-user
and select-all-users
:
-- name: insert-user
INSERT INTO users (id, first_name, last_name)
VALUES (:id, :first_name, :last_name);
-- named: select-all-users
SELECT * FROM users;
Then you can execute the desired script by just passing its name:
routine := orm.Routine("select-all-users")
// execute the routine
_, err = gateway.All(context.TODO(), routine, &users)
routine := orm.Routine("insert-user", &user)
// execute the routine
_, err = gateway.Exec(context.TODO(), routine)
Also you can execute raw SQL Scripts from your code:
query := orm.Query("SELECT * FROM users WHERE id = ?", 5432)
// fetch the records as a slice of users
rows, err := gateway.Only(context.TODO(), query, &user)
You can check our Getting Started Example.
For more information, how you can change the default behavior you can read the help documentation by executing:
We are open for any contributions. Just fork the project.
logo made by Free Pik