Skip to content

Commit 30a017e

Browse files
fix missing static method
1 parent d560e38 commit 30a017e

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javanile/hamper",
33
"description": "Developer friendly database library for vtiger",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"license": "MIT",
66
"type": "library",
77
"authors": [

src/Hamper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace Javanile\Hamper;
44

5+
56
class Hamper
67
{
78
/**
8-
* @var
9+
* @var HamperDatabase
910
*/
1011
protected static $instance;
1112

1213
/**
13-
*
14+
* Get singletone instance of database.
1415
*/
15-
public function getInstance()
16+
public static function getInstance()
1617
{
1718
if (self::$instance === null) {
1819
$pdb = \PearDatabase::getInstance();

src/HamperDatabase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,45 @@ public function fetchAll($sql, $params = [], $options = [])
155155
return $rows;
156156
}
157157

158+
/**
159+
* Inserts the given record within the selected table.
160+
*
161+
* @param string $table The table's name on which the insert has to be executed.
162+
* @param $key
163+
* @param $value
164+
* @param array $options Any additional option needed.
165+
*
166+
* @return bool
167+
* @throws HamperException
168+
* @usage query($sql, $params = [], $options = [])
169+
*/
170+
public function exists($table, $key, $value, $options=[])
171+
{
172+
$sql = "SELECT `${key}` FROM `{$table}` WHERE `${key}` = ? LIMIT 1";
173+
174+
$handler = OptionsHandlerFactory::createInstance($options);
175+
$results = $this->pearDatabase->pquery($sql, [$value], $handler->dieOnError, $handler->message);
176+
177+
if (!$results) {
178+
throw HamperException::forSqlError(array(
179+
'backtrace' => debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1),
180+
'pearDatabase' => $this->pearDatabase,
181+
//'sql' => $sql,
182+
//'params' => $params,
183+
//'dieOnError' => $handler->dieOnError,
184+
//'message' => $handler->message
185+
));
186+
}
187+
188+
try {
189+
return boolval($this->pearDatabase->query_result_rowdata($results));
190+
} catch (\Exception $e) {
191+
// The above HamperException prevent this legacy exception
192+
}
193+
194+
return false;
195+
}
196+
158197
/**
159198
* Inserts the given record within the selected table.
160199
*

tests/HamperDatabaseTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ public function testFetchAllFail()
6868
$this->hdb->fetchAll("POINTLESS BROKEN QUERY");
6969
}
7070

71+
public function testExists()
72+
{
73+
$data = [
74+
'field1' => md5(time().rand()).'1',
75+
'field2' => md5(time().rand()).'2',
76+
];
77+
78+
try {
79+
$this->hdb->query("CREATE TABLE IF NOT EXISTS test (field1 TEXT, field2 TEXT)");
80+
$res = $this->hdb->insert("test", $data);
81+
$this->assertTrue(is_object($res));
82+
$this->assertTrue($this->hdb->exists("test", "field1", $data['field1']));
83+
$this->assertFalse($this->hdb->exists("test", "field1", $data['field2']));
84+
} catch (HamperException $e) {
85+
die($e->getMessage());
86+
}
87+
}
88+
7189
public function testInsert()
7290
{
7391
$data = [

0 commit comments

Comments
 (0)