/** * fixes sequence values for pgsql * * @param array $aTable */ function _fixSequences($prefix, $table_name, &$aTable) { if ($this->oDbh->dbsyntax == 'pgsql') { $oTable = new OA_DB_Table(); foreach ($aTable['fields'] as $fieldName => $fieldProperties) { if (!empty($fieldProperties['autoincrement'])) { $tblName = $this->oDbh->quoteIdentifier($prefix . $table_name, true); $seqName = "{$prefix}{$table_name}_{$fieldName}_seq"; $maxValue = $this->oDbh->queryOne("SELECT MAX({$fieldName}) FROM {$tblName}"); $oTable->resetSequence($seqName, $maxValue + 1); } } } }
function test_resetSequence() { $oDbh =& OA_DB::singleton(); if ($oDbh->dbsyntax == 'pgsql') { $sequence = 'test_table1_test_id1_seq'; } else { if ($oDbh->dbsyntax == 'mysql') { $sequence = 'test_table1'; } } $conf =& $GLOBALS['_MAX']['CONF']; $conf['table']['prefix'] = ''; $oTable = new OA_DB_Table(); $this->_writeSequenceTestDatabaseSchema(); $oTable->init(MAX_PATH . '/var/test.xml'); $oTable->createTable('test_table1'); $aExistingTables = OA_DB_Table::listOATablesCaseSensitive(); $this->assertEqual($aExistingTables[0], 'test_table1'); if ($oDbh->dbsyntax == 'pgsql') { OA_DB::setCaseSensitive(); $aSequences = $oDbh->manager->listSequences(); OA_DB::disableCaseSensitive(); $this->assertEqual($aSequences[0], 'test_table1_test_id1'); } for ($i = 1; $i < 11; $i++) { $query = "INSERT INTO " . $oDbh->quoteIdentifier('test_table1', true) . " (test_desc1) VALUES ('{$i}')"; $oDbh->query($query); } $query = "SELECT * FROM " . $oDbh->quoteIdentifier('test_table1', true); $aRows = $oDbh->queryAll($query); $this->assertEqual(count($aRows), 10, 'incorrect number of rows in test_table1'); reset($aRows); foreach ($aRows as $k => $v) { $this->assertTrue($v['test_id1'] == $v['test_desc1'], 'sequence problem with new table'); } $query = "DELETE FROM " . $oDbh->quoteIdentifier('test_table1', true); $oDbh->query($query); $query = "SELECT * FROM " . $oDbh->quoteIdentifier('test_table1', true); $aRows = $oDbh->queryAll($query); $this->assertEqual(count($aRows), 0, 'failed to delete rows from test_table1'); $this->assertTrue($oTable->resetSequence($sequence), 'failed to reset sequence on test_table1'); for ($i = 1; $i < 11; $i++) { $query = "INSERT INTO " . $oDbh->quoteIdentifier('test_table1', true) . " (test_desc1) VALUES ('{$i}')"; $oDbh->query($query); } $query = "SELECT * FROM " . $oDbh->quoteIdentifier('test_table1', true); $aRows = $oDbh->queryAll($query); $this->assertEqual(count($aRows), 10, 'incorrect number of rows in test_table1'); reset($aRows); foreach ($aRows as $k => $v) { $this->assertTrue($v['test_id1'] == $v['test_desc1'], 'sequence problem after reset: ' . $v['test_id1'] . '=>' . $v['test_desc1']); } $query = "DELETE FROM " . $oDbh->quoteIdentifier('test_table1', true); $oDbh->query($query); $query = "SELECT * FROM " . $oDbh->quoteIdentifier('test_table1', true); $aRows = $oDbh->queryAll($query); $this->assertEqual(count($aRows), 0, 'failed to delete rows from test_table1'); // Test second parameter $this->assertTrue($oTable->resetSequence($sequence, 1000), 'failed to reset sequence on test_table1'); $query = "INSERT INTO " . $oDbh->quoteIdentifier('test_table1', true) . " (test_desc1) VALUES ('1')"; $oDbh->exec($query); $nextId = $oDbh->queryOne("SELECT test_id1 FROM " . $oDbh->quoteIdentifier('test_table1', true)); if ($oDbh->dbsyntax == 'pgsql') { $this->assertEqual($nextId, 1000); } else { $this->assertEqual($nextId, 1); } $oTable->dropTable('test_table1'); @unlink(MAX_PATH . '/var/test.xml'); }