From ad2aea9c70db9801228f8125159b41612cc30a34 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Fri, 19 Jun 2020 19:09:07 +0700 Subject: [PATCH] add database functions --- README.md | 14 +++++++++++ src/Lib/Database.php | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/LispFactory.php | 1 + 3 files changed, 73 insertions(+) create mode 100644 src/Lib/Database.php diff --git a/README.md b/README.md index 6c470d2..e9520be 100644 --- a/README.md +++ b/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"))` | `>` | 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 diff --git a/src/Lib/Database.php b/src/Lib/Database.php new file mode 100644 index 0000000..c0b9da0 --- /dev/null +++ b/src/Lib/Database.php @@ -0,0 +1,58 @@ +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() + )); + } +} diff --git a/src/LispFactory.php b/src/LispFactory.php index 111dd7a..687054d 100644 --- a/src/LispFactory.php +++ b/src/LispFactory.php @@ -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);