Skip to content

Commit

Permalink
feat: implemented pdo methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
smeghead committed May 2, 2024
1 parent 37cc314 commit e8c6cf7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

### Features

* Implemented pdo methods.

## v0.0.0 (2024-04-23)

### Features
Expand Down
45 changes: 37 additions & 8 deletions src/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
61 changes: 61 additions & 0 deletions tests/feature/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit e8c6cf7

Please sign in to comment.