Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // vim: set si ai expandtab tabstop=4 shiftwidth=4 softtabstop=4: | |
3 | ||
4 | /** | |
5 | * File for the Utils class | |
6 | * | |
7 | * (PHP 5) | |
8 | * | |
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 $ | |
14 | * @package CiviCRM | |
15 | * | |
16 | * This file is part of CiviCRM | |
17 | * | |
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. | |
22 | * | |
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. | |
27 | * | |
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/>. | |
31 | */ | |
32 | ||
33 | /** | |
34 | * Utility functions | |
35 | * @package CiviCRM | |
36 | */ | |
37 | class Utils { | |
38 | ||
39 | /** | |
40 | * PDO for the database | |
41 | * @var PDO | |
42 | */ | |
43 | public $pdo; | |
44 | ||
45 | /** | |
46 | * Construct an object for this database | |
47 | */ | |
48 | public function __construct($host, $user, $pass) { | |
49 | try { | |
50 | $this->pdo = new PDO("mysql:host={$host}", | |
51 | $user, $pass, | |
52 | array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE) | |
53 | ); | |
54 | } | |
55 | catch(PDOException$e) { | |
56 | echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL; | |
57 | exit(1); | |
58 | } | |
59 | } | |
60 | ||
61 | /** | |
62 | * Prepare and execute a query | |
63 | * | |
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 | |
68 | */ | |
69 | function do_query($query) { | |
70 | // echo "do_query($query)\n"; | |
71 | // $stmt = $this->pdo->query( $query, PDO::FETCH_ASSOC ); | |
72 | // echo "PDO returned"; | |
73 | // var_dump($stmt); | |
74 | $string = preg_replace("/^#[^\n]*$/m", "\n", $query); | |
75 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
76 | ||
77 | $queries = preg_split('/;\s*$/m', $string); | |
78 | foreach ($queries as $query) { | |
79 | $query = trim($query); | |
80 | if (!empty($query)) { | |
81 | $result = $this->pdo->query($query); | |
82 | if ($this->pdo->errorCode() == 0) { | |
83 | continue; | |
84 | } | |
85 | else { | |
86 | var_dump($result); | |
87 | var_dump($this->pdo->errorInfo()); | |
88 | // die( "Cannot execute $query: " . $this->pdo->errorInfo() ); | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | /******* | |
94 | if ( $this->pdo->errorCode() == 0 ) { | |
95 | //echo "returning the PDOStmt\n"; | |
96 | return $stmt; | |
97 | } | |
98 | ||
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 ){ | |
108 | echo " "; | |
109 | if ( array_key_exists( 'class', $frame ) ) { | |
110 | echo " class {$frame['class']}"; | |
111 | if ( array_key_exists( 'function', $frame ) ) { | |
112 | echo " method {$frame['function']}"; | |
113 | } | |
114 | } else { | |
115 | if ( array_key_exists( 'function', $frame ) ) { | |
116 | echo " function {$frame['function']}"; | |
117 | } | |
118 | } | |
119 | if ( array_key_exists( 'file', $frame ) ) { | |
120 | echo " file ". substr( $frame['file'], $cwd_len ); | |
121 | } | |
122 | if ( array_key_exists( 'line', $frame ) ) { | |
123 | echo " line {$frame['line']}"; | |
124 | } | |
125 | echo "\n"; | |
126 | } | |
127 | ******/ | |
128 | return TRUE; | |
129 | } | |
130 | } | |
131 | // class Utils | |
132 | ||
133 | // -- set Emacs parameters -- | |
134 | // Local variables: | |
135 | // mode: php; | |
136 | // tab-width: 4 | |
137 | // c-basic-offset: 4 | |
138 | // c-hanging-comment-ender-p: nil | |
139 | // indent-tabs-mode: nil | |
140 | // End: | |
141 |