123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- class InsertQuery_pgsql extends InsertQuery {
- public function execute() {
- if (!$this->preExecute()) {
- return NULL;
- }
- $stmt = $this->connection->prepareQuery((string) $this);
-
- $table_information = $this->connection->schema()->queryTableInformation($this->table);
- $max_placeholder = 0;
- $blobs = array();
- $blob_count = 0;
- foreach ($this->insertValues as $insert_values) {
- foreach ($this->insertFields as $idx => $field) {
- if (isset($table_information->blob_fields[$field])) {
- $blobs[$blob_count] = fopen('php://memory', 'a');
- fwrite($blobs[$blob_count], $insert_values[$idx]);
- rewind($blobs[$blob_count]);
- $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], PDO::PARAM_LOB);
-
- ++$blob_count;
- }
- else {
- $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]);
- }
- }
-
- if (!empty($table_information->serial_fields)) {
- foreach ($table_information->serial_fields as $index => $serial_field) {
- $serial_key = array_search($serial_field, $this->insertFields);
- if ($serial_key !== FALSE) {
- $serial_value = $insert_values[$serial_key];
-
-
- if ($index == 0) {
- $last_insert_id = $serial_value;
- }
-
-
-
-
-
-
- $this->connection->query("SELECT setval('" . $table_information->sequences[$index] . "', GREATEST(MAX(" . $serial_field . "), :serial_value)) FROM {" . $this->table . "}", array(':serial_value' => (int)$serial_value));
- }
- }
- }
- }
- if (!empty($this->fromQuery)) {
-
-
-
-
- $arguments = $this->fromQuery->getArguments();
- foreach ($arguments as $key => $value) {
- $stmt->bindParam($key, $arguments[$key]);
- }
- }
-
-
-
- $options = $this->queryOptions;
- if (!empty($table_information->sequences)) {
- $options['sequence_name'] = $table_information->sequences[0];
- }
-
- elseif ($options['return'] == Database::RETURN_INSERT_ID) {
- $options['return'] = Database::RETURN_NULL;
- }
-
- if (!empty($last_insert_id)) {
- $this->connection->query($stmt, array(), $options);
- }
- else {
- $last_insert_id = $this->connection->query($stmt, array(), $options);
- }
-
- $this->insertValues = array();
- return $last_insert_id;
- }
- public function __toString() {
-
- $comments = $this->connection->makeComment($this->comments);
-
- $insert_fields = array_merge($this->defaultFields, $this->insertFields);
-
-
- if (!empty($this->fromQuery)) {
- return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
- }
- $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
- $max_placeholder = 0;
- $values = array();
- if (count($this->insertValues)) {
- foreach ($this->insertValues as $insert_values) {
- $placeholders = array();
-
-
- $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
- $new_placeholder = $max_placeholder + count($insert_values);
- for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
- $placeholders[] = ':db_insert_placeholder_' . $i;
- }
- $max_placeholder = $new_placeholder;
- $values[] = '(' . implode(', ', $placeholders) . ')';
- }
- }
- else {
-
- $placeholders = array_fill(0, count($this->defaultFields), 'default');
- $values[] = '(' . implode(', ', $placeholders) . ')';
- }
- $query .= implode(', ', $values);
- return $query;
- }
- }
- class UpdateQuery_pgsql extends UpdateQuery {
- public function execute() {
- $max_placeholder = 0;
- $blobs = array();
- $blob_count = 0;
-
-
- $stmt = $this->connection->prepareQuery((string) $this);
-
- $table_information = $this->connection->schema()->queryTableInformation($this->table);
-
-
- $fields = $this->fields;
- $expression_fields = array();
- foreach ($this->expressionFields as $field => $data) {
- if (!empty($data['arguments'])) {
- foreach ($data['arguments'] as $placeholder => $argument) {
-
-
-
- $stmt->bindParam($placeholder, $data['arguments'][$placeholder]);
- }
- }
- unset($fields[$field]);
- }
- foreach ($fields as $field => $value) {
- $placeholder = ':db_update_placeholder_' . ($max_placeholder++);
- if (isset($table_information->blob_fields[$field])) {
- $blobs[$blob_count] = fopen('php://memory', 'a');
- fwrite($blobs[$blob_count], $value);
- rewind($blobs[$blob_count]);
- $stmt->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB);
- ++$blob_count;
- }
- else {
- $stmt->bindParam($placeholder, $fields[$field]);
- }
- }
- if (count($this->condition)) {
- $this->condition->compile($this->connection, $this);
- $arguments = $this->condition->arguments();
- foreach ($arguments as $placeholder => $value) {
- $stmt->bindParam($placeholder, $arguments[$placeholder]);
- }
- }
- $options = $this->queryOptions;
- $options['already_prepared'] = TRUE;
- $this->connection->query($stmt, $options);
- return $stmt->rowCount();
- }
- }
|