PageRenderTime 34ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Data_Grid/class.eyemysqladap.inc.php

https://bitbucket.org/evinw/lib
PHP | 410 lines | 156 code | 56 blank | 198 comment | 21 complexity | df926118d31b69fa83b9976ae0be910c MD5 | raw file
  1<?php
  2/**
  3 * EyeMySQLAdap
  4 * MySQL database adapter
  5 *
  6 * LICENSE: This source file is subject to the BSD license
  7 * that is available through the world-wide-web at the following URI:
  8 * http://www.eyesis.ca/license.txt.  If you did not receive a copy of
  9 * the BSD License and are unable to obtain it through the web, please
 10 * send a note to mike@eyesis.ca so I can send you a copy immediately.
 11 *
 12 * @author     Micheal Frank <mike@eyesis.ca>
 13 * @copyright  2008 Eyesis
 14 * @license    http://www.eyesis.ca/license.txt  BSD License
 15 * @version    v1.0.1 11/26/2008 9:41:46 AM
 16 */
 17
 18class EyeMySQLAdap {
 19    private $host, $user, $pass, $db_name;
 20
 21    /**
 22     * MySQL connection information
 23     *
 24     * @var resource
 25     */
 26
 27    private $link;
 28    /**
 29     * Result of last query
 30     *
 31     * @var resource
 32     */
 33
 34    private $result;
 35
 36    /**
 37     * Date and time
 38     *
 39     */
 40    const DATETIME = 'Y-m-d H:i:s';
 41
 42    /**
 43     * Date
 44     *
 45     */
 46    const DATE = 'Y-m-d';
 47
 48    /**
 49     * Constructor
 50     *
 51     * @param string $host MySQL host address
 52     * @param string $user Database user
 53     * @param string $password Database password
 54     * @param string $db Database name
 55     * @param boolean $persistant Is persistant connection
 56     * @param  boolean $connect_now Connect now
 57     * @return void
 58     */
 59    public function __construct($host, $user, $password, $db, $persistant = true, $connect_now = true) {
 60        $this->host = $host; // Host address
 61        $this->user = $user; // User
 62        $this->pass = $password; // Password
 63        $this->db_name = $db; // Database
 64
 65        if ($connect_now)
 66            $this->connect($persistant);
 67
 68        return;
 69    }
 70
 71    /**
 72     * Destructor
 73     *
 74     * @return void
 75     */
 76    public function __destruct() {
 77        $this->close();
 78    }
 79
 80    /**
 81     * Connect to the database
 82     *
 83     * @param boolean $persist Is persistant connection
 84     * @return boolean
 85     */
 86    public function connect($persist = true) {
 87        if ($persist)
 88            $link = mysql_pconnect($this->host, $this->user, $this->pass);
 89        else
 90            $link = mysql_connect($this->host, $this->user, $this->pass);
 91
 92        if (!$link)
 93            trigger_error('Could not connect to the database.', E_USER_ERROR);
 94
 95        if ($link) {
 96            $this->link = $link;
 97            if (mysql_select_db($this->db_name, $link))
 98                return true;
 99        }
100
101        return false;
102    }
103
104    /**
105     * Query the database
106     *
107     * @param string $query SQL query string
108     * @return resource MySQL result set
109     */
110    public function query($query) {
111        $result = mysql_query($query, $this->link);
112
113        $this->result = $result;
114
115        if ($result == false)
116            trigger_error('Uncovered an error in your SQL query script: "' . $this->error() . '"');
117
118        return $this->result;
119    }
120
121    /**
122     * Update the database
123     *
124     * @param array $values 3D array of fields and values to be updated
125     * @param string $table Table to update
126     * @param string $where Where condition
127     * @param string $limit Limit condition
128     * @return boolean Result
129     */
130    public function update(array $values, $table, $where = false, $limit = false) {
131        if (count($values) < 0)
132            return false;
133
134        $fields = array();
135        foreach ($values as $field => $val)
136            $fields[] = "`" . $field . "` = '" . $this->escapeString($val) . "'";
137
138        $where = ($where) ? " WHERE " . $where : '';
139        $limit = ($limit) ? " LIMIT " . $limit : '';
140
141        if ($this->query("UPDATE `" . $table . "` SET " . implode($fields, ", ") . $where . $limit))
142            return true;
143        else
144            return false;
145    }
146
147    /**
148     * Insert one new row
149     *
150     * @param array $values 3D array of fields and values to be inserted
151     * @param string $table Table to insert
152     * @return boolean Result
153     */
154    public function insert(array $values, $table) {
155        if (count($values) < 0)
156            return false;
157
158        foreach ($values as $field => $val)
159            $values[$field] = $this->escapeString($val);
160
161        if ($this->query("INSERT INTO `" . $table . "`(`" . implode(array_keys($values), "`, `") . "`) VALUES ('" . implode($values, "', '") . "')"))
162            return true;
163        else
164            return false;
165    }
166
167    /**
168     * Select
169     *
170     * @param mixed $fields Array or string of fields to retrieve
171     * @param string $table Table to retrieve from
172     * @param string $where Where condition
173     * @param string $orderby Order by clause
174     * @param string $limit Limit condition
175     * @return array Array of rows
176     */
177    public function select($fields, $table, $where = false, $orderby = false, $limit = false) {
178        if (is_array($fields))
179            $fields = "`" . implode($fields, "`, `") . "`";
180
181        $orderby = ($orderby) ? " ORDER BY " . $orderby : '';
182        $where = ($where) ? " WHERE " . $where : '';
183        $limit = ($limit) ? " LIMIT " . $limit : '';
184
185        $this->query("SELECT " . $fields . " FROM `" . $table . "`" . $where . $orderby . $limit);
186
187        if ($this->countRows() > 0) {
188            $rows = array();
189
190            while ($r = $this->fetchAssoc())
191                $rows[] = $r;
192
193            return $rows;
194        } else
195            return false;
196    }
197
198    /**
199     * Selects one row
200     *
201     * @param mixed $fields Array or string of fields to retrieve
202     * @param string $table Table to retrieve from
203     * @param string $where Where condition
204     * @param string $orderby Order by clause
205     * @return array Row values
206     */
207    public function selectOne($fields, $table, $where = false, $orderby = false) {
208        $result = $this->select($fields, $table, $where, $orderby, '1');
209
210        return $result[0];
211    }
212
213    /**
214     * Selects one value from one row
215     *
216     * @param mixed $field Name of field to retrieve
217     * @param string $table Table to retrieve from
218     * @param string $where Where condition
219     * @param string $orderby Order by clause
220     * @return array Field value
221     */
222    public function selectOneValue($field, $table, $where = false, $orderby = false) {
223        $result = $this->selectOne($field, $table, $where, $orderby);
224
225        return $result[$field];
226    }
227
228    /**
229     * Delete rows
230     *
231     * @param string $table Table to delete from
232     * @param string $where Where condition
233     * @param string $limit Limit condition
234     * @return boolean Result
235     */
236    public function delete($table, $where = false, $limit = 1) {
237        $where = ($where) ? " WHERE " . $where : '';
238        $limit = ($limit) ? " LIMIT " . $limit : '';
239
240        if ($this->query("DELETE FROM `" . $table . "`" . $where . $limit))
241            return true;
242        else
243            return false;
244    }
245
246    /**
247     * Fetch results by associative array
248     *
249     * @param mixed $query Select query or MySQL result
250     * @return array Row
251     */
252    public function fetchAssoc($query = false) {
253        $this->resCalc($query);
254        return mysql_fetch_assoc($query);
255    }
256
257    /**
258     * Fetch results by enumerated array
259     *
260     * @param mixed $query Select query or MySQL result
261     * @return array Row
262     */
263    public function fetchRow($query = false) {
264        $this->resCalc($query);
265        return mysql_fetch_row($query);
266    }
267
268    /**
269     * Fetch one row
270     *
271     * @param mixed $query Select query or MySQL result
272     * @return array
273     */
274    public function fetchOne($query = false) {
275        list($result) = $this->fetchRow($query);
276        return $result;
277    }
278
279    /**
280     * Fetch a field name in a result
281     *
282     * @param mixed $query Select query or MySQL result
283     * @param int $offset Field offset
284     * @return string Field name
285     */
286    public function fieldName($query = false, $offset) {
287        $this->resCalc($query);
288        return mysql_field_name($query, $offset);
289    }
290
291    /**
292     * Fetch all field names in a result
293     *
294     * @param mixed $query Select query or MySQL result
295     * @return array Field names
296     */
297    public function fieldNameArray($query = false) {
298        $names = array();
299
300        $field = $this->countFields($query);
301
302        for ($i = 0; $i < $field; $i++)
303            $names[] = $this->fieldName($query, $i);
304
305        return $names;
306    }
307
308    /**
309     * Free result memory
310     *
311     * @return boolean
312     */
313    public function freeResult() {
314        return mysql_free_result($this->result);
315    }
316
317    /**
318     * Add escape characters for importing data
319     *
320     * @param string $str String to parse
321     * @return string
322     */
323    public function escapeString($str) {
324        return mysql_real_escape_string($str, $this->link);
325    }
326
327    /**
328     * Count number of rows in a result
329     *
330     * @param mixed $result Select query or MySQL result
331     * @return int Number of rows
332     */
333    public function countRows($result = false) {
334        $this->resCalc($result);
335        return (int)mysql_num_rows($result);
336    }
337
338    /**
339     * Count number of fields in a result
340     *
341     * @param mixed $result Select query or MySQL result
342     * @return int Number of fields
343     */
344    public function countFields($result = false) {
345        $this->resCalc($result);
346        return (int)mysql_num_fields($result);
347    }
348
349    /**
350     * Get last inserted id of the last query
351     *
352     * @return int Inserted in
353     */
354    public function insertId() {
355        return (int)mysql_insert_id($this->link);
356    }
357
358    /**
359     * Get number of affected rows of the last query
360     *
361     * @return int Affected rows
362     */
363    public function affectedRows() {
364        return (int)mysql_affected_rows($this->link);
365    }
366
367    /**
368     * Get the error description from the last query
369     *
370     * @return string
371     */
372    public function error() {
373        return mysql_error($this->link);
374    }
375
376    /**
377     * Dump MySQL info to page
378     *
379     * @return void
380     */
381    public function dumpInfo() {
382        echo mysql_info($this->link);
383    }
384
385    /**
386     * Close the link connection
387     *
388     * @return boolean
389     */
390    public function close() {
391        return mysql_close($this->link);
392    }
393
394    /**
395     * Determine the data type of a query
396     *
397     * @param mixed $result Query string or MySQL result set
398     * @return void
399     */
400    private function resCalc(&$result) {
401        if ($result == false)
402            $result = $this->result;
403        else {
404            if (gettype($result) != 'resource')
405                $result = $this->query($result);
406        }
407
408        return;
409    }
410}