Skip to content

Commit 92682ca

Browse files
committed
first commit
1 parent 83ec50e commit 92682ca

14 files changed

Lines changed: 2472 additions & 27 deletions

Compiler/DB2.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/* ===========================================================================
3+
* Opis Project
4+
* http://opis.io
5+
* ===========================================================================
6+
* Copyright 2013 Marius Sarca
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ============================================================================ */
20+
21+
namespace Opis\Database\Compiler;
22+
23+
use Opis\Database\SQL\Compiler;
24+
use Opis\Database\SQL\Query;
25+
26+
class DB2 extends Compiler
27+
{
28+
29+
/**
30+
* Compiles a SELECT query.
31+
*
32+
* @access public
33+
* @param \Opis\Database\SQL\Query $query Query object.
34+
* @return array
35+
*/
36+
37+
public function select(Query $query)
38+
{
39+
if($query->getLimit() === null)
40+
{
41+
// No limit so we can just execute a normal query
42+
return parent::select($query);
43+
}
44+
else
45+
{
46+
// There is a limit so we need to emulate the LIMIT/OFFSET clause with ANSI-SQL
47+
$order = trim($this->orderings($query->getOrderings()));
48+
if(empty($order))
49+
{
50+
$order = 'ORDER BY (SELECT 0)';
51+
}
52+
$sql = $query->isDistinct() ? 'SELECT DISTINCT ' : 'SELECT ';
53+
$sql .= $this->columns($query->getColumns());
54+
$sql .= ', ROW_NUMBER() OVER (' . $order . ') AS opis_rownum';
55+
$sql .= ' FROM ';
56+
$sql .= $this->wrap($query->getTable());
57+
$sql .= $this->joins($query->getJoins());
58+
$sql .= $this->wheres($query->getWheres());
59+
$sql .= $this->groupings($query->getGroupings());
60+
$sql .= $this->havings($query->getHavings());
61+
$offset = ($query->getOffset() === null) ? 0 : $query->getOffset();
62+
$limit = $offset + $query->getLimit();
63+
$offset = $offset + 1;
64+
$sql = 'SELECT * FROM (' . $sql . ') AS m1 WHERE opis_rownum BETWEEN ' . $offset . ' AND ' . $limit;
65+
return array('sql' => $sql, 'params' => $this->params);
66+
}
67+
}
68+
}

Compiler/Firebird.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/* ===========================================================================
3+
* Opis Project
4+
* http://opis.io
5+
* ===========================================================================
6+
* Copyright 2013 Marius Sarca
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ============================================================================ */
20+
21+
namespace Opis\Database\Compiler;
22+
23+
use Opis\Database\SQL\Compiler;
24+
use Opis\Database\SQL\Query;
25+
26+
class Firebird extends Compiler
27+
{
28+
/**
29+
* Compiles LIMIT clauses.
30+
*
31+
* @access protected
32+
* @param int $limit Limit
33+
* @param int $offset Offset
34+
* @return string
35+
*/
36+
37+
protected function limit($limit, $offset = null)
38+
{
39+
return ($limit === null) ? '' : ' TO ' . ($limit + (($offset === null) ? 0 : $offset));
40+
}
41+
42+
/**
43+
* Compiles OFFSET clause.
44+
*
45+
* @access protected
46+
* @param int $limit Offset
47+
* @param int $offset Limit
48+
* @return string
49+
*/
50+
51+
protected function offset($offset, $limit = null)
52+
{
53+
return ($offset === null) ? ($limit === null) ? '' :' ROWS 1 ' : ' ROWS ' . ($offset + 1);
54+
}
55+
56+
/**
57+
* Compiles a SELECT query.
58+
*
59+
* @access public
60+
* @param \Opis\Database\SQL\Query $query Query object.
61+
* @return array
62+
*/
63+
64+
public function select(Query $query)
65+
{
66+
$sql = $query->isDistinct() ? 'SELECT DISTINCT ' : 'SELECT ';
67+
$sql .= $this->columns($query->getColumns());
68+
$sql .= ' FROM ';
69+
$sql .= $this->wrap($query->getTable());
70+
$sql .= $this->joins($query->getJoins());
71+
$sql .= $this->wheres($query->getWheres());
72+
$sql .= $this->groupings($query->getGroupings());
73+
$sql .= $this->orderings($query->getOrderings());
74+
$sql .= $this->havings($query->getHavings());
75+
$sql .= $this->offset($query->getOffset(), $query->getLimit());
76+
$sql .= $this->limit($query->getLimit(), $query->getOffset());
77+
return array('sql' => $sql, 'params' => $this->params);
78+
}
79+
}

Compiler/MySQL.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/* ===========================================================================
3+
* Opis Project
4+
* http://opis.io
5+
* ===========================================================================
6+
* Copyright 2013 Marius Sarca
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ============================================================================ */
20+
21+
namespace Opis\Database\Compiler;
22+
23+
use Opis\Database\SQL\Compiler;
24+
25+
class MySQL extends Compiler
26+
{
27+
28+
/** @var string Wrapper used to escape table and column names. */
29+
protected $wrapper = '`%s`';
30+
31+
}

Compiler/NuoDB.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/* ===========================================================================
3+
* Opis Project
4+
* http://opis.io
5+
* ===========================================================================
6+
* Copyright 2013 Marius Sarca
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ============================================================================ */
20+
21+
namespace Opis\Database\Compiler;
22+
23+
use Opis\Database\SQL\Compiler;
24+
25+
class NuoDB extends Compiler
26+
{
27+
28+
/** @var string Wrapper used to escape table and column names. */
29+
protected $wrapper = '"%s"';
30+
31+
/**
32+
* Compiles LIMIT clauses.
33+
*
34+
* @access protected
35+
* @param int $limit Limit
36+
* @return string
37+
*/
38+
39+
protected function limit($limit)
40+
{
41+
return ($limit === null) ? '' : ' FETCH ' . $limit;
42+
}
43+
44+
/**
45+
* Compiles OFFSET clauses.
46+
*
47+
* @access protected
48+
* @param int $offset Limit
49+
* @return string
50+
*/
51+
52+
protected function offset($offset)
53+
{
54+
return ($offset === null) ? '' : ' OFFSET ' . $offset;
55+
}
56+
}

Compiler/Oracle.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/* ===========================================================================
3+
* Opis Project
4+
* http://opis.io
5+
* ===========================================================================
6+
* Copyright 2013 Marius Sarca
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ============================================================================ */
20+
21+
namespace Opis\Database\Compiler;
22+
23+
use Opis\Database\SQL\Compiler;
24+
use Opis\Database\SQL\Query;
25+
26+
class Oracle extends Compiler
27+
{
28+
29+
/**
30+
* Compiles a SELECT query.
31+
*
32+
* @access public
33+
* @param \Opis\Database\SQL\Query $query Query object.
34+
* @return array
35+
*/
36+
37+
public function select(Query $query)
38+
{
39+
if($query->getLimit() === null)
40+
{
41+
// No limit so we can just execute a normal query
42+
return parent::select($query);
43+
}
44+
else
45+
{
46+
$sql = $query->isDistinct() ? 'SELECT DISTINCT ' : 'SELECT ';
47+
$sql .= $this->columns($query->getColumns());
48+
$sql .= ' FROM ';
49+
$sql .= $this->wrap($query->getTable());
50+
$sql .= $this->joins($query->getJoins());
51+
$sql .= $this->wheres($query->getWheres());
52+
$sql .= $this->groupings($query->getGroupings());
53+
$sql .= $this->orderings($query->getOrderings());
54+
$sql .= $this->havings($query->getHavings());
55+
if($query->getOffset() === null)
56+
{
57+
// No offset so we only need a simple subquery to emulate the LIMIT clause
58+
$sql = 'SELECT m1.* FROM (' . $sql . ') m1 WHERE rownum <= ' . $query->getLimit();
59+
}
60+
else
61+
{
62+
// There is an offset so we need to make a bunch of subqueries to emulate the LIMIT and OFFSET clauses
63+
$limit = $query->getLimit() + $query->getOffset();
64+
$offset = $query->getOffset() + 1;
65+
$sql = 'SELECT * FROM (SELECT m1.*, rownum AS opis_rownum FROM (' . $sql . ') m1 WHERE rownum <= ' . $limit . ') WHERE opis_rownum >= ' . $offset;
66+
}
67+
return array('sql' => $sql, 'params' => $this->params);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)