phel-lang pdo wrapper library.
Inherently, it is very easy to call the functionality of PHP classes from phel code. Therefore, it is not difficult to access the database using PDO directly.
However, if you have to think about PHP classes while writing phel code, there is a concern that a context switch will occur between the phel world and the PHP world, and you will not be able to concentrate on writing the phel code.
Therefore, I created a wrapper library (phel-pdo) that can handle PDO just by calling Phel functions.
Install from composer. https://packagist.org/packages/phel-lang/phel-pdo
composer require phel-lang/phel-pdo
This is an example of connecting to a file database, creating a table, inserting records, and searching on repl.
phel:1> (require phel\pdo)
phel\pdo
phel:2> (require phel\pdo\statement)
phel\pdo\statement
phel:3> (def connection-string "sqlite:database.db")
1
phel:4> (def conn (pdo/connect connection-string))
1
phel:5> (pdo/exec conn "create table t1 (id integer primary key autoincrement, name varchr(10))")
0
phel:6> (pdo/exec conn "insert into t1 (name) values ('phel'), ('php')")
2
phel:7> (def stmt (pdo/query conn "select * from t1 where id = 1"))
1
phel:8> (statement/fetch stmt)
{:id 1 :name phel}
phel:8> (def stmt (pdo/prepare conn "select * from t1 where id = :id"))
1
phel:9> (def stmt (statement/execute stmt {:id 1}))
1
phel:10> (statement/fetch stmt)
{:id 1 :name phel}
Represents a connection between PHP and a database server.
Initiates a transaction
(begin conn)
Commits a transaction
(commit conn)
Connect database and return connection object. Throws a PDOException if the attempt to connect to the requested database fails
(connect dns & [username password options])
Fetch the SQLSTATE associated with the last operation on the database handle
(error-code conn)
Fetch extended error information associated with the last operation on the database handle
(error-info conn)
Execute an SQL statement and return the number of affected rows
(exec conn stmt & [fetch-mode])
Retrieve a database connection attribute
(get-attribute conn attribute)
Return an array of available PDO drivers
(get-available-drivers conn)
Checks if inside a transaction
(in-transaction conn)
Returns the ID of the last inserted row or sequence value
(last-insert-id conn)
Prepares a statement for execution and returns a statement object
(prepare conn stmt & [fetch-mode])
Prepares and executes an SQL statement without placeholders
(query conn stmt & [fetch-mode])
Quotes a string for use in a query
(query conn string & [type])
Rolls back a transaction
(rollback conn)
Set an attribute
(set-attribute conn attribute value)
Represents a prepared statement and, after the statement is executed, an associated result set.
Binds a value to a parameter
(bind-value statement column value & [type])
Returns an SQL prepared command
(debug-dump-params statement)
Executes a prepared statement
(execute statement)
Fetches the next row from a result set
(fetch statement)
Fetches the remaining rows from a result set
(fetch-all statement)
Returns a single column from the next row of a result set
(fetch-column statement & [column])
docker compose build
docker compose run --rm php_cli bash
vendor/bin/phel test