1 | <?php |
||
41 | class Builder extends ObjectAbstract implements BuilderInterface |
||
42 | { |
||
43 | use DialectAwareTrait, SettingsTrait, ParameterAwareTrait; |
||
44 | |||
45 | /** |
||
46 | * tables |
||
47 | * |
||
48 | * @var array |
||
49 | * @access protected |
||
50 | */ |
||
51 | protected $tables = []; |
||
52 | |||
53 | /** |
||
54 | * Constructor |
||
55 | * |
||
56 | * ```php |
||
57 | * // builder with default table `users` and Mysql dialect |
||
58 | * $users = new Builder('users', new Mysql()) |
||
59 | * |
||
60 | * // builder with defult tables: `users` and `accounts` AS `a` |
||
61 | * $builder = new Builder(['users', 'accounts' => 'a']) |
||
62 | * |
||
63 | * // change default settings |
||
64 | * $builder = new Builder('users', new Mysql(), ['autoQuote' => false]); |
||
65 | * ``` |
||
66 | * |
||
67 | * @param string|array $table table[s] to build upon |
||
68 | * @param DialectInterface $dialect default dialect is `Mysql` |
||
69 | * @param array $settings builder settings |
||
70 | * @access public |
||
71 | */ |
||
72 | public function __construct( |
||
73 | $table = '', |
||
74 | DialectInterface $dialect = null, |
||
75 | array $settings = [] |
||
76 | ) { |
||
77 | $this |
||
78 | ->initParameter() |
||
79 | ->setSettings(array_replace($this->defaultSettings(), $settings)) |
||
80 | ->setDialect($dialect) |
||
81 | ->table($table); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Change table[s] |
||
86 | * |
||
87 | * @param $table change to table[s] |
||
88 | * @return $this |
||
89 | * @access public |
||
90 | */ |
||
91 | public function __invoke($table) |
||
92 | { |
||
93 | return $this->table($table); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | public function expr()/*# : ExpressionInterface */ |
||
100 | { |
||
101 | return new Expression($this); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | public function raw(/*# string */ $string)/*# : OutputInterface */ |
||
108 | { |
||
109 | // values found |
||
110 | if (func_num_args() > 1) { |
||
111 | $values = func_get_arg(1); |
||
112 | $string = $this->getParameter() |
||
113 | ->replaceQuestionMark($string, $values); |
||
114 | } |
||
115 | return new Raw($string); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * If has existing tables, return a new instance with provided table[s] |
||
120 | * |
||
121 | * {@inheritDoc} |
||
122 | */ |
||
123 | public function table($table, /*# string */ $alias = '') |
||
130 | |||
131 | /** |
||
132 | * Append to existing tables |
||
133 | * |
||
134 | * {@inheritDoc} |
||
135 | */ |
||
136 | public function from($table, /*# string */ $alias = '') |
||
142 | |||
143 | /** |
||
144 | * {@inheritDoc} |
||
145 | */ |
||
146 | public function select( |
||
154 | |||
155 | /** |
||
156 | * Convert to [$table => alias] or [$table] |
||
157 | * |
||
158 | * @param string|string[] $table |
||
159 | * @param string $alias |
||
160 | * @return array |
||
161 | * @access protected |
||
162 | */ |
||
163 | protected function fixTable( |
||
174 | |||
175 | /** |
||
176 | * Builder default settings |
||
177 | * |
||
178 | * @return array |
||
179 | * @access protected |
||
180 | */ |
||
181 | protected function defaultSettings()/*# : array */ |
||
193 | } |
||
194 |