diff --git a/composer.json b/composer.json index 54b85b6..bf591c4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "javanile/hamper", "description": "Developer friendly database library for vtiger", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "type": "library", "authors": [ diff --git a/src/Hamper.php b/src/Hamper.php index 10503f9..8e99db8 100644 --- a/src/Hamper.php +++ b/src/Hamper.php @@ -2,17 +2,18 @@ namespace Javanile\Hamper; + class Hamper { /** - * @var + * @var HamperDatabase */ protected static $instance; /** - * + * Get singletone instance of database. */ - public function getInstance() + public static function getInstance() { if (self::$instance === null) { $pdb = \PearDatabase::getInstance(); diff --git a/src/HamperDatabase.php b/src/HamperDatabase.php index ec58204..723dd6b 100644 --- a/src/HamperDatabase.php +++ b/src/HamperDatabase.php @@ -155,6 +155,45 @@ public function fetchAll($sql, $params = [], $options = []) return $rows; } + /** + * Inserts the given record within the selected table. + * + * @param string $table The table's name on which the insert has to be executed. + * @param $key + * @param $value + * @param array $options Any additional option needed. + * + * @return bool + * @throws HamperException + * @usage query($sql, $params = [], $options = []) + */ + public function exists($table, $key, $value, $options=[]) + { + $sql = "SELECT `${key}` FROM `{$table}` WHERE `${key}` = ? LIMIT 1"; + + $handler = OptionsHandlerFactory::createInstance($options); + $results = $this->pearDatabase->pquery($sql, [$value], $handler->dieOnError, $handler->message); + + if (!$results) { + throw HamperException::forSqlError(array( + 'backtrace' => debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1), + 'pearDatabase' => $this->pearDatabase, + //'sql' => $sql, + //'params' => $params, + //'dieOnError' => $handler->dieOnError, + //'message' => $handler->message + )); + } + + try { + return boolval($this->pearDatabase->query_result_rowdata($results)); + } catch (\Exception $e) { + // The above HamperException prevent this legacy exception + } + + return false; + } + /** * Inserts the given record within the selected table. * diff --git a/tests/HamperDatabaseTest.php b/tests/HamperDatabaseTest.php index fa3524c..af44759 100644 --- a/tests/HamperDatabaseTest.php +++ b/tests/HamperDatabaseTest.php @@ -68,6 +68,24 @@ public function testFetchAllFail() $this->hdb->fetchAll("POINTLESS BROKEN QUERY"); } + public function testExists() + { + $data = [ + 'field1' => md5(time().rand()).'1', + 'field2' => md5(time().rand()).'2', + ]; + + try { + $this->hdb->query("CREATE TABLE IF NOT EXISTS test (field1 TEXT, field2 TEXT)"); + $res = $this->hdb->insert("test", $data); + $this->assertTrue(is_object($res)); + $this->assertTrue($this->hdb->exists("test", "field1", $data['field1'])); + $this->assertFalse($this->hdb->exists("test", "field1", $data['field2'])); + } catch (HamperException $e) { + die($e->getMessage()); + } + } + public function testInsert() { $data = [