From 65d612689cfdc3c7daf4abda9b92cceb41e8705e Mon Sep 17 00:00:00 2001 From: wazsmwazsm <18748406022@163.com> Date: Tue, 19 Dec 2017 20:02:24 +0800 Subject: [PATCH] add last insert id method --- README.md | 2 +- README_CN.md | 2 +- src/WorkerF/DB/Drivers/ConnectorInterface.php | 9 ++++++ src/WorkerF/DB/Drivers/PDODriver.php | 14 ++++++++ src/WorkerF/DB/Drivers/Pgsql.php | 32 ++++++++++++++++++- tests/DB/PDODML.php | 17 +++++++++- 6 files changed, 72 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6689b01..f3c881b 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ - singleton db connection - use dependency injection - brief routing - - support mysql driver, timeout auto reconnect + - support mysql postgresql sqlite driver, timeout auto reconnect - support redis driver, based on predis Not allround but brief, Extensible, efficient diff --git a/README_CN.md b/README_CN.md index 6cfee9d..deb5328 100644 --- a/README_CN.md +++ b/README_CN.md @@ -11,7 +11,7 @@ - 单例的数据库连接 - 使用依赖注入 - 简洁的路由 - - 提供 mysql 驱动, 支持断线自动重连 + - 提供 mysql postgresql sqlite 驱动, 支持断线自动重连 - 提供 redis 驱动, 基于 predis WorkerA 不是一个全面的、多功能的框架, 它很小, 只有一些最基础的功能。 diff --git a/src/WorkerF/DB/Drivers/ConnectorInterface.php b/src/WorkerF/DB/Drivers/ConnectorInterface.php index 2afe273..b26d66c 100644 --- a/src/WorkerF/DB/Drivers/ConnectorInterface.php +++ b/src/WorkerF/DB/Drivers/ConnectorInterface.php @@ -438,6 +438,15 @@ public function avg($field); */ public function insert(array $data); + /** + * insert data, get last insert ID + * + * @param array $data + * @return null/int + * @throws \PDOException + */ + public function insertGetLastId(array $data); + /** * update data * diff --git a/src/WorkerF/DB/Drivers/PDODriver.php b/src/WorkerF/DB/Drivers/PDODriver.php index db0bdf7..82847c1 100644 --- a/src/WorkerF/DB/Drivers/PDODriver.php +++ b/src/WorkerF/DB/Drivers/PDODriver.php @@ -1414,6 +1414,20 @@ public function insert(array $data) return $this->_pdoSt->rowCount(); } + /** + * insert data, get last insert ID, this method dosen't apply to postgresql + * + * @param array $data + * @return null/int + * @throws \PDOException + */ + public function insertGetLastId(array $data) + { + $this->insert($data); + + return $this->_pdo->lastInsertId(); + } + /** * update data * diff --git a/src/WorkerF/DB/Drivers/Pgsql.php b/src/WorkerF/DB/Drivers/Pgsql.php index a92aa2b..380c688 100644 --- a/src/WorkerF/DB/Drivers/Pgsql.php +++ b/src/WorkerF/DB/Drivers/Pgsql.php @@ -62,7 +62,7 @@ protected function _connect() try { $this->_pdo = new PDO($dsn, $user, $password, $options); - + // charset set if(isset($charset)) { $this->_pdo->prepare("set names '$charset'")->execute(); @@ -84,5 +84,35 @@ protected function _connect() } } + /** + * get last insert ID for postgresql + * + * @param array $data + * @return null/int + * @throws \PDOException + */ + public function insertGetLastId(array $data) + { + // create build str + $field_str = ''; + $value_str = ''; + foreach ($data as $key => $value) { + $field_str .= ' '.self::_backquote($key).','; + $plh = self::_getPlh(); + $this->_bind_params[$plh] = $value; + $value_str .= ' '.$plh.','; + } + + $field_str = rtrim($field_str, ','); + $value_str = rtrim($value_str, ','); + + $this->_insert_str = ' ('.$field_str.') VALUES ('.$value_str.') RETURNING id '; + // execute + $this->_buildInsert(); + $this->_execute(); + $result = $this->_pdoSt->fetch(PDO::FETCH_ASSOC); + + return $result['id']; + } } diff --git a/tests/DB/PDODML.php b/tests/DB/PDODML.php index 0fe56f0..ab9858b 100644 --- a/tests/DB/PDODML.php +++ b/tests/DB/PDODML.php @@ -44,6 +44,21 @@ public function testInsert() $this->assertEquals($count_before + 1, $count_after); } + public function testInsertGetLastId() + { + $insert_data = [ + 'id' => 5, + 'c_id' => 1, + 'groupname' => 'test_group', + 'sort_num' => 50, + 'created' => time(), + ]; + + $last_id = self::$db->table('user_group')->insertGetLastId($insert_data); + + $this->assertEquals(5, $last_id); + } + public function testUpdate() { $update_data = [ @@ -110,5 +125,5 @@ public function testTrans() $this->assertEquals($count_before - 2, $count_after); } - + }