Changeset

8395:fbb9a1c2120e

mod_storage_sql: Add support for truncating deletion
author Kim Alvefur <zash@zash.se>
date Thu, 09 Nov 2017 16:50:36 +0100
parents 8394:4892c22403d5
children 8396:fbe1f99fb245
files plugins/mod_storage_sql.lua
diffstat 1 files changed, 29 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Fri Nov 10 09:44:30 2017 +0100
+++ b/plugins/mod_storage_sql.lua	Thu Nov 09 16:50:36 2017 +0100
@@ -374,7 +374,35 @@
 		end
 		archive_where(query, args, where);
 		archive_where_id_range(query, args, where);
-		sql_query = sql_query:format(t_concat(where, " AND "));
+		if query.truncate == nil then
+			sql_query = sql_query:format(t_concat(where, " AND "));
+		else
+			args[#args+1] = query.truncate;
+			local unlimited = "ALL";
+			if engine.params.driver == "SQLite3" then
+				sql_query = [[
+				DELETE FROM "prosodyarchive"
+				WHERE %s
+				ORDER BY "sort_id" %s
+				LIMIT %s OFFSET ?;
+				]];
+				unlimited = "-1";
+			else
+				sql_query = [[
+				DELETE FROM "prosodyarchive"
+				WHERE "sort_id" IN (
+					SELECT "sort_id" FROM "prosodyarchive"
+					WHERE %s
+					ORDER BY "sort_id" %s
+					LIMIT %s OFFSET ?
+				);]];
+				if engine.params.driver == "MySQL" then
+					unlimited = "18446744073709551615";
+				end
+			end
+			sql_query = string.format(sql_query, t_concat(where, " AND "),
+				query.reverse and "ASC" or "DESC", unlimited);
+		end
 		return engine:delete(sql_query, unpack(args));
 	end);
 	return ok and stmt:affected(), stmt;