query.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. return $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
  98. }
  99. $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
  100. $max_placeholder = 0;
  101. $values = array();
  102. if (count($this->insertValues)) {
  103. foreach ($this->insertValues as $insert_values) {
  104. $placeholders = array();
  105. // Default fields aren't really placeholders, but this is the most convenient
  106. // way to handle them.
  107. $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
  108. $new_placeholder = $max_placeholder + count($insert_values);
  109. for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
  110. $placeholders[] = ':db_insert_placeholder_' . $i;
  111. }
  112. $max_placeholder = $new_placeholder;
  113. $values[] = '(' . implode(', ', $placeholders) . ')';
  114. }
  115. }
  116. else {
  117. // If there are no values, then this is a default-only query. We still need to handle that.
  118. $placeholders = array_fill(0, count($this->defaultFields), 'default');
  119. $values[] = '(' . implode(', ', $placeholders) . ')';
  120. }
  121. $query .= implode(', ', $values);
  122. return $query;
  123. }
  124. }
  125. class UpdateQuery_pgsql extends UpdateQuery {
  126. public function execute() {
  127. $max_placeholder = 0;
  128. $blobs = array();
  129. $blob_count = 0;
  130. // Because we filter $fields the same way here and in __toString(), the
  131. // placeholders will all match up properly.
  132. $stmt = $this->connection->prepareQuery((string) $this);
  133. // Fetch the list of blobs and sequences used on that table.
  134. $table_information = $this->connection->schema()->queryTableInformation($this->table);
  135. // Expressions take priority over literal fields, so we process those first
  136. // and remove any literal fields that conflict.
  137. $fields = $this->fields;
  138. $expression_fields = array();
  139. foreach ($this->expressionFields as $field => $data) {
  140. if (!empty($data['arguments'])) {
  141. foreach ($data['arguments'] as $placeholder => $argument) {
  142. // We assume that an expression will never happen on a BLOB field,
  143. // which is a fairly safe assumption to make since in most cases
  144. // it would be an invalid query anyway.
  145. $stmt->bindParam($placeholder, $data['arguments'][$placeholder]);
  146. }
  147. }
  148. unset($fields[$field]);
  149. }
  150. foreach ($fields as $field => $value) {
  151. $placeholder = ':db_update_placeholder_' . ($max_placeholder++);
  152. if (isset($table_information->blob_fields[$field])) {
  153. $blobs[$blob_count] = fopen('php://memory', 'a');
  154. fwrite($blobs[$blob_count], $value);
  155. rewind($blobs[$blob_count]);
  156. $stmt->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB);
  157. ++$blob_count;
  158. }
  159. else {
  160. $stmt->bindParam($placeholder, $fields[$field]);
  161. }
  162. }
  163. if (count($this->condition)) {
  164. $this->condition->compile($this->connection, $this);
  165. $arguments = $this->condition->arguments();
  166. foreach ($arguments as $placeholder => $value) {
  167. $stmt->bindParam($placeholder, $arguments[$placeholder]);
  168. }
  169. }
  170. $options = $this->queryOptions;
  171. $options['already_prepared'] = TRUE;
  172. $this->connection->query($stmt, $options);
  173. return $stmt->rowCount();
  174. }
  175. }