diff options
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/DatabaseTable.vala | 51 | ||||
-rw-r--r-- | src/db/Db.vala | 4 | ||||
-rw-r--r-- | src/db/PhotoTable.vala | 7 | ||||
-rw-r--r-- | src/db/VideoTable.vala | 7 |
4 files changed, 69 insertions, 0 deletions
diff --git a/src/db/DatabaseTable.vala b/src/db/DatabaseTable.vala index dea797a..be45e5e 100644 --- a/src/db/DatabaseTable.vala +++ b/src/db/DatabaseTable.vala @@ -29,10 +29,61 @@ public abstract class DatabaseTable { public string table_name = null; + static Gee.HashMap<string, Regex> regex_map; + + private static void regexp_replace(Sqlite.Context context, Sqlite.Value[] args) { + var pattern = args[0].to_text(); + if (pattern == null) { + context.result_error("Missing regular expression", Sqlite.ERROR); + return; + } + + var text = args[1].to_text(); + if (text == null) { + return; + } + + var replacement = args[2].to_text(); + if (replacement == null) { + context.result_value(args[1]); + return; + } + + Regex re; + if (regex_map == null) { + regex_map = new Gee.HashMap<string, Regex>(); + } + if (regex_map.has_key(pattern)) { + re = regex_map[pattern]; + } else { + try { + re = new Regex(pattern, RegexCompileFlags.DEFAULT, RegexMatchFlags.DEFAULT); + regex_map[pattern] = re; + } catch (Error err) { + context.result_error("Invalid pattern: %s".printf(err.message), Sqlite.ERROR); + return; + } + } + + try { + var result = re.replace(text, -1, 0, replacement, RegexMatchFlags.DEFAULT); + context.result_text(result); + } catch (Error err) { + context.result_error("Replacement failed: %s".printf(err.message), Sqlite.ERROR); + } + } + + [CCode (cname="SQLITE_DETERMINISTIC", cheader_filename="sqlite3.h")] + extern static int SQLITE_DETERMINISTIC; + private static void prepare_db(string filename) { // Open DB. int res = Sqlite.Database.open_v2(filename, out db, Sqlite.OPEN_READWRITE | Sqlite.OPEN_CREATE, null); + + db.create_function("regexp_replace", 3, Sqlite.UTF8 | SQLITE_DETERMINISTIC, null, + DatabaseTable.regexp_replace, null, null); + if (res != Sqlite.OK) AppWindow.panic(_("Unable to open/create photo database %s: error code %d").printf(filename, res)); diff --git a/src/db/Db.vala b/src/db/Db.vala index 5072967..7f76f2d 100644 --- a/src/db/Db.vala +++ b/src/db/Db.vala @@ -55,6 +55,10 @@ public VerifyResult verify_database(out string app_version, out int schema_versi if (result != VerifyResult.OK) return result; } + + PhotoTable.clean_comments(); + VideoTable.clean_comments(); + return VerifyResult.OK; } diff --git a/src/db/PhotoTable.vala b/src/db/PhotoTable.vala index 420b209..d74cbd1 100644 --- a/src/db/PhotoTable.vala +++ b/src/db/PhotoTable.vala @@ -1123,6 +1123,13 @@ public class PhotoTable : DatabaseTable { throw_error("PhotoTable.upgrade_for_unset_timestamp", res); } } + + public static void clean_comments() throws DatabaseError { + var result = db.exec("UPDATE PhotoTable SET comment = regexp_replace('^charset=\\w+\\s*', comment, '') WHERE comment like 'charset=%'"); + if (result != Sqlite.OK) { + throw_error("Cleaning comments from charset", result); + } + } } diff --git a/src/db/VideoTable.vala b/src/db/VideoTable.vala index 8af1278..67c50ba 100644 --- a/src/db/VideoTable.vala +++ b/src/db/VideoTable.vala @@ -480,5 +480,12 @@ public class VideoTable : DatabaseTable { } } + public static void clean_comments() throws DatabaseError { + var result = db.exec("UPDATE VideoTable SET comment = regexp_replace('^charset=\\w+\\s*', comment, '') WHERE comment like 'charset=%'"); + if (result != Sqlite.OK) { + throw_error("Cleaning comments from charset", result); + } + } + } |