Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 11354:10fba62332c5
mod_storage_sql: Implement map-like API for archives
Used by mod_http_file_share, muc moderation, etc.
Tests tweaked because they failed on stanza internals that happen
becasue of re-serialization. Namespaces differ since inheritance is
implicit when building but explicit after parsing.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 01 Feb 2021 12:47:05 +0100 |
parent | 11283:f415176281fa |
child | 11625:04abe65b8067 |
comparison
equal
deleted
inserted
replaced
11353:367e6beaf8ab | 11354:10fba62332c5 |
---|---|
480 return row[1], value, row[4], row[5]; | 480 return row[1], value, row[4], row[5]; |
481 end | 481 end |
482 end, total; | 482 end, total; |
483 end | 483 end |
484 | 484 |
485 function archive_store:get(username, key) | |
486 local iter, err = self:find(username, { key = key }) | |
487 if not iter then return iter, err; end | |
488 for _, stanza, when, with in iter do | |
489 return stanza, when, with; | |
490 end | |
491 return nil, "item-not-found"; | |
492 end | |
493 | |
494 function archive_store:set(username, key, new_value, new_when, new_with) | |
495 local user,store = username,self.store; | |
496 local ok, result = engine:transaction(function () | |
497 | |
498 local update_query = [[ | |
499 UPDATE "prosodyarchive" | |
500 SET %s | |
501 WHERE %s | |
502 ]]; | |
503 local args = { host, user or "", store, key }; | |
504 local setf = {}; | |
505 local where = { "\"host\" = ?", "\"user\" = ?", "\"store\" = ?", "\"key\" = ?"}; | |
506 | |
507 if new_value then | |
508 table.insert(setf, '"type" = ?') | |
509 table.insert(setf, '"value" = ?') | |
510 local t, value = serialize(new_value); | |
511 table.insert(args, 1, t); | |
512 table.insert(args, 2, value); | |
513 end | |
514 | |
515 if new_when then | |
516 table.insert(setf, 1, '"when" = ?') | |
517 table.insert(args, 1, new_when); | |
518 end | |
519 | |
520 if new_with then | |
521 table.insert(setf, 1, '"with" = ?') | |
522 table.insert(args, 1, new_with); | |
523 end | |
524 | |
525 update_query = update_query:format(t_concat(setf, ", "), t_concat(where, " AND ")); | |
526 return engine:update(update_query, unpack(args)); | |
527 end); | |
528 if not ok then return ok, result; end | |
529 return result:affected() == 1; | |
530 end | |
531 | |
485 function archive_store:summary(username, query) | 532 function archive_store:summary(username, query) |
486 query = query or {}; | 533 query = query or {}; |
487 local user,store = username,self.store; | 534 local user,store = username,self.store; |
488 local ok, result = engine:transaction(function() | 535 local ok, result = engine:transaction(function() |
489 local sql_query = [[ | 536 local sql_query = [[ |