summaryrefslogtreecommitdiff
path: root/includes/database
diff options
context:
space:
mode:
Diffstat (limited to 'includes/database')
-rw-r--r--includes/database/database.inc44
-rw-r--r--includes/database/query.inc32
-rw-r--r--includes/database/sqlite/query.inc12
3 files changed, 88 insertions, 0 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc
index f07e07373..60d4b415f 100644
--- a/includes/database/database.inc
+++ b/includes/database/database.inc
@@ -255,6 +255,13 @@ abstract class DatabaseConnection extends PDO {
protected $deleteClass = NULL;
/**
+ * The name of the Truncate class for this connection.
+ *
+ * @var string
+ */
+ protected $truncateClass = NULL;
+
+ /**
* The name of the Insert class for this connection.
*
* @var string
@@ -763,6 +770,26 @@ abstract class DatabaseConnection extends PDO {
}
/**
+ * Prepare and return a TRUNCATE query object.
+ *
+ * @see TruncateQuery
+ * @param $options
+ * An array of options on the query.
+ * @return
+ * A new DeleteQuery object.
+ */
+ public function truncate($table, array $options = array()) {
+ if (empty($this->truncateClass)) {
+ $this->truncateClass = 'TruncateQuery_' . $this->driver();
+ if (!class_exists($this->truncateClass)) {
+ $this->truncateClass = 'TruncateQuery';
+ }
+ }
+ $class = $this->truncateClass;
+ return new $class($this, $table, $options);
+ }
+
+ /**
* Returns a DatabaseSchema object for manipulating the schema of this database.
*
* This method will lazy-load the appropriate schema library file.
@@ -1881,6 +1908,23 @@ function db_delete($table, array $options = array()) {
}
/**
+ * Returns a new TruncateQuery object for the active database.
+ *
+ * @param $table
+ * The table from which to delete.
+ * @param $options
+ * An array of options to control how the query operates.
+ * @return
+ * A new TruncateQuery object for this connection.
+ */
+function db_truncate($table, array $options = array()) {
+ if (empty($options['target']) || $options['target'] == 'slave') {
+ $options['target'] = 'default';
+ }
+ return Database::getConnection($options['target'])->truncate($table, $options);
+}
+
+/**
* Returns a new SelectQuery object for the active database.
*
* @param $table
diff --git a/includes/database/query.inc b/includes/database/query.inc
index 8c7281820..8cdaaf5e1 100644
--- a/includes/database/query.inc
+++ b/includes/database/query.inc
@@ -829,6 +829,38 @@ class DeleteQuery extends Query implements QueryConditionInterface {
}
}
+
+/**
+ * General class for an abstracted TRUNCATE operation.
+ */
+class TruncateQuery extends Query {
+
+ /**
+ * The table from which to delete.
+ *
+ * @var string
+ */
+ protected $table;
+
+ public function __construct(DatabaseConnection $connection, $table, array $options = array()) {
+ $options['return'] = Database::RETURN_AFFECTED;
+ parent::__construct($connection, $options);
+ $this->table = $table;
+ }
+
+ public function compile(DatabaseConnection $connection) {
+ return $this->condition->compile($connection);
+ }
+
+ public function execute() {
+ return $this->connection->query((string)$this, array(), $this->queryOptions);
+ }
+
+ public function __toString() {
+ return 'TRUNCATE {' . $this->connection->escapeTable($this->table) . '} ';
+ }
+}
+
/**
* General class for an abstracted UPDATE operation.
*/
diff --git a/includes/database/sqlite/query.inc b/includes/database/sqlite/query.inc
index ab6e2da50..98fa80754 100644
--- a/includes/database/sqlite/query.inc
+++ b/includes/database/sqlite/query.inc
@@ -136,5 +136,17 @@ class DeleteQuery_sqlite extends DeleteQuery {
}
/**
+ * SQLite specific implementation of TruncateQuery.
+ *
+ * SQLite doesn't support TRUNCATE, but a DELETE query with no condition has
+ * exactly the effect (it is implemented by DROPing the table).
+ */
+class TruncateQuery_sqlite extends TruncateQuery {
+ public function __toString() {
+ return 'DELETE FROM {' . $this->connection->escapeTable($this->table) . '} ';
+ }
+}
+
+/**
* @} End of "ingroup database".
*/