2012-10-22 17:23:01 -04:00
|
|
|
<?php
|
2018-02-01 13:06:32 +00:00
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
2012-10-22 17:23:01 -04:00
|
|
|
class database extends database_base
|
|
|
|
|
{
|
2017-12-04 13:25:35 +00:00
|
|
|
public $iFetchType = MYSQLI_ASSOC;
|
2012-10-22 17:23:01 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Name connection eloquent
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $nameConnection;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expression regex validate version mysql.
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $regexVersionMysql = '@[0-9]+\.[0-9]+\.[0-9]+@';
|
|
|
|
|
|
2012-10-22 17:23:01 -04:00
|
|
|
/**
|
2017-12-04 13:25:35 +00:00
|
|
|
* class database constructor.
|
2012-10-22 17:23:01 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $type adapter type
|
|
|
|
|
* @param string $server server
|
|
|
|
|
* @param string $user db user
|
|
|
|
|
* @param string $pass db user password
|
|
|
|
|
* @param string $database Database name
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function __construct($type = null, $server = null, $user = null, $pass = null, $database = null)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($type === null) {
|
|
|
|
|
$type = config('connections.driver');
|
|
|
|
|
}
|
|
|
|
|
if ($server === null) {
|
|
|
|
|
$server = config('connections.workflow.host');
|
|
|
|
|
}
|
|
|
|
|
if ($user === null) {
|
|
|
|
|
$user = config('connections.workflow.username');
|
|
|
|
|
}
|
|
|
|
|
if ($pass === null) {
|
|
|
|
|
$pass = config('connections.workflow.password');
|
|
|
|
|
}
|
|
|
|
|
if ($database === null) {
|
|
|
|
|
$database = config('connections.workflow.database');
|
|
|
|
|
}
|
|
|
|
|
$this->sType = $type;
|
|
|
|
|
$this->sServer = $server;
|
|
|
|
|
$this->sUser = $user;
|
|
|
|
|
$this->sPass = $pass;
|
|
|
|
|
$this->sDataBase = $database;
|
2012-10-22 17:23:01 -04:00
|
|
|
$this->sQuoteCharacter = '`';
|
|
|
|
|
$this->nullString = 'null';
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$this->setNameConnection('workflow');
|
|
|
|
|
if ($type !== config('connections.driver') ||
|
|
|
|
|
$server !== config('connections.workflow.host') ||
|
|
|
|
|
$user !== config('connections.workflow.username') ||
|
|
|
|
|
$pass !== config('connections.workflow.password') ||
|
|
|
|
|
$database !== config('connections.workflow.database')) {
|
|
|
|
|
$this->setNameConnection('DATABASE_' . $database);
|
|
|
|
|
InstallerModule::setNewConnection($this->getNameConnection(), $server, $user, $pass, $database, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->oConnection = true;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
$this->oConnection = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getNameConnection()
|
|
|
|
|
{
|
|
|
|
|
return $this->nameConnection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $nameConnection
|
|
|
|
|
*/
|
|
|
|
|
public function setNameConnection($nameConnection)
|
|
|
|
|
{
|
|
|
|
|
$this->nameConnection = $nameConnection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRegexVersionMysql()
|
|
|
|
|
{
|
|
|
|
|
return $this->regexVersionMysql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $regexVersionMysql
|
|
|
|
|
*/
|
|
|
|
|
public function setRegexVersionMysql($regexVersionMysql)
|
|
|
|
|
{
|
|
|
|
|
$this->regexVersionMysql = $regexVersionMysql;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate the sql sentence to create a table
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param array $columns array of columns
|
|
|
|
|
* @return string $sql the sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateCreateTableSQL($table, $columns)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$keys = '';
|
|
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . '(';
|
2012-10-22 17:23:01 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
foreach ($columns as $columnName => $parameters) {
|
|
|
|
|
if ($columnName !== 'INDEXES') {
|
|
|
|
|
if (!empty($columnName) && isset($parameters['Type']) && !empty($parameters['Type'])) {
|
|
|
|
|
$sql .= $this->sQuoteCharacter . $columnName . $this->sQuoteCharacter . ' ' . $parameters['Type'];
|
2012-10-22 17:23:01 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Null']) && $parameters['Null'] === 'YES') {
|
|
|
|
|
$sql .= ' NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= ' NOT NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['AutoIncrement']) && $parameters['AutoIncrement']) {
|
|
|
|
|
$sql .= ' AUTO_INCREMENT PRIMARY KEY';
|
2016-09-09 15:15:51 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Key']) && $parameters['Key'] == 'PRI') {
|
|
|
|
|
$keys .= $this->sQuoteCharacter . $columnName . $this->sQuoteCharacter . ',';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Default'])) {
|
|
|
|
|
$sql .= " DEFAULT '" . trim($parameters['Default']) . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= ',';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = substr($sql, 0, -1);
|
|
|
|
|
if ($keys != '') {
|
|
|
|
|
$sql .= ',PRIMARY KEY(' . substr($keys, 0, -1) . ')';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= ')ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
|
2012-10-22 17:23:01 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
return $sql;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a drop table sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @return string sql sentence string
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateDropTableSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'DROP TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-04 16:28:17 -04:00
|
|
|
/**
|
|
|
|
|
* generate rename table sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $sTableOld old table name
|
|
|
|
|
* @return string $sql sql sentence
|
2014-09-04 16:28:17 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateRenameTableSQL($sTableOld)
|
2014-09-04 16:28:17 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = 'ALTER TABLE ' . $sTableOld . ' RENAME TO RBAC_' . $sTableOld;
|
|
|
|
|
return $sql;
|
2014-09-04 16:28:17 -04:00
|
|
|
}
|
|
|
|
|
|
2012-10-22 17:23:01 -04:00
|
|
|
/**
|
|
|
|
|
* generate drop column sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $column column name
|
|
|
|
|
* @return string $sql sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateDropColumnSQL($table, $column)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' DROP COLUMN ' . $this->sQuoteCharacter . $column . $this->sQuoteCharacter;
|
|
|
|
|
return $sql;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2016-09-09 15:15:51 -04:00
|
|
|
/**
|
|
|
|
|
* This method has to refactor
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $column
|
|
|
|
|
* @param string $parameters
|
2016-09-09 15:15:51 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateCheckAddColumnSQL($table, $column, $parameters)
|
2016-09-09 15:15:51 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' DROP PRIMARY KEY ';
|
2016-09-09 15:15:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method has to refactor
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $column
|
|
|
|
|
* @param string $parameters
|
2016-09-09 15:15:51 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function deleteAllIndexesIntable($table, $column = null, $parameters = null)
|
2016-09-09 15:15:51 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' DROP INDEX indexLoginLog ';
|
2016-09-09 15:15:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method is used exclusively to verify if it was made changes in the DB to solve the HOR-1787 issue, later
|
|
|
|
|
* a generic method which covers all the possible similar problems found in the HOR-1787 issue will be generated.
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $column
|
|
|
|
|
* @param array $parameters
|
2016-09-09 15:15:51 -04:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function checkPatchHor1787($table, $column = null, $parameters = [])
|
2016-09-09 15:15:51 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['AutoIncrement']) && $parameters['AutoIncrement'] && $table == 'LOGIN_LOG') {
|
2016-09-09 15:15:51 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 17:23:01 -04:00
|
|
|
/**
|
|
|
|
|
* generate an add column sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $column column name
|
|
|
|
|
* @param array $parameters parameters of field like typo or if it can be null
|
|
|
|
|
* @return string $sql sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateAddColumnSQL($table, $column, $parameters)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = '';
|
|
|
|
|
if (isset($parameters['Type']) && isset($parameters['Null'])) {
|
|
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' ADD COLUMN ' . $this->sQuoteCharacter . $column . $this->sQuoteCharacter . ' ' . $parameters['Type'];
|
|
|
|
|
if ($parameters['Null'] == 'YES') {
|
|
|
|
|
$sql .= ' NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= ' NOT NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['AutoIncrement']) && $parameters['AutoIncrement']) {
|
|
|
|
|
$sql .= ' AUTO_INCREMENT';
|
2017-01-11 17:05:48 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['PrimaryKey']) && $parameters['PrimaryKey']) {
|
|
|
|
|
$sql .= ' PRIMARY KEY';
|
2017-08-08 09:53:00 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Unique']) && $parameters['Unique']) {
|
|
|
|
|
$sql .= ' UNIQUE';
|
2017-08-08 09:53:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//we need to check the property AI
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['AI'])) {
|
|
|
|
|
if ($parameters['AI'] == 1) {
|
|
|
|
|
$sql .= ' AUTO_INCREMENT';
|
2012-10-22 17:23:01 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($parameters['Default'] != '') {
|
|
|
|
|
$sql .= " DEFAULT '" . $parameters['Default'] . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Default'])) {
|
|
|
|
|
$sql .= " DEFAULT '" . $parameters['Default'] . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return $sql;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a change column sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $column column name
|
|
|
|
|
* @param array $parameters parameters of field like typo or if it can be null
|
|
|
|
|
* @param string $columnNewName column new name
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateChangeColumnSQL($table, $column, $parameters, $columnNewName = '')
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' CHANGE COLUMN ' . $this->sQuoteCharacter . ($columnNewName != '' ? $columnNewName : $column) . $this->sQuoteCharacter . ' ' . $this->sQuoteCharacter . $column . $this->sQuoteCharacter;
|
|
|
|
|
if (isset($parameters['Type'])) {
|
|
|
|
|
$sql .= ' ' . $parameters['Type'];
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (isset($parameters['Null'])) {
|
|
|
|
|
if ($parameters['Null'] === 'YES') {
|
|
|
|
|
$sql .= ' NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= ' NOT NULL';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
|
|
|
|
|
if (isset($parameters['Default'])) {
|
|
|
|
|
if (empty(trim($parameters['Default'])) && $parameters['Type'] === 'datetime') {
|
2012-10-17 16:16:00 +00:00
|
|
|
//do nothing
|
2012-10-22 17:23:01 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql .= " DEFAULT '" . $parameters['Default'] . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (!isset($parameters['Default']) && isset($parameters['Null']) && $parameters['Null'] === 'YES') {
|
|
|
|
|
$sql .= ' DEFAULT NULL ';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return $sql;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate and get the primary key in a sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @return string $sql sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateGetPrimaryKeysSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($table)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The table name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' WHERE Seq_in_index = 1';
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to drop the primary key
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @return string sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateDropPrimaryKeysSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($table)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The table name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' DROP PRIMARY KEY';
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to add multiple primary keys
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param array $primaryKeys array of primary keys
|
|
|
|
|
* @return string sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateAddPrimaryKeysSQL($table, $primaryKeys)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($table)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The table name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' ADD PRIMARY KEY (';
|
|
|
|
|
foreach ($primaryKeys as $key) {
|
|
|
|
|
$sql .= $this->sQuoteCharacter . $key . $this->sQuoteCharacter . ',';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = substr($sql, 0, -1) . ')';
|
|
|
|
|
return $sql;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to drop an index
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $indexName index name
|
|
|
|
|
* @return string sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateDropKeySQL($table, $indexName)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($table)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The table name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($indexName)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The column name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' DROP INDEX ' . $this->sQuoteCharacter . $indexName . $this->sQuoteCharacter;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to add indexes or primary keys
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @param string $indexName index name
|
|
|
|
|
* @param array $keys array of keys
|
|
|
|
|
* @return string sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateAddKeysSQL($table, $indexName, $keys)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$indexType = 'INDEX';
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($indexName === 'primaryKey' || $indexName === 'PRIMARY') {
|
2012-10-22 17:23:01 -04:00
|
|
|
$indexType = 'PRIMARY';
|
|
|
|
|
$indexName = 'KEY';
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' ADD ' . $indexType . ' ' . $indexName . ' (';
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
|
$sql .= $this->sQuoteCharacter . $key . $this->sQuoteCharacter . ', ';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$sql = substr($sql, 0, -2);
|
|
|
|
|
$sql .= ')';
|
|
|
|
|
return $sql;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to show the tables
|
|
|
|
|
*
|
|
|
|
|
* @return sql sentence
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateShowTablesSQL()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'SHOW TABLES';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to show the tables with a like sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return string sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateShowTablesLikeSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return "SHOW TABLES LIKE '" . $table . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to show the tables with a like sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @return string sql sentence
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateDescTableSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty($table)) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('The table name cannot be empty!');
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'DESC ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw $exception;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to show some table indexes
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table table name
|
|
|
|
|
* @return string sql sentence
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function generateTableIndexSQL($table)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $table . $this->sQuoteCharacter . ' ';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* execute a sentence to check if there is connection
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return boolean
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function isConnected()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $this->oConnection;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generate a sentence to show the tables with a like sentence
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $query sql query string
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function logQuery($query)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$found = false;
|
2018-02-01 13:06:32 +00:00
|
|
|
if (substr($query, 0, 6) === 'SELECT') {
|
2012-10-22 17:23:01 -04:00
|
|
|
$found = true;
|
2018-02-01 13:06:32 +00:00
|
|
|
} else {
|
|
|
|
|
$option = substr($query, 0, 4);
|
|
|
|
|
$options = ['SHOW', 'DESC', 'USE '];
|
|
|
|
|
if (in_array($option, $options, true)) {
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
if (!$found) {
|
2012-10-22 17:23:01 -04:00
|
|
|
$logDir = PATH_DATA . 'log';
|
2017-12-04 13:25:35 +00:00
|
|
|
if (!file_exists($logDir)) {
|
|
|
|
|
if (!mkdir($logDir)) {
|
2012-10-22 17:23:01 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$logFile = "$logDir/query.log";
|
2017-12-04 13:25:35 +00:00
|
|
|
$fp = fopen($logFile, 'a+');
|
2012-10-22 17:23:01 -04:00
|
|
|
if ($fp !== false) {
|
2018-02-01 13:06:32 +00:00
|
|
|
fwrite($fp, date('Y-m-d H:i:s') . ' ' . $this->sDataBase . ' ' . $query . "\n");
|
2017-12-04 13:25:35 +00:00
|
|
|
fclose($fp);
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
} catch (Exception $exception) {
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* execute a sql query
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $query
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function executeQuery($query)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->logQuery($query);
|
2012-10-22 17:23:01 -04:00
|
|
|
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
if (!$this->oConnection) {
|
2017-12-04 13:25:35 +00:00
|
|
|
throw new Exception('invalid connection to database ' . $this->sDataBase);
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$result = DB::connection($this->getNameConnection())
|
|
|
|
|
->select($query);
|
|
|
|
|
$result = array_map(function ($value) {
|
|
|
|
|
$data = (array)$value;
|
|
|
|
|
if ($this->iFetchType === 2) {
|
|
|
|
|
$data = $data[key($data)];
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}, $result);
|
|
|
|
|
return $result;
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
$this->logQuery($exception->getMessage());
|
|
|
|
|
return [];
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* close the current connection
|
2012-10-22 17:23:01 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return void
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function close()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($this->getNameConnection() !== 'workflow') {
|
|
|
|
|
DB::disconnect($this->getNameConnection());
|
2017-12-04 13:25:35 +00:00
|
|
|
}
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Generate sql insert
|
2012-10-22 17:23:01 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateInsertSQL($table, $data)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields = [];
|
|
|
|
|
$values = [];
|
2012-10-22 17:23:01 -04:00
|
|
|
foreach ($data as $field) {
|
|
|
|
|
$fields[] = $field['field'];
|
2017-12-04 13:25:35 +00:00
|
|
|
if (!is_null($field['value'])) {
|
2012-10-22 17:23:01 -04:00
|
|
|
switch ($field['type']) {
|
|
|
|
|
case 'text':
|
|
|
|
|
case 'date':
|
2018-02-01 13:06:32 +00:00
|
|
|
$values[] = "'" . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']) . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
case 'int':
|
|
|
|
|
default:
|
2018-02-01 13:06:32 +00:00
|
|
|
$values[] = DB::connection($this->getNameConnection())->getPdo()->quote($field['value']);
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$values[] = $this->nullString;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields = array_map([$this, 'putQuotes'], $fields);
|
2017-12-04 13:25:35 +00:00
|
|
|
$sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->putQuotes($table), implode(', ', $fields), implode(', ', $values));
|
2012-10-22 17:23:01 -04:00
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Generate update sql
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param array $keys
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateUpdateSQL($table, $keys, $data)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields = [];
|
|
|
|
|
$where = [];
|
2012-10-22 17:23:01 -04:00
|
|
|
foreach ($data as $field) {
|
2017-12-04 13:25:35 +00:00
|
|
|
if (!is_null($field['value'])) {
|
2012-10-22 17:23:01 -04:00
|
|
|
switch ($field['type']) {
|
|
|
|
|
case 'text':
|
|
|
|
|
case 'date':
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields[] = $this->putQuotes($field['field']) . " = '" . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']) . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
case 'int':
|
|
|
|
|
default:
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields[] = $this->putQuotes($field['field']) . " = " . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']);
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$values[] = $this->nullString;
|
|
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
if (in_array($field['field'], $keys)) {
|
|
|
|
|
$where[] = $fields[count($fields) - 1];
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
$sql = sprintf("UPDATE %s SET %s WHERE %s", $this->putQuotes($table), implode(', ', $fields), implode(', ', $where));
|
2012-10-22 17:23:01 -04:00
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Generate delete table
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param array $keys
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateDeleteSQL($table, $keys, $data)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields = [];
|
|
|
|
|
$where = [];
|
2012-10-22 17:23:01 -04:00
|
|
|
foreach ($data as $field) {
|
2017-12-04 13:25:35 +00:00
|
|
|
if (in_array($field['field'], $keys)) {
|
|
|
|
|
if (!is_null($field['value'])) {
|
2012-10-22 17:23:01 -04:00
|
|
|
switch ($field['type']) {
|
|
|
|
|
case 'text':
|
|
|
|
|
case 'date':
|
2018-02-01 13:06:32 +00:00
|
|
|
$where[] = $this->putQuotes($field['field']) . " = '" . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']) . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
case 'int':
|
|
|
|
|
default:
|
2018-02-01 13:06:32 +00:00
|
|
|
$where[] = $this->putQuotes($field['field']) . " = " . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']);
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$values[] = $this->nullString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
$sql = sprintf("DELETE FROM %s WHERE %s", $this->putQuotes($table), implode(', ', $where));
|
2012-10-22 17:23:01 -04:00
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Generate sql select
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param array $keys
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function generateSelectSQL($table, $keys, $data)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$fields = [];
|
|
|
|
|
$where = [];
|
2012-10-22 17:23:01 -04:00
|
|
|
foreach ($data as $field) {
|
2017-12-04 13:25:35 +00:00
|
|
|
if (in_array($field['field'], $keys)) {
|
|
|
|
|
if (!is_null($field['value'])) {
|
2012-10-22 17:23:01 -04:00
|
|
|
switch ($field['type']) {
|
|
|
|
|
case 'text':
|
|
|
|
|
case 'date':
|
2018-02-01 13:06:32 +00:00
|
|
|
$where[] = $this->putQuotes($field['field']) . " = '" . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']) . "'";
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
case 'int':
|
|
|
|
|
default:
|
2018-02-01 13:06:32 +00:00
|
|
|
$where[] = $this->putQuotes($field['field']) . " = " . DB::connection($this->getNameConnection())->getPdo()->quote($field['value']);
|
2012-10-22 17:23:01 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$values[] = $this->nullString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
$sql = sprintf("SELECT * FROM %s WHERE %s", $this->putQuotes($table), implode(', ', $where));
|
2012-10-22 17:23:01 -04:00
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 13:25:35 +00:00
|
|
|
private function putQuotes($element)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
return $this->sQuoteCharacter . $element . $this->sQuoteCharacter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*=================================================================================================*/
|
|
|
|
|
/**
|
|
|
|
|
* concatString
|
|
|
|
|
* Generates a string equivalent to the chosen database.
|
|
|
|
|
*
|
|
|
|
|
* author Hector Cortez <hector@gmail.com>
|
|
|
|
|
* date 2010-08-04
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return string $concat
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function concatString()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
$nums = func_num_args();
|
|
|
|
|
$vars = func_get_args();
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
$concat = ' CONCAT(';
|
2017-12-04 13:25:35 +00:00
|
|
|
for ($i = 0; $i < $nums; $i++) {
|
|
|
|
|
if (isset($vars[$i])) {
|
2018-02-01 13:06:32 +00:00
|
|
|
$concat .= $vars[$i];
|
2012-10-22 17:23:01 -04:00
|
|
|
if (($i + 1) < $nums) {
|
2018-02-01 13:06:32 +00:00
|
|
|
$concat .= ', ';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$concat .= ')';
|
2012-10-22 17:23:01 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
return $concat;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
/*
|
2012-10-22 17:23:01 -04:00
|
|
|
* query functions for class class.case.php
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* concatString
|
|
|
|
|
* Generates a string equivalent to the case when
|
|
|
|
|
*
|
|
|
|
|
* author Hector Cortez <hector@gmail.com>
|
|
|
|
|
* date 2010-08-04
|
|
|
|
|
*
|
|
|
|
|
* @return string $sCompare
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getCaseWhen($compareValue, $trueResult, $falseResult)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'IF(' . $compareValue . ', ' . $trueResult . ', ' . $falseResult . ') ';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a string equivalent to create table ObjectPermission
|
|
|
|
|
*
|
|
|
|
|
* class.case.php
|
|
|
|
|
* function verifyTable()
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function createTableObjectPermission()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return "CREATE TABLE IF NOT EXISTS `OBJECT_PERMISSION` (
|
2010-12-02 23:34:41 +00:00
|
|
|
`OP_UID` varchar(32) NOT NULL,
|
|
|
|
|
`PRO_UID` varchar(32) NOT NULL,
|
|
|
|
|
`TAS_UID` varchar(32) NOT NULL,
|
|
|
|
|
`USR_UID` varchar(32) NOT NULL,
|
|
|
|
|
`OP_USER_RELATION` int(1) NOT NULL default '1',
|
|
|
|
|
`OP_TASK_SOURCE` varchar(32) NOT NULL,
|
|
|
|
|
`OP_PARTICIPATE` int(1) NOT NULL default '1',
|
|
|
|
|
`OP_OBJ_TYPE` varchar(15) NOT NULL default 'ANY',
|
|
|
|
|
`OP_OBJ_UID` varchar(32) NOT NULL,
|
|
|
|
|
`OP_ACTION` varchar(10) NOT NULL default 'VIEW',
|
|
|
|
|
KEY `PRO_UID` (`PRO_UID`,`TAS_UID`,`USR_UID`,`OP_TASK_SOURCE`,`OP_OBJ_UID`)
|
2018-02-01 13:06:32 +00:00
|
|
|
)ENGINE=InnoDB DEFAULT CHARSET=latin1";
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
/*
|
2012-10-22 17:23:01 -04:00
|
|
|
* query functions for class class.report.php
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* Generates a string query
|
|
|
|
|
*
|
|
|
|
|
* class.report.php
|
|
|
|
|
* function generatedReport4()
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getSelectReport4()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
|
|
|
|
|
$sqlGroupBy = " USER ";
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
2010-12-02 23:34:41 +00:00
|
|
|
MIN(AD.DEL_DURATION) AS MIN,
|
|
|
|
|
MAX(AD.DEL_DURATION) AS MAX,
|
|
|
|
|
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
|
|
|
|
AVG(AD.DEL_DURATION) AS PROMEDIO
|
|
|
|
|
FROM APPLICATION AS A
|
|
|
|
|
LEFT JOIN APP_DELEGATION AS AD ON(A.APP_UID = AD.APP_UID AND AD.DEL_INDEX=1)
|
|
|
|
|
LEFT JOIN USERS AS U ON(U.USR_UID = A.APP_INIT_USER)
|
|
|
|
|
WHERE A.APP_UID<>''
|
2012-10-22 17:23:01 -04:00
|
|
|
GROUP BY " . $sqlGroupBy;
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a string query
|
|
|
|
|
*
|
|
|
|
|
* class.report.php
|
|
|
|
|
* function generatedReport4_filter()
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getSelectReport4Filter($var)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
|
|
|
|
|
$sqlGroupBy = " USER ";
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
2010-12-02 23:34:41 +00:00
|
|
|
MIN(AD.DEL_DURATION) AS MIN,
|
|
|
|
|
MAX(AD.DEL_DURATION) AS MAX,
|
|
|
|
|
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
|
|
|
|
AVG(AD.DEL_DURATION) AS PROMEDIO
|
|
|
|
|
FROM APPLICATION AS A
|
|
|
|
|
LEFT JOIN APP_DELEGATION AS AD ON(A.APP_UID = AD.APP_UID AND AD.DEL_INDEX=1)
|
|
|
|
|
LEFT JOIN USERS AS U ON(U.USR_UID = A.APP_INIT_USER)
|
2012-10-17 16:16:00 +00:00
|
|
|
" . $var . "
|
2012-10-22 17:23:01 -04:00
|
|
|
GROUP BY " . $sqlGroupBy;
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a string query
|
|
|
|
|
*
|
|
|
|
|
* class.report.php
|
|
|
|
|
* function generatedReport5()
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getSelectReport5()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
|
|
|
|
|
$sqlGroupBy = " USER ";
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
2010-12-02 23:34:41 +00:00
|
|
|
MIN(AD.DEL_DURATION) AS MIN,
|
|
|
|
|
MAX(AD.DEL_DURATION) AS MAX,
|
|
|
|
|
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
|
|
|
|
AVG(AD.DEL_DURATION) AS PROMEDIO
|
|
|
|
|
FROM APP_DELEGATION AS AD
|
|
|
|
|
LEFT JOIN PROCESS AS P ON (P.PRO_UID = AD.PRO_UID)
|
|
|
|
|
LEFT JOIN USERS AS U ON(U.USR_UID = AD.USR_UID)
|
|
|
|
|
WHERE AD.APP_UID<>'' AND AD.DEL_FINISH_DATE IS NULL
|
2012-10-22 17:23:01 -04:00
|
|
|
GROUP BY " . $sqlGroupBy;
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a string query
|
|
|
|
|
*
|
|
|
|
|
* class.report.php
|
|
|
|
|
* function generatedReport5_filter()
|
|
|
|
|
*
|
|
|
|
|
* @return string $sql
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getSelectReport5Filter($var)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
|
|
|
|
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
|
|
|
|
|
$sqlGroupBy = " USER ";
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
2010-12-02 23:34:41 +00:00
|
|
|
MIN(AD.DEL_DURATION) AS MIN,
|
|
|
|
|
MAX(AD.DEL_DURATION) AS MAX,
|
|
|
|
|
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
|
|
|
|
AVG(AD.DEL_DURATION) AS PROMEDIO
|
|
|
|
|
FROM APP_DELEGATION AS AD
|
|
|
|
|
LEFT JOIN PROCESS AS P ON (P.PRO_UID = AD.PRO_UID)
|
|
|
|
|
LEFT JOIN USERS AS U ON(U.USR_UID = AD.USR_UID)
|
2012-10-17 16:16:00 +00:00
|
|
|
" . $var . "
|
2012-10-22 17:23:01 -04:00
|
|
|
GROUP BY " . $sqlGroupBy;
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-17 16:16:00 +00:00
|
|
|
/*
|
2012-10-22 17:23:01 -04:00
|
|
|
* query functions for class class.net.php
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Version mysql
|
|
|
|
|
*
|
|
|
|
|
* @param string $driver
|
|
|
|
|
* @param string $host
|
|
|
|
|
* @param string $port
|
|
|
|
|
* @param string $user
|
|
|
|
|
* @param string $pass
|
|
|
|
|
* @param string $database
|
|
|
|
|
* @return string version mysql
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getServerVersion($driver, $host, $port, $user, $pass, $database)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$connection = 'TEST_VERSION';
|
|
|
|
|
InstallerModule::setNewConnection($connection, $host, $user, $pass, $database, $port);
|
|
|
|
|
|
|
|
|
|
$results = DB::connection($connection)
|
|
|
|
|
->select(DB::raw('select version()'));
|
|
|
|
|
|
|
|
|
|
preg_match($this->getRegexVersionMysql(), $results[0]->{'version()'}, $version);
|
|
|
|
|
|
|
|
|
|
DB::disconnect($connection);
|
|
|
|
|
|
|
|
|
|
return $version[0];
|
|
|
|
|
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
|
throw new Exception($exception->getMessage());
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 17:37:47 -04:00
|
|
|
/*
|
2012-10-22 17:23:01 -04:00
|
|
|
* query functions for class class.net.php, class.reportTables.php
|
|
|
|
|
*
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate drop table
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @return string sql
|
|
|
|
|
*/
|
|
|
|
|
public function getDropTable($tableName)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'DROP TABLE IF EXISTS `' . $tableName . '`';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Generate Description table
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @return string sql
|
|
|
|
|
*/
|
|
|
|
|
public function getTableDescription($tableName)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'DESC ' . $tableName;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getFieldNull()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return 'Null';
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* @param $validate
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getValidate($validate)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $validate;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines whether a table exists
|
|
|
|
|
* It is part of class.reportTables.php
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function reportTableExist()
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$result = DB::select("show tables like 'REPORT_TABLE'");
|
|
|
|
|
return count($result) > 0;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* It is part of class.pagedTable.php
|
|
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate limit sql
|
|
|
|
|
*
|
|
|
|
|
* @param int $currentPage
|
|
|
|
|
* @param int $rowsPerPage
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getLimitRenderTable($currentPage, $rowsPerPage)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return ' LIMIT ' . (($currentPage - 1) * $rowsPerPage) . ', ' . $rowsPerPage;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determining the existence of a table
|
2017-12-04 13:25:35 +00:00
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @param string $database
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2012-10-22 17:23:01 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function tableExists($tableName, $database)
|
2012-10-22 17:23:01 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$result = DB::connect($this->getNameConnection())
|
|
|
|
|
->select("show tables like '$tableName'");
|
|
|
|
|
$flag = count($result) > 0;
|
|
|
|
|
|
|
|
|
|
} catch (\Illuminate\Database\QueryException $exception) {
|
|
|
|
|
$flag = false;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
return $flag;
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|
2019-03-18 16:17:23 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate drop trigger SQL
|
|
|
|
|
*
|
|
|
|
|
* @param string $triggerName
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDropTrigger($triggerName)
|
|
|
|
|
{
|
|
|
|
|
return "DROP TRIGGER IF EXISTS `{$triggerName}`;";
|
|
|
|
|
}
|
2019-03-20 10:58:58 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate alter table with or without adding the indexes
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName
|
|
|
|
|
* @param array $columns
|
|
|
|
|
* @param array $indexes
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function generateAddColumnsSql($tableName, $columns, $indexes = [])
|
|
|
|
|
{
|
|
|
|
|
$indexesAlreadyAdded = [];
|
|
|
|
|
$sql = 'ALTER TABLE ' . $this->sQuoteCharacter . $tableName . $this->sQuoteCharacter . ' ';
|
|
|
|
|
foreach ($columns as $columnName => $parameters) {
|
|
|
|
|
if (isset($parameters['Type']) && isset($parameters['Null'])) {
|
|
|
|
|
$sql .= 'ADD COLUMN ' . $this->sQuoteCharacter . $columnName . $this->sQuoteCharacter . ' ' . $parameters['Type'];
|
|
|
|
|
if ($parameters['Null'] == 'YES') {
|
|
|
|
|
$sql .= ' NULL';
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ' NOT NULL';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isset($parameters['AutoIncrement']) && $parameters['AutoIncrement']) {
|
|
|
|
|
$sql .= ' AUTO_INCREMENT';
|
|
|
|
|
}
|
|
|
|
|
if (isset($parameters['PrimaryKey']) && $parameters['PrimaryKey']) {
|
|
|
|
|
$sql .= ' PRIMARY KEY';
|
|
|
|
|
$indexesAlreadyAdded[] = $columnName;
|
|
|
|
|
}
|
|
|
|
|
if (isset($parameters['Unique']) && $parameters['Unique']) {
|
|
|
|
|
$sql .= ' UNIQUE';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We need to check the property AI
|
|
|
|
|
if (isset($parameters['AI'])) {
|
|
|
|
|
if ($parameters['AI'] == 1) {
|
|
|
|
|
$sql .= ' AUTO_INCREMENT';
|
|
|
|
|
} else {
|
|
|
|
|
if ($parameters['Default'] != '') {
|
|
|
|
|
$sql .= " DEFAULT '" . $parameters['Default'] . "'";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (isset($parameters['Default'])) {
|
|
|
|
|
$sql .= " DEFAULT '" . $parameters['Default'] . "'";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sql .= ', ';
|
|
|
|
|
}
|
|
|
|
|
foreach ($indexes as $indexName => $indexColumns) {
|
|
|
|
|
$indexType = 'INDEX';
|
|
|
|
|
if ($indexName === 'primaryKey' || $indexName === 'PRIMARY') {
|
|
|
|
|
$indexType = 'PRIMARY';
|
|
|
|
|
$indexName = 'KEY';
|
|
|
|
|
// If is primary key is not needed add a new index, the column already was added like primary key
|
|
|
|
|
if (count($indexColumns) == 1 && $indexesAlreadyAdded == $indexColumns) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sql .= 'ADD ' . $indexType . ' ' . $indexName . ' (';
|
|
|
|
|
foreach ($indexColumns as $column) {
|
|
|
|
|
$sql .= $this->sQuoteCharacter . $column . $this->sQuoteCharacter . ', ';
|
|
|
|
|
}
|
|
|
|
|
$sql = substr($sql, 0, -2);
|
|
|
|
|
$sql .= '), ';
|
|
|
|
|
}
|
|
|
|
|
$sql = rtrim($sql, ', ');
|
|
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
|
}
|
2012-10-22 17:23:01 -04:00
|
|
|
}
|