2 // vim: set si ai expandtab tabstop=4 shiftwidth=4 softtabstop=4:
5 * File for the Utils class
9 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
10 * @copyright Copyright CiviCRM LLC (C) 2009
11 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
12 * GNU Affero General Public License version 3
13 * @version $Id: Utils.php 40328 2012-05-11 23:06:13Z allen $
16 * This file is part of CiviCRM
18 * CiviCRM is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Affero General Public License
20 * as published by the Free Software Foundation; either version 3 of
21 * the License, or (at your option) any later version.
23 * CiviCRM is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Affero General Public License for more details.
28 * You should have received a copy of the GNU Affero General Public
29 * License along with this program. If not, see
30 * <http://www.gnu.org/licenses/>.
40 * PDO for the database
46 * Construct an object for this database
48 public function __construct($host, $port, $user, $pass) {
50 $this->pdo
= new PDO("mysql:host={$host}" . ($port ?
";port=$port" : ""),
52 array(PDO
::MYSQL_ATTR_USE_BUFFERED_QUERY
=> TRUE)
55 catch(PDOException
$e) {
56 echo "Can't connect to MySQL server:" . PHP_EOL
. $e->getMessage() . PHP_EOL
;
62 * Prepare and execute a query
64 * If the query fails, output a diagnostic message
65 * @param string Query to run
66 * @return mixed PDOStatement => Results of the query
67 * false => Query failed
69 function do_query($query) {
70 // echo "do_query($query)\n";
71 // $stmt = $this->pdo->query( $query, PDO::FETCH_ASSOC );
72 // echo "PDO returned";
74 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
75 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
77 $queries = preg_split('/;\s*$/m', $string);
78 foreach ($queries as $query) {
79 $query = trim($query);
81 $result = $this->pdo
->query($query);
82 if ($this->pdo
->errorCode() == 0) {
87 var_dump($this->pdo
->errorInfo());
88 // die( "Cannot execute $query: " . $this->pdo->errorInfo() );
94 if ( $this->pdo->errorCode() == 0 ) {
95 //echo "returning the PDOStmt\n";
99 // operation failed, so output description of where and why
100 $errorInfo = $this->pdo->errorInfo();
101 echo "Oops, can't do query:\n {$query}\n in "
102 . basename( __FILE__) . " line " . __LINE__.":\n "
103 . $errorInfo[0] . ": " . $errorInfo[2] . "\n Call stack:\n";
104 $backtrace = debug_backtrace();
105 $dir_name = dirname( __FILE__ );
106 $cwd_len = strlen( $dir_name ) + 1;
107 foreach( $backtrace as $frame ){
109 if ( array_key_exists( 'class', $frame ) ) {
110 echo " class {$frame['class']}";
111 if ( array_key_exists( 'function', $frame ) ) {
112 echo " method {$frame['function']}";
115 if ( array_key_exists( 'function', $frame ) ) {
116 echo " function {$frame['function']}";
119 if ( array_key_exists( 'file', $frame ) ) {
120 echo " file ". substr( $frame['file'], $cwd_len );
122 if ( array_key_exists( 'line', $frame ) ) {
123 echo " line {$frame['line']}";
133 // -- set Emacs parameters --
138 // c-hanging-comment-ender-p: nil
139 // indent-tabs-mode: nil