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.
52 public function __construct($host, $port, $user, $pass) {
54 $this->pdo
= new PDO("mysql:host={$host}" . ($port ?
";port=$port" : ""),
56 array(PDO
::MYSQL_ATTR_USE_BUFFERED_QUERY
=> TRUE)
59 catch (PDOException
$e) {
60 echo "Can't connect to MySQL server:" . PHP_EOL
. $e->getMessage() . PHP_EOL
;
66 * Prepare and execute a query.
68 * If the query fails, output a diagnostic message
69 * @param string $query
73 public function do_query($query) {
74 // echo "do_query($query)\n";
75 // $stmt = $this->pdo->query( $query, PDO::FETCH_ASSOC );
76 // echo "PDO returned";
78 $string = preg_replace("/^#[^\n]*$/m", "\n", $query);
79 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
81 $queries = preg_split('/;\s*$/m', $string);
82 foreach ($queries as $query) {
83 $query = trim($query);
85 $result = $this->pdo
->query($query);
86 if ($this->pdo
->errorCode() == 0) {
91 var_dump($this->pdo
->errorInfo());
92 // die( "Cannot execute $query: " . $this->pdo->errorInfo() );
98 * if ( $this->pdo->errorCode() == 0 ) {
99 * //echo "returning the PDOStmt\n";
103 * // operation failed, so output description of where and why
104 * $errorInfo = $this->pdo->errorInfo();
105 * echo "Oops, can't do query:\n {$query}\n in "
106 * . basename( __FILE__) . " line " . __LINE__.":\n "
107 * . $errorInfo[0] . ": " . $errorInfo[2] . "\n Call stack:\n";
108 * $backtrace = debug_backtrace();
109 * $dir_name = dirname( __FILE__ );
110 * $cwd_len = strlen( $dir_name ) + 1;
111 * foreach ($backtrace as $frame ) {
113 * if ( array_key_exists( 'class', $frame ) ) {
114 * echo " class {$frame['class']}";
115 * if ( array_key_exists( 'function', $frame ) ) {
116 * echo " method {$frame['function']}";
120 * if ( array_key_exists( 'function', $frame ) ) {
121 * echo " function {$frame['function']}";
124 * if ( array_key_exists( 'file', $frame ) ) {
125 * echo " file ". substr( $frame['file'], $cwd_len );
127 * if ( array_key_exists( 'line', $frame ) ) {
128 * echo " line {$frame['line']}";
139 // -- set Emacs parameters --
144 // c-hanging-comment-ender-p: nil
145 // indent-tabs-mode: nil