Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 785e63d

Browse files
author
Jamie Snape
committed
Update UtilityComponent to use PDO
1 parent e3cc6a7 commit 785e63d

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

core/controllers/components/UtilityComponent.php

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public static function formatSize($sizeInBytes, $separator = ',')
343343
}
344344
}
345345

346-
/** Safe delete function. Checks ifthe file can be deleted. */
346+
/** Safe delete function. Checks if the file can be deleted. */
347347
public static function safedelete($filename)
348348
{
349349
if(!file_exists($filename))
@@ -353,65 +353,62 @@ public static function safedelete($filename)
353353
unlink($filename);
354354
}
355355

356-
/** Function to run the sql script */
357-
static function run_mysql_from_file($sqlfile, $host, $username, $password, $dbname, $port)
356+
/** Function to run a SQL script */
357+
static function run_query_from_file($adapter, $sqlfile, $host, $username, $password, $dbname, $port)
358358
{
359-
$db = mysql_connect($host.":".$port, $username, $password);
360-
$select = mysql_select_db($dbname, $db);
361-
if(!$db || !$select)
359+
$db = Zend_Db::factory($adapter, array(
360+
'host' => $host,
361+
'port' => $port,
362+
'username' => $username,
363+
'password' => $password,
364+
'dbname' => $dbname));
365+
366+
try
367+
{
368+
$db->getConnection();
369+
}
370+
catch(Zend_Exception $exception)
362371
{
363372
throw new Zend_Exception("Unable to connect.");
364373
}
365-
$requetes = "";
366374

367-
$sql = file($sqlfile);
368-
foreach($sql as $l)
375+
$sql = '';
376+
$lines = file($sqlfile);
377+
foreach($lines as $line)
369378
{
370-
if(substr(trim($l), 0, 2) != "--")
379+
if(trim($line) != '' && substr(trim($line), 0, 2) != '--' && substr($line, 0, 1) != '#')
371380
{
372-
$requetes .= $l;
381+
$sql .= $line;
373382
}
374383
}
375-
376-
$reqs = explode(";", $requetes);
377-
foreach($reqs as $req)
378-
{// And they are executed
379-
if(!mysql_query($req, $db) && trim($req) != "")
384+
$queries = explode(';', $sql);
385+
foreach($queries as $query)
386+
{
387+
try
388+
{
389+
$db->query($query);
390+
}
391+
catch(Zend_Exception $exception)
380392
{
381-
throw new Zend_Exception("Unable to execute: ".$req );
393+
if(trim($query) != '')
394+
{
395+
throw new Zend_Exception("Unable to connect.");
396+
}
382397
}
383398
}
384399
return true;
385400
}
401+
402+
/** Function to run a MySQL script */
403+
static function run_mysql_from_file($sqlfile, $host, $username, $password, $dbname, $port)
404+
{
405+
return self::run_query_from_file('Pdo_Mysql', $sqlfile, $host, $username, $password, $dbname, $port);
406+
}
386407

387-
/** Function to run the sql script */
408+
/** Function to run a PostgreSQL script */
388409
static function run_pgsql_from_file($sqlfile, $host, $username, $password, $dbname, $port)
389410
{
390-
$pgdb = pg_connect("host = ".$host." port = ".$port." dbname = ".$dbname." user = ".$username." password = ".$password);
391-
$file_content = file($sqlfile);
392-
$query = "";
393-
$linnum = 0;
394-
foreach($file_content as $sql_line)
395-
{
396-
$tsl = trim($sql_line);
397-
if(($sql_line != "") && (substr($tsl, 0, 2) != "--") && (substr($tsl, 0, 1) != "#"))
398-
{
399-
$query .= $sql_line;
400-
if(preg_match("/;\s*$/", $sql_line))
401-
{
402-
$query = str_replace(";", "", "$query");
403-
$result = pg_query($query);
404-
if(!$result)
405-
{
406-
echo "Error line:".$linnum."<br>";
407-
return pg_last_error();
408-
}
409-
$query = "";
410-
}
411-
}
412-
$linnum++;
413-
} // end for each line
414-
return true;
411+
return self::run_query_from_file('Pdo_Pgsql', $sqlfile, $host, $username, $password, $dbname, $port);
415412
}
416413

417414
/**

0 commit comments

Comments
 (0)