Created
March 8, 2014 12:12
-
-
Save eusonlito/9429576 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| Interface iDB | |
| { | |
| public function __construct ($opcions); | |
| public function gardar ($chave, $valor); | |
| public function recuperar ($chave); | |
| public function borrar ($chave); | |
| public function existe ($chave); | |
| } | |
| class Arquivo implements iDB | |
| { | |
| private $arquivo = ''; | |
| private $contido = array(); | |
| public function __construct ($arquivo) | |
| { | |
| $this->arquivo = $arquivo; | |
| if (!is_file($this->arquivo)) { | |
| if (@file_put_contents($this->arquivo, '') === false) { | |
| throw new Exception('Ups! non se puido crear o arquivo que nos indicas'); | |
| } | |
| } else if (!is_writable($this->arquivo)) { | |
| throw new Exception('Ups! o arquivo que nos indicas non é editable'); | |
| } | |
| $this->contido = unserialize(file_get_contents($this->arquivo)) ?: array(); | |
| } | |
| public function _store () | |
| { | |
| return (file_put_contents($this->arquivo, serialize($this->contido), LOCK_EX) !== false) ? true : false; | |
| } | |
| public function gardar ($chave, $valor) | |
| { | |
| $this->contido[$chave] = $valor; | |
| return $this->_store(); | |
| } | |
| public function recuperar ($chave) | |
| { | |
| return $this->existe($chave) ? $this->contido[$chave] : null; | |
| } | |
| public function borrar ($chave) | |
| { | |
| if ($this->existe($chave) === false) { | |
| return true; | |
| } | |
| unset($this->contido[$chave]); | |
| return $this->_store(); | |
| } | |
| public function existe ($chave) | |
| { | |
| return array_key_exists($chave, $this->contido); | |
| } | |
| } | |
| class BaseDatos implements iDB | |
| { | |
| private $PDO; | |
| public function __construct ($opcions) | |
| { | |
| try { | |
| $this->PDO = new PDO($opcions['driver'].':host='.$opcions['host'].';dbname='.$opcions['dbname'], $opcions['user'], $opcions['pass']); | |
| } catch (Exception $e) { | |
| throw new Exception('Ups! non se puido conectar a base de datos: '.$e->getMessage()); | |
| } | |
| $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| } | |
| private function _query ($q, $params) | |
| { | |
| try { | |
| $Q = $this->PDO->prepare($q); | |
| $Q->execute($params); | |
| return $Q; | |
| } catch (Exception $e) { | |
| throw new Exception('Ups! non se puido executar a operación: '.$e->getMessage()); | |
| } | |
| } | |
| public function gardar ($chave, $valor) | |
| { | |
| if ($this->existe($chave)) { | |
| $q = 'UPDATE datos SET valor = :valor WHERE chave = :chave LIMIT 1'; | |
| } else { | |
| $q = 'INSERT INTO datos SET valor = :valor, chave = :chave'; | |
| } | |
| $this->_query($q, array( | |
| ':chave' => $chave, | |
| ':valor' => $valor | |
| )); | |
| return true; | |
| } | |
| public function recuperar ($chave) | |
| { | |
| $Q = $this->_query('SELECT valor FROM datos WHERE chave = :chave LIMIT 1', array( | |
| ':chave' => $chave | |
| )); | |
| return $Q->fetchColumn(); | |
| } | |
| public function borrar ($chave) | |
| { | |
| $this->_query('DELETE FROM datos WHERE chave = :chave LIMIT 1', array( | |
| ':chave' => $chave | |
| )); | |
| return true; | |
| } | |
| public function existe ($chave) | |
| { | |
| $Q = $this->_query('SELECT id FROM datos WHERE chave = :chave LIMIT 1', array( | |
| ':chave' => $chave | |
| )); | |
| return $Q->fetch() ? true : false; | |
| } | |
| } | |
| for ($i = 1; $i <= 10; $i++) { | |
| echo '<pre>'; | |
| echo '<h1>Test '.$i.'</h1>'; | |
| $A = new Arquivo('/tmp/base-datos.dat'); | |
| $BD = new BaseDatos(array( | |
| 'driver' => 'mysql', | |
| 'host' => 'localhost', | |
| 'dbname' => 'test', | |
| 'user' => 'root', | |
| 'pass' => '' | |
| )); | |
| $chave = 'chave-'.rand(0, 5); | |
| $valor = substr(md5(microtime()), mt_rand(0, 10), 10); | |
| var_dump('A recuperar '.$chave.': '.$A->recuperar($chave)); | |
| var_dump('BD recuperar '.$chave.': '.$BD->recuperar($chave)); | |
| var_dump('A gardar '.$chave.': '.$A->gardar($chave, $valor)); | |
| var_dump('BD gardar '.$chave.': '.$BD->gardar($chave, $valor)); | |
| var_dump('A recuperar '.$chave.': '.$A->recuperar($chave)); | |
| var_dump('BD recuperar '.$chave.': '.$BD->recuperar($chave)); | |
| var_dump('A existe '.$chave.': '.$A->existe($chave)); | |
| var_dump('BD existe '.$chave.': '.$BD->existe($chave)); | |
| var_dump('A borrar '.$chave.': '.$A->borrar($chave)); | |
| var_dump('BD borrar '.$chave.': '.$BD->borrar($chave)); | |
| var_dump('A existe '.$chave.': '.$A->existe($chave)); | |
| var_dump('BD existe '.$chave.': '.$BD->existe($chave)); | |
| var_dump('A gardar '.$chave.': '.$A->gardar($chave, $valor)); | |
| var_dump('BD gardar '.$chave.': '.$BD->gardar($chave, $valor)); | |
| } | |
| echo '<h1>Reset</h1>'; | |
| for ($i = 0; $i <= 5; $i++) { | |
| var_dump('A borrar chave-'.$i.': '.$A->borrar('chave-'.$i)); | |
| var_dump('BD borrar chave-'.$i.': '.$BD->borrar('chave-'.$i)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment