Files
luos/gulliver/system/class.database_mysql.php

945 lines
30 KiB
PHP
Raw Normal View History

<?php
/**
* class.database_mysql.php
*
* @package gulliver.system
*
* ProcessMaker Open Source Edition
* Copyright (C) 2004 - 2011 Colosa Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
*
*/
/**
*
* @package gulliver.system
*
*/
class database extends database_base
{
2017-12-04 13:25:35 +00:00
public $iFetchType = MYSQLI_ASSOC;
/**
2017-12-04 13:25:35 +00:00
* class database constructor.
*
2017-12-04 13:25:35 +00:00
* @param string $sType adapter type
* @param string $sServer server
* @param string $sUser db user
* @param string $sPass db user password
* @param string $sDataBase Database name
*/
2017-12-04 13:25:35 +00:00
public function __construct($sType = DB_ADAPTER, $sServer = DB_HOST, $sUser = DB_USER, $sPass = DB_PASS, $sDataBase = DB_NAME)
{
$this->sType = $sType;
$this->sServer = $sServer;
$this->sUser = $sUser;
$this->sPass = $sPass;
$this->sDataBase = $sDataBase;
2017-12-04 13:25:35 +00:00
$this->oConnection = mysqli_connect($sServer, $sUser, $sPass, $sDataBase) or die('Could not connect to database...');
$this->sQuoteCharacter = '`';
$this->nullString = 'null';
}
/**
* generate the sql sentence to create a table
*
* @param $sTable table name
* @param $aColumns array of columns
* @return $sSql the sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateCreateTableSQL($sTable, $aColumns)
{
$sKeys = '';
$sSQL = 'CREATE TABLE IF NOT EXISTS ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . '(';
foreach ($aColumns as $sColumnName => $aParameters) {
if ($sColumnName != 'INDEXES') {
2017-12-04 13:25:35 +00:00
if ($sColumnName != '' && isset($aParameters['Type']) && $aParameters['Type'] != '') {
$sSQL .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ' ' . $aParameters['Type'];
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Null']) && $aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
2017-12-04 13:25:35 +00:00
$sSQL .= ' NOT NULL';
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['AutoIncrement']) && $aParameters['AutoIncrement']) {
$sSQL .= ' AUTO_INCREMENT PRIMARY KEY';
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Key']) && $aParameters['Key'] == 'PRI') {
$sKeys .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ',';
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Default'])) {
2016-12-23 16:05:41 -04:00
$sSQL .= " DEFAULT '" . trim($aParameters['Default']) . "'";
}
$sSQL .= ',';
}
}
}
2017-12-04 13:25:35 +00:00
$sSQL = substr($sSQL, 0, -1);
if ($sKeys != '') {
2017-12-04 13:25:35 +00:00
$sSQL .= ',PRIMARY KEY(' . substr($sKeys, 0, -1) . ')';
}
$sSQL .= ')ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci' . $this->sEndLine;
return $sSQL;
}
/**
* generate a drop table sentence
*
* @param $sTable table name
* @return sql sentence string
*/
2017-12-04 13:25:35 +00:00
public function generateDropTableSQL($sTable)
{
return 'DROP TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
}
/**
* generate rename table sentence
*
* @param $sTableOld old table name
* @return $sSql sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateRenameTableSQL($sTableOld)
{
$sSQL = 'ALTER TABLE ' . $sTableOld . ' RENAME TO RBAC_' . $sTableOld;
return $sSQL;
}
/**
* generate drop column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @return $sSql sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateDropColumnSQL($sTable, $sColumn)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . $this->sEndLine;
return $sSQL;
}
/**
* This method has to refactor
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return string
*/
public function generateCheckAddColumnSQL($sTable, $sColumn, $aParameters)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP PRIMARY KEY ';
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* This method has to refactor
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return string
*/
public function deleteAllIndexesIntable($sTable, $sColumn, $aParameters)
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP INDEX indexLoginLog ';
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* 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.
* @param $sTable
* @param $sColumn
* @param $aParameters
* @return bool
*/
public function checkPatchHor1787($sTable, $sColumn, $aParameters)
{
if (isset($aParameters['AutoIncrement']) && $aParameters['AutoIncrement'] && $sTable == 'LOGIN_LOG') {
return true;
}
return false;
}
/**
* generate an add column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @param $aParameters parameters of field like typo or if it can be null
* @return $sSql sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateAddColumnSQL($sTable, $sColumn, $aParameters)
{
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Type']) && isset($aParameters['Null'])) {
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . ' ' . $aParameters['Type'];
if ($aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
$sSQL .= ' NOT NULL';
}
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['AutoIncrement']) && $aParameters['AutoIncrement']) {
2017-08-08 09:53:00 -04:00
$sSQL .= ' AUTO_INCREMENT';
2017-01-11 17:05:48 -04:00
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['PrimaryKey']) && $aParameters['PrimaryKey']) {
2017-08-08 09:53:00 -04:00
$sSQL .= ' PRIMARY KEY';
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Unique']) && $aParameters['Unique']) {
2017-08-08 09:53:00 -04:00
$sSQL .= ' UNIQUE';
}
//we need to check the property AI
2017-12-04 13:25:35 +00:00
if (isset($aParameters['AI'])) {
if ($aParameters['AI'] == 1) {
$sSQL .= ' AUTO_INCREMENT';
} else {
if ($aParameters['Default'] != '') {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
}
} else {
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Default'])) {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
}
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* generate a change column sentence
*
* @param $sTable table name
* @param $sColumn column name
* @param $aParameters parameters of field like typo or if it can be null
* @param $sColumnNewName column new name
* @return $sSql sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateChangeColumnSQL($sTable, $sColumn, $aParameters, $sColumnNewName = '')
{
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' CHANGE COLUMN ' . $this->sQuoteCharacter . ($sColumnNewName != '' ? $sColumnNewName : $sColumn) . $this->sQuoteCharacter . ' ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter;
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Type'])) {
$sSQL .= ' ' . $aParameters['Type'];
}
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Null'])) {
if ($aParameters['Null'] == 'YES') {
$sSQL .= ' NULL';
} else {
$sSQL .= ' NOT NULL';
}
}
//if (isset($aParameters['AI'])) {
// if ($aParameters['AI'] == 1) {
// $sSQL .= ' AUTO_INCREMENT';
// }
// else {
// if (isset($aParameters['Default'])) {
// if ($aParameters['Default'] != '') {
// $sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
// }
// }
// }
//}
//else {
2017-12-04 13:25:35 +00:00
if (isset($aParameters['Default'])) {
if (trim($aParameters['Default']) == '' && $aParameters['Type'] == 'datetime') {
//do nothing
} else {
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
}
//}
}
2017-12-04 13:25:35 +00:00
if (!isset($aParameters['Default']) && isset($aParameters['Null']) && $aParameters['Null'] == 'YES') {
$sSQL .= " DEFAULT NULL ";
}
//}
$sSQL .= $this->sEndLine;
return $sSQL;
}
/**
* Generate and get the primary key in a sentence
*
* @param $sTable table name
* @return $sSql sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateGetPrimaryKeysSQL($sTable)
{
try {
if ($sTable == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The table name cannot be empty!');
}
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' WHERE Seq_in_index = 1' . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to drop the primary key
*
* @param $sTable table name
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateDropPrimaryKeysSQL($sTable)
{
try {
if ($sTable == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The table name cannot be empty!');
}
return 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP PRIMARY KEY' . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to add multiple primary keys
*
* @param $sTable table name
* @param $aPrimaryKeys array of primary keys
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateAddPrimaryKeysSQL($sTable, $aPrimaryKeys)
{
try {
if ($sTable == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The table name cannot be empty!');
}
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD PRIMARY KEY (';
foreach ($aPrimaryKeys as $sKey) {
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ',';
}
2017-12-04 13:25:35 +00:00
$sSQL = substr($sSQL, 0, -1) . ')' . $this->sEndLine;
return $sSQL;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to drop an index
*
* @param $sTable table name
* @param $sIndexName index name
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateDropKeySQL($sTable, $sIndexName)
{
try {
if ($sTable == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The table name cannot be empty!');
}
if ($sIndexName == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The column name cannot be empty!');
}
return 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP INDEX ' . $this->sQuoteCharacter . $sIndexName . $this->sQuoteCharacter . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to add indexes or primary keys
*
* @param $sTable table name
* @param $indexName index name
* @param $aKeys array of keys
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateAddKeysSQL($sTable, $indexName, $aKeys)
{
try {
$indexType = 'INDEX';
if ($indexName == 'primaryKey' || $indexName == 'PRIMARY') {
$indexType = 'PRIMARY';
$indexName = 'KEY';
}
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD ' . $indexType . ' ' . $indexName . ' (';
foreach ($aKeys as $sKey) {
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ', ';
}
2017-12-04 13:25:35 +00:00
$sSQL = substr($sSQL, 0, -2);
$sSQL .= ')' . $this->sEndLine;
return $sSQL;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to show the tables
*
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateShowTablesSQL()
{
return 'SHOW TABLES' . $this->sEndLine;
}
/**
* generate a sentence to show the tables with a like sentence
*
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateShowTablesLikeSQL($sTable)
{
return "SHOW TABLES LIKE '" . $sTable . "'" . $this->sEndLine;
}
/**
* generate a sentence to show the tables with a like sentence
*
* @param $sTable table name
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateDescTableSQL($sTable)
{
try {
if ($sTable == '') {
2017-12-04 13:25:35 +00:00
throw new Exception('The table name cannot be empty!');
}
return 'DESC ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
} catch (Exception $oException) {
throw $oException;
}
}
/**
* generate a sentence to show some table indexes
*
* @param $sTable table name
* @return sql sentence
*/
2017-12-04 13:25:35 +00:00
public function generateTableIndexSQL($sTable)
{
return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . " " . $this->sEndLine;
//return 'SHOW INDEX FROM ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . " WHERE Key_name <> 'PRIMARY'" . $this->sEndLine;
}
/**
* execute a sentence to check if there is connection
*
* @return void
*/
2017-12-04 13:25:35 +00:00
public function isConnected()
{
2017-12-04 13:25:35 +00:00
$connect = false;
if ($this->oConnection !== false) {
$this->executeQuery('USE ' . $this->sDataBase);
$connect = true;
}
2017-12-04 13:25:35 +00:00
return $connect;
}
/**
* generate a sentence to show the tables with a like sentence
*
* @param $sQuery sql query string
* @return void
*/
2017-12-04 13:25:35 +00:00
public function logQuery($sQuery)
{
try {
$found = false;
2017-12-04 13:25:35 +00:00
if (substr($sQuery, 0, 6) == 'SELECT') {
$found = true;
}
2017-12-04 13:25:35 +00:00
if (substr($sQuery, 0, 4) == 'SHOW') {
$found = true;
}
2017-12-04 13:25:35 +00:00
if (substr($sQuery, 0, 4) == 'DESC') {
$found = true;
}
2017-12-04 13:25:35 +00:00
if (substr($sQuery, 0, 4) == 'USE ') {
$found = true;
}
2017-12-04 13:25:35 +00:00
if (!$found) {
$logDir = PATH_DATA . 'log';
2017-12-04 13:25:35 +00:00
if (!file_exists($logDir)) {
if (!mkdir($logDir)) {
return;
}
}
$logFile = "$logDir/query.log";
2017-12-04 13:25:35 +00:00
$fp = fopen($logFile, 'a+');
if ($fp !== false) {
2017-12-04 13:25:35 +00:00
fwrite($fp, date("Y-m-d H:i:s") . " " . $this->sDataBase . " " . $sQuery . "\n");
fclose($fp);
}
}
} catch (Exception $oException) {
}
}
/**
* execute a sql query
*
* @param $sQuery table name
* @return void
*/
2017-12-04 13:25:35 +00:00
public function executeQuery($sQuery)
{
2017-12-04 13:25:35 +00:00
$this->logQuery($sQuery);
try {
if ($this->oConnection) {
2017-12-04 13:25:35 +00:00
mysqli_select_db($this->oConnection, $this->sDataBase);
$result = mysqli_query($this->oConnection, $sQuery);
mysqli_use_result($this->oConnection);
return $result;
} else {
2017-12-04 13:25:35 +00:00
throw new Exception('invalid connection to database ' . $this->sDataBase);
}
} catch (Exception $oException) {
2017-12-04 13:25:35 +00:00
$this->logQuery($oException->getMessage());
throw $oException;
}
}
/**
* count the rows of a dataset
*
* @param $oDataset
* @return the number of rows
*/
2017-12-04 13:25:35 +00:00
public function countResults($oDataset)
{
2017-12-04 13:25:35 +00:00
return mysqli_num_rows($oDataset);
}
/**
* count an array of the registry from a dataset
*
2017-12-04 13:25:35 +00:00
* @param $dataSet
* @return the registry
*/
2017-12-04 13:25:35 +00:00
public function getRegistry($dataSet)
{
2017-12-04 13:25:35 +00:00
$response = null;
if ($dataSet !== false) {
$response = mysqli_fetch_array($dataSet, $this->iFetchType);
}
return $response;
}
/**
* close the current connection
*
* @return void
*/
2017-12-04 13:25:35 +00:00
public function close()
{
2017-12-04 13:25:35 +00:00
mysqli_close($this->oConnection);
}
2017-12-04 13:25:35 +00:00
public function generateInsertSQL($table, $data)
{
2017-12-04 13:25:35 +00:00
$fields = array();
$values = array();
foreach ($data as $field) {
$fields[] = $field['field'];
2017-12-04 13:25:35 +00:00
if (!is_null($field['value'])) {
switch ($field['type']) {
case 'text':
case 'date':
2017-12-04 13:25:35 +00:00
$values[] = "'" . mysqli_real_escape_string($this->oConnection, $field['value']) . "'";
break;
case 'int':
default:
2017-12-04 13:25:35 +00:00
$values[] = mysqli_real_escape_string($this->oConnection, $field['value']);
break;
}
} else {
$values[] = $this->nullString;
}
}
2017-12-04 13:25:35 +00:00
$fields = array_map(array($this, 'putQuotes'
), $fields);
$sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->putQuotes($table), implode(', ', $fields), implode(', ', $values));
return $sql;
}
2017-12-04 13:25:35 +00:00
public function generateUpdateSQL($table, $keys, $data)
{
2017-12-04 13:25:35 +00:00
$fields = array();
$where = array();
foreach ($data as $field) {
2017-12-04 13:25:35 +00:00
if (!is_null($field['value'])) {
switch ($field['type']) {
case 'text':
case 'date':
2017-12-04 13:25:35 +00:00
$fields[] = $this->putQuotes($field['field']) . " = '" . mysqli_real_escape_string($this->oConnection, $field['value']) . "'";
break;
case 'int':
default:
2017-12-04 13:25:35 +00:00
$fields[] = $this->putQuotes($field['field']) . " = " . mysqli_real_escape_string($this->oConnection, $field['value']);
break;
}
} else {
$values[] = $this->nullString;
}
2017-12-04 13:25:35 +00:00
if (in_array($field['field'], $keys)) {
$where[] = $fields[count($fields) - 1];
}
}
2017-12-04 13:25:35 +00:00
$sql = sprintf("UPDATE %s SET %s WHERE %s", $this->putQuotes($table), implode(', ', $fields), implode(', ', $where));
return $sql;
}
2017-12-04 13:25:35 +00:00
public function generateDeleteSQL($table, $keys, $data)
{
2017-12-04 13:25:35 +00:00
$fields = array();
$where = array();
foreach ($data as $field) {
2017-12-04 13:25:35 +00:00
if (in_array($field['field'], $keys)) {
if (!is_null($field['value'])) {
switch ($field['type']) {
case 'text':
case 'date':
2017-12-04 13:25:35 +00:00
$where[] = $this->putQuotes($field['field']) . " = '" . mysqli_real_escape_string($this->oConnection, $field['value']) . "'";
break;
case 'int':
default:
2017-12-04 13:25:35 +00:00
$where[] = $this->putQuotes($field['field']) . " = " . mysqli_real_escape_string($this->oConnection, $field['value']);
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));
return $sql;
}
2017-12-04 13:25:35 +00:00
public function generateSelectSQL($table, $keys, $data)
{
2017-12-04 13:25:35 +00:00
$fields = array();
$where = array();
foreach ($data as $field) {
2017-12-04 13:25:35 +00:00
if (in_array($field['field'], $keys)) {
if (!is_null($field['value'])) {
switch ($field['type']) {
case 'text':
case 'date':
2017-12-04 13:25:35 +00:00
$where[] = $this->putQuotes($field['field']) . " = '" . mysqli_real_escape_string($this->oConnection, $field['value']) . "'";
break;
case 'int':
default:
2017-12-04 13:25:35 +00:00
$where[] = $this->putQuotes($field['field']) . " = " . mysqli_real_escape_string($this->oConnection, $field['value']);
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));
return $sql;
}
2017-12-04 13:25:35 +00:00
private function putQuotes($element)
{
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
*
* @return string $sConcat
*/
2017-12-04 13:25:35 +00:00
public function concatString()
{
$nums = func_num_args();
$vars = func_get_args();
$sConcat = " CONCAT(";
2017-12-04 13:25:35 +00:00
for ($i = 0; $i < $nums; $i++) {
if (isset($vars[$i])) {
$sConcat .= $vars[$i];
if (($i + 1) < $nums) {
$sConcat .= ", ";
}
}
}
$sConcat .= ")";
return $sConcat;
}
/*
* 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)
{
$sCompare = "IF(" . $compareValue . ", " . $trueResult . ", " . $falseResult . ") ";
return $sCompare;
}
/**
* 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()
{
$sql = "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`)
2014-10-08 16:32:12 -04:00
)ENGINE=InnoDB DEFAULT CHARSET=latin1;";
return $sql;
}
/*
* 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()
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$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<>''
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)
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$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)
" . $var . "
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()
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$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
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)
{
$sqlConcat = " CONCAT(U.USR_LASTNAME,' ',USR_FIRSTNAME) AS USER ";
$sqlGroupBy = " USER ";
$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)
" . $var . "
GROUP BY " . $sqlGroupBy;
return $sql;
}
/*
* query functions for class class.net.php
*
*/
2017-12-04 13:25:35 +00:00
public function getServerVersion($driver, $dbIP, $dbPort, $dbUser, $dbPasswd, $dbSourcename)
{
2017-12-04 13:25:35 +00:00
if ($link = mysqli_connect($dbIP, $dbUser, $dbPasswd, $dbSourcename)) {
$v = mysqli_get_server_info($link);
} else {
2017-12-04 13:25:35 +00:00
throw new Exception(mysqli_error($link));
}
2017-12-04 13:25:35 +00:00
return (isset($v)) ? $v : 'none';
}
/*
* query functions for class class.net.php, class.reportTables.php
*
*/
2017-12-04 13:25:35 +00:00
public function getDropTable($sTableName)
{
$sql = 'DROP TABLE IF EXISTS `' . $sTableName . '`';
return $sql;
}
2017-12-04 13:25:35 +00:00
public function getTableDescription($sTableName)
{
$sql = "DESC " . $sTableName;
return $sql;
}
2017-12-04 13:25:35 +00:00
public function getFieldNull()
{
$fieldName = "Null";
return $fieldName;
}
2017-12-04 13:25:35 +00:00
public function getValidate($validate)
{
$oValidate = $validate;
return $oValidate;
}
/**
* Determines whether a table exists
* It is part of class.reportTables.php
*/
2017-12-04 13:25:35 +00:00
public function reportTableExist()
{
2015-03-25 11:25:00 -04:00
$filter = new InputFilter();
$DB_NAME = $filter->validateInput(DB_NAME);
$bExists = true;
2017-12-04 13:25:35 +00:00
$oConnection = mysqli_connect(DB_HOST, DB_USER, DB_PASS);
mysqli_select_db($oConnection, $DB_NAME);
$oDataset = mysqli_query($oConnection, 'SELECT COUNT(*) FROM REPORT_TABLE') || ($bExists = false);
return $bExists;
}
/**
* It is part of class.pagedTable.php
*/
2017-12-04 13:25:35 +00:00
public function getLimitRenderTable($nCurrentPage, $nRowsPerPage)
{
$sql = ' LIMIT ' . (($nCurrentPage - 1) * $nRowsPerPage) . ', ' . $nRowsPerPage;
return $sql;
}
/**
* Determining the existence of a table
2017-12-04 13:25:35 +00:00
*
* @param string $tableName
* @param string $database
*
* @return bool
*/
2017-12-04 13:25:35 +00:00
public function tableExists($tableName, $database)
{
2017-12-04 13:25:35 +00:00
mysqli_select_db($this->oConnection, $database);
$tables = array();
$tablesResult = mysqli_query($this->oConnection, "SHOW TABLES FROM $database;");
while ($row = mysqli_fetch_row($tablesResult)) {
$tables[] = $row[0];
}
2017-12-04 13:25:35 +00:00
if (in_array($tableName, $tables)) {
return true;
}
return false;
}
}