Skip to content

Commit 9d05ca7

Browse files
committed
Initial import
0 parents  commit 9d05ca7

27 files changed

+2346
-0
lines changed

Column.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* The Column class represents one or many columns to be retrieved in a SQL Select statement
6+
*
7+
* @author David Negrier
8+
* @Component
9+
*/
10+
class Column implements SelectExpressionInterface {
11+
12+
/**
13+
* Optional table name
14+
*
15+
* @Property
16+
* @var string
17+
*/
18+
public $tableName;
19+
20+
/**
21+
* The name of the column.
22+
* Use "*" to retrieve all columns.
23+
*
24+
* @Property
25+
* @Compulsory
26+
* @var string
27+
*/
28+
public $columnName;
29+
30+
/**
31+
* Optional alias name
32+
*
33+
* @Property
34+
* @var string
35+
*/
36+
public $alias;
37+
38+
/**
39+
* Constructor
40+
*
41+
* @param string $tableName
42+
* @param string $columnName
43+
* @param string $alias
44+
*/
45+
public function __construct($tableName = null, $columnName = null, $alias = null) {
46+
$this->tableName = $tableName;
47+
$this->columnName = $columnName;
48+
$this->alias = $alias;
49+
}
50+
51+
/**
52+
* (non-PHPdoc)
53+
* @see database\querywriter.SqlRenderInterface::toSql()
54+
*/
55+
public function toSql(\DB_ConnectionInterface $dbConnection) {
56+
$sql = "";
57+
if ($this->tableName) {
58+
$sql .= $dbConnection->escapeDBItem($this->tableName).".";
59+
}
60+
if ($this->columnName != "*") {
61+
$sql .= $dbConnection->escapeDBItem($this->columnName);
62+
} else {
63+
$sql .= "*";
64+
}
65+
if ($this->alias) {
66+
$sql .= " AS ".$dbConnection->escapeDBItem($this->alias);
67+
}
68+
return $sql;
69+
}
70+
}

Join.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* Represents a LEFT JOIN, JOIN, RIGHT JOIN or OUTER JOIN in a SELECT query.
6+
*
7+
* @author David Negrier
8+
* @Component
9+
*/
10+
class Join implements TableReferenceInterface {
11+
/**
12+
* The left table in the join
13+
*
14+
* @Property
15+
* @Compulsory
16+
* @var TableReferenceInterface
17+
*/
18+
public $left;
19+
20+
/**
21+
* The right table in the join
22+
*
23+
* @Property
24+
* @Compulsory
25+
* @var TableReferenceInterface
26+
*/
27+
public $right;
28+
29+
/**
30+
* The ON condition.
31+
*
32+
* @Property
33+
* @Compulsory
34+
* @var filters\FilterInterface
35+
*/
36+
public $on;
37+
38+
/**
39+
* The kind of Join to apply
40+
*
41+
* @Property
42+
* @Compulsory
43+
* @OneOf ("LEFT JOIN", "JOIN", "RIGHT JOIN", "OUTER JOIN")
44+
* @var string
45+
*/
46+
public $joinType = "LEFT JOIN";
47+
48+
/**
49+
* (non-PHPdoc)
50+
* @see database\querywriter.SqlRenderInterface::toSql()
51+
*/
52+
public function toSql(\DB_ConnectionInterface $dbConnection) {
53+
$sql = "(".$this->left->toSql($dbConnection);
54+
$sql .= " ".$this->joinType." ";
55+
$sql .= $this->right->toSql($dbConnection);
56+
$sql .= " ON ";
57+
$sql .= $this->on->toSql($dbConnection);
58+
$sql .= ")";
59+
return $sql;
60+
}
61+
}

Select.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* The Select class represents a SQL Select statement
6+
*
7+
* @author David Negrier
8+
* @Component
9+
*/
10+
class Select {
11+
12+
/**
13+
* The connection to the database.
14+
*
15+
* @Property
16+
* @Compulsory
17+
* @var \DB_ConnectionInterface
18+
*/
19+
public $connection;
20+
21+
/**
22+
* If true, the DISTINCT keyword will be used with this select statement.
23+
*
24+
* @Property
25+
* @var bool
26+
*/
27+
public $distinct;
28+
29+
/**
30+
* This list of columns (or select expressions) that should be returned by the query.
31+
*
32+
* @Property
33+
* @Compulsory
34+
* @var array<SelectExpressionInterface>
35+
*/
36+
public $columns = array();
37+
38+
/**
39+
* One or many tables or joins to add to the select statement.
40+
* Advice: if you want to perform joins, create a JOIN first and put tables in the join.
41+
*
42+
* @Property
43+
* @Compulsory
44+
* @var TableReferenceInterface
45+
*/
46+
public $from;
47+
48+
/**
49+
* The where condition.
50+
*
51+
* @Property
52+
* @var filters\FilterInterface
53+
*/
54+
public $where;
55+
56+
/**
57+
* The list of fields to group on.
58+
*
59+
* @Property
60+
* @var array<OrderByGroupByElementInterface>
61+
*/
62+
public $groupBy = array();
63+
64+
/**
65+
* The having condition.
66+
*
67+
* @Property
68+
* @var filters\FilterInterface
69+
*/
70+
public $having;
71+
72+
/**
73+
* The list of fields to order on.
74+
*
75+
* @Property
76+
* @var array<OrderByGroupByElementInterface>
77+
*/
78+
public $orderBy = array();
79+
80+
/**
81+
* The limit number of fields to return.
82+
*
83+
* @Property
84+
* @var int
85+
*/
86+
public $limit;
87+
88+
/**
89+
* Start returning firlds from field number $offset.
90+
*
91+
* @Property
92+
* @var int
93+
*/
94+
public $offset;
95+
96+
/**
97+
* Returns the SELECT statement in a string.
98+
*
99+
* @return string
100+
*/
101+
public function toSql() {
102+
$sql = "SELECT ";
103+
if ($this->distinct) {
104+
$sql .= "DISTINCT ";
105+
}
106+
// Apply the toSql function on the columns
107+
$connection = $this->connection;
108+
109+
$columnsSql = array_map(function(SelectExpressionInterface $elem) use ($connection) {
110+
return $elem->toSql($connection);
111+
}, $this->columns);
112+
$sql .= implode(", ", $columnsSql);
113+
114+
$sql .= " FROM ";
115+
116+
$sql .= $this->from->toSql($connection);
117+
118+
if ($this->where) {
119+
$sql .= " WHERE ".$this->where->toSql($connection);
120+
}
121+
122+
return $sql;
123+
}
124+
}

SelectExpressionInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* Classes implementing this interface can be added in the columns list of a SQL statement.
6+
*
7+
* @author David
8+
*/
9+
interface SelectExpressionInterface extends SqlRenderInterface {
10+
11+
}

SqlRenderInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* Objects implementing SqlRenderInterface can be rendered with the toSql method.
6+
*
7+
* @author David Negrier
8+
*/
9+
interface SqlRenderInterface {
10+
11+
/**
12+
* Renders the object as a SQL string
13+
*
14+
* @param \DB_ConnectionInterface $dbConnection
15+
* @return string
16+
*/
17+
public function toSql(\DB_ConnectionInterface $dbConnection);
18+
}

TableReference.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* Represents a SQL table in a SELECT query.
6+
*
7+
* @author David Negrier
8+
* @Component
9+
*/
10+
class TableReference implements TableReferenceInterface {
11+
12+
/**
13+
* The table name.
14+
*
15+
* @Property
16+
* @Compulsory
17+
* @Important
18+
* @var string
19+
*/
20+
public $tableName;
21+
22+
/**
23+
* The alias.
24+
*
25+
* @Property
26+
* @Important
27+
* @var string
28+
*/
29+
public $alias;
30+
31+
/**
32+
* Renders the object as a SQL string
33+
* @return string
34+
*/
35+
public function toSql(\DB_ConnectionInterface $dbConnection) {
36+
$sql = $dbConnection->escapeDBItem($this->tableName);
37+
if ($this->alias) {
38+
$sql .= " AS ".$dbConnection->escapeDBItem($this->alias);
39+
}
40+
return $sql;
41+
}
42+
}

TableReferenceInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace database\querywriter;
3+
4+
/**
5+
* Represents a SQL table or JOIN clause to put in a WHERE SQL statement.
6+
*
7+
* @author David Negrier
8+
* @Component
9+
*/
10+
interface TableReferenceInterface extends SqlRenderInterface {
11+
12+
}

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "mouf/database.querywritter",
3+
"description": "TODO",
4+
"keywords": ["security", "user", "user management"],
5+
"homepage": "https://github.com/thecodingmachine/database.querywritter",
6+
"type": "library",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "David Négrier",
11+
"email": "[email protected]",
12+
"homepage": "http://mouf-php.com"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.3.0"
17+
},
18+
"autoload": {
19+
"psr-0": {
20+
"Mouf": "src/"
21+
}
22+
}
23+
}

database.png

7.67 KB
Loading

0 commit comments

Comments
 (0)