Skip to content

Commit 80b43b0

Browse files
committed
issue arangodb#235: add Statement::setFailOnWarning()
1 parent caafbc4 commit 80b43b0

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

lib/ArangoDBClient/Statement.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ class Statement
116116
* @var bool
117117
*/
118118
private $_cache;
119+
120+
/**
121+
* Whether or not the cache should abort when it encounters a warning
122+
*
123+
* @var bool
124+
*/
125+
private $_failOnWarning = false;
126+
127+
/**
128+
* Approximate memory limit value (in bytes) that a query can use on the server-side
129+
* (a value of 0 or less will mean the memory is not limited)
130+
*
131+
* @var int
132+
*/
133+
private $_memoryLimit = 0;
119134

120135
/**
121136
* resultType
@@ -149,6 +164,16 @@ class Statement
149164
* Bind variables index
150165
*/
151166
const ENTRY_BINDVARS = 'bindVars';
167+
168+
/**
169+
* Fail on warning flag
170+
*/
171+
const ENTRY_FAIL_ON_WARNING = 'failOnWarning';
172+
173+
/**
174+
* Memory limit threshold for query
175+
*/
176+
const ENTRY_MEMORY_LIMIT = 'memoryLimit';
152177

153178
/**
154179
* Full count option index
@@ -215,6 +240,14 @@ public function __construct(Connection $connection, array $data)
215240
if (isset($data[Cursor::ENTRY_CACHE])) {
216241
$this->_cache = (bool) $data[Cursor::ENTRY_CACHE];
217242
}
243+
244+
if (isset($data[self::ENTRY_FAIL_ON_WARNING])) {
245+
$this->_failOnWarning = (bool) $data[self::ENTRY_FAIL_ON_WARNING];
246+
}
247+
248+
if (isset($data[self::ENTRY_MEMORY_LIMIT])) {
249+
$this->_memoryLimit = (int) $data[self::ENTRY_MEMORY_LIMIT];
250+
}
218251
}
219252

220253
/**
@@ -430,7 +463,7 @@ public function setFullCount($value)
430463
{
431464
$this->_fullCount = (bool) $value;
432465
}
433-
466+
434467
/**
435468
* Get the full count option value of the statement
436469
*
@@ -462,6 +495,51 @@ public function getCache()
462495
{
463496
return $this->_cache;
464497
}
498+
499+
/**
500+
* Set whether or not the cache should abort when it encounters a warning
501+
*
502+
* @param bool $value - value for fail-on-warning
503+
*
504+
* @return void
505+
*/
506+
public function setFailOnWarning($value = true)
507+
{
508+
$this->_failOnWarning = (bool) $value;
509+
}
510+
511+
/**
512+
* Get the configured value for fail-on-warning
513+
*
514+
* @return bool - current value of fail-on-warning option
515+
*/
516+
public function getFailOnWarning()
517+
{
518+
return $this->_failOnWarning;
519+
}
520+
521+
/**
522+
* Set the approximate memory limit threshold to be used by the query on the server-side
523+
* (a value of 0 or less will mean the memory is not limited)
524+
*
525+
* @param int $value - value for memory limit
526+
*
527+
* @return void
528+
*/
529+
public function setMemoryLimit($value)
530+
{
531+
$this->_memoryLimit = (int) $value;
532+
}
533+
534+
/**
535+
* Get the configured memory limit for the statement
536+
*
537+
* @return int - current value of memory limit option
538+
*/
539+
public function getMemoryLimit()
540+
{
541+
return $this->_memoryLimit;
542+
}
465543

466544
/**
467545
* Set the batch size for the statement
@@ -510,14 +588,19 @@ private function buildData()
510588
self::ENTRY_QUERY => $this->_query,
511589
self::ENTRY_COUNT => $this->_doCount,
512590
'options' => [
513-
self::FULL_COUNT => $this->_fullCount
591+
self::FULL_COUNT => $this->_fullCount,
592+
self::ENTRY_FAIL_ON_WARNING => $this->_failOnWarning
514593
]
515594
];
516595

517596
if ($this->_cache !== null) {
518597
$data[Cursor::ENTRY_CACHE] = $this->_cache;
519598
}
520599

600+
if ($this->_memoryLimit > 0) {
601+
$data[self::ENTRY_MEMORY_LIMIT] = $this->_memoryLimit;
602+
}
603+
521604
if ($this->_bindVars->getCount() > 0) {
522605
$data[self::ENTRY_BINDVARS] = $this->_bindVars->getAll();
523606
}

tests/StatementTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,42 @@ public function testStatementReturnWarnings()
139139
$warnings = $cursor->getWarnings();
140140
static::assertEquals(1562, $warnings[0]['code']);
141141
}
142+
143+
/**
144+
* Test fail if a warning occurs during querying
145+
*/
146+
public function testStatementFailOnWarning()
147+
{
148+
$connection = $this->connection;
149+
150+
$statement = new Statement($connection, ['query' => 'RETURN 1/0']);
151+
$statement->setFailOnWarning();
152+
153+
try {
154+
$cursor = $statement->execute();
155+
static::assertTrue(false);
156+
} catch (ServerException $e) {
157+
static::assertEquals(1562, $e->getServerCode());
158+
}
159+
}
160+
161+
/**
162+
* Test fail with memory limit hit
163+
*/
164+
public function testStatementFailWithMemoryLimit()
165+
{
166+
$connection = $this->connection;
167+
168+
$statement = new Statement($connection, ['query' => 'FOR i IN 1..100000 RETURN CONCAT("test", TO_NUMBER(i))']);
169+
$statement->setMemoryLimit(10000);
170+
171+
try {
172+
$cursor = $statement->execute();
173+
static::assertTrue(false);
174+
} catch (ServerException $e) {
175+
static::assertEquals(32, $e->getServerCode());
176+
}
177+
}
142178

143179

144180
/**

0 commit comments

Comments
 (0)