db/postgres
Connecting to PostgreSQL.
import db.postgres
This package implements the Postgres protocol directly and needs no outside library; the example needs a running Postgres server, so it is checked and not run. Every row of a result is fetched from the server at once, so RowCount is known before you read any of them.
Types
Database
struct Database| Name | Type | Description |
|---|---|---|
handle | void* | The library pointer. |
affected | i64 = 0 | The number of rows changed by the last statement. |
import db
import db.postgres
func main:
if not postgres.Available():
println "the Postgres client is not available"
ret
end
println postgres.ClientVersion()
d := postgres.Open("host=127.0.0.1 port=5432 dbname=shop user=app password=s3cret")
if not postgres.Ok(d):
println postgres.Error(d)
ret
end
println postgres.ServerVersion(d), postgres.DatabaseName(d), postgres.UserName(d), postgres.HostName(d), postgres.Port(d)
println postgres.Ping(d)
_ := postgres.Exec(d, "CREATE TABLE IF NOT EXISTS items (id SERIAL PRIMARY KEY, name TEXT, price NUMERIC)")
_ := postgres.Exec(d, "INSERT INTO items (name, price) VALUES (" + postgres.Quote(d, "tea") + ", 12.5)")
println postgres.ExecCount(d, "UPDATE items SET price = price * 1.1")
rows := postgres.Query(d, "SELECT id, name, price FROM items ORDER BY id")
println rows.Ok(), rows.ColumnCount(), rows.RowCount(), rows.ColumnName(1)
until rows.Next():
println rows.Int(0), rows.Text(1), rows.Float(2), rows.Int64(0), rows.IsNull(2), rows.ColumnType(1)
end
_ := rows.Close()
println postgres.QueryInt(d, "SELECT COUNT(*) FROM items"), postgres.QueryText(d, "SELECT version()")
c := postgres.Conn(d)
tx := c.Begin()
_ := tx.Exec("DELETE FROM items")
_ := tx.Rollback()
println db.QueryInt(c, "SELECT COUNT(*) FROM items"), d.Kind()
postgres.Reset(d)
_ := postgres.Close(d)
_ := postgres.OpenFull("db.example.com", 5432, "app", "s3cret", "shop")
endKind
func Kind(): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Ping
func Ping(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-postgres-Client.salam
Begin
func Begin(): dyn db.TxThe example for this item is shown with the ones above: db-postgres-Client.salam
LastInsertId
func LastInsertId(): i64The example for this item is shown with the ones above: db-postgres-Client.salam
Changes
func Changes(): i64The example for this item is shown with the ones above: db-postgres-Client.salam
Error
func Error(): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Quote
func Quote(s: str): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Rows
struct Rows| Name | Type | Description |
|---|---|---|
handle | void* | The result pointer. |
row | int = -1 | The index of the current row. |
count | int = 0 | The number of rows. |
The example for this item is shown with the ones above: db-postgres-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Next
func Next(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
ColumnCount
func ColumnCount(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
RowCount
func RowCount(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
ColumnName
func ColumnName(col: int): strThe example for this item is shown with the ones above: db-postgres-Client.salam
ColumnType
func ColumnType(col: int): intTYPE_... constants in db.The example for this item is shown with the ones above: db-postgres-Client.salam
IsNull
func IsNull(col: int): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Text
func Text(col: int): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Int
func Int(col: int): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Int64
func Int64(col: int): i64The example for this item is shown with the ones above: db-postgres-Client.salam
Float
func Float(col: int): f64The example for this item is shown with the ones above: db-postgres-Client.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Txn
struct Txn| Name | Type | Description |
|---|---|---|
handle | void* | The connection pointer. |
The example for this item is shown with the ones above: db-postgres-Client.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-postgres-Client.salam
Commit
func Commit(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Rollback
func Rollback(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Functions
Available
func Available(): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
ClientVersion
func ClientVersion(): intThe example for this item is shown with the ones above: db-postgres-Client.salam
ServerVersion
func ServerVersion(d: Database): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Open
func Open(conninfo: str): Databasehost=... dbname=..., or a postgresql:// address.The example for this item is shown with the ones above: db-postgres-Client.salam
OpenFull
func OpenFull(host: str, port: int, user: str, pass: str, dbname: str): DatabaseThe example for this item is shown with the ones above: db-postgres-Client.salam
Ok
func Ok(d: Database): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Ping
func Ping(d: Database): boolThe example for this item is shown with the ones above: db-postgres-Client.salam
Reset
func Reset(d: Database)The example for this item is shown with the ones above: db-postgres-Client.salam
Close
func Close(d: Database): intThe example for this item is shown with the ones above: db-postgres-Client.salam
Error
func Error(d: Database): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Exec
func Exec(d: Database, sql: str): intThe example for this item is shown with the ones above: db-postgres-Client.salam
ExecCount
func ExecCount(d: Database, sql: str): i64The example for this item is shown with the ones above: db-postgres-Client.salam
Query
func Query(d: Database, sql: str): RowsThe example for this item is shown with the ones above: db-postgres-Client.salam
Quote
func Quote(d: Database, s: str): strThe example for this item is shown with the ones above: db-postgres-Client.salam
DatabaseName
func DatabaseName(d: Database): strThe example for this item is shown with the ones above: db-postgres-Client.salam
UserName
func UserName(d: Database): strThe example for this item is shown with the ones above: db-postgres-Client.salam
HostName
func HostName(d: Database): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Port
func Port(d: Database): strThe example for this item is shown with the ones above: db-postgres-Client.salam
QueryInt
func QueryInt(d: Database, sql: str): intThe example for this item is shown with the ones above: db-postgres-Client.salam
QueryText
func QueryText(d: Database, sql: str): strThe example for this item is shown with the ones above: db-postgres-Client.salam
Conn
func Conn(d: Database): dyn db.ConnectionConnection.The example for this item is shown with the ones above: db-postgres-Client.salam