Overview

Namespaces

  • Flea
  • None

Classes

  • Flea
  • Flea\BuildUtil
  • Flea\Cache
  • Flea\DataBase
  • Flea\DataBaseCRUD
  • Flea\DataList
  • Flea\DataUtil
  • Flea\Debug
  • Flea\FileUtil
  • Flea\General
  • Flea\Header
  • Flea\InitUtil
  • Flea\LangList
  • Flea\Login
  • Flea\LoginFormHelper
  • Flea\LoginTableName
  • Flea\LoginUser
  • Flea\Page
  • Flea\PageList
  • Flea\PageListCreate
  • Flea\SqlQuery
  • Flea\TagUtil
  • Flea\UrlUtil
  • Flea\ValueObject
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * The MIT License
  5:  *
  6:  * Copyright 2014 Damien Doussaud (namide.com).
  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
 16:  * all 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
 24:  * THE SOFTWARE.
 25:  */
 26: 
 27: namespace Flea;
 28: 
 29: /**
 30:  * Base object used to generate a save of object.
 31:  * With this saving you can instantiate the same object.
 32:  *
 33:  * @author Namide
 34:  */
 35: class DataBase {
 36: 
 37:     private static $_INSTANCE = array();
 38:     private $_pdo;
 39: 
 40:     /**
 41:      * Analyzes the object and obtains his table 's name for the data base.
 42:      * 
 43:      * @param object $obj       Object to analyze
 44:      * @return string           Name of the table for this object
 45:      */
 46:     public static function objectToTableName($obj) {
 47:         return stripslashes(get_class($obj));
 48:     }
 49: 
 50:     /**
 51:      * Count the lines in a table
 52:      * 
 53:      * @param SqlQuery $query   Conditions (where, table...) of the request
 54:      * @return int              Number of lines
 55:      */
 56:     public function count(SqlQuery $query) {
 57:         try {
 58:             $stmt = $this->_pdo->prepare($query->getRequest());
 59: 
 60:             if ($stmt) {
 61:                 $binds = $query->getBinds();
 62:                 if ($binds !== null) {
 63:                     foreach ($binds as $bind) {
 64:                         $stmt->bindValue($bind[0], $bind[1], $bind[2]);
 65:                     }
 66:                 }
 67: 
 68:                 $stmt->execute();
 69:                 $count = $stmt->fetchColumn();
 70:                 $stmt = null;
 71:                 return $count;
 72:             }
 73: 
 74:             $stmt = null;
 75:             return 0;
 76:         } catch (PDOException $e) {
 77:             return 0;
 78:         }
 79:     }
 80: 
 81:     /**
 82:      * Try if the table exist
 83:      * 
 84:      * @param type $tableName   Name of the table
 85:      * @return boolean          It exist?
 86:      */
 87:     public function exist($tableName) {
 88: 
 89:         try {
 90:             $sql = 'SELECT 1 FROM `' . $tableName . '` LIMIT 1';
 91: 
 92:             $att = $this->_pdo->getAttribute(\PDO::ATTR_ERRMODE);
 93:             $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
 94:             $result = $this->_pdo->query($sql);
 95:             $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, $att);
 96:         } catch (PDOException $e) {
 97:             return false;
 98:         }
 99: 
100:         return ($result !== false);
101:     }
102: 
103:     /**
104:      * Execute a request on the database
105:      * 
106:      * @param SqlQuery $query   Conditions (where, table...) of the request
107:      * @return boolean          method is well executed
108:      */
109:     public function execute(SqlQuery $query) {
110:         try {
111:             $stmt = $this->_pdo->prepare($query->getRequest());
112: 
113:             if ($query->getType() === SqlQuery::$TYPE_MULTI_INSERT) {
114:                 $stmt->execute($query->getBinds());
115:             } else {
116:                 $binds = $query->getBinds();
117:                 if ($binds !== null) {
118:                     foreach ($binds as $bind) {
119:                         $stmt->bindValue($bind[0], $bind[1], $bind[2]);
120:                     }
121:                 }
122: 
123:                 $stmt->execute();
124:             }
125: 
126:             $stmt = null;
127: 
128:             return true;
129:         } catch (PDOException $e) {
130:             if (_DEBUG) {
131:                 Debug::getInstance()->addError('Execution database error: ' . $e->getMessage());
132:             }
133:         }
134:         return false;
135:     }
136: 
137:     /**
138:      * Get the first entry of the request
139:      * 
140:      * @param SqlQuery $query   Conditions (where, table...) of the request
141:      * @return array            The entriy
142:      */
143:     public function fetch(SqlQuery $query) {
144:         try {
145:             $stmt = $this->_pdo->prepare($query->getRequest());
146: 
147:             if ($stmt === false) {
148:                 return array();
149:             }
150: 
151:             $binds = $query->getBinds();
152:             if ($binds !== null) {
153:                 foreach ($binds as $bind) {
154:                     $stmt->bindValue($bind[0], $bind[1], $bind[2]);
155:                 }
156:             }
157:             $stmt->execute();
158:             $arrValues = $stmt->fetch(\PDO::FETCH_ASSOC);
159:             $stmt = null;
160: 
161:             return $arrValues;
162:         } catch (PDOException $e) {
163:             if (_DEBUG) {
164:                 Debug::getInstance()->addError('fetch_all() database error: ' . $e->getMessage());
165:             }
166:         }
167:         return array();
168:     }
169: 
170:     /**
171:      * Get all the entries of the request
172:      * 
173:      * @param SqlQuery $query   Conditions (where, table...) of the request
174:      * @return array            All the entries
175:      */
176:     public function fetchAll(SqlQuery $query) {
177:         try {
178:             $stmt = $this->_pdo->prepare($query->getRequest());
179: 
180:             if ($stmt === false) {
181:                 return array();
182:             }
183: 
184:             $binds = $query->getBinds();
185:             if ($binds !== null) {
186:                 foreach ($binds as $bind) {
187:                     $stmt->bindValue($bind[0], $bind[1], $bind[2]);
188:                 }
189:             }
190:             $stmt->execute();
191:             $arrValues = $stmt->fetchAll(\PDO::FETCH_ASSOC);
192:             $stmt = null;
193: 
194:             return $arrValues;
195:         } catch (PDOException $e) {
196:             if (_DEBUG) {
197:                 Debug::getInstance()->addError('fetch_all() database error: ' . $e->getMessage());
198:             }
199:         }
200:         return array();
201:     }
202: 
203:     /**
204:      * Use the static method getInstance() to construct this object
205:      * 
206:      * @param string $dsn       Database source name
207:      */
208:     private function __construct($dsn) {
209:         try {
210:             $this->_pdo = new \PDO($dsn, _DB_USER, _DB_PASS, _DB_OPTIONS);
211:             $this->_multiReqNum = 0;
212: 
213:             if (_DEBUG) {
214:                 $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
215:             } else {
216:                 $this->_pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
217:             }
218:         } catch (PDOException $e) {
219:             if (_DEBUG) {
220:                 Debug::getInstance()->addError('Initialize database error: '
221:                         . $e->getMessage());
222:             }
223:         }
224:     }
225: 
226:     /**
227:      * Get the database.
228:      * This multiton avoids to open/close untimely the database.
229:      * 
230:      * @param string $dsn       Database source name
231:      * @return DataBase         The DataBase
232:      */
233:     public static function getInstance($dsn) {
234:         if (!isset(self::$_INSTANCE[$dsn])) {
235:             self::$_INSTANCE[$dsn] = new DataBase($dsn);
236:         }
237:         return self::$_INSTANCE[$dsn];
238:     }
239: 
240:     private function __clone() {
241:         
242:     }
243: 
244: }
245: 
Flea API documentation generated by ApiGen