summaryrefslogtreecommitdiff
path: root/drivers/adodb-sqlite3.inc.php
diff options
context:
space:
mode:
authorMark Newnham <mark@newnhams.com>2021-01-23 18:11:22 -0700
committerDamien Regad <dregad@mantisbt.org>2021-01-24 16:56:25 +0100
commit407e4c57fc172e450f0ad7baac3343f7632b1193 (patch)
treec00c22480fcee8c4be07cf7fd5fa20c23801a089 /drivers/adodb-sqlite3.inc.php
parent1ca83f510c6b3821b7d48d5f40779b0cf988d0ae (diff)
downloadadodb-407e4c57fc172e450f0ad7baac3343f7632b1193.tar.gz
adodb-407e4c57fc172e450f0ad7baac3343f7632b1193.tar.bz2
adodb-407e4c57fc172e450f0ad7baac3343f7632b1193.zip
metaIndexes does not return primary key correctly, see #656 (#660)
The primary key parameter was reversed . In addition, the method tried to obtain primary key information from the sqlite_master table, instead of the pragma (cherry picked from commit 45a1d6a567727a846fe2fa218c3f77dbc77464fc)
Diffstat (limited to 'drivers/adodb-sqlite3.inc.php')
-rw-r--r--drivers/adodb-sqlite3.inc.php80
1 files changed, 72 insertions, 8 deletions
diff --git a/drivers/adodb-sqlite3.inc.php b/drivers/adodb-sqlite3.inc.php
index be18edaf..976a7af3 100644
--- a/drivers/adodb-sqlite3.inc.php
+++ b/drivers/adodb-sqlite3.inc.php
@@ -417,8 +417,8 @@ class ADODB_sqlite3 extends ADOConnection {
{
return $this->_connectionID->close();
}
-
- function MetaIndexes($table, $primary = FALSE, $owner = false)
+
+ function metaIndexes($table, $primary = FALSE, $owner = false)
{
$false = false;
// save old fetch mode
@@ -428,8 +428,36 @@ class ADODB_sqlite3 extends ADOConnection {
if ($this->fetchMode !== FALSE) {
$savem = $this->SetFetchMode(FALSE);
}
- $SQL=sprintf("SELECT name,sql FROM sqlite_master WHERE type='index' AND LOWER(tbl_name)='%s'", strtolower($table));
- $rs = $this->Execute($SQL);
+
+ $pragmaData = array();
+
+ /*
+ * If we want the primary key, we must extract
+ * it from the table statement, and the pragma
+ */
+ if ($primary)
+ {
+ $sql = sprintf('PRAGMA table_info([%s]);',
+ strtolower($table)
+ );
+ $pragmaData = $this->getAll($sql);
+ }
+
+ /*
+ * Exclude the empty entry for the primary index
+ */
+ $sqlite = "SELECT name,sql
+ FROM sqlite_master
+ WHERE type='index'
+ AND sql IS NOT NULL
+ AND LOWER(tbl_name)='%s'";
+
+ $SQL = sprintf($sqlite,
+ strtolower($table)
+ );
+
+ $rs = $this->execute($SQL);
+
if (!is_object($rs)) {
if (isset($savem)) {
$this->SetFetchMode($savem);
@@ -439,10 +467,10 @@ class ADODB_sqlite3 extends ADOConnection {
}
$indexes = array ();
- while ($row = $rs->FetchRow()) {
- if ($primary && preg_match("/primary/i",$row[1]) == 0) {
- continue;
- }
+
+ while ($row = $rs->FetchRow())
+ {
+
if (!isset($indexes[$row[0]])) {
$indexes[$row[0]] = array(
'unique' => preg_match("/unique/i",$row[1]),
@@ -457,10 +485,46 @@ class ADODB_sqlite3 extends ADOConnection {
preg_match_all('/\((.*)\)/',$row[1],$indexExpression);
$indexes[$row[0]]['columns'] = array_map('trim',explode(',',$indexExpression[1][0]));
}
+
if (isset($savem)) {
$this->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
}
+
+ /*
+ * If we want primary, add it here
+ */
+ if ($primary){
+
+ /*
+ * Check the previously retrieved pragma to search
+ * with a closure
+ */
+
+ $pkIndexData = array('unique'=>1,'columns'=>array());
+
+ $pkCallBack = function ($value, $key) use (&$pkIndexData) {
+
+ /*
+ * As we iterate the elements check for pk index and sort
+ */
+ if ($value[5] > 0)
+ {
+ $pkIndexData['columns'][$value[5]] = strtolower($value[1]);
+ ksort($pkIndexData['columns']);
+ }
+ };
+
+ array_walk($pragmaData,$pkCallBack);
+
+ /*
+ * If we found no columns, there is no
+ * primary index
+ */
+ if (count($pkIndexData['columns']) > 0)
+ $indexes['PRIMARY'] = $pkIndexData;
+ }
+
return $indexes;
}