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 | * | |
6c6e6187 TO |
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 | |
6a488035 | 12 | * GNU Affero General Public License version 3 |
6c6e6187 TO |
13 | * @version $Id: Utils.php 40328 2012-05-11 23:06:13Z allen $ |
14 | * @package CiviCRM | |
6a488035 TO |
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 | |
6c6e6187 | 35 | * @package CiviCRM |
6a488035 | 36 | */ |
649d6dd0 | 37 | class CiviTestPdoUtils { |
6a488035 TO |
38 | |
39 | /** | |
eceb18cc | 40 | * PDO for the database. |
6c6e6187 | 41 | * @var PDO |
6a488035 TO |
42 | */ |
43 | public $pdo; | |
44 | ||
be709432 TO |
45 | public $dbName; |
46 | ||
6a488035 | 47 | /** |
eceb18cc | 48 | * Construct an object for this database. |
6a488035 | 49 | */ |
f3e16511 TO |
50 | public function __construct($dsn) { |
51 | require_once "DB.php"; | |
52 | $dsninfo = DB::parseDSN($dsn); | |
53 | $host = $dsninfo['hostspec']; | |
54 | $port = @$dsninfo['port']; | |
55 | $user = $dsninfo['username']; | |
56 | $pass = $dsninfo['password']; | |
be709432 | 57 | $this->dbName = $dsninfo['database']; |
f3e16511 | 58 | |
6a488035 | 59 | try { |
fc3c781d | 60 | $this->pdo = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""), |
6a488035 TO |
61 | $user, $pass, |
62 | array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE) | |
63 | ); | |
64 | } | |
92915c55 | 65 | catch (PDOException$e) { |
6a488035 TO |
66 | echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL; |
67 | exit(1); | |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
eceb18cc | 72 | * Prepare and execute a query. |
6a488035 | 73 | * |
72b3a70c CW |
74 | * If the query fails, output a diagnostic message |
75 | * @param string $query | |
76 | * Query to run | |
77 | * @return bool | |
6a488035 | 78 | */ |
00be9182 | 79 | public function do_query($query) { |
6a488035 TO |
80 | // echo "do_query($query)\n"; |
81 | // $stmt = $this->pdo->query( $query, PDO::FETCH_ASSOC ); | |
82 | // echo "PDO returned"; | |
83 | // var_dump($stmt); | |
84 | $string = preg_replace("/^#[^\n]*$/m", "\n", $query); | |
85 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); | |
86 | ||
87 | $queries = preg_split('/;\s*$/m', $string); | |
88 | foreach ($queries as $query) { | |
89 | $query = trim($query); | |
90 | if (!empty($query)) { | |
91 | $result = $this->pdo->query($query); | |
92 | if ($this->pdo->errorCode() == 0) { | |
93 | continue; | |
94 | } | |
95 | else { | |
96 | var_dump($result); | |
97 | var_dump($this->pdo->errorInfo()); | |
98 | // die( "Cannot execute $query: " . $this->pdo->errorInfo() ); | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | /******* | |
92915c55 TO |
104 | * if ( $this->pdo->errorCode() == 0 ) { |
105 | * //echo "returning the PDOStmt\n"; | |
106 | * return $stmt; | |
107 | * } | |
108 | * | |
109 | * // operation failed, so output description of where and why | |
110 | * $errorInfo = $this->pdo->errorInfo(); | |
111 | * echo "Oops, can't do query:\n {$query}\n in " | |
112 | * . basename( __FILE__) . " line " . __LINE__.":\n " | |
113 | * . $errorInfo[0] . ": " . $errorInfo[2] . "\n Call stack:\n"; | |
114 | * $backtrace = debug_backtrace(); | |
115 | * $dir_name = dirname( __FILE__ ); | |
116 | * $cwd_len = strlen( $dir_name ) + 1; | |
117 | * foreach ($backtrace as $frame ) { | |
118 | * echo " "; | |
119 | * if ( array_key_exists( 'class', $frame ) ) { | |
120 | * echo " class {$frame['class']}"; | |
121 | * if ( array_key_exists( 'function', $frame ) ) { | |
122 | * echo " method {$frame['function']}"; | |
123 | * } | |
124 | * } | |
125 | * else { | |
126 | * if ( array_key_exists( 'function', $frame ) ) { | |
127 | * echo " function {$frame['function']}"; | |
128 | * } | |
129 | * } | |
130 | * if ( array_key_exists( 'file', $frame ) ) { | |
131 | * echo " file ". substr( $frame['file'], $cwd_len ); | |
132 | * } | |
133 | * if ( array_key_exists( 'line', $frame ) ) { | |
134 | * echo " line {$frame['line']}"; | |
135 | * } | |
136 | * echo "\n"; | |
137 | * } | |
6a488035 TO |
138 | ******/ |
139 | return TRUE; | |
140 | } | |
96025800 | 141 | |
6a488035 TO |
142 | } |
143 | // class Utils | |
144 | ||
145 | // -- set Emacs parameters -- | |
146 | // Local variables: | |
147 | // mode: php; | |
148 | // tab-width: 4 | |
149 | // c-basic-offset: 4 | |
150 | // c-hanging-comment-ender-p: nil | |
151 | // indent-tabs-mode: nil | |
152 | // End: |