Exemplo n.º 1
0
 /**
  * Parses a string into a SQL expression and it's alias
  *
  * @param String $name The SQL string to parse
  * @return array Returns an array where the first element is the
  *      SQL expression and the second is the alias
  */
 public static function parseSQLAlias($string)
 {
     $string = (string) $string;
     // If there is no obvious alias, take an easy out
     if (!\r8\str\contains(" AS ", $string)) {
         $alias = null;
     } else {
         if (!\r8\str\contains("`", $string)) {
             list($string, $alias) = explode(" AS ", $string, 2);
             $alias = trim($alias);
         } else {
             $parser = new \r8\Quoter();
             list($string, $alias) = $parser->clearQuotes()->setQuote("`")->parse($string)->setIncludeQuoted(FALSE)->explode(" AS ");
             $alias = trim($alias);
         }
     }
     $string = trim($string);
     if (\r8\IsEmpty($string)) {
         $string = null;
     }
     $alias = \r8\str\stripW($alias);
     if (\r8\IsEmpty($alias)) {
         $alias = null;
     }
     return array($string, $alias);
 }
Exemplo n.º 2
0
 /**
  * Cleans up a SQL query for comparison
  *
  * @param String $sql The SQL to clean
  * @return String
  */
 public static function cleanSQL($sql)
 {
     $quoter = new \r8\Quoter();
     $quoter->setQuote('"')->setQuote("'")->setQuote("`");
     $parsed = $quoter->parse($sql);
     $parsed->setIncludeQuoted(FALSE)->setIncludeUnquoted(TRUE);
     $keywords = '/\\b(?:' . implode("|", self::$keywords) . ')\\b/i';
     $breaks = '/\\b(' . implode("|", self::$breaks) . ')\\b/i';
     $parsed->filter(new \r8\Filter\Chain(r8(new \r8\Curry\Call('str_replace'))->setLeft(array("\n", "\r"), " "), r8(new \r8\Curry\Call('\\r8\\str\\stripRepeats'))->setRight(" "), r8(new \r8\Curry\Call('preg_replace_callback'))->setLeft($keywords, function ($value) {
         return strtoupper($value[0]);
     }), r8(new \r8\Curry\Call('preg_replace'))->setLeft($breaks, "\n\\1")));
     return trim($parsed->__toString(), " ;");
 }
Exemplo n.º 3
0
 public function testFilter()
 {
     $list = new \r8\Quoter();
     $parsed = $list->parse("string 'with' quotes")->filter(new \r8\Curry\Call("strtoupper"))->__toString();
     $this->assertSame("STRING 'WITH' QUOTES", $parsed);
     $parsed = $list->parse("string 'with' quotes")->setIncludeQuoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString();
     $this->assertSame("STRING 'with' QUOTES", $parsed);
     $parsed = $list->parse("string 'with' quotes")->setIncludeUnquoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString();
     $this->assertSame("string 'WITH' quotes", $parsed);
     $parsed = $list->parse("string 'with' quotes")->setIncludeUnquoted(FALSE)->setIncludeQuoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString();
     $this->assertSame("string 'with' quotes", $parsed);
 }
Exemplo n.º 4
0
 public function testParse_oddQuotes()
 {
     $quoter = new \r8\Quoter();
     $quoter->clearQuotes()->setQuote("<({", array("END OF QUOTE", "))"));
     $result = $quoter->parse("<({This isEND OF QUOTE a string <({with stuff)) in it");
     $this->assertThat($result, $this->isInstanceOf("r8\\Quoter\\Parsed"));
     $this->assertSame(array("<({This isEND OF QUOTE", " a string ", "<({with stuff))", " in it"), array_map("strval", $result->getSections()));
     $offset = $result->getSections();
     $offset = $offset[0];
     $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Quoted"));
     $this->assertSame("This is", $offset->getContent());
     $this->assertSame("<({", $offset->getOpenQuote());
     $this->assertSame("END OF QUOTE", $offset->getCloseQuote());
     $offset = $result->getSections();
     $offset = $offset[1];
     $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Unquoted"));
     $this->assertSame(" a string ", $offset->getContent());
     $offset = $result->getSections();
     $offset = $offset[2];
     $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Quoted"));
     $this->assertSame("with stuff", $offset->getContent());
     $this->assertSame("<({", $offset->getOpenQuote());
     $this->assertSame("))", $offset->getCloseQuote());
     $offset = $result->getSections();
     $offset = $offset[3];
     $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Unquoted"));
     $this->assertSame(" in it", $offset->getContent());
 }