SQLite: Respect column order and DESC in primary key
Also respect DESC indexes in altering table.
This commit is contained in:
parent
33236f7a83
commit
114aaec91e
|
@ -319,23 +319,27 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function indexes($table, $connection2 = null) {
|
function indexes($table, $connection2 = null) {
|
||||||
|
global $connection;
|
||||||
|
if (!is_object($connection2)) {
|
||||||
|
$connection2 = $connection;
|
||||||
|
}
|
||||||
$return = array();
|
$return = array();
|
||||||
$primary = array();
|
$sql = $connection2->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . q($table));
|
||||||
foreach (fields($table) as $field) {
|
if (preg_match('~\bPRIMARY\s+KEY\s*\((([^)"]+|"[^"]*")++)~i', $sql, $match)) {
|
||||||
if ($field["primary"]) {
|
$return[""] = array("type" => "PRIMARY", "columns" => array(), "descs" => array());
|
||||||
$primary[] = $field["field"];
|
preg_match_all('~((("[^"]*+")+)|(\S+))(\s+(ASC|DESC))?(,\s*|$)~i', $match[1], $matches, PREG_SET_ORDER);
|
||||||
|
foreach ($matches as $match) {
|
||||||
|
$return[""]["columns"][] = idf_unescape($match[2]) . $match[4];
|
||||||
|
$return[""]["descs"][] = (preg_match('~DESC~i', $match[5]) ? '1' : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($primary) {
|
$sqls = get_key_vals("SELECT name, sql FROM sqlite_master WHERE type = 'index' AND tbl_name = " . q($table), $connection2);
|
||||||
$return[""] = array("type" => "PRIMARY", "columns" => $primary, "lengths" => array()); //! column order, descending
|
foreach (get_rows("PRAGMA index_list(" . table($table) . ")", $connection2) as $row) {
|
||||||
}
|
|
||||||
$sqls = get_key_vals("SELECT name, sql FROM sqlite_master WHERE type = 'index' AND tbl_name = " . q($table));
|
|
||||||
foreach (get_rows("PRAGMA index_list(" . table($table) . ")") as $row) {
|
|
||||||
$name = $row["name"];
|
$name = $row["name"];
|
||||||
if (!preg_match("~^sqlite_~", $name)) {
|
if (!preg_match("~^sqlite_~", $name)) {
|
||||||
$return[$name]["type"] = ($row["unique"] ? "UNIQUE" : "INDEX");
|
$return[$name]["type"] = ($row["unique"] ? "UNIQUE" : "INDEX");
|
||||||
$return[$name]["lengths"] = array();
|
$return[$name]["lengths"] = array();
|
||||||
foreach (get_rows("PRAGMA index_info(" . idf_escape($name) . ")") as $row1) {
|
foreach (get_rows("PRAGMA index_info(" . idf_escape($name) . ")", $connection2) as $row1) {
|
||||||
$return[$name]["columns"][] = $row1["name"];
|
$return[$name]["columns"][] = $row1["name"];
|
||||||
}
|
}
|
||||||
$return[$name]["descs"] = array();
|
$return[$name]["descs"] = array();
|
||||||
|
@ -477,11 +481,11 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
|
||||||
$indexes = array();
|
$indexes = array();
|
||||||
foreach (indexes($table) as $key_name => $index) {
|
foreach (indexes($table) as $key_name => $index) {
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach ($index["columns"] as $column) {
|
foreach ($index["columns"] as $key => $column) {
|
||||||
if (!$originals[$column]) {
|
if (!$originals[$column]) {
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
$columns[] = $originals[$column];
|
$columns[] = $originals[$column] . ($index["descs"][$key] ? " DESC" : "");
|
||||||
}
|
}
|
||||||
$columns = "(" . implode(", ", $columns) . ")";
|
$columns = "(" . implode(", ", $columns) . ")";
|
||||||
if ($index["type"] != "PRIMARY") {
|
if ($index["type"] != "PRIMARY") {
|
||||||
|
|
Loading…
Reference in a new issue