示例#1
0
 /**
  * @param Settings|string|Closure|null $settings
  * @throws Service\Exception
  */
 public function __construct($settings = null)
 {
     $settingsClass = static::getSettingsClassName();
     if (null === $settingsClass) {
         $settingsClass = Settings::className();
     }
     if (null !== $settings) {
         if ($settings instanceof Closure) {
             $settings = self::resolveClosure($settings);
         }
         if (is_string($settings)) {
             $this->settings = new $settingsClass($settings);
         } elseif ($settings instanceof $settingsClass) {
             $this->settings = $settings;
         } else {
             throw new Service\Exception('Invalid argument. ' . Debug::varBrief($settings), Service\Exception::SETTINGS_REQUIRED);
         }
     } else {
         $this->settings = new $settingsClass();
     }
 }
示例#2
0
 public function build(Quoter $quoter = null)
 {
     if ($this->binds) {
         if ($quoter === null && $this->quoter !== null) {
             $quoter = $this->quoter;
         }
         if ($quoter === null) {
             if (null === self::$rawQuoter) {
                 self::$rawQuoter = new Raw();
             }
             $quoter = self::$rawQuoter;
         }
         $statement = $this->statement;
         $replace = array();
         $unnamed = true;
         // check binds array type
         $i = 0;
         foreach ($this->binds as $key => $value) {
             if ($unnamed && $key !== $i++) {
                 $unnamed = false;
                 break;
             }
         }
         if ($unnamed) {
             $pos = 0;
             $placeholderLength = strlen($this->placeholder);
             foreach ($this->binds as $value) {
                 $pos = strpos($statement, $this->placeholder, $pos);
                 if ($pos !== false) {
                     $value = $quoter->quote($value);
                     $statement = substr_replace($statement, $value, $pos, $placeholderLength);
                     $pos += strlen($value);
                 } else {
                     throw new Exception('Placeholder \'' . $this->placeholder . '\' not found ("' . $statement . '"), ' . Debug::varBrief($this->binds), Exception::PLACEHOLDER_NOT_FOUND);
                 }
             }
             if (strpos($statement, $this->placeholder, $pos) !== false) {
                 throw new Exception('Redundant placeholder: "' . $statement . '"', Exception::PLACEHOLDER_REDUNDANT);
             }
             $result = $statement;
         } else {
             foreach ($this->binds as $key => $value) {
                 $replace[$this->namedPrefix . $key] = $quoter->quote($value);
             }
             $result = strtr($statement, $replace);
         }
     } else {
         $result = $this->statement;
     }
     return $result;
 }
示例#3
0
 public function build(Quoter $quoter = null)
 {
     if ($this->isEmpty()) {
         return '';
     }
     if (null === $quoter) {
         if ($this->database()) {
             $quoter = $this->database()->getDriver();
         }
     }
     $result = '';
     if ($this->binds) {
         if ($quoter === null) {
             throw new Exception('Missing quoter', Exception::MISSING_QUOTER);
         }
         $statement = $this->statement;
         $replace = array();
         $unnamed = true;
         // check binds array type
         $i = 0;
         foreach ($this->binds as $key => $value) {
             if ($unnamed && $key !== $i++) {
                 $unnamed = false;
                 break;
             }
         }
         if ($unnamed) {
             $pos = 0;
             foreach ($this->binds as $value) {
                 $pos = strpos($statement, '?', $pos);
                 if ($pos !== false) {
                     $value = $quoter->quote($value);
                     $statement = substr_replace($statement, $value, $pos, 1);
                     $pos += strlen($value);
                 } else {
                     throw new \Yaoi\Database\Exception('Placeholder \'?\' not found ("' . $statement . '"), ' . Debug::varBrief($this->binds), \Yaoi\Database\Exception::PLACEHOLDER_NOT_FOUND);
                 }
             }
             if (strpos($statement, '?', $pos) !== false) {
                 throw new \Yaoi\Database\Exception('Redundant placeholder: "' . $statement . '"', \Yaoi\Database\Exception::PLACEHOLDER_REDUNDANT);
             }
             $result = $statement;
         } else {
             foreach ($this->binds as $key => $value) {
                 $replace[':' . $key] = $quoter->quote($value);
             }
             $result = strtr($statement, $replace);
         }
     } elseif ($this->statement) {
         $result = $this->statement;
     }
     foreach ($this->queue as $item) {
         /**
          * @var SimpleExpression $expression
          */
         $expression = $item[1];
         if (!$expression->isEmpty()) {
             $result .= ($result ? $item[0] : '') . $expression->build($quoter);
         }
     }
     return $this->as ? '(' . $result . ') AS ' . $this->as : $result;
 }