PMCORE-1491

This commit is contained in:
Julio Cesar Laura Avendaño
2020-06-10 23:00:46 +00:00
parent 48dc990279
commit 5c34f18a3a
3 changed files with 47 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ class DBQueryTest extends TestCase
/** /**
* Verify the execution of a common SQL statement. * Verify the execution of a common SQL statement.
* Note, this test is now using Laravel DB backend work
* @test * @test
*/ */
public function testStandardExecuteQuery() public function testStandardExecuteQuery()

View File

@@ -206,6 +206,7 @@ class DbConnections
}); });
foreach ($externalDbs as $externalDb) { foreach ($externalDbs as $externalDb) {
$conf['datasources'][$externalDb->DBS_UID] = []; $conf['datasources'][$externalDb->DBS_UID] = [];
$laravelConfig = [];
$flagTns = ($externalDb->DBS_TYPE == "oracle" && $externalDb->DBS_CONNECTION_TYPE == "TNS")? 1 : 0; $flagTns = ($externalDb->DBS_TYPE == "oracle" && $externalDb->DBS_CONNECTION_TYPE == "TNS")? 1 : 0;
// Build the appropriate items to add to our Propel configuration // Build the appropriate items to add to our Propel configuration
// Let's grab the decrypted password // Let's grab the decrypted password
@@ -235,12 +236,26 @@ class DbConnections
. $externalDb->DBS_USERNAME . ':' . $passw . '@' . $externalDb->DBS_SERVER . $dbsPort . '/' . $externalDb->DBS_USERNAME . ':' . $passw . '@' . $externalDb->DBS_SERVER . $dbsPort . '/'
. $externalDb->DBS_DATABASE_NAME . $encoding; . $externalDb->DBS_DATABASE_NAME . $encoding;
} }
$laravelConfig = [
'driver' => $externalDb->DBS_TYPE === 'mssql' ? 'sqlsrv' : $externalDb->DBS_TYPE, // MSSQL driver is not supported anymore, only SQLSRV
'host' => $externalDb->DBS_SERVER,
'port' => $externalDb->DBS_PORT == '' ? null : $externalDb->DBS_PORT,
'database' => $externalDb->DBS_DATABASE_NAME,
'username' => $externalDb->DBS_USERNAME,
'password' => $passw,
'charset' => trim($externalDb->DBS_ENCODE) == '' ? null : trim($externalDb->DBS_ENCODE),
'options' => [PDO::ATTR_STRINGIFY_FETCHES => true] // For keep the old behaviour, all values are transformed to strings
];
} else { } else {
// Is oracle and TNS, let's provide a TNS based DSN // Is oracle and TNS, let's provide a TNS based DSN
$conf["datasources"][$externalDb->DBS_UID]["connection"] = $externalDb->DBS_TYPE . "://" $conf["datasources"][$externalDb->DBS_UID]["connection"] = $externalDb->DBS_TYPE . "://"
. $externalDb->DBS_USERNAME . ":" . $passw . "@" . $externalDb->DBS_TNS; . $externalDb->DBS_USERNAME . ":" . $passw . "@" . $externalDb->DBS_TNS;
} }
$conf['datasources'][$externalDb->DBS_UID]['adapter'] = $externalDb->DBS_TYPE; $conf['datasources'][$externalDb->DBS_UID]['adapter'] = $externalDb->DBS_TYPE;
// Load the config for the external database into laravel
config([
'database.connections.' . $externalDb->DBS_UID => $laravelConfig
]);
} }
Propel::initConfiguration($conf); Propel::initConfiguration($conf);
$lastProcessId = $_SESSION['PROCESS']; $lastProcessId = $_SESSION['PROCESS'];

View File

@@ -32,6 +32,7 @@ use ProcessMaker\BusinessModel\Cases as BusinessModelCases;
use ProcessMaker\Core\System; use ProcessMaker\Core\System;
use ProcessMaker\Plugins\PluginRegistry; use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Util\ElementTranslation; use ProcessMaker\Util\ElementTranslation;
use Illuminate\Support\Facades\DB;
/** /**
* ProcessMaker has made a number of its PHP functions available be used in triggers and conditions. * ProcessMaker has made a number of its PHP functions available be used in triggers and conditions.
@@ -243,8 +244,22 @@ function executeQuery ($SqlStatement, $DBConnectionUID = 'workflow', $aParameter
{ {
$sysSys = (!empty(config("system.workspace")))? config("system.workspace") : "Undefined"; $sysSys = (!empty(config("system.workspace")))? config("system.workspace") : "Undefined";
$aContext = \Bootstrap::getDefaultContextLog(); $aContext = \Bootstrap::getDefaultContextLog();
$con = Propel::getConnection( $DBConnectionUID );
// This means the DBConnectionUID is not loaded yet, so we'll force DbConnections::loadAdditionalConnections
if (is_null(config('database.connections.' . $DBConnectionUID . '.driver'))) {
// Force to load the external connections
DbConnections::loadAdditionalConnections();
if (config('database.connections.' . $DBConnectionUID . '.driver') !== 'oracle') {
// If the connections drivers are "mysql", "pgsql" or "sqlsrv" we're using Laravel
$con = DB::connection($DBConnectionUID);
$con->beginTransaction();
} else {
// If the connection driver is "oracle" we're using the native oci8 functions
$con = Propel::getConnection($DBConnectionUID);
$con->begin(); $con->begin();
}
}
$blackList = System::getQueryBlackList(); $blackList = System::getQueryBlackList();
$listQueries = explode('|', isset($blackList['queries']) ? $blackList['queries'] : ''); $listQueries = explode('|', isset($blackList['queries']) ? $blackList['queries'] : '');
$aListAllTables = explode( $aListAllTables = explode(
@@ -299,36 +314,34 @@ function executeQuery ($SqlStatement, $DBConnectionUID = 'workflow', $aParameter
$statement = str_replace( '(', '', $statement ); $statement = str_replace( '(', '', $statement );
$result = false; $result = false;
if (getEngineDataBaseName( $con ) != 'oracle') {
// Check to see if we're not running oracle, which is usually a safe default
if (config('database.connections.' . $DBConnectionUID . '.driver') != 'oracle') {
switch (true) { switch (true) {
case preg_match( "/^(SELECT|EXECUTE|EXEC|SHOW|DESCRIBE|EXPLAIN|BEGIN)\s/i", $statement ): case preg_match( "/^(SELECT|EXECUTE|EXEC|SHOW|DESCRIBE|EXPLAIN|BEGIN)\s/i", $statement ):
$rs = $con->executeQuery( $SqlStatement ); $result = $con->select( $SqlStatement );
$result = Array ();
$i = 1; // Convert to 1 index key array of array results
while ($rs->next()) { $result = collect($result)->map(function($x) { return (array)$x; })->toArray();
$result[$i ++] = $rs->getRow(); array_unshift($result, []);
} unset($result[0]);
$rs->close();
$con->commit(); $con->commit();
break; break;
case preg_match( "/^INSERT\s/i", $statement ): case preg_match( "/^INSERT\s/i", $statement ):
$rs = $con->executeUpdate( $SqlStatement ); $result = $con->insert( $SqlStatement );
$result = $con->getUpdateCount();
$con->commit(); $con->commit();
break; break;
case preg_match( "/^REPLACE\s/i", $statement ): case preg_match( "/^REPLACE\s/i", $statement ):
$rs = $con->executeUpdate( $SqlStatement ); $result = $con->update( $SqlStatement );
$result = $con->getUpdateCount();
$con->commit(); $con->commit();
break; break;
case preg_match( "/^UPDATE\s/i", $statement ): case preg_match( "/^UPDATE\s/i", $statement ):
$rs = $con->executeUpdate( $SqlStatement ); $result = $con->update( $SqlStatement );
$result = $con->getUpdateCount();
$con->commit(); $con->commit();
break; break;
case preg_match( "/^DELETE\s/i", $statement ): case preg_match( "/^DELETE\s/i", $statement ):
$rs = $con->executeUpdate( $SqlStatement ); $result = $con->delete( $SqlStatement );
$result = $con->getUpdateCount();
$con->commit(); $con->commit();
break; break;
} }