|
1 | 1 | # gm_pgsqloo
|
2 | 2 | PostgreSQL binary module for Garry's Mod
|
| 3 | + |
| 4 | +## Example |
| 5 | +```lua |
| 6 | +require "pgsqloo" |
| 7 | + |
| 8 | +local db = pgsqloo.Connection("postgresql://postgres@localhost/postgres?application_name=pgsqloo") |
| 9 | + |
| 10 | +function db:OnConnected(err) |
| 11 | + if err then |
| 12 | + print("Failed to connect to database:", err) |
| 13 | + else |
| 14 | + print("Successfully connected to database") |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +db:Connect() |
| 19 | + |
| 20 | +db:Query("SELECT * FROM users", function(err, result) |
| 21 | + if err then |
| 22 | + print("Failed to get all users:", err) |
| 23 | + else |
| 24 | + print("Users:") |
| 25 | + PrintTable(result) |
| 26 | + end |
| 27 | +end) |
| 28 | + |
| 29 | +local task = db:Query("SELECT * FROM stats WHERE level = $1 AND money > $2", {80, 99999}):Wait() |
| 30 | +if task:IsSuccess() then |
| 31 | + PrintTable(task:GetResult()) |
| 32 | +else |
| 33 | + print(task:GetError()) |
| 34 | +end |
| 35 | +``` |
| 36 | + |
| 37 | +## API |
| 38 | +### Global API |
| 39 | +```lua |
| 40 | +-- See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING |
| 41 | +pgsqloo.Connection(url: string) -> PostgreConnection |
| 42 | +``` |
| 43 | +### PostgreConnection |
| 44 | +```lua |
| 45 | +PostgreConnection:Connect() -> PostgreTask<nil> |
| 46 | +PostgreConnection:Query(sql: string, args: table?, callback: function(err: string?, result: Result?)?) -> PostgreTask<Result> |
| 47 | +PostgreConnection:PreparedQuery(name: string, args: table?, callback: function(err: string?, result: Result?)?) -> PostgreTask<Result> |
| 48 | +PostgreConnection:Prepare(name: string, sql: string) |
| 49 | +PostgreConnection:Unprepare(name: string) |
| 50 | +PostgreConnection:Disconnect(wait: bool = false) |
| 51 | +PostgreConnection:IsConnected() -> bool |
| 52 | +PostgreConnection:IsConnecting() -> bool |
| 53 | +PostgreConnection:Database() -> string |
| 54 | +PostgreConnection:Username() -> string |
| 55 | +PostgreConnection:Hostname() -> string |
| 56 | +PostgreConnection:Port() -> string |
| 57 | +PostgreConnection:BackendPID() -> number |
| 58 | +PostgreConnection:ProtocolVersion() -> number |
| 59 | +PostgreConnection:ServerVersion() -> number |
| 60 | +PostgreConnection:GetClientEncoding() -> string |
| 61 | +PostgreConnection:SetClientEncoding(encoding: string) |
| 62 | +PostgreConnection:EncodingID() -> number |
| 63 | + |
| 64 | +-- Events |
| 65 | +PostgreConnection.OnConnected(conn: PostgreConnection, err: string?) |
| 66 | +``` |
| 67 | +### PostgreTask |
| 68 | +```lua |
| 69 | +PostgreTask:Wait() -> PostgreTask |
| 70 | +PostgreTask:IsSuccess() -> bool |
| 71 | +PostgreTask:IsError() -> bool |
| 72 | +PostgreTask:IsProcessing() -> bool |
| 73 | +PostgreTask:GetResult() -> ResultType/nil |
| 74 | +PostgreTask:GetError() -> string/nil |
| 75 | +``` |
0 commit comments