@@ -343,7 +343,7 @@ public static function formatSize($sizeInBytes, $separator = ',')
343
343
}
344
344
}
345
345
346
- /** Safe delete function. Checks ifthe file can be deleted. */
346
+ /** Safe delete function. Checks if the file can be deleted. */
347
347
public static function safedelete ($ filename )
348
348
{
349
349
if (!file_exists ($ filename ))
@@ -353,65 +353,62 @@ public static function safedelete($filename)
353
353
unlink ($ filename );
354
354
}
355
355
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 )
358
358
{
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 )
362
371
{
363
372
throw new Zend_Exception ("Unable to connect. " );
364
373
}
365
- $ requetes = "" ;
366
374
367
- $ sql = file ($ sqlfile );
368
- foreach ($ sql as $ l )
375
+ $ sql = '' ;
376
+ $ lines = file ($ sqlfile );
377
+ foreach ($ lines as $ line )
369
378
{
370
- if (substr (trim ($ l ), 0 , 2 ) != " -- " )
379
+ if (trim ( $ line ) != '' && substr (trim ($ line ), 0 , 2 ) != ' -- ' && substr ( $ line , 0 , 1 ) != ' # ' )
371
380
{
372
- $ requetes .= $ l ;
381
+ $ sql .= $ line ;
373
382
}
374
383
}
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 )
380
392
{
381
- throw new Zend_Exception ("Unable to execute: " .$ req );
393
+ if (trim ($ query ) != '' )
394
+ {
395
+ throw new Zend_Exception ("Unable to connect. " );
396
+ }
382
397
}
383
398
}
384
399
return true ;
385
400
}
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
+ }
386
407
387
- /** Function to run the sql script */
408
+ /** Function to run a PostgreSQL script */
388
409
static function run_pgsql_from_file ($ sqlfile , $ host , $ username , $ password , $ dbname , $ port )
389
410
{
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 );
415
412
}
416
413
417
414
/**
0 commit comments