Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- `pdo/get-attribute` / `pdo/set-attribute` now dispatch on the handle: pass a connection (as before) or a statement to reach `PDOStatement::getAttribute` / `setAttribute` ([#12]).
- `pdo/error-code` / `pdo/error-info` now dispatch on the handle: pass a statement to read `PDOStatement::errorCode` / `errorInfo` ([#14]).

## [0.1.0] - 2026-05-13

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ All functions live in the `phel.pdo` namespace.
| `in-transaction` | `(in-transaction conn)` | `true` if a transaction is active. |
| `get-attribute` / `set-attribute` | `(get-attribute handle attr)` / `(set-attribute handle attr value)` | PDO attribute access; `handle` is a connection or a statement. |
| `get-available-drivers` | `(get-available-drivers conn)` | Vector of installed PDO drivers. |
| `error-code` | `(error-code conn)` | SQLSTATE of the last operation. |
| `error-info` | `(error-info conn)` | `[sqlstate driver-code driver-message]`. |
| `error-code` | `(error-code handle)` | SQLSTATE of the last operation; `handle` is a connection or a statement. |
| `error-info` | `(error-info handle)` | `[sqlstate driver-code driver-message]`; `handle` is a connection or a statement. |

### Statement

Expand Down
20 changes: 10 additions & 10 deletions src/pdo.phel
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

(declare statement?)

(defn- attr-target
"The PDO handle behind a connection or statement struct."
(defn- handle-target
"The raw PDO handle behind a connection or statement struct."
[handle]
(if (statement? handle) (handle :stmt) (handle :pdo)))

(defn ^bool set-attribute
"Set an attribute on a connection or statement"
[handle attribute value]
(php/-> (attr-target handle) (setAttribute attribute value)))
(php/-> (handle-target handle) (setAttribute attribute value)))

(defn get-attribute
"Retrieve an attribute from a connection or statement"
[handle attribute]
(php/-> (attr-target handle) (getAttribute attribute)))
(php/-> (handle-target handle) (getAttribute attribute)))

(defn connect
"Connect database and return connection object.
Expand Down Expand Up @@ -93,15 +93,15 @@
(last-insert-id conn)))

(defn ^int error-code
"Fetch the SQLSTATE associated with the last operation on the database handle"
[conn]
(php/intval (php/-> (conn :pdo) (errorCode))))
"Fetch the SQLSTATE associated with the last operation on a connection or statement"
[handle]
(php/intval (php/-> (handle-target handle) (errorCode))))

(defn error-info
"Fetch extended error information associated with the last operation on the database handle"
[conn]
"Fetch extended error information associated with the last operation on a connection or statement"
[handle]
(let [[sqlstate driver-code driver-message]
(values (php->phel (php/-> (conn :pdo) (errorInfo))))]
(values (php->phel (php/-> (handle-target handle) (errorInfo))))]
[(php/intval sqlstate) (php/intval driver-code) driver-message]))

(defn ^bool in-transaction
Expand Down
2 changes: 0 additions & 2 deletions src/pdo/statement.phel
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@
;; Not implemented yet:
;;
;; PDOStatement::bindColumn — Bind a column to a PHP variable
;; PDOStatement::errorCode — Fetch the SQLSTATE associated with the last operation on the statement handle
;; PDOStatement::errorInfo — Fetch extended error information associated with the last operation on the statement handle
;; PDOStatement::getColumnMeta — Returns metadata for a column in a result set
;; PDOStatement::getIterator — Gets result set iterator
;; PDOStatement::nextRowset — Advances to the next rowset in a multi-rowset statement handle
16 changes: 16 additions & 0 deletions tests/pdo_test.phel
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,19 @@
(pdo/exec *conn* "insert into t1 (id, name) values (1, 'php')")
(catch \PDOException _e nil))
(is (= [23000 19 "UNIQUE constraint failed: t1.id"] (pdo/error-info *conn*))))

(deftest statement-error-code-on-constraint-violation
(seed-t1 *conn*)
(let [stmt (pdo/prepare *conn* "insert into t1 (id, name) values (1, 'php')")]
(try
(pdo/execute stmt)
(catch \PDOException _e nil))
(is (= 23000 (pdo/error-code stmt)))))

(deftest statement-error-info-on-constraint-violation
(seed-t1 *conn*)
(let [stmt (pdo/prepare *conn* "insert into t1 (id, name) values (1, 'php')")]
(try
(pdo/execute stmt)
(catch \PDOException _e nil))
(is (= [23000 19 "UNIQUE constraint failed: t1.id"] (pdo/error-info stmt)))))
Loading