diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a3ef4a..7f2bbe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +### Features + + * Implemented pdo methods. + ## v0.0.0 (2024-04-23) ### Features diff --git a/src/pdo.phel b/src/pdo.phel index d759e3b..78d15d4 100644 --- a/src/pdo.phel +++ b/src/pdo.phel @@ -3,12 +3,22 @@ (defstruct connection [pdo]) +(defn set-attribute + "Set an attribute" + [conn attribute value] + (php/-> (conn :pdo) (setAttribute attribute value))) + +(defn get-attribute + "Retrieve a database connection attribute" + [conn attribute] + (php/-> (conn :pdo) (getAttribute attribute))) + (defn connect "Connect database and return connection object. Throws a PDOException if the attempt to connect to the requested database fails " [dsn & [username password options]] (let [conn (connection (php/new \PDO dsn username password options))] - (php/-> (conn :pdo) (setAttribute (php/:: \PDO ATTR_ERRMODE) (php/:: \PDO ERRMODE_EXCEPTION))) + (set-attribute conn (php/:: \PDO ATTR_ERRMODE) (php/:: \PDO ERRMODE_EXCEPTION)) conn)) (defn exec @@ -46,13 +56,32 @@ [conn string & [type]] (php/-> (conn :pdo) (quote string (or type (php/:: \PDO PARAM_STR))))) +(defn last-insert-id + "Returns the ID of the last inserted row or sequence value" + [conn] + (php/intval (php/-> (conn :pdo) (lastInsertId)))) + +(defn error-code + "Fetch the SQLSTATE associated with the last operation on the database handle" + [conn] + (php/intval (php/-> (conn :pdo) (errorCode)))) + +(defn error-info + "Fetch extended error information associated with the last operation on the database handle" + [conn] + (let [[error-code driver-error-code driver-message] (values (php-array-to-map (php/-> (conn :pdo) (errorInfo))))] + [(php/intval error-code) (php/intval driver-error-code) driver-message])) + +(defn in-transaction + "Checks if inside a transaction" + [conn] + (php/-> (conn :pdo) (inTransaction))) + +(defn get-available-drivers + "Return an array of available PDO drivers" + [conn] + (values (php-array-to-map (php/-> (conn :pdo) (getAvailableDrivers))))) + # not implement yet. -#PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle -#PDO::errorInfo — Fetch extended error information associated with the last operation on the database handle -#PDO::getAttribute — Retrieve a database connection attribute #PDO::getAvailableDrivers — Return an array of available PDO drivers -#PDO::inTransaction — Checks if inside a transaction -#PDO::lastInsertId — Returns the ID of the last inserted row or sequence value -#PDO::prepare — Prepares a statement for execution and returns a statement object -#PDO::setAttribute — Set an attribute diff --git a/tests/feature/pdo.phel b/tests/feature/pdo.phel index c539d93..076a6c5 100644 --- a/tests/feature/pdo.phel +++ b/tests/feature/pdo.phel @@ -76,4 +76,65 @@ (let [conn (pdo/connect "sqlite::memory:")] (is (= "'I''m fine.'" (pdo/quote conn "I'm fine."))))) +(deftest test-last-insert-id + (let [conn (pdo/connect "sqlite::memory:")] + (is (= 0 (pdo/exec + conn + "create table t1 (id integer primary key autoincrement, name varchr(10))"))) + (is (= 1 (pdo/exec + conn + "insert into t1 (name) values ('phel')"))) + (is (= 1 (pdo/last-insert-id conn))))) + +(deftest test-error-code + (let [conn (pdo/connect "sqlite::memory:")] + (is (= 0 (pdo/exec + conn + "create table t1 (id integer primary key autoincrement, name varchr(10))"))) + (is (= 1 (pdo/exec + conn + "insert into t1 (name) values ('phel')"))) + (try + (pdo/exec + conn + "insert into t1 (id, name) values (1, 'php')") + (catch \PDOException e "error")) + (is (= 23000 (pdo/error-code conn))))) +(deftest test-error-info + (let [conn (pdo/connect "sqlite::memory:")] + (is (= 0 (pdo/exec + conn + "create table t1 (id integer primary key autoincrement, name varchr(10))"))) + (is (= 1 (pdo/exec + conn + "insert into t1 (name) values ('phel')"))) + (try + (pdo/exec + conn + "insert into t1 (id, name) values (1, 'php')") + (catch \PDOException e "error")) + (is (= [23000 19 "UNIQUE constraint failed: t1.id"] (pdo/error-info conn))))) + +(deftest test-attribute + (let [conn (pdo/connect "sqlite::memory:")] + (pdo/set-attribute + conn + (php/:: \PDO ATTR_ERRMODE) + (php/:: \PDO ERRMODE_SILENT)) + (is (= (php/:: \PDO ERRMODE_SILENT) + (pdo/get-attribute + conn + (php/:: \PDO ATTR_ERRMODE)))))) + +(deftest test-in-transaction + (let [conn (pdo/connect "sqlite::memory:")] + (is (= false (pdo/in-transaction conn))) + (is (pdo/begin conn)) + (is (= true (pdo/in-transaction conn))) + (is (pdo/commit conn)) + (is (= false (pdo/in-transaction conn))))) + +(deftest test-get-available-drivers + (let [conn (pdo/connect "sqlite::memory:")] + (is (= ["sqlite"] (pdo/get-available-drivers conn)))))