88 */
99namespace SqlParser \Utils ;
1010
11+ use SqlParser \Lexer ;
1112use SqlParser \Parser ;
1213use SqlParser \Statement ;
1314use SqlParser \Token ;
@@ -406,32 +407,6 @@ public static function getAll($query)
406407 return $ ret ;
407408 }
408409
409- /**
410- * Gets the type of clause.
411- *
412- * @param string $clause The clause.
413- *
414- * @return string
415- */
416- public static function getClauseType ($ clause )
417- {
418- $ type = '' ;
419- for ($ i = 0 , $ len = strlen ($ clause ); $ i < $ len ; ++$ i ) {
420- if ((empty ($ type )) && (ctype_space ($ clause [$ i ]))) {
421- // Skipping whitespaces if we haven't started determining the
422- // type.
423- continue ;
424- }
425- if (!ctype_alnum ($ clause [$ i ])) {
426- // The type contains only alphanumeric characters.
427- break ;
428- }
429- // Adding character.
430- $ type .= $ clause [$ i ];
431- }
432- return $ type ;
433- }
434-
435410 /**
436411 * Gets a specific clause.
437412 *
@@ -474,11 +449,23 @@ public static function getClause($statement, $list, $clause, $type = 0, $skipFir
474449 */
475450 $ clauses = array_flip (array_keys ($ statement ::$ CLAUSES ));
476451
452+ /**
453+ * Lexer used for lexing the clause.
454+ * @var Lexer
455+ */
456+ $ lexer = new Lexer ($ clause );
457+
458+ /**
459+ * The type of this clause.
460+ * @var string
461+ */
462+ $ clauseType = $ lexer ->list ->getNextOfType (Token::TYPE_KEYWORD )->value ;
463+
477464 /**
478465 * The index of this clause.
479466 * @var int
480467 */
481- $ clauseIdx = $ clauses [static :: getClauseType ( $ clause ) ];
468+ $ clauseIdx = $ clauses [$ clauseType ];
482469
483470 for ($ i = $ statement ->first ; $ i <= $ statement ->last ; ++$ i ) {
484471 $ token = $ list ->tokens [$ i ];
@@ -493,16 +480,15 @@ public static function getClause($statement, $list, $clause, $type = 0, $skipFir
493480
494481 if ($ brackets == 0 ) {
495482 // Checking if we changed sections.
496- if ($ token ->type === Token::TYPE_KEYWORD ) {
497- if (isset ($ clauses [$ token ->value ])) {
498- if ($ clauses [$ token ->value ] >= $ currIdx ) {
499- $ currIdx = $ clauses [$ token ->value ];
500- if (($ skipFirst ) && ($ currIdx == $ clauseIdx )) {
501- // This token is skipped (not added to the old
502- // clause) because it will be replaced.
503- continue ;
504- }
505- }
483+ if (($ token ->type === Token::TYPE_KEYWORD )
484+ && (isset ($ clauses [$ token ->value ]))
485+ && ($ clauses [$ token ->value ] >= $ currIdx )
486+ ) {
487+ $ currIdx = $ clauses [$ token ->value ];
488+ if (($ skipFirst ) && ($ currIdx == $ clauseIdx )) {
489+ // This token is skipped (not added to the old
490+ // clause) because it will be replaced.
491+ continue ;
506492 }
507493 }
508494 }
@@ -526,25 +512,30 @@ public static function getClause($statement, $list, $clause, $type = 0, $skipFir
526512 *
527513 * @param Statement $statement The parsed query that has to be modified.
528514 * @param TokensList $list The list of tokens.
529- * @param string $clause The clause to be replaced.
515+ * @param string $old The type of the clause that should be
516+ * replaced. This can be an entire clause.
517+ * @param string $new The new clause. If this parameter is omitted
518+ * it is considered to be equal with `$old`.
530519 * @param bool $onlyType Whether only the type of the clause should
531520 * be replaced or the entire clause.
532521 *
533522 * @return string
534523 */
535- public static function replaceClause ($ statement , $ list , $ clause , $ onlyType = false )
524+ public static function replaceClause ($ statement , $ list , $ old , $ new = null , $ onlyType = false )
536525 {
537526 // TODO: Update the tokens list and the statement.
538527
528+ if ($ new === null ) {
529+ $ new = $ old ;
530+ }
531+
539532 if ($ onlyType ) {
540- return static ::getClause ($ statement , $ list , $ clause , -1 , false ) . ' ' .
541- $ clause . ' ' .
542- static ::getCLause ($ statement , $ list , $ clause , 0 ) . ' ' .
543- static ::getClause ($ statement , $ list , $ clause , 1 , false );
533+ return static ::getClause ($ statement , $ list , $ old , -1 , false ) . ' ' .
534+ $ new . ' ' . static ::getCLause ($ statement , $ list , $ old , 0 ) . ' ' .
535+ static ::getClause ($ statement , $ list , $ old , 1 , false );
544536 }
545537
546- return static ::getClause ($ statement , $ list , $ clause , -1 , false ) . ' ' .
547- $ clause . ' ' .
548- static ::getClause ($ statement , $ list , $ clause , 1 , false );
538+ return static ::getClause ($ statement , $ list , $ old , -1 , false ) . ' ' .
539+ $ new . ' ' . static ::getClause ($ statement , $ list , $ old , 1 , false );
549540 }
550541}
0 commit comments