Skip to content

Commit

Permalink
Improve code readability by using of empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpp committed Mar 17, 2024
1 parent 8d48d48 commit f86e3de
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/drivers/elastic.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ function select($table, $select, $where, $group, $order = array(), $limit = 1, $
} else {
$fields = $hit["_source"];
}

foreach ($fields as $key => $val) {
$row[$key] = (is_array($val) ? json_encode($val) : $val);
}
Expand Down
37 changes: 37 additions & 0 deletions plugins/drivers/elastic5.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function query($path, array $content = null, $method = 'GET') {

return $driver->select($matches[1], array("*"), $where, null, array(), $matches[3]);
}

return $this->rootQuery(($this->_db != "" ? "$this->_db/" : "/") . ltrim($path, '/'), $content, $method);
}

Expand Down Expand Up @@ -91,6 +92,7 @@ function connect($server, $username, $password) {

function select_db($database) {
$this->_db = $database;

return true;
}

Expand Down Expand Up @@ -132,9 +134,11 @@ class Min_Driver extends Min_SQL {
function select($table, $select, $where, $group, $order = array(), $limit = 1, $page = 0, $print = false) {
$data = array();
$query = (min_version(7) ? "" : "$table/") . "_search";

if ($select != array("*")) {
$data["fields"] = $select;
}

if ($order) {
$sort = array();
foreach ($order as $col) {
Expand All @@ -143,12 +147,14 @@ function select($table, $select, $where, $group, $order = array(), $limit = 1, $
}
$data["sort"] = $sort;
}

if ($limit) {
$data["size"] = +$limit;
if ($page) {
$data["from"] = ($page * $limit);
}
}

foreach ($where as $val) {
if (preg_match('~^\((.+ OR .+)\)$~', $val, $matches)) {
$parts = explode(" OR ", $matches[1]);
Expand Down Expand Up @@ -176,35 +182,42 @@ function select($table, $select, $where, $group, $order = array(), $limit = 1, $
}
}
}

$start = microtime(true);
$search = $this->_conn->query($query, $data);

if ($print) {
echo adminer()->selectQuery("$query: " . json_encode($data), $start, !$search);
}
if (!$search) {
return false;
}

$return = array();
foreach ($search['hits']['hits'] as $hit) {
$row = array();
if ($select == array("*")) {
$row["_id"] = $hit["_id"];
}

$fields = $hit['_source'];
if ($select != array("*")) {
$fields = array();
foreach ($select as $key) {
$fields[$key] = $key == "_id" ? [$hit["_id"]] : $hit['fields'][$key];
}
}

foreach ($fields as $key => $val) {
if ($data["fields"]) {
$val = $val[0];
}
$row[$key] = (is_array($val) ? json_encode($val) : $val); //! display JSON and others differently
}

$return[] = $row;
}

return new Min_Result($return);
}

Expand All @@ -214,8 +227,10 @@ function update($type, $record, $queryWhere, $limit = 0, $separator = "\n") {
if (count($parts) == 2) {
$id = trim($parts[1]);
$query = "$type/$id";

return $this->_conn->query($query, $record, 'POST');
}

return false;
}

Expand All @@ -224,6 +239,7 @@ function insert($type, $record) {
$query = "$type/$id";
$response = $this->_conn->query($query, $record, 'POST');
$this->_conn->last_id = $response['_id'];

return $response['created'];
}

Expand All @@ -241,14 +257,17 @@ function delete($type, $queryWhere, $limit = 0) {
}
}
}

$this->_conn->affected_rows = 0;

foreach ($ids as $id) {
$query = "{$type}/{$id}";
$response = $this->_conn->query($query, '{}', 'DELETE');
if (is_array($response) && $response['found'] == true) {
$this->_conn->affected_rows++;
}
}

return $this->_conn->affected_rows;
}

Expand All @@ -261,13 +280,16 @@ function convertOperator($operator) {

function connect() {
$connection = new Min_DB;

list($server, $username, $password) = adminer()->credentials();
if ($password != "" && $connection->connect($server, $username, "")) {
return lang('Database does not support password.');
}

if ($connection->connect($server, $username, $password)) {
return $connection;
}

return $connection->error;
}

Expand All @@ -277,6 +299,7 @@ function support($feature) {

function logged_user() {
$credentials = adminer()->credentials();

return $credentials[1];
}

Expand All @@ -286,6 +309,7 @@ function get_databases() {
$return = array_keys($return);
sort($return, SORT_STRING);
}

return $return;
}

Expand All @@ -307,13 +331,15 @@ function engines() {
function count_tables($databases) {
$return = array();
$result = connection()->query('_stats');

if ($result && $result['indices']) {
$indices = $result['indices'];
foreach ($indices as $indice => $stats) {
$indexing = $stats['total']['indexing'];
$return[$indice] = $indexing['index_total'];
}
}

return $return;
}

Expand All @@ -326,6 +352,7 @@ function tables_list() {
if ($return) {
$return = array_fill_keys(array_keys($return[connection()->_db]["mappings"]), 'table');
}

return $return;
}

Expand All @@ -340,7 +367,9 @@ function table_status($name = "", $fast = false) {
)
)
), "POST");

$return = array();

if ($search) {
$tables = $search["aggregations"]["count_by_type"]["buckets"];
foreach ($tables as $table) {
Expand All @@ -354,6 +383,7 @@ function table_status($name = "", $fast = false) {
}
}
}

return $return;
}

Expand All @@ -375,6 +405,7 @@ function indexes($table, $connection2 = null) {

function fields($table) {
$mappings = array();

if (min_version(7)) {
$result = connection()->query("_mapping");
if ($result) {
Expand Down Expand Up @@ -419,6 +450,7 @@ function fields($table) {
unset($return[$name]["privileges"]["update"]);
}
}

return $return;
}

Expand Down Expand Up @@ -477,9 +509,11 @@ function alter_table($table, $name, $fields, $foreign, $comment, $engine, $colla
'type' => $field_type
);
}

if (!empty($properties)) {
$properties = array('properties' => $properties);
}

return connection()->query("_mapping/{$name}", $properties, 'PUT');
}

Expand All @@ -492,6 +526,7 @@ function drop_tables($tables) {
foreach ($tables as $table) { //! convert to bulk api
$return = $return && connection()->query(urlencode($table), null, 'DELETE');
}

return $return;
}

Expand All @@ -502,6 +537,7 @@ function last_id() {
function driver_config() {
$types = array();
$structured_types = array();

foreach (array(
lang('Numbers') => array("long" => 3, "integer" => 5, "short" => 8, "byte" => 10, "double" => 20, "float" => 66, "half_float" => 12, "scaled_float" => 21),
lang('Date and time') => array("date" => 10),
Expand All @@ -511,6 +547,7 @@ function driver_config() {
$types += $val;
$structured_types[$key] = array_keys($val);
}

return array(
'possible_drivers' => array("json + allow_url_fopen"),
'jush' => "elastic",
Expand Down

0 comments on commit f86e3de

Please sign in to comment.