1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Dialect\Mysql; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Query\Traits\SettingsTrait; |
20
|
|
|
use Phossa2\Query\Traits\DialectAwareTrait; |
21
|
|
|
use Phossa2\Query\Interfaces\BuilderInterface; |
22
|
|
|
use Phossa2\Query\Interfaces\DialectInterface; |
23
|
|
|
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Builder |
27
|
|
|
* |
28
|
|
|
* @package Phossa2\Query |
29
|
|
|
* @author Hong Zhang <[email protected]> |
30
|
|
|
* @see ObjectAbstract |
31
|
|
|
* @see BuilderInterface |
32
|
|
|
* @see SelectStatementInterface |
33
|
|
|
* @version 2.0.0 |
34
|
|
|
* @since 2.0.0 added |
35
|
|
|
*/ |
36
|
|
|
class Builder extends ObjectAbstract implements BuilderInterface |
37
|
|
|
{ |
38
|
|
|
use DialectAwareTrait, SettingsTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* tables |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $tables = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor |
50
|
|
|
* |
51
|
|
|
* ```php |
52
|
|
|
* // builder with default table `users` and Mysql dialect |
53
|
|
|
* $users = new Builder('users', new Mysql()) |
54
|
|
|
* |
55
|
|
|
* // builder with defult tables: `users` and `accounts` AS `a` |
56
|
|
|
* $builder = new Builder(['users', 'accounts' => 'a']) |
57
|
|
|
* |
58
|
|
|
* // change default settings |
59
|
|
|
* $builder = new Builder('users', new Mysql(), ['autoQuote' => false]); |
60
|
|
|
* ``` |
61
|
|
|
* |
62
|
|
|
* @param string|array $table table[s] to build upon |
63
|
|
|
* @param DialectInterface $dialect default dialect is `Mysql` |
64
|
|
|
* @param array $settings builder settings |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
$table = '', |
69
|
|
|
DialectInterface $dialect = null, |
70
|
|
|
array $settings = [] |
71
|
|
|
) { |
72
|
|
|
$this |
73
|
|
|
->setSettings($settings) |
74
|
|
|
->setDialect($dialect) |
75
|
|
|
->table($table); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Change table[s] |
80
|
|
|
* |
81
|
|
|
* @param $table change to table[s] |
82
|
|
|
* @return $this |
83
|
|
|
* @access public |
84
|
|
|
*/ |
85
|
|
|
public function __invoke($table) |
86
|
|
|
{ |
87
|
|
|
return $this->table($table); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function expr()/*# : ExpressionInterface */ |
94
|
|
|
{ |
95
|
|
|
// @TODO |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function raw(/*# string */ $string)/*# : RawInterface */ |
102
|
|
|
{ |
103
|
|
|
// @TODO |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* If has existing tables, return a new instance with provided table[s] |
108
|
|
|
* |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function table($table, /*# string */ $alias = '') |
112
|
|
|
{ |
113
|
|
|
$tbl = $this->fixTable($table, $alias); |
114
|
|
|
$clone = [] === $this->tables ? $this : clone $this; |
115
|
|
|
$clone->tables = $tbl; |
116
|
|
|
return $clone; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Append to existing tables |
121
|
|
|
* |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function from($table, /*# string */ $alias = '') |
125
|
|
|
{ |
126
|
|
|
$tbl = $this->fixTable($table, $alias); |
127
|
|
|
$this->tables = array_merge($this->tables, $tbl); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
|
|
public function select( |
135
|
|
|
$col = '', |
136
|
|
|
/*# string */ $alias = '' |
137
|
|
|
)/*# : SelectStatementInterface */ { |
138
|
|
|
/* @var SelectStatementInterface $select */ |
139
|
|
|
$select = $this->getDialect()->select($this); |
140
|
|
|
return $select->table($this->tables)->col($col, $alias); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Convert to [$table => alias] or [$table] |
145
|
|
|
* |
146
|
|
|
* @param string|string[] $table |
147
|
|
|
* @param string $alias |
148
|
|
|
* @return array |
149
|
|
|
* @access protected |
150
|
|
|
*/ |
151
|
|
|
protected function fixTable( |
152
|
|
|
$table, |
153
|
|
|
/*# string */ $alias = '' |
154
|
|
|
)/*# : array */ { |
155
|
|
|
if (empty($table)) { |
156
|
|
|
$table = []; |
157
|
|
|
} elseif (!is_array($table)) { |
158
|
|
|
$table = empty($alias) ? [$table] : [$table => $alias]; |
159
|
|
|
} |
160
|
|
|
return $table; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|