Skip to content

Commit

Permalink
add last insert id method
Browse files Browse the repository at this point in the history
  • Loading branch information
wazsmwazsm committed Dec 19, 2017
1 parent 65e2ef3 commit 65d6126
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- 单例的数据库连接
- 使用依赖注入
- 简洁的路由
- 提供 mysql 驱动, 支持断线自动重连
- 提供 mysql postgresql sqlite 驱动, 支持断线自动重连
- 提供 redis 驱动, 基于 predis

WorkerA 不是一个全面的、多功能的框架, 它很小, 只有一些最基础的功能。
Expand Down
9 changes: 9 additions & 0 deletions src/WorkerF/DB/Drivers/ConnectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions src/WorkerF/DB/Drivers/PDODriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
32 changes: 31 additions & 1 deletion src/WorkerF/DB/Drivers/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'];
}

}
17 changes: 16 additions & 1 deletion tests/DB/PDODML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -110,5 +125,5 @@ public function testTrans()
$this->assertEquals($count_before - 2, $count_after);

}

}

0 comments on commit 65d6126

Please sign in to comment.