Finish SQLite

Don't require num_rows
This commit is contained in:
Jakub Vrana 2010-05-27 16:10:17 +02:00
parent 55140a6ae8
commit 782921b671
12 changed files with 121 additions and 94 deletions

View file

@ -151,7 +151,7 @@ foreach ($engines as $engine) {
<p>
<?php echo lang('Table name'); ?>: <input name="name" maxlength="64" value="<?php echo h($row["name"]); ?>">
<?php echo ($engines ? html_select("Engine", array("" => "(" . lang('engine') . ")") + $engines, $row["Engine"]) : ""); ?>
<?php echo ($collations && $jush != "mssql" ? html_select("Collation", array("" => "(" . lang('collation') . ")") + $collations, $row["Collation"]) : ""); ?>
<?php echo ($collations && !ereg("sqlite|mssql", $jush) ? html_select("Collation", array("" => "(" . lang('collation') . ")") + $collations, $row["Collation"]) : ""); ?>
<input type="submit" value="<?php echo lang('Save'); ?>">
<table cellspacing="0" id="edit-fields" class="nowrap">
<?php $comments = edit_fields($row["fields"], $collations, "TABLE", $suhosin, $foreign_keys, $row["Comment"] != ""); ?>

View file

@ -92,7 +92,7 @@ if (isset($_GET["mssql"])) {
function Min_Result($result) {
$this->_result = $result;
$this->num_rows = sqlsrv_has_rows($result); //! sqlsrv_num_rows($result)
// $this->num_rows = sqlsrv_num_rows($result); // available only in scrollable results
}
function _convert($row) {

View file

@ -767,6 +767,19 @@ if (!defined("DRIVER")) {
return queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")");
}
/** Insert or update data in the table
* @param string
* @param array
* @return bool
*/
function insert_update($table, $set) {
foreach ($set as $key => $val) {
$set[$key] = "$key = $val";
}
$update = implode(", ", $set);
return queries("INSERT INTO " . table($table) . " SET $update ON DUPLICATE KEY UPDATE $update");
}
/** Get last auto increment ID
* @return string
*/
@ -822,6 +835,14 @@ if (!defined("DRIVER")) {
return $connection->result("SHOW CREATE TABLE " . table($table), 1);
}
/** Get SQL command to truncate table
* @param string
* @return string
*/
function truncate_sql($table) {
return "TRUNCATE " . table($table);
}
/** Get SQL command to change database
* @param string
* @return string

View file

@ -83,7 +83,6 @@ if (isset($_GET["oracle"])) {
function Min_Result($result) {
$this->_result = $result;
$this->num_rows = -1; // all results unbuffered
}
function _convert($row) {

View file

@ -39,6 +39,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return "'" . sqlite_escape_string($string) . "'";
}
function store_result() {
return $this->_result;
}
function result($query, $field = 0) {
$result = $this->query($query);
if (!is_object($result)) {
@ -118,6 +122,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return "'" . $this->_link->escapeString($string) . "'";
}
function store_result() {
return $this->_result;
}
function result($query, $field = 0) {
$result = $this->query($query);
if (!is_object($result)) {
@ -133,7 +141,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
function Min_Result($result) {
$this->_result = $result;
$this->num_rows = 1; //!
}
function fetch_assoc() {
@ -190,10 +197,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return $this->_result = $this->query($query);
}
function store_result() {
return $this->_result;
}
function next_result() {
return false;
}
@ -238,7 +241,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
}
function tables_list() {
return get_key_vals("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view')", 1);
return get_key_vals("SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') ORDER BY (name = 'sqlite_sequence'), name", 1);
}
function count_tables($databases) {
@ -278,14 +281,14 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
if (is_object($result)) {
while ($row = $result->fetch_assoc()) {
$type = strtolower($row["type"]);
$default = $row["dflt_value"];
$return[$row["name"]] = array(
"field" => $row["name"],
"type" => (eregi("int", $type) ? "integer" : (eregi("char|clob|text", $type) ? "text" : (eregi("blob", $type) ? "blob" : (eregi("real|floa|doub", $type) ? "real" : "numeric")))),
"full_type" => $type,
"default" => $row["dflt_value"],
"default" => (ereg("'(.*)'", $default, $match) ? str_replace("''", "'", $match[1]) : ($default == "NULL" ? null : $default)),
"null" => !$row["notnull"],
"auto_increment" => eregi('^integer$', $type) && $row["pk"], //! possible false positive
"collation" => null, //!
"privileges" => array("select" => 1, "insert" => 1, "update" => 1),
"primary" => $row["pk"],
);
@ -489,6 +492,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return queries("INSERT INTO " . table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : "DEFAULT VALUES"));
}
function insert_update($table, $set) {
return queries("REPLACE INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ")");
}
function last_id() {
global $connection;
return $connection->result("SELECT LAST_INSERT_ROWID()");
@ -498,6 +505,10 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return $connection->query("EXPLAIN $query");
}
function types() {
return array();
}
function schemas() {
return array();
}
@ -515,9 +526,11 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
return $connection->result("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = " . $connection->quote($table));
}
function truncate_sql($table) {
return "DELETE FROM " . table($table);
}
function use_sql($database) {
global $connection;
return "ATTACH " . $connection->quote($database) . " AS " . idf_escape($database);
}
function trigger_sql($table, $style) {

View file

@ -157,10 +157,11 @@ page_header(lang('Export'), "", ($_GET["export"] != "" ? array("table" => $_GET[
<?php
$db_style = array('', 'USE', 'DROP+CREATE', 'CREATE');
$table_style = array('', 'DROP+CREATE', 'CREATE');
$data_style = array('', 'TRUNCATE+INSERT', 'INSERT', 'INSERT+UPDATE');
$data_style = array('', 'TRUNCATE+INSERT', 'INSERT');
if ($jush == "sql") {
$db_style[] = 'CREATE+ALTER';
$table_style[] = 'CREATE+ALTER';
$data_style[] = 'INSERT+UPDATE';
}
parse_str($_COOKIE["adminer_export"], $row);
if (!$row) {
@ -169,10 +170,10 @@ if (!$row) {
$checked = ($_GET["dump"] == "");
echo "<tr><th>" . lang('Output') . "<td>" . $adminer->dumpOutput(0, $row["output"]) . "\n";
echo "<tr><th>" . lang('Format') . "<td>" . $adminer->dumpFormat(0, $row["format"]) . "\n";
echo "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
echo ($jush == "sqlite" ? "" : "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
. (support("routine") ? checkbox("routines", 1, $checked, lang('Routines')) : "")
. (support("event") ? checkbox("events", 1, $checked, lang('Events')) : "")
;
);
echo "<tr><th>" . lang('Tables') . "<td>" . html_select('table_style', $table_style, $row["table_style"])
. (support("trigger") ? checkbox("triggers", 1, $row["table_style"], lang('Triggers')) : "")
;

View file

@ -304,7 +304,7 @@ document.getElementById('username').focus();
* @return array expressions to join by AND
*/
function selectSearchProcess($fields, $indexes) {
global $connection;
global $connection, $jush;
$return = array();
foreach ($indexes as $i => $index) {
if ($index["type"] == "FULLTEXT" && $_GET["fulltext"][$i] != "") {
@ -330,7 +330,7 @@ document.getElementById('username').focus();
foreach ($fields as $name => $field) {
if (is_numeric($val["val"]) || !ereg('int|float|double|decimal', $field["type"])) {
$name = idf_escape($name);
$cols[] = (ereg('char|text|enum|set', $field["type"]) && !ereg('^utf8', $field["collation"]) ? "CONVERT($name USING utf8)" : $name);
$cols[] = ($jush == "sql" && ereg('char|text|enum|set', $field["type"]) && !ereg('^utf8', $field["collation"]) ? "CONVERT($name USING utf8)" : $name);
}
}
$return[] = ($cols ? "(" . implode("$cond OR ", $cols) . "$cond)" : "0");

View file

@ -5,77 +5,73 @@
* @return null
*/
function select($result, $connection2 = null) {
if (!$result->num_rows) {
echo "<p class='message'>" . lang('No rows.') . "\n";
} else {
echo "<table cellspacing='0' class='nowrap'>\n";
$links = array(); // colno => orgtable - create links from these columns
$indexes = array(); // orgtable => array(column => colno) - primary keys
$columns = array(); // orgtable => array(column => ) - not selected columns in primary key
$blobs = array(); // colno => bool - display bytes for blobs
$types = array(); // colno => type - display char in <code>
odd(''); // reset odd for each result
for ($i=0; $row = $result->fetch_row(); $i++) {
if (!$i) {
echo "<thead><tr>";
for ($j=0; $j < count($row); $j++) {
$field = $result->fetch_field();
$orgtable = $field->orgtable;
$orgname = $field->orgname;
if ($orgtable != "") {
if (!isset($indexes[$orgtable])) {
// find primary key in each table
$indexes[$orgtable] = array();
foreach (indexes($orgtable, $connection2) as $index) {
if ($index["type"] == "PRIMARY") {
$indexes[$orgtable] = array_flip($index["columns"]);
break;
}
$links = array(); // colno => orgtable - create links from these columns
$indexes = array(); // orgtable => array(column => colno) - primary keys
$columns = array(); // orgtable => array(column => ) - not selected columns in primary key
$blobs = array(); // colno => bool - display bytes for blobs
$types = array(); // colno => type - display char in <code>
odd(''); // reset odd for each result
for ($i=0; $row = $result->fetch_row(); $i++) {
if (!$i) {
echo "<table cellspacing='0' class='nowrap'>\n";
echo "<thead><tr>";
for ($j=0; $j < count($row); $j++) {
$field = $result->fetch_field();
$orgtable = $field->orgtable;
$orgname = $field->orgname;
if ($orgtable != "") {
if (!isset($indexes[$orgtable])) {
// find primary key in each table
$indexes[$orgtable] = array();
foreach (indexes($orgtable, $connection2) as $index) {
if ($index["type"] == "PRIMARY") {
$indexes[$orgtable] = array_flip($index["columns"]);
break;
}
$columns[$orgtable] = $indexes[$orgtable];
}
if (isset($columns[$orgtable][$orgname])) {
unset($columns[$orgtable][$orgname]);
$indexes[$orgtable][$orgname] = $j;
$links[$j] = $orgtable;
}
$columns[$orgtable] = $indexes[$orgtable];
}
if ($field->charsetnr == 63) { // 63 - binary
$blobs[$j] = true;
}
$types[$j] = $field->type;
echo "<th" . ($orgtable != "" || $field->name != $orgname ? " title='" . h(($orgtable != "" ? "$orgtable." : "") . $orgname) . "'" : "") . ">" . h($field->name);
}
echo "</thead>\n";
}
echo "<tr" . odd() . ">";
foreach ($row as $key => $val) {
if (!isset($val)) {
$val = "<i>NULL</i>";
} else {
if ($blobs[$key] && !is_utf8($val)) {
$val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
} elseif (!strlen($val)) { // strlen - SQLite can return int
$val = "&nbsp;"; // some content to print a border
} else {
$val = h($val);
if ($types[$key] == 254) { // 254 - char
$val = "<code>$val</code>";
}
}
if (isset($links[$key]) && !$columns[$links[$key]]) {
$link = "edit=" . urlencode($links[$key]);
foreach ($indexes[$links[$key]] as $col => $j) {
$link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
}
$val = "<a href='" . h(ME . $link) . "'>$val</a>";
if (isset($columns[$orgtable][$orgname])) {
unset($columns[$orgtable][$orgname]);
$indexes[$orgtable][$orgname] = $j;
$links[$j] = $orgtable;
}
}
echo "<td>$val";
if ($field->charsetnr == 63) { // 63 - binary
$blobs[$j] = true;
}
$types[$j] = $field->type;
echo "<th" . ($orgtable != "" || $field->name != $orgname ? " title='" . h(($orgtable != "" ? "$orgtable." : "") . $orgname) . "'" : "") . ">" . h($field->name);
}
echo "</thead>\n";
}
echo "<tr" . odd() . ">";
foreach ($row as $key => $val) {
if (!isset($val)) {
$val = "<i>NULL</i>";
} else {
if ($blobs[$key] && !is_utf8($val)) {
$val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
} elseif (!strlen($val)) { // strlen - SQLite can return int
$val = "&nbsp;"; // some content to print a border
} else {
$val = h($val);
if ($types[$key] == 254) { // 254 - char
$val = "<code>$val</code>";
}
}
if (isset($links[$key]) && !$columns[$links[$key]]) {
$link = "edit=" . urlencode($links[$key]);
foreach ($indexes[$links[$key]] as $col => $j) {
$link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
}
$val = "<a href='" . h(ME . $link) . "'>$val</a>";
}
}
echo "<td>$val";
}
echo "</table>\n";
}
echo ($i ? "</table>" : "<p class='message'>" . lang('No rows.')) . "\n";
}
/** Get referencable tables with single column primary key except self

View file

@ -20,7 +20,7 @@ function dump_table($table, $style, $is_view = false) {
$create = create_sql($table);
if ($create) {
if ($style == "DROP+CREATE") {
echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n";
echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . table($table) . ";\n";
}
echo ($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n";
}
@ -86,7 +86,7 @@ CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
UNTIL done END REPEAT;
CLOSE columns;
IF @alter_table != '' OR add_columns != '' THEN
SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . idf_escape($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');
SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');
END IF;
END;;
DELIMITER ;
@ -104,10 +104,10 @@ function dump_data($table, $style, $select = "") {
$max_packet = ($jush == "sqlite" ? 0 : 1048576); // default, minimum is 1024
if ($style) {
if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") {
echo "TRUNCATE " . idf_escape($table) . ";\n";
echo truncate_sql($table) . ";\n";
}
$fields = fields($table);
$result = $connection->query(($select ? $select : "SELECT * FROM " . idf_escape($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
$result = $connection->query(($select ? $select : "SELECT * FROM " . table($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
if ($result) {
$insert = "";
$buffer = "";
@ -116,7 +116,7 @@ function dump_data($table, $style, $select = "") {
dump_csv($row);
} else {
if (!$insert) {
$insert = "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES";
$insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES";
}
foreach ($row as $key => $val) {
$row[$key] = (isset($val) ? (ereg('int|float|double|decimal', $fields[$key]["type"]) ? $val : $connection->quote($val)) : "NULL"); //! columns looking like functions

View file

@ -626,7 +626,7 @@ function search_tables() {
$name = $adminer->tableName($table_status);
if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
$result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));
if ($result->num_rows) {
if ($result->fetch_row()) {
if (!$found) {
echo "<ul>\n";
$found = true;

View file

@ -144,12 +144,11 @@ if ($_POST && !$error) {
$cols = $matches2[1];
$affected--;
} else {
$set = "";
$set = array();
foreach ($matches2[1] as $i => $col) {
$set .= ", " . idf_escape($cols[$i]) . " = " . ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
$set[idf_escape($cols[$i])] = ($col == "" && $fields[$cols[$i]]["null"] ? "NULL" : $connection->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col))));
}
$set = substr($set, 1);
$result = queries("INSERT INTO " . table($TABLE) . " SET$set ON DUPLICATE KEY UPDATE$set");
$result = insert_update($TABLE, $set);
if (!$result) {
break;
}
@ -159,7 +158,7 @@ if ($_POST && !$error) {
queries("COMMIT");
}
queries_redirect(remove_from_uri("page"), lang('%d row(s) have been imported.', $affected), $result);
queries("ROLLBACK");
queries("ROLLBACK"); // after queries_redirect() to not overwrite error
} else {
$error = upload_error($file);
}

View file

@ -26,8 +26,6 @@ Saving of MySQL 5 BIT data type - don't use quote()
? Geometry support
SQLite:
CSV import - ON DUPLICATE KEY UPDATE
Export - CREATE DATABASE
Delimiter in export and SQL command
Backward keys in Editor