| Drupal | PHP Cross Reference | Content Management Systems |
1 <?php 2 3 /** 4 * @ingroup database 5 * @{ 6 */ 7 8 /** 9 * @file 10 * Query code for PostgreSQL embedded database engine. 11 */ 12 13 14 class InsertQuery_pgsql extends InsertQuery { 15 16 public function execute() { 17 if (!$this->preExecute()) { 18 return NULL; 19 } 20 21 $stmt = $this->connection->prepareQuery((string) $this); 22 23 // Fetch the list of blobs and sequences used on that table. 24 $table_information = $this->connection->schema()->queryTableInformation($this->table); 25 26 $max_placeholder = 0; 27 $blobs = array(); 28 $blob_count = 0; 29 foreach ($this->insertValues as $insert_values) { 30 foreach ($this->insertFields as $idx => $field) { 31 if (isset($table_information->blob_fields[$field])) { 32 $blobs[$blob_count] = fopen('php://memory', 'a'); 33 fwrite($blobs[$blob_count], $insert_values[$idx]); 34 rewind($blobs[$blob_count]); 35 36 $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], PDO::PARAM_LOB); 37 38 // Pre-increment is faster in PHP than increment. 39 ++$blob_count; 40 } 41 else { 42 $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]); 43 } 44 } 45 // Check if values for a serial field has been passed. 46 if (!empty($table_information->serial_fields)) { 47 foreach ($table_information->serial_fields as $index => $serial_field) { 48 $serial_key = array_search($serial_field, $this->insertFields); 49 if ($serial_key !== FALSE) { 50 $serial_value = $insert_values[$serial_key]; 51 52 // Force $last_insert_id to the specified value. This is only done 53 // if $index is 0. 54 if ($index == 0) { 55 $last_insert_id = $serial_value; 56 } 57 // Set the sequence to the bigger value of either the passed 58 // value or the max value of the column. It can happen that another 59 // thread calls nextval() which could lead to a serial number being 60 // used twice. However, trying to insert a value into a serial 61 // column should only be done in very rare cases and is not thread 62 // safe by definition. 63 $this->connection->query("SELECT setval('" . $table_information->sequences[$index] . "', GREATEST(MAX(" . $serial_field . "), :serial_value)) FROM {" . $this->table . "}", array(':serial_value' => (int)$serial_value)); 64 } 65 } 66 } 67 } 68 if (!empty($this->fromQuery)) { 69 // bindParam stores only a reference to the variable that is followed when 70 // the statement is executed. We pass $arguments[$key] instead of $value 71 // because the second argument to bindParam is passed by reference and 72 // the foreach statement assigns the element to the existing reference. 73 $arguments = $this->fromQuery->getArguments(); 74 foreach ($arguments as $key => $value) { 75 $stmt->bindParam($key, $arguments[$key]); 76 } 77 } 78 79 // PostgreSQL requires the table name to be specified explicitly 80 // when requesting the last insert ID, so we pass that in via 81 // the options array. 82 $options = $this->queryOptions; 83 84 if (!empty($table_information->sequences)) { 85 $options['sequence_name'] = $table_information->sequences[0]; 86 } 87 // If there are no sequences then we can't get a last insert id. 88 elseif ($options['return'] == Database::RETURN_INSERT_ID) { 89 $options['return'] = Database::RETURN_NULL; 90 } 91 // Only use the returned last_insert_id if it is not already set. 92 if (!empty($last_insert_id)) { 93 $this->connection->query($stmt, array(), $options); 94 } 95 else { 96 $last_insert_id = $this->connection->query($stmt, array(), $options); 97 } 98 99 // Re-initialize the values array so that we can re-use this query. 100 $this->insertValues = array(); 101 102 return $last_insert_id; 103 } 104 105 public function __toString() { 106 // Create a sanitized comment string to prepend to the query. 107 $comments = $this->connection->makeComment($this->comments); 108 109 // Default fields are always placed first for consistency. 110 $insert_fields = array_merge($this->defaultFields, $this->insertFields); 111 112 // If we're selecting from a SelectQuery, finish building the query and 113 // pass it back, as any remaining options are irrelevant. 114 if (!empty($this->fromQuery)) { 115 return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery; 116 } 117 118 $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; 119 120 $max_placeholder = 0; 121 $values = array(); 122 if (count($this->insertValues)) { 123 foreach ($this->insertValues as $insert_values) { 124 $placeholders = array(); 125 126 // Default fields aren't really placeholders, but this is the most convenient 127 // way to handle them. 128 $placeholders = array_pad($placeholders, count($this->defaultFields), 'default'); 129 130 $new_placeholder = $max_placeholder + count($insert_values); 131 for ($i = $max_placeholder; $i < $new_placeholder; ++$i) { 132 $placeholders[] = ':db_insert_placeholder_' . $i; 133 } 134 $max_placeholder = $new_placeholder; 135 $values[] = '(' . implode(', ', $placeholders) . ')'; 136 } 137 } 138 else { 139 // If there are no values, then this is a default-only query. We still need to handle that. 140 $placeholders = array_fill(0, count($this->defaultFields), 'default'); 141 $values[] = '(' . implode(', ', $placeholders) . ')'; 142 } 143 144 $query .= implode(', ', $values); 145 146 return $query; 147 } 148 } 149 150 class UpdateQuery_pgsql extends UpdateQuery { 151 public function execute() { 152 $max_placeholder = 0; 153 $blobs = array(); 154 $blob_count = 0; 155 156 // Because we filter $fields the same way here and in __toString(), the 157 // placeholders will all match up properly. 158 $stmt = $this->connection->prepareQuery((string) $this); 159 160 // Fetch the list of blobs and sequences used on that table. 161 $table_information = $this->connection->schema()->queryTableInformation($this->table); 162 163 // Expressions take priority over literal fields, so we process those first 164 // and remove any literal fields that conflict. 165 $fields = $this->fields; 166 $expression_fields = array(); 167 foreach ($this->expressionFields as $field => $data) { 168 if (!empty($data['arguments'])) { 169 foreach ($data['arguments'] as $placeholder => $argument) { 170 // We assume that an expression will never happen on a BLOB field, 171 // which is a fairly safe assumption to make since in most cases 172 // it would be an invalid query anyway. 173 $stmt->bindParam($placeholder, $data['arguments'][$placeholder]); 174 } 175 } 176 unset($fields[$field]); 177 } 178 179 foreach ($fields as $field => $value) { 180 $placeholder = ':db_update_placeholder_' . ($max_placeholder++); 181 182 if (isset($table_information->blob_fields[$field])) { 183 $blobs[$blob_count] = fopen('php://memory', 'a'); 184 fwrite($blobs[$blob_count], $value); 185 rewind($blobs[$blob_count]); 186 $stmt->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB); 187 ++$blob_count; 188 } 189 else { 190 $stmt->bindParam($placeholder, $fields[$field]); 191 } 192 } 193 194 if (count($this->condition)) { 195 $this->condition->compile($this->connection, $this); 196 197 $arguments = $this->condition->arguments(); 198 foreach ($arguments as $placeholder => $value) { 199 $stmt->bindParam($placeholder, $arguments[$placeholder]); 200 } 201 } 202 203 $options = $this->queryOptions; 204 $options['already_prepared'] = TRUE; 205 $this->connection->query($stmt, $options); 206 207 return $stmt->rowCount(); 208 } 209 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title