/** * 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; }
/** * Sends a request to execute a prepared statement with given parameters, and waits for the result. * * @return \resource */ public function Execute() { return $this->Result = pg_execute($this->Connection, $this->Name, $this->Parameters->GetValueArray()); }