1414use PhpDb \Sql \Select ;
1515use PhpDb \Sql \TableIdentifier ;
1616
17+ use function count ;
18+ use function implode ;
19+ use function is_string ;
20+ use function preg_split ;
1721use function str_replace ;
22+ use function strpos ;
23+ use function substr ;
24+
25+ use const PREG_SPLIT_DELIM_CAPTURE ;
1826
1927class SqlProcessor
2028{
29+ private const IDENTIFIER_PATTERN
30+ = '/\b(?!(?:AS|AND|OR|BETWEEN)\b)([a-zA-Z_]\w*+(?:\.[a-zA-Z_]\w*+)*)(?!\s*\()/i ' ;
31+
2132 private string $ paramPrefix = '' ;
2233
2334 private int $ subselectCount = 0 ;
@@ -92,11 +103,68 @@ public function resolveTable(Select|string|TableIdentifier|null $table): string|
92103 return $ table ;
93104 }
94105
95- if ($ table instanceof TableIdentifier) {
96- return $ table ->resolveTable ($ this );
106+ if (! $ table instanceof TableIdentifier) {
107+ $ table = new TableIdentifier ($ table );
108+ }
109+
110+ return $ this ->resolveTableRef ($ table );
111+ }
112+
113+ public function resolveTableWithAlias (TableIdentifier $ ref ): string
114+ {
115+ $ resolved = $ this ->resolveTableRef ($ ref );
116+
117+ if ($ ref ->getAlias () !== null ) {
118+ $ resolved .= ' AS ' . $ this ->platform ->quoteIdentifier ($ ref ->getAlias ());
119+ }
120+
121+ return $ resolved ;
122+ }
123+
124+ public function getQuotedPrefix (TableIdentifier $ ref ): string
125+ {
126+ if ($ ref ->getAlias () !== null ) {
127+ return $ this ->platform ->quoteIdentifier ($ ref ->getAlias ())
128+ . $ this ->identifierSeparator ;
97129 }
98130
99- return (new TableIdentifier ($ table ))->resolveTable ($ this );
131+ return $ this ->resolveTableRef ($ ref ) . $ this ->identifierSeparator ;
132+ }
133+
134+ private function resolveTableRef (TableIdentifier $ ref ): string
135+ {
136+ $ table = $ ref ->getTable ();
137+
138+ if (is_string ($ table )) {
139+ return $ this ->platform ->quoteIdentifier (name: $ table , prefix: $ ref ->getSchema ());
140+ }
141+
142+ if ($ table instanceof Select) {
143+ return '( ' . $ this ->processSubSelect ($ table ) . ') ' ;
144+ }
145+
146+ $ pi = 0 ;
147+ return $ table ->toSql ($ this , '' , $ pi );
148+ }
149+
150+ public function renderQuotedIdentifiers (string $ part ): string
151+ {
152+ $ identifiers = preg_split (self ::IDENTIFIER_PATTERN , $ part , -1 , PREG_SPLIT_DELIM_CAPTURE );
153+ $ count = count ($ identifiers );
154+
155+ for ($ idx = 1 ; $ idx < $ count ; $ idx += 2 ) {
156+ $ dot = strpos ($ identifiers [$ idx ], '. ' );
157+ if ($ dot !== false ) {
158+ $ identifiers [$ idx ] = '" ' . substr ($ identifiers [$ idx ], 0 , $ dot ) . '"." ' . substr (
159+ $ identifiers [$ idx ],
160+ $ dot + 1
161+ ) . '" ' ;
162+ } else {
163+ $ identifiers [$ idx ] = '" ' . $ identifiers [$ idx ] . '" ' ;
164+ }
165+ }
166+
167+ return implode ('' , $ identifiers );
100168 }
101169
102170 public function renderTable (string $ table , ?string $ alias = null ): string
@@ -110,6 +178,7 @@ public function renderExpression(
110178 ): string {
111179 if ($ this ->parameterContainer === null ) {
112180 $ paramIndex = 0 ;
181+
113182 return $ expression ->toSql ($ this , '' , $ paramIndex );
114183 }
115184
0 commit comments