ODE STYLE Formating gulliver/system/class.database_mssql.php
Change format files in gulliver/system/class.database_mssql.php
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* class.database_mssql.php
|
||||
*
|
||||
* @package gulliver.system
|
||||
*
|
||||
* ProcessMaker Open Source Edition
|
||||
@@ -25,50 +26,52 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package gulliver.system
|
||||
*/
|
||||
*
|
||||
*/
|
||||
|
||||
G::LoadSystem('database_base');
|
||||
|
||||
class database extends database_base {
|
||||
G::LoadSystem( 'database_base' );
|
||||
|
||||
class database extends database_base
|
||||
{
|
||||
public $iFetchType = MSSQL_ASSOC;
|
||||
|
||||
public function __construct($sType = DB_ADAPTER, $sServer = DB_HOST, $sUser = DB_USER, $sPass = DB_PASS, $sDataBase = DB_NAME) {
|
||||
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;
|
||||
$this->oConnection = @mssql_connect($sServer, $sUser, $sPass) || null;
|
||||
$this->oConnection = @mssql_connect( $sServer, $sUser, $sPass ) || null;
|
||||
$this->sQuoteCharacter = ' ';
|
||||
$this->nullString = 'NULL';
|
||||
$this->sQuoteCharacterBegin = '[';
|
||||
$this->sQuoteCharacterEnd = ']';
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function generateCreateTableSQL($sTable, $aColumns) {
|
||||
public function generateCreateTableSQL ($sTable, $aColumns)
|
||||
{
|
||||
$sKeys = '';
|
||||
$sSQL = 'CREATE TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . '(';
|
||||
|
||||
foreach ($aColumns as $sColumnName => $aParameters) {
|
||||
if ($sColumnName != 'INDEXES') {
|
||||
|
||||
if ( $sColumnName != '' && isset($aParameters['Type']) && $aParameters['Type'] != '' ){
|
||||
if ($sColumnName != '' && isset( $aParameters['Type'] ) && $aParameters['Type'] != '') {
|
||||
$sSQL .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ' ' . $aParameters['Type'];
|
||||
|
||||
if ( isset($aParameters['Null']) && $aParameters['Null'] == 'YES') {
|
||||
if (isset( $aParameters['Null'] ) && $aParameters['Null'] == 'YES') {
|
||||
$sSQL .= ' NULL';
|
||||
} else {
|
||||
$sSQL .= ' NOT NULL';
|
||||
}
|
||||
if ( isset($aParameters['Key']) && $aParameters['Key'] == 'PRI') {
|
||||
if (isset( $aParameters['Key'] ) && $aParameters['Key'] == 'PRI') {
|
||||
$sKeys .= $this->sQuoteCharacter . $sColumnName . $this->sQuoteCharacter . ',';
|
||||
}
|
||||
|
||||
if ( isset($aParameters['Default']) && $aParameters['Default'] != '' ) {
|
||||
if (isset( $aParameters['Default'] ) && $aParameters['Default'] != '') {
|
||||
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
|
||||
}
|
||||
|
||||
@@ -76,54 +79,57 @@ class database extends database_base {
|
||||
}
|
||||
}
|
||||
}
|
||||
$sSQL = substr($sSQL, 0, -1);
|
||||
$sSQL = substr( $sSQL, 0, - 1 );
|
||||
if ($sKeys != '') {
|
||||
$sSQL .= ',PRIMARY KEY(' . substr($sKeys, 0, -1) . ')';
|
||||
$sSQL .= ',PRIMARY KEY(' . substr( $sKeys, 0, - 1 ) . ')';
|
||||
}
|
||||
$sSQL .= ')' . $this->sEndLine;
|
||||
|
||||
return $sSQL;
|
||||
}
|
||||
|
||||
public function generateDropTableSQL($sTable) {
|
||||
public function generateDropTableSQL ($sTable)
|
||||
{
|
||||
return 'DROP TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
|
||||
}
|
||||
|
||||
public function generateDropColumnSQL($sTable, $sColumn) {
|
||||
public function generateDropColumnSQL ($sTable, $sColumn)
|
||||
{
|
||||
// SQL Server first should remove the restriction before the Elimination of the field
|
||||
$oConstraint = $this->dropFieldConstraint($sTable, $sColumn);
|
||||
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter .
|
||||
' DROP COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . $this->sEndLine;
|
||||
$oConstraint = $this->dropFieldConstraint( $sTable, $sColumn );
|
||||
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' DROP COLUMN ' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . $this->sEndLine;
|
||||
return $sSQL;
|
||||
}
|
||||
|
||||
public function generateAddColumnSQL($sTable, $sColumn, $aParameters) {
|
||||
if ( isset($aParameters['Type']) && isset($aParameters['Null']) ) {
|
||||
public function generateAddColumnSQL ($sTable, $sColumn, $aParameters)
|
||||
{
|
||||
if (isset( $aParameters['Type'] ) && isset( $aParameters['Null'] )) {
|
||||
$sDefault = "";
|
||||
$sType = $aParameters['Type'];
|
||||
$sDataType = $aParameters['Type'];
|
||||
if(! in_array($sDataType, array("TEXT", "DATE") )) {
|
||||
$sType = substr($sType, 0, strpos($sType,'('));
|
||||
if (! in_array( $sDataType, array ("TEXT","DATE"
|
||||
) )) {
|
||||
$sType = substr( $sType, 0, strpos( $sType, '(' ) );
|
||||
}
|
||||
switch($sType) {
|
||||
case 'VARCHAR' :
|
||||
case 'TEXT' : $sDefault = " DEFAULT '' ";
|
||||
switch ($sType) {
|
||||
case 'VARCHAR':
|
||||
case 'TEXT':
|
||||
$sDefault = " DEFAULT '' ";
|
||||
break;
|
||||
case 'DATE' : $sDataType = " CHAR(19) ";
|
||||
case 'DATE':
|
||||
$sDataType = " CHAR(19) ";
|
||||
$sDefault = " DEFAULT '0000-00-00' "; // The date data type to use char (19)
|
||||
break;
|
||||
case 'INT' :
|
||||
case 'FLOAT' : $sDataType = $sType;
|
||||
case 'INT':
|
||||
case 'FLOAT':
|
||||
$sDataType = $sType;
|
||||
$sDefault = " DEFAULT 0 ";
|
||||
break;
|
||||
}
|
||||
$sSQL = "ALTER TABLE " . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter .
|
||||
" ADD " . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter .
|
||||
" " . $aParameters['Type'];
|
||||
$sSQL = "ALTER TABLE " . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . " ADD " . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . " " . $aParameters['Type'];
|
||||
if ($aParameters['Null'] == 'YES') {
|
||||
$sSQL .= " NULL";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$sSQL .= " NOT NULL " . $sDefault;
|
||||
|
||||
}
|
||||
@@ -132,18 +138,16 @@ class database extends database_base {
|
||||
$sKeys .= 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter .
|
||||
' ADD PRIMARY KEY (' . $this->sQuoteCharacter . $sColumn . $this->sQuoteCharacter . ')' . $this->sEndLine;
|
||||
}*/
|
||||
if (isset($aParameters['AI'])) {
|
||||
if (isset( $aParameters['AI'] )) {
|
||||
if ($aParameters['AI'] == 1) {
|
||||
$sSQL .= ' AUTO_INCREMENT';
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if ($aParameters['Default'] != '') {
|
||||
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (isset($aParameters['Default']) && $aParameters['Default'] != '') {
|
||||
} else {
|
||||
if (isset( $aParameters['Default'] ) && $aParameters['Default'] != '') {
|
||||
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
|
||||
}
|
||||
}
|
||||
@@ -151,18 +155,16 @@ class database extends database_base {
|
||||
return $sSQL;
|
||||
}
|
||||
|
||||
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;
|
||||
if (isset($aParameters['Type'])) {
|
||||
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;
|
||||
if (isset( $aParameters['Type'] )) {
|
||||
$sSQL .= ' ' . $aParameters['Type'];
|
||||
}
|
||||
if (isset($aParameters['Null'])) {
|
||||
if (isset( $aParameters['Null'] )) {
|
||||
if ($aParameters['Null'] == 'YES') {
|
||||
$sSQL .= ' NULL';
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$sSQL .= ' NOT NULL';
|
||||
}
|
||||
}
|
||||
@@ -179,15 +181,14 @@ class database extends database_base {
|
||||
// }
|
||||
//}
|
||||
//else {
|
||||
if (isset($aParameters['Default'])) {
|
||||
if ( trim($aParameters['Default'] == '') && $aParameters['Type'] == 'datetime' ) {
|
||||
if (isset( $aParameters['Default'] )) {
|
||||
if (trim( $aParameters['Default'] == '' ) && $aParameters['Type'] == 'datetime') {
|
||||
//do nothing
|
||||
}
|
||||
else
|
||||
} else
|
||||
$sSQL .= " DEFAULT '" . $aParameters['Default'] . "'";
|
||||
//}
|
||||
}
|
||||
if (!isset($aParameters['Default']) && isset($aParameters['Null']) && $aParameters['Null'] == 'YES') {
|
||||
if (! isset( $aParameters['Default'] ) && isset( $aParameters['Null'] ) && $aParameters['Null'] == 'YES') {
|
||||
$sSQL .= " DEFAULT NULL ";
|
||||
}
|
||||
//}
|
||||
@@ -195,36 +196,31 @@ class database extends database_base {
|
||||
return $sSQL;
|
||||
}
|
||||
|
||||
public function generateGetPrimaryKeysSQL($sTable) {
|
||||
public function generateGetPrimaryKeysSQL ($sTable)
|
||||
{
|
||||
try {
|
||||
if ($sTable == '') {
|
||||
throw new Exception('The table name cannot be empty!');
|
||||
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) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get primary key
|
||||
* @parameter string $sTable
|
||||
*
|
||||
* @param eter string $sTable
|
||||
* @return string $sPrimaryKey
|
||||
*/
|
||||
public function getPrimaryKey($sTable)
|
||||
public function getPrimaryKey ($sTable)
|
||||
{
|
||||
try {
|
||||
$sSQL = " SELECT c.COLUMN_NAME " .
|
||||
" FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk , " .
|
||||
" INFORMATION_SCHEMA.KEY_COLUMN_USAGE c " .
|
||||
" WHERE pk.TABLE_NAME = '" . trim($sTable) . "' " .
|
||||
" AND CONSTRAINT_TYPE = 'PRIMARY KEY' " .
|
||||
" AND c.TABLE_NAME = pk.TABLE_NAME " .
|
||||
" AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME ";
|
||||
$oPrimaryKey = $this->executeQuery($sSQL);
|
||||
$aPrimaryKey = mssql_fetch_array($oPrimaryKey);
|
||||
mssql_free_result($oPrimaryKey);
|
||||
$sSQL = " SELECT c.COLUMN_NAME " . " FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk , " . " INFORMATION_SCHEMA.KEY_COLUMN_USAGE c " . " WHERE pk.TABLE_NAME = '" . trim( $sTable ) . "' " . " AND CONSTRAINT_TYPE = 'PRIMARY KEY' " . " AND c.TABLE_NAME = pk.TABLE_NAME " . " AND c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME ";
|
||||
$oPrimaryKey = $this->executeQuery( $sSQL );
|
||||
$aPrimaryKey = mssql_fetch_array( $oPrimaryKey );
|
||||
mssql_free_result( $oPrimaryKey );
|
||||
return $aPrimaryKey[0];
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
@@ -233,100 +229,95 @@ class database extends database_base {
|
||||
|
||||
/**
|
||||
* Get Field Constraint
|
||||
* @parameter string $sTable
|
||||
* @parameter string $sField
|
||||
*
|
||||
* @param eter string $sTable
|
||||
* @param eter string $sField
|
||||
* @return string $sFieldConstraint
|
||||
*/
|
||||
public function getFieldConstraint($sTable, $sField)
|
||||
public function getFieldConstraint ($sTable, $sField)
|
||||
{
|
||||
try {
|
||||
$sSQL = " select a.name " .
|
||||
" from sysobjects a " .
|
||||
" inner join syscolumns b on a.id = b.cdefault " .
|
||||
" where a.xtype = 'D' " .
|
||||
" and a.parent_obj = (select id from sysobjects where xtype = 'U' and name = '" . trim($sTable) . "') " .
|
||||
" and b.name = '" . trim($sField) . "' ";
|
||||
$sSQL = " select a.name " . " from sysobjects a " . " inner join syscolumns b on a.id = b.cdefault " . " where a.xtype = 'D' " . " and a.parent_obj = (select id from sysobjects where xtype = 'U' and name = '" . trim( $sTable ) . "') " . " and b.name = '" . trim( $sField ) . "' ";
|
||||
|
||||
$oFieldConstraint = $this->executeQuery($sSQL);
|
||||
$aFieldConstraint = mssql_fetch_array($oFieldConstraint);
|
||||
mssql_free_result($oFieldConstraint);
|
||||
$oFieldConstraint = $this->executeQuery( $sSQL );
|
||||
$aFieldConstraint = mssql_fetch_array( $oFieldConstraint );
|
||||
mssql_free_result( $oFieldConstraint );
|
||||
return $aFieldConstraint[0];
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* drop Field Constraint
|
||||
* @parameter string $sTable
|
||||
* @parameter string $sField
|
||||
*
|
||||
* @param eter string $sTable
|
||||
* @param eter string $sField
|
||||
* @return object $oFieldConstraint
|
||||
*/
|
||||
public function dropFieldConstraint($sTable, $sField)
|
||||
public function dropFieldConstraint ($sTable, $sField)
|
||||
{
|
||||
try {
|
||||
$sConstraint = $this->getFieldConstraint($sTable, $sField);
|
||||
$sSQL = "ALTER TABLE " . $sTable . " DROP CONSTRAINT " . $sConstraint . $this->sEndLine ;
|
||||
$oFieldConstraint = $this->executeQuery($sSQL);
|
||||
$sConstraint = $this->getFieldConstraint( $sTable, $sField );
|
||||
$sSQL = "ALTER TABLE " . $sTable . " DROP CONSTRAINT " . $sConstraint . $this->sEndLine;
|
||||
$oFieldConstraint = $this->executeQuery( $sSQL );
|
||||
return $oFieldConstraint;
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function generateDropPrimaryKeysSQL($sTable) {
|
||||
public function generateDropPrimaryKeysSQL ($sTable)
|
||||
{
|
||||
try {
|
||||
if ($sTable == '') {
|
||||
throw new Exception('The table name cannot be empty!');
|
||||
throw new Exception( 'The table name cannot be empty!' );
|
||||
}
|
||||
$sPrimayKey = $this->getPrimaryKey($sTable);
|
||||
$sPrimayKey = $this->getPrimaryKey( $sTable );
|
||||
|
||||
return ' ALTER TABLE ' . $sTable . ' DROP CONSTRAINT ' . $sPrimayKey . $this->sEndLine;
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function generateAddPrimaryKeysSQL($sTable, $aPrimaryKeys) {
|
||||
public function generateAddPrimaryKeysSQL ($sTable, $aPrimaryKeys)
|
||||
{
|
||||
try {
|
||||
if ($sTable == '') {
|
||||
throw new Exception('The table name cannot be empty!');
|
||||
throw new Exception( 'The table name cannot be empty!' );
|
||||
}
|
||||
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter .
|
||||
' ADD PRIMARY KEY (';
|
||||
$sSQL = 'ALTER TABLE ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . ' ADD PRIMARY KEY (';
|
||||
foreach ($aPrimaryKeys as $sKey) {
|
||||
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ',';
|
||||
}
|
||||
$sSQL = substr($sSQL, 0, -1) . ')' . $this->sEndLine;
|
||||
$sSQL = substr( $sSQL, 0, - 1 ) . ')' . $this->sEndLine;
|
||||
return $sSQL;
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function generateDropKeySQL($sTable, $sIndexName) {
|
||||
public function generateDropKeySQL ($sTable, $sIndexName)
|
||||
{
|
||||
try {
|
||||
if ($sTable == '') {
|
||||
throw new Exception('The table name cannot be empty!');
|
||||
throw new Exception( 'The table name cannot be empty!' );
|
||||
}
|
||||
if ($sIndexName == '') {
|
||||
throw new Exception('The column name cannot be empty!');
|
||||
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) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function generateAddKeysSQL($sTable, $indexName, $aKeys) {
|
||||
public function generateAddKeysSQL ($sTable, $indexName, $aKeys)
|
||||
{
|
||||
try {
|
||||
$indexType = 'INDEX';
|
||||
if ( $indexName == 'primaryKey' || $indexName == 'PRIMARY' ) {
|
||||
if ($indexName == 'primaryKey' || $indexName == 'PRIMARY') {
|
||||
$indexType = 'PRIMARY';
|
||||
$indexName = 'KEY';
|
||||
}
|
||||
@@ -334,201 +325,212 @@ class database extends database_base {
|
||||
foreach ($aKeys as $sKey) {
|
||||
$sSQL .= $this->sQuoteCharacter . $sKey . $this->sQuoteCharacter . ', ';
|
||||
}
|
||||
$sSQL = substr($sSQL, 0, -2);
|
||||
$sSQL = substr( $sSQL, 0, - 2 );
|
||||
$sSQL .= ')' . $this->sEndLine;
|
||||
return $sSQL;
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function generateShowTablesSQL() {
|
||||
public function generateShowTablesSQL ()
|
||||
{
|
||||
return 'SHOW TABLES' . $this->sEndLine;
|
||||
}
|
||||
|
||||
public function generateShowTablesLikeSQL($sTable) {
|
||||
public function generateShowTablesLikeSQL ($sTable)
|
||||
{
|
||||
return "SHOW TABLES LIKE '" . $sTable . "'" . $this->sEndLine;
|
||||
}
|
||||
|
||||
public function generateDescTableSQL($sTable) {
|
||||
public function generateDescTableSQL ($sTable)
|
||||
{
|
||||
try {
|
||||
if ($sTable == '') {
|
||||
throw new Exception('The table name cannot be empty!');
|
||||
throw new Exception( 'The table name cannot be empty!' );
|
||||
}
|
||||
return 'DESC ' . $this->sQuoteCharacter . $sTable . $this->sQuoteCharacter . $this->sEndLine;
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function generateTableIndexSQL($sTable) {
|
||||
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;
|
||||
}
|
||||
|
||||
public function isConnected() {
|
||||
if ( !$this->oConnection )
|
||||
public function isConnected ()
|
||||
{
|
||||
if (! $this->oConnection)
|
||||
return false;
|
||||
return $this->executeQuery( 'USE ' . $this->sDataBase );
|
||||
}
|
||||
|
||||
public function logQuery($sQuery ) {
|
||||
public function logQuery ($sQuery)
|
||||
{
|
||||
try {
|
||||
$found = false;
|
||||
if ( substr($sQuery,0, 6) == 'SELECT' ) $found = true;
|
||||
if ( substr($sQuery,0, 4) == 'SHOW' ) $found = true;
|
||||
if ( substr($sQuery,0, 4) == 'DESC' ) $found = true;
|
||||
if ( substr($sQuery,0, 4) == 'USE ' ) $found = true;
|
||||
if ( ! $found ) {
|
||||
if (substr( $sQuery, 0, 6 ) == 'SELECT')
|
||||
$found = true;
|
||||
if (substr( $sQuery, 0, 4 ) == 'SHOW')
|
||||
$found = true;
|
||||
if (substr( $sQuery, 0, 4 ) == 'DESC')
|
||||
$found = true;
|
||||
if (substr( $sQuery, 0, 4 ) == 'USE ')
|
||||
$found = true;
|
||||
if (! $found) {
|
||||
$logFile = PATH_DATA . 'log' . PATH_SEP . 'query.log';
|
||||
$fp = fopen ( $logFile, 'a+' );
|
||||
fwrite ( $fp, date("Y-m-d H:i:s") . " " . $this->sDataBase . " " . $sQuery . "\n" );
|
||||
fclose ( $fp );
|
||||
$fp = fopen( $logFile, 'a+' );
|
||||
fwrite( $fp, date( "Y-m-d H:i:s" ) . " " . $this->sDataBase . " " . $sQuery . "\n" );
|
||||
fclose( $fp );
|
||||
}
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
}
|
||||
}
|
||||
|
||||
public function executeQuery($sQuery) {
|
||||
$this->logQuery( $sQuery);
|
||||
public function executeQuery ($sQuery)
|
||||
{
|
||||
$this->logQuery( $sQuery );
|
||||
|
||||
try {
|
||||
if ($this->oConnection) {
|
||||
@mssql_select_db($this->sDataBase);
|
||||
@mssql_select_db( $this->sDataBase );
|
||||
|
||||
return @mssql_query($sQuery);
|
||||
return @mssql_query( $sQuery );
|
||||
} else {
|
||||
throw new Exception( 'invalid connection to database ' . $this->sDataBase );
|
||||
}
|
||||
else {
|
||||
throw new Exception('invalid connection to database ' . $this->sDataBase );
|
||||
}
|
||||
}
|
||||
catch (Exception $oException) {
|
||||
} catch (Exception $oException) {
|
||||
$this->logQuery( $oException->getMessage() );
|
||||
throw $oException;
|
||||
}
|
||||
}
|
||||
|
||||
public function countResults($oDataset) {
|
||||
return @mssql_num_rows($oDataset);
|
||||
public function countResults ($oDataset)
|
||||
{
|
||||
return @mssql_num_rows( $oDataset );
|
||||
}
|
||||
|
||||
public function getRegistry($oDataset) {
|
||||
return @mssql_fetch_array($oDataset, $this->iFetchType);
|
||||
public function getRegistry ($oDataset)
|
||||
{
|
||||
return @mssql_fetch_array( $oDataset, $this->iFetchType );
|
||||
}
|
||||
|
||||
public function close() {
|
||||
@mssql_close($this->oConnection);
|
||||
public function close ()
|
||||
{
|
||||
@mssql_close( $this->oConnection );
|
||||
}
|
||||
|
||||
public function generateInsertSQL($table, $data) {
|
||||
$fields = array();
|
||||
$values = array();
|
||||
public function generateInsertSQL ($table, $data)
|
||||
{
|
||||
$fields = array ();
|
||||
$values = array ();
|
||||
foreach ($data as $field) {
|
||||
$fields[] = $field['field'];
|
||||
if (!is_null($field['value'])) {
|
||||
if (! is_null( $field['value'] )) {
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
case 'date':
|
||||
$values[] = "'" . addslashes($field['value']) . "'";
|
||||
$values[] = "'" . addslashes( $field['value'] ) . "'";
|
||||
break;
|
||||
case 'int':
|
||||
default:
|
||||
$values[] = addslashes($field['value']);
|
||||
$values[] = addslashes( $field['value'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$values[] = $this->nullString;
|
||||
}
|
||||
}
|
||||
$fields = array_map(array($this, 'putQuotes'), $fields);
|
||||
$sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->putQuotes($table), implode(', ', $fields), implode(', ', $values));
|
||||
$fields = array_map( array ($this,'putQuotes'
|
||||
), $fields );
|
||||
$sql = sprintf( "INSERT INTO %s (%s) VALUES (%s)", $this->putQuotes( $table ), implode( ', ', $fields ), implode( ', ', $values ) );
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function generateUpdateSQL($table, $keys, $data) {
|
||||
$fields = array();
|
||||
$where = array();
|
||||
public function generateUpdateSQL ($table, $keys, $data)
|
||||
{
|
||||
$fields = array ();
|
||||
$where = array ();
|
||||
foreach ($data as $field) {
|
||||
if (!is_null($field['value'])) {
|
||||
if (! is_null( $field['value'] )) {
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
case 'date':
|
||||
$fields[] = $this->putQuotes($field['field']) . " = '" . addslashes($field['value']) . "'";
|
||||
$fields[] = $this->putQuotes( $field['field'] ) . " = '" . addslashes( $field['value'] ) . "'";
|
||||
break;
|
||||
case 'int':
|
||||
default:
|
||||
$fields[] = $this->putQuotes($field['field']) . " = " . addslashes($field['value']);
|
||||
$fields[] = $this->putQuotes( $field['field'] ) . " = " . addslashes( $field['value'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$values[] = $this->nullString;
|
||||
}
|
||||
if (in_array($field['field'], $keys)) {
|
||||
$where[] = $fields[count($fields) - 1];
|
||||
if (in_array( $field['field'], $keys )) {
|
||||
$where[] = $fields[count( $fields ) - 1];
|
||||
}
|
||||
}
|
||||
$sql = sprintf("UPDATE %s SET %s WHERE %s", $this->putQuotes($table), implode(', ', $fields), implode(', ', $where));
|
||||
$sql = sprintf( "UPDATE %s SET %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $fields ), implode( ', ', $where ) );
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function generateDeleteSQL($table, $keys, $data) {
|
||||
$fields = array();
|
||||
$where = array();
|
||||
public function generateDeleteSQL ($table, $keys, $data)
|
||||
{
|
||||
$fields = array ();
|
||||
$where = array ();
|
||||
foreach ($data as $field) {
|
||||
if (in_array($field['field'], $keys)) {
|
||||
if (!is_null($field['value'])) {
|
||||
if (in_array( $field['field'], $keys )) {
|
||||
if (! is_null( $field['value'] )) {
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
case 'date':
|
||||
$where[] = $this->putQuotes($field['field']) . " = '" . addslashes($field['value']) . "'";
|
||||
$where[] = $this->putQuotes( $field['field'] ) . " = '" . addslashes( $field['value'] ) . "'";
|
||||
break;
|
||||
case 'int':
|
||||
default:
|
||||
$where[] = $this->putQuotes($field['field']) . " = " . addslashes($field['value']);
|
||||
$where[] = $this->putQuotes( $field['field'] ) . " = " . addslashes( $field['value'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$values[] = $this->nullString;
|
||||
}
|
||||
}
|
||||
}
|
||||
$sql = sprintf("DELETE FROM %s WHERE %s", $this->putQuotes($table), implode(', ', $where));
|
||||
$sql = sprintf( "DELETE FROM %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $where ) );
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function generateSelectSQL($table, $keys, $data) {
|
||||
$fields = array();
|
||||
$where = array();
|
||||
public function generateSelectSQL ($table, $keys, $data)
|
||||
{
|
||||
$fields = array ();
|
||||
$where = array ();
|
||||
foreach ($data as $field) {
|
||||
if (in_array($field['field'], $keys)) {
|
||||
if (!is_null($field['value'])) {
|
||||
if (in_array( $field['field'], $keys )) {
|
||||
if (! is_null( $field['value'] )) {
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
case 'date':
|
||||
$where[] = $this->putQuotes($field['field']) . " = '" . mysql_real_escape_string($field['value']) . "'";
|
||||
$where[] = $this->putQuotes( $field['field'] ) . " = '" . mysql_real_escape_string( $field['value'] ) . "'";
|
||||
break;
|
||||
case 'int':
|
||||
default:
|
||||
$where[] = $this->putQuotes($field['field']) . " = " . mysql_real_escape_string($field['value']);
|
||||
$where[] = $this->putQuotes( $field['field'] ) . " = " . mysql_real_escape_string( $field['value'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$values[] = $this->nullString;
|
||||
}
|
||||
}
|
||||
}
|
||||
$sql = sprintf("SELECT * FROM %s WHERE %s", $this->putQuotes($table), implode(', ', $where));
|
||||
$sql = sprintf( "SELECT * FROM %s WHERE %s", $this->putQuotes( $table ), implode( ', ', $where ) );
|
||||
return $sql;
|
||||
}
|
||||
|
||||
private function putQuotes($element) {
|
||||
private function putQuotes ($element)
|
||||
{
|
||||
return $this->sQuoteCharacterBegin . $element . $this->sQuoteCharacterEnd;
|
||||
}
|
||||
|
||||
@@ -547,10 +549,10 @@ class database extends database_base {
|
||||
$nums = func_num_args();
|
||||
$vars = func_get_args();
|
||||
$sConcat = "";
|
||||
for($i = 0;$i < $nums; $i++) {
|
||||
if(isset($vars[$i])) {
|
||||
for ($i = 0; $i < $nums; $i ++) {
|
||||
if (isset( $vars[$i] )) {
|
||||
$sConcat .= $vars[$i];
|
||||
if(($i+1) < $nums)
|
||||
if (($i + 1) < $nums)
|
||||
$sConcat .= " + ";
|
||||
}
|
||||
}
|
||||
@@ -620,8 +622,7 @@ class database extends database_base {
|
||||
$sqlConcat = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME AS [USER] ";
|
||||
$sqlGroupBy = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME ";
|
||||
|
||||
$sql = "SELECT " . $sqlConcat . ", " .
|
||||
" COUNT(*) AS CANTCASES,
|
||||
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
||||
MIN(AD.DEL_DURATION) AS MIN,
|
||||
MAX(AD.DEL_DURATION) AS MAX,
|
||||
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
||||
@@ -649,8 +650,7 @@ class database extends database_base {
|
||||
$sqlConcat = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME AS [USER] ";
|
||||
$sqlGroupBy = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME ";
|
||||
|
||||
$sql = " SELECT " . $sqlConcat . ", " .
|
||||
" COUNT(*) AS CANTCASES,
|
||||
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
||||
MIN(AD.DEL_DURATION) AS MIN,
|
||||
MAX(AD.DEL_DURATION) AS MAX,
|
||||
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
||||
@@ -658,7 +658,7 @@ class database extends database_base {
|
||||
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."
|
||||
" . $var . "
|
||||
GROUP BY " . $sqlGroupBy;
|
||||
|
||||
return $sql;
|
||||
@@ -679,8 +679,7 @@ class database extends database_base {
|
||||
$sqlConcat = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME AS [USER] ";
|
||||
$sqlGroupBy = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME ";
|
||||
|
||||
$sql = " SELECT " . $sqlConcat . ", " .
|
||||
" COUNT(*) AS CANTCASES,
|
||||
$sql = " SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
||||
MIN(AD.DEL_DURATION) AS MIN,
|
||||
MAX(AD.DEL_DURATION) AS MAX,
|
||||
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
||||
@@ -709,8 +708,7 @@ class database extends database_base {
|
||||
$sqlConcat = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME AS [USER] ";
|
||||
$sqlGroupBy = " U.USR_LASTNAME + ' ' + USR_FIRSTNAME ";
|
||||
|
||||
$sql = "SELECT " . $sqlConcat . ", " .
|
||||
" COUNT(*) AS CANTCASES,
|
||||
$sql = "SELECT " . $sqlConcat . ", " . " COUNT(*) AS CANTCASES,
|
||||
MIN(AD.DEL_DURATION) AS MIN,
|
||||
MAX(AD.DEL_DURATION) AS MAX,
|
||||
SUM(AD.DEL_DURATION) AS TOTALDUR,
|
||||
@@ -718,7 +716,7 @@ class database extends database_base {
|
||||
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."
|
||||
" . $var . "
|
||||
GROUP BY " . $sqlGroupBy;
|
||||
|
||||
return $sql;
|
||||
@@ -731,34 +729,31 @@ class database extends database_base {
|
||||
function getServerVersion($driver, $dbIP, $dbPort, $dbUser, $dbPasswd, $dbSourcename)
|
||||
{
|
||||
|
||||
if(strlen(trim($dbIP))<=0)
|
||||
if (strlen( trim( $dbIP ) ) <= 0)
|
||||
$dbIP = DB_HOST;
|
||||
if($link = @mssql_connect($dbIP, $dbUser, $dbPasswd)){
|
||||
if ($link = @mssql_connect( $dbIP, $dbUser, $dbPasswd )) {
|
||||
@mssql_select_db( DB_NAME, $link );
|
||||
$oResult = @mssql_query("select substring(@@version, 21, 6) + ' (' + CAST(SERVERPROPERTY ('productlevel') as varchar(10)) + ') ' + CAST(SERVERPROPERTY('productversion') AS VARCHAR(15)) + ' ' + CAST(SERVERPROPERTY ('edition') AS VARCHAR(25)) as version; ", $link);
|
||||
$aResult = @mssql_fetch_array($oResult);
|
||||
@mssql_free_result($oResult);
|
||||
$oResult = @mssql_query( "select substring(@@version, 21, 6) + ' (' + CAST(SERVERPROPERTY ('productlevel') as varchar(10)) + ') ' + CAST(SERVERPROPERTY('productversion') AS VARCHAR(15)) + ' ' + CAST(SERVERPROPERTY ('edition') AS VARCHAR(25)) as version; ", $link );
|
||||
$aResult = @mssql_fetch_array( $oResult );
|
||||
@mssql_free_result( $oResult );
|
||||
$v = $aResult[0];
|
||||
} else {
|
||||
throw new Exception(@mssql_error($link));
|
||||
throw new Exception( @mssql_error( $link ) );
|
||||
}
|
||||
return (isset($v))?$v:'none';
|
||||
return (isset( $v )) ? $v : 'none';
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* query functions for class class.net.php
|
||||
*
|
||||
*/
|
||||
function getDropTable($sTableName)
|
||||
{
|
||||
$sql = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='" . $sTableName . "' AND xtype='U') " .
|
||||
"DROP TABLE ['" . $sTableName . "']";
|
||||
$sql = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='" . $sTableName . "' AND xtype='U') " . "DROP TABLE ['" . $sTableName . "']";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
function getTableDescription($sTableName)
|
||||
{
|
||||
$sql = " select column_name as Field,
|
||||
@@ -777,8 +772,7 @@ class database extends database_base {
|
||||
when 'No' then 'NO' else 'YES' END) AS AsNull,
|
||||
COLUMN_DEFAULT as [Default]
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = '" . trim($sTableName) . "'" .
|
||||
" Order by Ordinal_Position asc ";
|
||||
WHERE table_name = '" . trim( $sTableName ) . "'" . " Order by Ordinal_Position asc ";
|
||||
return $sql;
|
||||
}
|
||||
|
||||
@@ -801,9 +795,9 @@ class database extends database_base {
|
||||
function reportTableExist()
|
||||
{
|
||||
$bExists = true;
|
||||
$oConnection = mssql_connect(DB_HOST, DB_USER, DB_PASS);
|
||||
mssql_select_db(DB_NAME);
|
||||
$oDataset = mssql_query('SELECT COUNT(*) FROM REPORT_TABLE') || ($bExists = false);
|
||||
$oConnection = mssql_connect( DB_HOST, DB_USER, DB_PASS );
|
||||
mssql_select_db( DB_NAME );
|
||||
$oDataset = mssql_query( 'SELECT COUNT(*) FROM REPORT_TABLE' ) || ($bExists = false);
|
||||
|
||||
return $bExists;
|
||||
}
|
||||
@@ -820,13 +814,14 @@ class database extends database_base {
|
||||
/**
|
||||
* Determining the existence of a table
|
||||
*/
|
||||
function tableExists ($table, $db) {
|
||||
function tableExists($table, $db)
|
||||
{
|
||||
$sql = "SELECT * FROM sysobjects WHERE name='" . $table . "' AND type='u'";
|
||||
$bExists = true;
|
||||
$oConnection = mssql_connect(DB_HOST, DB_USER, DB_PASS);
|
||||
mssql_select_db(DB_NAME);
|
||||
$oDataset = mssql_query($sql) || ($bExists = false);
|
||||
$oConnection = mssql_connect( DB_HOST, DB_USER, DB_PASS );
|
||||
mssql_select_db( DB_NAME );
|
||||
$oDataset = mssql_query( $sql ) || ($bExists = false);
|
||||
return $bExists;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user