| 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 | * @param $host |
| 48 | * @param $port |
| 49 | * @param $user |
| 50 | * @param $pass |
| 51 | */ |
| 52 | public function __construct($host, $port, $user, $pass) { |
| 53 | try { |
| 54 | $this->pdo = new PDO("mysql:host={$host}" . ($port ? ";port=$port" : ""), |
| 55 | $user, $pass, |
| 56 | array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE) |
| 57 | ); |
| 58 | } |
| 59 | catch (PDOException$e) { |
| 60 | echo "Can't connect to MySQL server:" . PHP_EOL . $e->getMessage() . PHP_EOL; |
| 61 | exit(1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Prepare and execute a query |
| 67 | * |
| 68 | * If the query fails, output a diagnostic message |
| 69 | * @param string $query |
| 70 | * Query to run |
| 71 | * @return bool |
| 72 | */ |
| 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"; |
| 77 | // var_dump($stmt); |
| 78 | $string = preg_replace("/^#[^\n]*$/m", "\n", $query); |
| 79 | $string = preg_replace("/^(--[^-]).*/m", "\n", $string); |
| 80 | |
| 81 | $queries = preg_split('/;\s*$/m', $string); |
| 82 | foreach ($queries as $query) { |
| 83 | $query = trim($query); |
| 84 | if (!empty($query)) { |
| 85 | $result = $this->pdo->query($query); |
| 86 | if ($this->pdo->errorCode() == 0) { |
| 87 | continue; |
| 88 | } |
| 89 | else { |
| 90 | var_dump($result); |
| 91 | var_dump($this->pdo->errorInfo()); |
| 92 | // die( "Cannot execute $query: " . $this->pdo->errorInfo() ); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /******* |
| 98 | * if ( $this->pdo->errorCode() == 0 ) { |
| 99 | * //echo "returning the PDOStmt\n"; |
| 100 | * return $stmt; |
| 101 | * } |
| 102 | * |
| 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 ) { |
| 112 | * echo " "; |
| 113 | * if ( array_key_exists( 'class', $frame ) ) { |
| 114 | * echo " class {$frame['class']}"; |
| 115 | * if ( array_key_exists( 'function', $frame ) ) { |
| 116 | * echo " method {$frame['function']}"; |
| 117 | * } |
| 118 | * } |
| 119 | * else { |
| 120 | * if ( array_key_exists( 'function', $frame ) ) { |
| 121 | * echo " function {$frame['function']}"; |
| 122 | * } |
| 123 | * } |
| 124 | * if ( array_key_exists( 'file', $frame ) ) { |
| 125 | * echo " file ". substr( $frame['file'], $cwd_len ); |
| 126 | * } |
| 127 | * if ( array_key_exists( 'line', $frame ) ) { |
| 128 | * echo " line {$frame['line']}"; |
| 129 | * } |
| 130 | * echo "\n"; |
| 131 | * } |
| 132 | ******/ |
| 133 | return TRUE; |
| 134 | } |
| 135 | } |
| 136 | // class Utils |
| 137 | |
| 138 | // -- set Emacs parameters -- |
| 139 | // Local variables: |
| 140 | // mode: php; |
| 141 | // tab-width: 4 |
| 142 | // c-basic-offset: 4 |
| 143 | // c-hanging-comment-ender-p: nil |
| 144 | // indent-tabs-mode: nil |
| 145 | // End: |