|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2022 Nikos Siatras |
| 7 | +
|
| 8 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + of this software and associated documentation files (the "Software"), to deal |
| 10 | + in the Software without restriction, including without limitation the rights |
| 11 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + copies of the Software, and to permit persons to whom the Software is |
| 13 | + furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | + The above copyright notice and this permission notice shall be included in all |
| 16 | + copies or substantial portions of the Software. |
| 17 | +
|
| 18 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +class MySQLCommand |
| 28 | +{ |
| 29 | + |
| 30 | + private MySQLConnection $fMySQLConnection; // MySQLConnection of this command |
| 31 | + private String $fQuery; // Query to execute |
| 32 | + private $fPreparedStatement = NULL; // The prepared statement of this command |
| 33 | + public $Parameters = NULL; // Command parameters |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs a new MySQLCommand |
| 37 | + * @param MySQLConnection $mySQLConnection is the MySQLConnection |
| 38 | + * @param string $query the query to execute |
| 39 | + */ |
| 40 | + public function __construct(MySQLConnection $mySQLConnection, String $query = "") |
| 41 | + { |
| 42 | + $this->fMySQLConnection = $mySQLConnection; |
| 43 | + $this->Parameters = new MySQLCommandParameters($this); |
| 44 | + $this->fQuery = $query; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Execute query and return the number of affected rows. |
| 49 | + * @return the number of affected rows. |
| 50 | + */ |
| 51 | + public function ExecuteQuery() |
| 52 | + { |
| 53 | + if (sizeof($this->Parameters->getParameters()) > 0) // Query with parameters (Prepared Statement) |
| 54 | + { |
| 55 | + $this->fPreparedStatement = $this->fMySQLConnection->getLink()->stmt_init(); |
| 56 | + |
| 57 | + if (!$this->fPreparedStatement->prepare($this->fQuery)) |
| 58 | + { |
| 59 | + throw new Exception("ExecuteQuery failed to prepare statement: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // Prepared statement created. |
| 64 | + // Bind parameters and execute |
| 65 | + $this->BindParametersToQuery(); |
| 66 | + if (!$this->fPreparedStatement->execute()) |
| 67 | + { |
| 68 | + throw new Exception("Execute query failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + return $this->fMySQLConnection->getLink()->affected_rows; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + else // Straight mySQL Query withour parameters |
| 77 | + { |
| 78 | + if (!$this->fMySQLConnection->getLink()->query($this->fQuery)) |
| 79 | + { |
| 80 | + throw new Exception("Execute query failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + return $this->fMySQLConnection->getLink()->affected_rows; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Execute reader command. |
| 91 | + * Returns MySQLDataReader |
| 92 | + * @return MySQLDataReader |
| 93 | + */ |
| 94 | + public function ExecuteReader() |
| 95 | + { |
| 96 | + $this->fPreparedStatement = NULL; |
| 97 | + |
| 98 | + if (sizeof($this->Parameters->getParameters()) > 0) // Query with parameters (Prepared Statement) |
| 99 | + { |
| 100 | + $this->fPreparedStatement = $this->fMySQLConnection->getLink()->stmt_init(); |
| 101 | + |
| 102 | + if (!$this->fPreparedStatement->prepare($this->fQuery)) |
| 103 | + { |
| 104 | + throw new Exception("ExecuteReader failed to prepare statement: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + // Prepared statement created. |
| 109 | + // Bind parameters and execute reader |
| 110 | + $this->BindParametersToQuery(); |
| 111 | + if (!$this->fPreparedStatement->execute()) |
| 112 | + { |
| 113 | + throw new Exception("Execute reader failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 114 | + } |
| 115 | + |
| 116 | + return new MySQLDataReader($this->fPreparedStatement); |
| 117 | + } |
| 118 | + } |
| 119 | + else // Simple query withour prepared statement and parameters |
| 120 | + { |
| 121 | + if (!$result = $this->fMySQLConnection->getLink()->query($this->fQuery)) |
| 122 | + { |
| 123 | + throw new Exception("Execute reader failed: (" . $this->fMySQLConnection->getLink()->errno . ") " . $this->fMySQLConnection->getLink()->error); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + return new MySQLDataReader($result); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Sets a new query for the MySQLCommand |
| 134 | + * $query: is the query to set |
| 135 | + * @param type $query |
| 136 | + */ |
| 137 | + public function setQuery($query) |
| 138 | + { |
| 139 | + $this->fQuery = $query; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Return's the query been given to this command |
| 144 | + * @return string the command's query |
| 145 | + */ |
| 146 | + public function getQuery() |
| 147 | + { |
| 148 | + return $this->fQuery; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns the MySQLConnection of this MySQLCommand |
| 153 | + * @return type MySQLConnection |
| 154 | + */ |
| 155 | + public function getMySQLConnection() |
| 156 | + { |
| 157 | + return $this->fMySQLConnection; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Return's the last inserted ID |
| 162 | + * @return type |
| 163 | + */ |
| 164 | + public function getLastInsertID() |
| 165 | + { |
| 166 | + return $this->fMySQLConnection->getLink()->insert_id; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Bind parameter values to query. |
| 171 | + * This method should be called every time before query execution. |
| 172 | + * @return type |
| 173 | + */ |
| 174 | + private function BindParametersToQuery() |
| 175 | + { |
| 176 | + $parameterTypes = ""; |
| 177 | + $ar = array(); |
| 178 | + |
| 179 | + foreach ($this->Parameters->getParameters() as $key => $value) |
| 180 | + { |
| 181 | + $parameterTypes .= $value->Type; |
| 182 | + } |
| 183 | + $ar[] = $parameterTypes; |
| 184 | + |
| 185 | + foreach ($this->Parameters->getParameters() as $key => $value) |
| 186 | + { |
| 187 | + $ar[] = &$value->Value; |
| 188 | + } |
| 189 | + |
| 190 | + call_user_func_array(array($this->fPreparedStatement, 'bind_param'), $ar); |
| 191 | + } |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | +?> |
0 commit comments