Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / TableGateway / Feature / SequenceFeature.php
blobb5d8ab6cf278ebf9e8b569e2c5955ec6f6e059d8
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Db\TableGateway\Feature;
12 use Zend\Db\Sql\Insert;
13 use Zend\Db\Adapter\Driver\ResultInterface;
14 use Zend\Db\Adapter\Driver\StatementInterface;
16 class SequenceFeature extends AbstractFeature
18 /**
19 * @var string
21 protected $primaryKeyField;
23 /**
24 * @var string
26 protected $sequenceName;
28 /**
29 * @var int
31 protected $sequenceValue;
34 /**
35 * @param string $primaryKeyField
36 * @param string $sequenceName
38 public function __construct($primaryKeyField, $sequenceName)
40 $this->primaryKeyField = $primaryKeyField;
41 $this->sequenceName = $sequenceName;
44 /**
45 * @param Insert $insert
47 public function preInsert(Insert $insert)
49 $columns = $insert->getRawState('columns');
50 $values = $insert->getRawState('values');
51 $key = array_search($this->primaryKeyField, $columns);
52 if ($key !== false) {
53 $this->sequenceValue = $values[$key];
54 return $insert;
57 $this->sequenceValue = $this->nextSequenceId();
58 if ($this->sequenceValue === null) {
59 return $insert;
62 $insert->values(array($this->primaryKeyField => $this->sequenceValue), Insert::VALUES_MERGE);
63 return $insert;
66 public function postInsert(StatementInterface $statement, ResultInterface $result)
68 if ($this->sequenceValue !== null) {
69 $this->tableGateway->lastInsertValue = $this->sequenceValue;
73 /**
74 * Generate a new value from the specified sequence in the database, and return it.
75 * @return int
77 public function nextSequenceId()
79 $platform = $this->tableGateway->adapter->getPlatform();
80 $platformName = $platform->getName();
82 switch ($platformName) {
83 case 'Oracle':
84 $sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.NEXTVAL FROM dual';
85 break;
86 case 'PostgreSQL':
87 $sql = 'SELECT NEXTVAL(\'' . $this->sequenceName . '\')';
88 break;
89 default :
90 return null;
93 $statement = $this->tableGateway->adapter->createStatement();
94 $statement->prepare($sql);
95 $result = $statement->execute();
96 $sequence = $result->current();
97 unset($statement, $result);
98 return $sequence['nextval'];
102 * Return the most recent value from the specified sequence in the database.
103 * @return int
105 public function lastSequenceId()
107 $platform = $this->tableGateway->adapter->getPlatform();
108 $platformName = $platform->getName();
110 switch ($platformName) {
111 case 'Oracle':
112 $sql = 'SELECT ' . $platform->quoteIdentifier($this->sequenceName) . '.CURRVAL FROM dual';
113 break;
114 case 'PostgreSQL':
115 $sql = 'SELECT CURRVAL(\'' . $this->sequenceName . '\')';
116 break;
117 default :
118 return null;
121 $statement = $this->tableGateway->adapter->createStatement();
122 $statement->prepare($sql);
123 $result = $statement->execute();
124 $sequence = $result->current();
125 unset($statement, $result);
126 return $sequence['currval'];