mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
add database functions
This commit is contained in:
parent
51bc382413
commit
ad2aea9c70
14
README.md
14
README.md
@ -193,6 +193,20 @@ Name | Example | Example result | Description
|
||||
`>` | `(> 1 2)` | `false` | Return true if first argument is greater than second.
|
||||
`>=` | `(>= 1 2)` | `false` | Return true if first argument is greater or equal to second.
|
||||
|
||||
### Database functions
|
||||
|
||||
This is just a simple wrapper for [PDO](https://www.php.net/manual/en/book.pdo.php).
|
||||
|
||||
Name | Example | Example result | Description
|
||||
----------- | ------- | -------------- | -----------
|
||||
db-open | `(def d (db-open "mysql:host=localhost;dbname=test" "testuser" "testpw"))` | `<object<PDO>>` | Open a database connection.
|
||||
db-execute | `(db-execute d "INSERT INTO test_table (col1, col2) values (?, ?)" [1, 2])` | `1` | Execute a SQL statement and return the number of affected rows.
|
||||
db-query | `(db-query d "SELECT * FROM test_table WHERE col1 = ?" [1])` | | Execute a SELECT statement.
|
||||
db-last-id | `(db-last-id d)` | `"1"` | Return the last id of auto-increment column.
|
||||
db-trans | `(db-trans d)` | `true` | Start a transaction.
|
||||
db-commit | `(db-commit d)` | `true` | Commit a transaction.
|
||||
db-rollback | `(db-rollback d)` | `true` | Roll back a transaction.
|
||||
|
||||
### IO functions
|
||||
|
||||
Name | Example | Example result | Description
|
||||
|
58
src/Lib/Database.php
Normal file
58
src/Lib/Database.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace MadLisp\Lib;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
|
||||
use MadLisp\Collection;
|
||||
use MadLisp\CoreFunc;
|
||||
use MadLisp\Env;
|
||||
use MadLisp\Hash;
|
||||
use MadLisp\Vector;
|
||||
|
||||
class Database implements ILib
|
||||
{
|
||||
public function register(Env $env): void
|
||||
{
|
||||
$env->set('db-open', new CoreFunc('db-open', 'Open a database connection.', 1, 4,
|
||||
function (string $dsn, ?string $username = null, ?string $password = null, ?Hash $options = null) {
|
||||
return new PDO($dsn, $username, $password, $options ? $options->getData() : []);
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('db-execute', new CoreFunc('db-execute', 'Execute a database statement.', 2, 3,
|
||||
function (PDO $pdo, string $sql, ?Collection $args = null) {
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($args ? $args->getData() : []);
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('db-query', new CoreFunc('db-query', 'Execute a database query.', 2, 4,
|
||||
function (PDO $pdo, string $sql, ?Collection $args, bool $rowVectors = false) {
|
||||
$stmt = $pdo->prepare($sql);
|
||||
$stmt->execute($args ? $args->getData() : []);
|
||||
$rows = $stmt->fetchAll($rowVectors ? PDO::FETCH_NUM : PDO::FETCH_ASSOC);
|
||||
return new Vector(
|
||||
array_map(fn ($row) => $rowVectors ? new Vector($row) : new Hash($row), $rows)
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('db-last-id', new CoreFunc('db-last-id', 'Get the last id of auto-increment column.', 1, 1,
|
||||
fn (PDO $pdo) => $pdo->lastInsertId()
|
||||
));
|
||||
|
||||
$env->set('db-trans', new CoreFunc('db-trans', 'Start a transaction.', 1, 1,
|
||||
fn (PDO $pdo) => $pdo->beginTransaction()
|
||||
));
|
||||
|
||||
$env->set('db-commit', new CoreFunc('db-commit', 'Commit a transaction.', 1, 1,
|
||||
fn (PDO $pdo) => $pdo->commit()
|
||||
));
|
||||
|
||||
$env->set('db-rollback', new CoreFunc('db-rollback', 'Roll back a transaction.', 1, 1,
|
||||
fn (PDO $pdo) => $pdo->rollBack()
|
||||
));
|
||||
}
|
||||
}
|
@ -44,6 +44,7 @@ class LispFactory
|
||||
// Register core libraries
|
||||
(new Lib\Collections())->register($env);
|
||||
(new Lib\Compare())->register($env);
|
||||
(new Lib\Database())->register($env);
|
||||
(new Lib\IO())->register($env);
|
||||
(new Lib\Json())->register($env);
|
||||
(new Lib\Math())->register($env);
|
||||
|
Loading…
Reference in New Issue
Block a user