diff options
| author | Brice Favre <brice@users.noreply.github.com> | 2019-12-30 01:07:07 +0100 |
|---|---|---|
| committer | Damien Regad <dregad@mantisbt.org> | 2019-12-30 01:07:07 +0100 |
| commit | 7958b13e3752ea802fce5a9fb1aec99f07a1993c (patch) | |
| tree | 3c3240003feb486367fa0908863004ac804444bd | |
| parent | 8e9198a320898ab7e841134b23a8dc18b30770fa (diff) | |
| download | adodb-7958b13e3752ea802fce5a9fb1aec99f07a1993c.tar.gz adodb-7958b13e3752ea802fce5a9fb1aec99f07a1993c.tar.bz2 adodb-7958b13e3752ea802fce5a9fb1aec99f07a1993c.zip | |
PDO MySQL: add GenId / CreateSequence support
Fixes #465
| -rw-r--r-- | drivers/adodb-pdo_mysql.inc.php | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/drivers/adodb-pdo_mysql.inc.php b/drivers/adodb-pdo_mysql.inc.php index 17c6f70f..b18aee9f 100644 --- a/drivers/adodb-pdo_mysql.inc.php +++ b/drivers/adodb-pdo_mysql.inc.php @@ -21,7 +21,10 @@ class ADODB_pdo_mysql extends ADODB_pdo { var $sysDate = 'CURDATE()'; var $sysTimeStamp = 'NOW()'; var $hasGenID = true; - var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);"; + var $_genIDSQL = "UPDATE %s SET id=LAST_INSERT_ID(id+1);"; + var $_genSeqSQL = "CREATE TABLE if NOT EXISTS %s (id int not null)"; + var $_genSeqCountSQL = "SELECT count(*) FROM %s"; + var $_genSeq2SQL = "INSERT INTO %s VALUES (%s)"; var $_dropSeqSQL = "drop table %s"; var $fmtTimeStamp = "'Y-m-d H:i:s'"; var $nameQuote = '`'; @@ -310,4 +313,41 @@ class ADODB_pdo_mysql extends ADODB_pdo { } return $s; } + + function GenID($seqname='adodbseq',$startID=1) + { + $getnext = sprintf($this->_genIDSQL,$seqname); + $holdtransOK = $this->_transOK; // save the current status + $rs = @$this->Execute($getnext); + if (!$rs) { + if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset + $this->Execute(sprintf($this->_genSeqSQL,$seqname)); + $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname)); + if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); + $rs = $this->Execute($getnext); + } + + if ($rs) { + $this->genID = $this->_connectionID->lastInsertId($seqname); + $rs->Close(); + } else { + $this->genID = 0; + } + + return $this->genID; + } + + + function createSequence($seqname='adodbseq',$startID=1) + { + if (empty($this->_genSeqSQL)) { + return false; + } + $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname,$startID)); + if (!$ok) { + return false; + } + + return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); + } } |
