Skip to content

Commit 691c784

Browse files
author
Kolya Siryk
committed
add limit and offset to query
1 parent 3bfc39a commit 691c784

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

src/sokyrko/yii/salesforce/data/ActiveQuery.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace sokyrko\yii\salesforce\data;
1010

11-
use Akeneo\SalesForce\Connector\SalesForceClient;
12-
use Akeneo\SalesForce\Query\QueryBuilder;
1311
use sokyrko\yii\salesforce\components\SalesforceComponent;
1412
use yii\base\InvalidCallException;
1513
use yii\base\InvalidParamException;
@@ -43,6 +41,7 @@ class ActiveQuery implements ActiveQueryInterface
4341
'where' => [],
4442
'orderBy' => [],
4543
'limit' => '',
44+
'offset' => '',
4645
];
4746

4847
/**
@@ -95,6 +94,8 @@ protected function parseSelect($what)
9594
public function asArray($value = true)
9695
{
9796
$this->asArray = $value;
97+
98+
return $this;
9899
}
99100

100101
/**
@@ -247,7 +248,15 @@ public function getRawQuery()
247248
$q->orderBy($this->parseOrderBy($this->queryParts['orderBy']));
248249
}
249250

250-
// TODO: support limit and join
251+
if ($this->queryParts['limit']) {
252+
$q->limit($this->queryParts['limit']);
253+
}
254+
255+
if ($this->queryParts['offset']) {
256+
$q->offset($this->queryParts['offset']);
257+
}
258+
259+
// TODO: support join
251260

252261
return $q->getQuery();
253262
}
@@ -527,7 +536,7 @@ public function addOrderBy($columns)
527536
*/
528537
public function limit($limit)
529538
{
530-
$this->queryParts['limit'][] = $limit;
539+
$this->queryParts['limit'] = $limit;
531540

532541
return $this;
533542
}
@@ -540,7 +549,9 @@ public function limit($limit)
540549
*/
541550
public function offset($offset)
542551
{
543-
throw new InvalidCallException('Not implemented yet.');
552+
$this->queryParts['offset'] = $offset;
553+
554+
return $this;
544555
}
545556

546557
/**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace sokyrko\yii\salesforce\data;
4+
5+
/**
6+
* Class QueryBuilder
7+
*
8+
* @package sokyrko\yii\salesforce\data
9+
*/
10+
class QueryBuilder extends \Akeneo\SalesForce\Query\QueryBuilder
11+
{
12+
/**
13+
* @param int $limit
14+
*
15+
* @return $this
16+
*/
17+
public function limit($limit)
18+
{
19+
$this->query = sprintf('%s LIMIT %s', $this->query, $limit);
20+
21+
return $this;
22+
}
23+
24+
/**
25+
* @param int $offset
26+
*
27+
* @return $this
28+
*/
29+
public function offset($offset)
30+
{
31+
$this->query = sprintf('%s OFFSET %s', $this->query, $offset);
32+
33+
return $this;
34+
}
35+
}

tests/unit/ActiveQueryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,20 @@ public function testWhereAndOrderBy()
7171
Airport::find()->where(['Name' => 'Test'])->orderBy(['Id' => SORT_ASC])->getRawQuery()
7272
);
7373
}
74+
75+
public function testLimit()
76+
{
77+
Assert::assertEquals(
78+
'SELECT Id, Name FROM Airport__c LIMIT 100',
79+
Airport::find()->limit(100)->getRawQuery()
80+
);
81+
}
82+
83+
public function testOffset()
84+
{
85+
Assert::assertEquals(
86+
'SELECT Id, Name FROM Airport__c OFFSET 100',
87+
Airport::find()->offset(100)->getRawQuery()
88+
);
89+
}
7490
}

0 commit comments

Comments
 (0)