query.inc 7.7 KB

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