Skip to content

Commit

Permalink
fix missing static method
Browse files Browse the repository at this point in the history
  • Loading branch information
francescobianco committed Jun 22, 2020
1 parent d560e38 commit 30a017e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
7 changes: 4 additions & 3 deletions src/Hamper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions src/HamperDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/HamperDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 30a017e

Please sign in to comment.