2011-05-12 14:15:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* dbFacile - A Database API that should have existed from the start
|
|
|
|
* Version 0.4.3
|
|
|
|
*
|
|
|
|
* This code is covered by the MIT license http://en.wikipedia.org/wiki/MIT_License
|
|
|
|
*
|
|
|
|
* By Alan Szlosek from http://www.greaterscope.net/projects/dbFacile
|
|
|
|
*
|
|
|
|
* The non-OO version of dbFacile. It's a bit simplistic, but gives you the
|
|
|
|
* really useful bits in non-class form.
|
|
|
|
*
|
|
|
|
* Usage
|
|
|
|
* 1. Connect to MySQL as you normally would ... this code uses an existing connection
|
|
|
|
* 2. Use dbFacile as you normally would, without the object context
|
|
|
|
* 3. Oh, and dbFetchAll() is now dbFetchRows()
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
2016-11-11 01:40:53 +00:00
|
|
|
*/
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
use LibreNMS\DB\Eloquent;
|
2019-03-12 23:59:03 -05:00
|
|
|
use LibreNMS\Util\Laravel;
|
2017-04-06 16:02:37 -05:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Performs a query using the given string.
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @param array $parameters
|
2018-08-17 15:29:20 -05:00
|
|
|
* @return bool if query was successful or not
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#building-queries
|
|
|
|
* @see https://laravel.com/docs/eloquent#building-queries
|
2018-08-17 15:29:20 -05:00
|
|
|
*/
|
|
|
|
function dbQuery($sql, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
|
|
|
if (empty($parameters)) {
|
|
|
|
// don't use prepared statements for queries without parameters
|
|
|
|
return Eloquent::DB()->getPdo()->exec($sql) !== false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:49:34 -05:00
|
|
|
return Eloquent::DB()->statement($sql, (array) $parameters);
|
2018-08-17 15:29:20 -05:00
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2020-09-21 15:40:17 +02:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
2018-08-17 15:29:20 -05:00
|
|
|
}
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param string $table
|
2018-08-17 15:29:20 -05:00
|
|
|
* @return null|int
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#inserting-and-updating-models
|
|
|
|
* @see https://laravel.com/docs/eloquent#inserting-and-updating-models
|
2018-08-17 15:29:20 -05:00
|
|
|
*/
|
2016-11-11 01:40:53 +00:00
|
|
|
function dbInsert($data, $table)
|
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
$sql = 'INSERT IGNORE INTO `' . $table . '` (`' . implode('`,`', array_keys($data)) . '`) VALUES (' . implode(',', dbPlaceHolders($data)) . ')';
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
2018-08-17 20:49:34 -05:00
|
|
|
$result = Eloquent::DB()->insert($sql, (array) $data);
|
2018-08-17 15:29:20 -05:00
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $data, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
}
|
2016-11-11 01:40:53 +00:00
|
|
|
|
|
|
|
if ($result) {
|
2018-08-17 15:29:20 -05:00
|
|
|
return Eloquent::DB()->getPdo()->lastInsertId();
|
2016-11-11 01:40:53 +00:00
|
|
|
} else {
|
2018-08-17 15:29:20 -05:00
|
|
|
return null;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
}//end dbInsert()
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Passed an array and a table name, it attempts to insert the data into the table.
|
|
|
|
* $data is an array (rows) of key value pairs. keys are fields. Rows need to have same fields.
|
|
|
|
* Check for boolean false to determine whether insert failed
|
2018-08-17 15:29:20 -05:00
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param string $table
|
2018-08-17 15:29:20 -05:00
|
|
|
* @return bool
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#inserting-and-updating-models
|
|
|
|
* @see https://laravel.com/docs/eloquent#inserting-and-updating-models
|
2018-08-17 15:29:20 -05:00
|
|
|
*/
|
2016-11-11 01:40:53 +00:00
|
|
|
function dbBulkInsert($data, $table)
|
|
|
|
{
|
2017-09-03 13:58:39 -05:00
|
|
|
// check that data isn't an empty array
|
|
|
|
if (empty($data)) {
|
2016-11-11 01:40:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-06 19:42:25 -07:00
|
|
|
|
2017-09-03 13:58:39 -05:00
|
|
|
// make sure we have fields to insert
|
|
|
|
$fields = array_keys(reset($data));
|
|
|
|
if (empty($fields)) {
|
2016-11-11 01:40:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:42:25 -07:00
|
|
|
// Break into managable chunks to prevent situations where insert
|
|
|
|
// fails due to prepared statement having too many placeholders.
|
|
|
|
$data_chunks = array_chunk($data, 10000, true);
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2019-05-06 19:42:25 -07:00
|
|
|
foreach ($data_chunks as $data_chunk) {
|
|
|
|
try {
|
|
|
|
$result = Eloquent::DB()->table($table)->insert((array) $data_chunk);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
} catch (PDOException $pdoe) {
|
|
|
|
// FIXME query?
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', "Bulk insert $table", $data_chunk, $pdoe));
|
2019-05-06 19:42:25 -07:00
|
|
|
}
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbBulkInsert()
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Passed an array, table name, WHERE clause, and placeholder parameters, it attempts to update a record.
|
|
|
|
* Returns the number of affected rows
|
2018-08-17 15:29:20 -05:00
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param string $table
|
|
|
|
* @param string $where
|
|
|
|
* @param array $parameters
|
2018-08-17 15:29:20 -05:00
|
|
|
* @return bool|int
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#inserting-and-updating-models
|
|
|
|
* @see https://laravel.com/docs/eloquent#inserting-and-updating-models
|
2018-08-17 15:29:20 -05:00
|
|
|
*/
|
|
|
|
function dbUpdate($data, $table, $where = null, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
|
|
|
// need field name and placeholder value
|
|
|
|
// but how merge these field placeholders with actual $parameters array for the WHERE clause
|
|
|
|
$sql = 'UPDATE `' . $table . '` set ';
|
|
|
|
foreach ($data as $key => $value) {
|
2018-08-17 15:29:20 -05:00
|
|
|
$sql .= '`' . $key . '`=';
|
|
|
|
if (is_array($value)) {
|
|
|
|
$sql .= reset($value);
|
|
|
|
unset($data[$key]);
|
|
|
|
} else {
|
|
|
|
$sql .= '?';
|
|
|
|
}
|
|
|
|
$sql .= ',';
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
// strip keys
|
|
|
|
$data = array_values($data);
|
|
|
|
|
2016-11-11 01:40:53 +00:00
|
|
|
$sql = substr($sql, 0, -1);
|
|
|
|
// strip off last comma
|
|
|
|
if ($where) {
|
|
|
|
$sql .= ' WHERE ' . $where;
|
|
|
|
$data = array_merge($data, $parameters);
|
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
2018-08-17 20:49:34 -05:00
|
|
|
$result = Eloquent::DB()->update($sql, (array) $data);
|
2018-08-17 15:29:20 -05:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $data, $pdoe));
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbUpdate()
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#deleting-models
|
|
|
|
* @see https://laravel.com/docs/eloquent#deleting-models
|
|
|
|
*/
|
2016-11-11 01:40:53 +00:00
|
|
|
function dbDelete($table, $where = null, $parameters = [])
|
|
|
|
{
|
|
|
|
$sql = 'DELETE FROM `' . $table . '`';
|
|
|
|
if ($where) {
|
|
|
|
$sql .= ' WHERE ' . $where;
|
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
2018-08-17 20:49:34 -05:00
|
|
|
$result = Eloquent::DB()->delete($sql, (array) $parameters);
|
2018-08-17 15:29:20 -05:00
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
}
|
2016-11-23 00:57:19 -06:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return $result;
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbDelete()
|
|
|
|
|
2017-12-01 01:53:26 -06:00
|
|
|
/**
|
|
|
|
* Delete orphaned entries from a table that no longer have a parent in parent_table
|
2017-12-06 16:44:23 -06:00
|
|
|
* Format of parents array is as follows table.table_key_column<.target_key_column>
|
2017-12-01 01:53:26 -06:00
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param string $target_table The table to delete entries from
|
|
|
|
* @param array $parents an array of parent tables to check.
|
2017-12-01 01:53:26 -06:00
|
|
|
* @return bool|int
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent#deleting-models
|
|
|
|
* @see https://laravel.com/docs/eloquent#deleting-models
|
2017-12-01 01:53:26 -06:00
|
|
|
*/
|
2017-12-06 16:44:23 -06:00
|
|
|
function dbDeleteOrphans($target_table, $parents)
|
2017-12-01 01:53:26 -06:00
|
|
|
{
|
2017-12-06 16:44:23 -06:00
|
|
|
if (empty($parents)) {
|
|
|
|
// don't delete all entries if parents is missing
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-28 17:25:30 -05:00
|
|
|
$target_table = $target_table;
|
2017-12-06 16:44:23 -06:00
|
|
|
$sql = "DELETE T FROM `$target_table` T";
|
|
|
|
$where = [];
|
|
|
|
|
|
|
|
foreach ((array) $parents as $parent) {
|
2021-03-28 17:25:30 -05:00
|
|
|
$parent_parts = explode('.', $parent);
|
2017-12-06 16:44:23 -06:00
|
|
|
if (count($parent_parts) == 2) {
|
|
|
|
[$parent_table, $parent_column] = $parent_parts;
|
|
|
|
$target_column = $parent_column;
|
|
|
|
} elseif (count($parent_parts) == 3) {
|
|
|
|
[$parent_table, $parent_column, $target_column] = $parent_parts;
|
|
|
|
} else {
|
|
|
|
// invalid input
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= " LEFT JOIN `$parent_table` ON `$parent_table`.`$parent_column` = T.`$target_column`";
|
|
|
|
$where[] = " `$parent_table`.`$parent_column` IS NULL";
|
|
|
|
}
|
2017-12-01 01:53:26 -06:00
|
|
|
|
2017-12-06 16:44:23 -06:00
|
|
|
$query = "$sql WHERE" . implode(' AND', $where);
|
2017-12-01 01:53:26 -06:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
|
|
|
$result = Eloquent::DB()->delete($query);
|
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $query, [], $pdoe));
|
2017-12-01 01:53:26 -06:00
|
|
|
}
|
2018-08-17 15:29:20 -05:00
|
|
|
|
|
|
|
return $result;
|
2017-12-01 01:53:26 -06:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Fetches all of the rows (associatively) from the last performed query.
|
|
|
|
* Most other retrieval functions build off this
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbFetchRows($sql, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
global $PDO_FETCH_ASSOC;
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
|
|
|
$PDO_FETCH_ASSOC = true;
|
2018-08-17 20:49:34 -05:00
|
|
|
$rows = Eloquent::DB()->select($sql, (array) $parameters);
|
2018-07-13 17:08:00 -05:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return $rows;
|
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
} finally {
|
|
|
|
$PDO_FETCH_ASSOC = false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:08:00 -05:00
|
|
|
return [];
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbFetchRows()
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Like fetch(), accepts any number of arguments
|
|
|
|
* The first argument is an sprintf-ready query stringTypes
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbFetchRow($sql = null, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
global $PDO_FETCH_ASSOC;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$PDO_FETCH_ASSOC = true;
|
2018-08-17 20:49:34 -05:00
|
|
|
$row = Eloquent::DB()->selectOne($sql, (array) $parameters);
|
2016-11-11 01:40:53 +00:00
|
|
|
|
|
|
|
return $row;
|
2018-08-17 15:29:20 -05:00
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
} finally {
|
|
|
|
$PDO_FETCH_ASSOC = false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
2018-08-17 15:29:20 -05:00
|
|
|
|
|
|
|
return [];
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbFetchRow()
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* Fetches the first call from the first row returned by the query
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbFetchCell($sql, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
global $PDO_FETCH_ASSOC;
|
2016-11-23 00:57:19 -06:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
try {
|
|
|
|
$PDO_FETCH_ASSOC = true;
|
2018-08-17 20:49:34 -05:00
|
|
|
$row = Eloquent::DB()->selectOne($sql, (array) $parameters);
|
2018-08-17 15:29:20 -05:00
|
|
|
if ($row) {
|
|
|
|
return reset($row);
|
|
|
|
// shift first field off first row
|
|
|
|
}
|
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
} finally {
|
|
|
|
$PDO_FETCH_ASSOC = false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
2018-08-17 15:29:20 -05:00
|
|
|
|
2016-11-11 01:40:53 +00:00
|
|
|
return null;
|
|
|
|
}//end dbFetchCell()
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
2016-11-11 01:40:53 +00:00
|
|
|
* This method is quite different from fetchCell(), actually
|
|
|
|
* It fetches one cell from each row and places all the values in 1 array
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbFetchColumn($sql, $parameters = [])
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
2018-08-17 15:29:20 -05:00
|
|
|
global $PDO_FETCH_ASSOC;
|
|
|
|
|
|
|
|
$cells = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$PDO_FETCH_ASSOC = true;
|
2018-08-17 20:49:34 -05:00
|
|
|
foreach (Eloquent::DB()->select($sql, (array) $parameters) as $row) {
|
2018-08-17 15:29:20 -05:00
|
|
|
$cells[] = reset($row);
|
|
|
|
}
|
|
|
|
$PDO_FETCH_ASSOC = false;
|
|
|
|
|
|
|
|
return $cells;
|
|
|
|
} catch (PDOException $pdoe) {
|
2023-05-24 22:21:54 +02:00
|
|
|
dbHandleException(new QueryException('dbFacile', $sql, $parameters, $pdoe));
|
2018-08-17 15:29:20 -05:00
|
|
|
} finally {
|
|
|
|
$PDO_FETCH_ASSOC = false;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
return [];
|
2016-11-11 01:40:53 +00:00
|
|
|
}//end dbFetchColumn()
|
|
|
|
|
2021-11-30 09:33:14 +01:00
|
|
|
/**
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbHandleException(QueryException $exception)
|
|
|
|
{
|
|
|
|
$message = $exception->getMessage();
|
|
|
|
|
|
|
|
if ($exception->getCode() == 2002) {
|
|
|
|
$message = 'Could not connect to database! ' . $message;
|
2017-01-07 17:32:38 +00:00
|
|
|
}
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
// ? bindings should already be replaced, just replace named bindings
|
|
|
|
foreach ($exception->getBindings() as $key => $value) {
|
|
|
|
if (is_string($key)) {
|
|
|
|
$message = str_replace(":$key", $value, $message);
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:59:03 -05:00
|
|
|
$message .= $exception->getTraceAsString();
|
2016-11-11 01:40:53 +00:00
|
|
|
|
2019-03-12 23:59:03 -05:00
|
|
|
if (Laravel::isBooted()) {
|
2018-08-17 15:29:20 -05:00
|
|
|
Log::error($message);
|
|
|
|
} else {
|
|
|
|
c_echo('%rSQL Error!%n ');
|
|
|
|
echo $message . PHP_EOL;
|
2016-11-11 01:40:53 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 15:29:20 -05:00
|
|
|
// TODO remove this
|
|
|
|
// exit;
|
|
|
|
}
|
2016-11-11 01:40:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a data array, this returns an array of placeholders
|
|
|
|
* These may be question marks, or ":email" type
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param array $values
|
2016-11-11 01:40:53 +00:00
|
|
|
* @return array
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
2016-11-11 01:40:53 +00:00
|
|
|
*/
|
2018-08-17 15:29:20 -05:00
|
|
|
function dbPlaceHolders(&$values)
|
2016-11-11 01:40:53 +00:00
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
foreach ($values as $key => $value) {
|
2018-08-17 15:29:20 -05:00
|
|
|
if (is_array($value)) {
|
|
|
|
// array wrapped values are raw sql
|
|
|
|
$data[] = reset($value);
|
|
|
|
unset($values[$key]);
|
|
|
|
} elseif (is_numeric($key)) {
|
2016-11-11 01:40:53 +00:00
|
|
|
$data[] = '?';
|
|
|
|
} else {
|
|
|
|
$data[] = ':' . $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}//end dbPlaceHolders()
|
|
|
|
|
feature: Wireless Sensors Overhaul (#6471)
* feature: Wireless Sensors
Includes client counts for ios and unifi
Graphing could use some improvement.
Alerting and threshold ui not implemented
WIP: starting OO based wireless sensors.
Class based functionality working
remove old functional files
add schema file
discovery needs to be enabled, not polling
fix up schema
fix Unifi discovery not returning an array
Add some debug when discovering a sensor.
Fix style.
Add missing semicolin
Add a null object (Generic) for OS.
Fill out some phpdocs
Re-organized code
Each sensor type now has it's own discovery and polling interface
Custom polling tested with Unifi CCQ
Left to do:
Implement UI (Graphs and Custom thresholds)
Alerting
Testing
Fix event message text
Remove runDiscovery and runPolling from OS, they are unused and don't belong there.
Cleanups/docs
Missed this file.
Remove the requirement to fetch the current value to check validity.
Do that automatically if current is not specified
A few cleanups here and there
First pass at graphing.
device_ and wireless_ graphs added.
Add RouterOS support
Singleton OS instance isn't required right now.
Remove that to allow some memory to be freed.
Add wireless to the device list metrics.
Make all metrics clickable
Tweak graphs a bit
Implement limit configuration page.
Use sensors page as common code instead of duplicating.
Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value.
Add some wireless alert rules to the library.
Add documentation.
Add unifi client counts by ssid in addition to radio.
Optimize Sensor polling a bit.
Add HP MSM clients support (for full controller)
Fix function accessibility
Formalize the discovery and poller interfaces.
Add Xirrus clients and noise floor
move module interfaces to a more appropriate place.
push caching code up to os, unsure about this do to the limitations
No point in selectively enabling wireless discovery. We only discover if the device supports something.
Add RSSI, Power, and Rate.
Add these sensors for Ubnt Airos.
Clean up some copyrights.
Reduce the amount of files need to add new types.
Leave graph files for consistency and to allow customization.
Remove the old wifi clients graph completely.
ciscowlc should have improved counts (total and per-ssid)
Schema didn't get added.
Impelement the rest of the AirOS sensors
Reformat and re-organize the Airos.php class.
Add several UBNT AirFiber sensors
A few fixes add links to the section headers
Add HP MSM mibs.
* Schema file got dropped in rebase.
* Add wireless menu to view sensors across all devices.
Icons in the menu need help :/
* Add HeliOS, Mimosa, and Siklu support
Sensors added SNR + Noise
* Add power and utilization to Unifi
* Update polling to prefetch all sensor data in a few snmp requests as possible
* Add Extendair: tx+rx power, aggregate rate, frequency
* Add a check for duplicate sensors in discovery. Just print an error for now.
* Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed)
Fix an incorrect link in the wireless sensors table
* Add error rate and change all bps and Hz to use si units
* Fixes to limits and frequency display
* Fix overview graph frequency display
A few decimal place tweaks
* Don't allow switching sensor and wireless-sensor graphs, it doesn't work.
Change individual distance graphs to use si units
* Go through the OS and make sure I got all the sensors I can (probably missed some still)
Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS.
Message to help developers adding supports that don't return an array from discover functions.
* Fix some issues
* Remove noise and signal for now at least
A couple more fixes
Add a notification
* Oopsie
* Bonus AirFiber sensors
2017-05-01 23:49:11 -05:00
|
|
|
/**
|
|
|
|
* Generate a string of placeholders to pass to fill in a list
|
|
|
|
* result will look like this: (?, ?, ?, ?)
|
|
|
|
*
|
2023-06-13 13:35:00 +02:00
|
|
|
* @param $count
|
feature: Wireless Sensors Overhaul (#6471)
* feature: Wireless Sensors
Includes client counts for ios and unifi
Graphing could use some improvement.
Alerting and threshold ui not implemented
WIP: starting OO based wireless sensors.
Class based functionality working
remove old functional files
add schema file
discovery needs to be enabled, not polling
fix up schema
fix Unifi discovery not returning an array
Add some debug when discovering a sensor.
Fix style.
Add missing semicolin
Add a null object (Generic) for OS.
Fill out some phpdocs
Re-organized code
Each sensor type now has it's own discovery and polling interface
Custom polling tested with Unifi CCQ
Left to do:
Implement UI (Graphs and Custom thresholds)
Alerting
Testing
Fix event message text
Remove runDiscovery and runPolling from OS, they are unused and don't belong there.
Cleanups/docs
Missed this file.
Remove the requirement to fetch the current value to check validity.
Do that automatically if current is not specified
A few cleanups here and there
First pass at graphing.
device_ and wireless_ graphs added.
Add RouterOS support
Singleton OS instance isn't required right now.
Remove that to allow some memory to be freed.
Add wireless to the device list metrics.
Make all metrics clickable
Tweak graphs a bit
Implement limit configuration page.
Use sensors page as common code instead of duplicating.
Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value.
Add some wireless alert rules to the library.
Add documentation.
Add unifi client counts by ssid in addition to radio.
Optimize Sensor polling a bit.
Add HP MSM clients support (for full controller)
Fix function accessibility
Formalize the discovery and poller interfaces.
Add Xirrus clients and noise floor
move module interfaces to a more appropriate place.
push caching code up to os, unsure about this do to the limitations
No point in selectively enabling wireless discovery. We only discover if the device supports something.
Add RSSI, Power, and Rate.
Add these sensors for Ubnt Airos.
Clean up some copyrights.
Reduce the amount of files need to add new types.
Leave graph files for consistency and to allow customization.
Remove the old wifi clients graph completely.
ciscowlc should have improved counts (total and per-ssid)
Schema didn't get added.
Impelement the rest of the AirOS sensors
Reformat and re-organize the Airos.php class.
Add several UBNT AirFiber sensors
A few fixes add links to the section headers
Add HP MSM mibs.
* Schema file got dropped in rebase.
* Add wireless menu to view sensors across all devices.
Icons in the menu need help :/
* Add HeliOS, Mimosa, and Siklu support
Sensors added SNR + Noise
* Add power and utilization to Unifi
* Update polling to prefetch all sensor data in a few snmp requests as possible
* Add Extendair: tx+rx power, aggregate rate, frequency
* Add a check for duplicate sensors in discovery. Just print an error for now.
* Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed)
Fix an incorrect link in the wireless sensors table
* Add error rate and change all bps and Hz to use si units
* Fixes to limits and frequency display
* Fix overview graph frequency display
A few decimal place tweaks
* Don't allow switching sensor and wireless-sensor graphs, it doesn't work.
Change individual distance graphs to use si units
* Go through the OS and make sure I got all the sensors I can (probably missed some still)
Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS.
Message to help developers adding supports that don't return an array from discover functions.
* Fix some issues
* Remove noise and signal for now at least
A couple more fixes
Add a notification
* Oopsie
* Bonus AirFiber sensors
2017-05-01 23:49:11 -05:00
|
|
|
* @return string placholder list
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
feature: Wireless Sensors Overhaul (#6471)
* feature: Wireless Sensors
Includes client counts for ios and unifi
Graphing could use some improvement.
Alerting and threshold ui not implemented
WIP: starting OO based wireless sensors.
Class based functionality working
remove old functional files
add schema file
discovery needs to be enabled, not polling
fix up schema
fix Unifi discovery not returning an array
Add some debug when discovering a sensor.
Fix style.
Add missing semicolin
Add a null object (Generic) for OS.
Fill out some phpdocs
Re-organized code
Each sensor type now has it's own discovery and polling interface
Custom polling tested with Unifi CCQ
Left to do:
Implement UI (Graphs and Custom thresholds)
Alerting
Testing
Fix event message text
Remove runDiscovery and runPolling from OS, they are unused and don't belong there.
Cleanups/docs
Missed this file.
Remove the requirement to fetch the current value to check validity.
Do that automatically if current is not specified
A few cleanups here and there
First pass at graphing.
device_ and wireless_ graphs added.
Add RouterOS support
Singleton OS instance isn't required right now.
Remove that to allow some memory to be freed.
Add wireless to the device list metrics.
Make all metrics clickable
Tweak graphs a bit
Implement limit configuration page.
Use sensors page as common code instead of duplicating.
Clean up some javascript interactions: Allow enter on values to save. Cancel if update is not needed. Enable the clear custom button after setting a custom value.
Add some wireless alert rules to the library.
Add documentation.
Add unifi client counts by ssid in addition to radio.
Optimize Sensor polling a bit.
Add HP MSM clients support (for full controller)
Fix function accessibility
Formalize the discovery and poller interfaces.
Add Xirrus clients and noise floor
move module interfaces to a more appropriate place.
push caching code up to os, unsure about this do to the limitations
No point in selectively enabling wireless discovery. We only discover if the device supports something.
Add RSSI, Power, and Rate.
Add these sensors for Ubnt Airos.
Clean up some copyrights.
Reduce the amount of files need to add new types.
Leave graph files for consistency and to allow customization.
Remove the old wifi clients graph completely.
ciscowlc should have improved counts (total and per-ssid)
Schema didn't get added.
Impelement the rest of the AirOS sensors
Reformat and re-organize the Airos.php class.
Add several UBNT AirFiber sensors
A few fixes add links to the section headers
Add HP MSM mibs.
* Schema file got dropped in rebase.
* Add wireless menu to view sensors across all devices.
Icons in the menu need help :/
* Add HeliOS, Mimosa, and Siklu support
Sensors added SNR + Noise
* Add power and utilization to Unifi
* Update polling to prefetch all sensor data in a few snmp requests as possible
* Add Extendair: tx+rx power, aggregate rate, frequency
* Add a check for duplicate sensors in discovery. Just print an error for now.
* Add Bit Error Ratio (named error-ratio to allow for bit error rate to be added if needed)
Fix an incorrect link in the wireless sensors table
* Add error rate and change all bps and Hz to use si units
* Fixes to limits and frequency display
* Fix overview graph frequency display
A few decimal place tweaks
* Don't allow switching sensor and wireless-sensor graphs, it doesn't work.
Change individual distance graphs to use si units
* Go through the OS and make sure I got all the sensors I can (probably missed some still)
Because pollWirelessChannelAsFrequency() is generic and a little complex, so pull it up to OS.
Message to help developers adding supports that don't return an array from discover functions.
* Fix some issues
* Remove noise and signal for now at least
A couple more fixes
Add a notification
* Oopsie
* Bonus AirFiber sensors
2017-05-01 23:49:11 -05:00
|
|
|
*/
|
|
|
|
function dbGenPlaceholders($count)
|
|
|
|
{
|
|
|
|
return '(' . implode(',', array_fill(0, $count, '?')) . ')';
|
|
|
|
}
|
2018-02-10 14:38:25 -06:00
|
|
|
|
2018-03-14 20:25:19 +00:00
|
|
|
/**
|
|
|
|
* Synchronize a relationship to a list of related ids
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param string $table
|
|
|
|
* @param string $target_column column name for the target
|
|
|
|
* @param int $target column target id
|
|
|
|
* @param string $list_column related column names
|
|
|
|
* @param array $list list of related ids
|
2018-03-14 20:25:19 +00:00
|
|
|
* @return array [$inserted, $deleted]
|
2021-11-30 09:33:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Please use Eloquent instead; https://laravel.com/docs/eloquent
|
|
|
|
* @see https://laravel.com/docs/eloquent
|
2018-03-14 20:25:19 +00:00
|
|
|
*/
|
|
|
|
function dbSyncRelationship($table, $target_column = null, $target = null, $list_column = null, $list = null)
|
|
|
|
{
|
|
|
|
$inserted = 0;
|
|
|
|
|
|
|
|
$delete_query = "`$target_column`=? AND `$list_column`";
|
|
|
|
$delete_params = [$target];
|
|
|
|
if (! empty($list)) {
|
|
|
|
$delete_query .= ' NOT IN ' . dbGenPlaceholders(count($list));
|
|
|
|
$delete_params = array_merge($delete_params, $list);
|
|
|
|
}
|
|
|
|
$deleted = (int) dbDelete($table, $delete_query, $delete_params);
|
|
|
|
|
|
|
|
$db_list = dbFetchColumn("SELECT `$list_column` FROM `$table` WHERE `$target_column`=?", [$target]);
|
|
|
|
foreach ($list as $item) {
|
|
|
|
if (! in_array($item, $db_list)) {
|
|
|
|
dbInsert([$target_column => $target, $list_column => $item], $table);
|
|
|
|
$inserted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$inserted, $deleted];
|
|
|
|
}
|