예제 #1
0
 /**
  * Initializes a new instance of the PgStmt.
  * 
  * @param \resource $connection The PostgreSQL connection instance.
  * @param \resource $stmt The PostgreSQL stmt instance.
  * @param DBParameterCollection $params The query parameters array. Default: NULL.
  */
 function __construct($connection, $query, $parameters = NULL)
 {
     $this->Parameters = new DBParameterCollection();
     $this->Name = uniqid();
     $this->Connection = $connection;
     $this->Result = NULL;
     // build query and parameters
     $index = -1;
     $this->Query = preg_replace_callback('/(\\@[^\\s\\,\\;\\.\\)\\(\\{\\}\\[\\]]+)|(\\%s?)|(\\?{1}?)/', function ($m) use($index, $parameters) {
         if ($m[0] == '%s' || $m[0] == '?') {
             $index++;
             $this->Parameters->Add($parameters->Items[$index]);
             return '$' . $this->Parameters->Count();
         } else {
             if (($parameter = $parameters->Get($m[0])) != NULL) {
                 if ($index != -1) {
                     throw new \ErrorException('Do not use explicit parameter names together with implicit.');
                 }
                 $this->Parameters->Add($parameter);
                 return '$' . $this->Parameters->Count();
             } else {
                 return $m[0];
             }
         }
     }, $query);
 }
예제 #2
0
 /**
  * Prepares the query to execution.
  * 
  * @param \string $commandText The SQL query.
  * @param DBParameterCollection $commandParameters The query parameters.
  * 
  * @throws \InvalidArgumentException if the query is empty.
  * @throws \mysqli_sql_exception 
  * 
  * @return \mysqli_stmt
  */
 private function PrepareQuery($commandText, $commandParameters)
 {
     if ($commandText == NULL || $commandText == '') {
         throw new \InvalidArgumentException('Command text is required. Value can not be empty.');
     }
     // open connection
     if ($this->ConnectionMode === ConnectionMode::Auto) {
         $this->Connect();
     }
     // build query and parameters
     $query = $commandText;
     $parameters = new DBParameterCollection();
     $index = -1;
     $query = preg_replace_callback('/(\\@[^\\s\\,\\;\\.\\)\\(\\{\\}\\[\\]]+)|(\\%s?)|(\\?{1}?)/', function ($m) use($index, $parameters, $commandParameters) {
         if ($m[0] == '%s' || $m[0] == '?') {
             $index++;
             $parameters->Add($commandParameters->Items[$index]);
             return '?';
         } else {
             if (($parameter = $commandParameters->Get($m[0])) != NULL) {
                 if ($index != -1) {
                     throw new \ErrorException('Do not use explicit parameter names together with implicit.');
                 }
                 $parameters->Add($parameter);
                 return '?';
             } else {
                 return $m[0];
             }
         }
     }, $query);
     // prepare
     if (($stmt = $this->Connection->prepare($query)) === FALSE) {
         $this->ThrowException($stmt);
     }
     // parameters
     if ($parameters != NULL && $parameters->Count() > 0) {
         $p = $parameters->GetValueArray();
         $types = $parameters->GetTypes();
         array_unshift($p, $types);
         if (!call_user_func_array(array($stmt, 'bind_param'), $p)) {
             if ($this->ConnectionMode === ConnectionMode::Auto) {
                 $this->Disconnect();
             }
             throw new \mysqli_sql_exception('Culd not bind_param.');
         }
     }
     return $stmt;
 }