version, sqlite
[guerillascript.git] / main / modules / sbapi / scriptstorage.js
blob43427fefd9f9e7a59a3038cad044d69070dec389
1 /*
2 * Copyright 2004-2007 Aaron Boodman
3 * Portions copyright 2015 Ketmar Dark <ketmar@ketmar.no-ip.org>
4 * Contributors: See contributors list in install.rdf and CREDITS
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * Note that this license applies only to the Greasemonkey extension source
14 * files, not to the user scripts which it runs. User scripts are licensed
15 * separately by their authors.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
28 ////////////////////////////////////////////////////////////////////////////////
29 function ScriptStorage (nfo) {
30 this.dbBaseName = nfo.name;
31 this.dbObj = null;
32 return this;
36 ScriptStorage.prototype.__defineGetter__("dbFile", function () {
37 let file = getUserDBDir();
38 file.append(this.dbBaseName+".db");
39 return file;
40 });
43 ScriptStorage.prototype.__defineGetter__("db", function () {
44 if (this.dbObj == null) {
45 this.dbObj = Services.storage.openDatabase(this.dbFile);
46 // the auto_vacuum pragma has to be set before the table is created
47 //this.dbObj.executeSimpleSQL("PRAGMA auto_vacuum = INCREMENTAL;");
48 //this.dbObj.executeSimpleSQL("PRAGMA incremental_vacuum(10);");
49 this.dbObj.executeSimpleSQL("PRAGMA page_size = 512;");
50 this.dbObj.executeSimpleSQL("PRAGMA auto_vacuum = NONE;");
51 //this.dbObj.executeSimpleSQL("PRAGMA journal_mode = WAL;");
52 //this.dbObj.executeSimpleSQL("PRAGMA wal_autocheckpoint = 10;");
53 this.dbObj.executeSimpleSQL("PRAGMA journal_mode = DELETE;"); // will slow down bulk inserts, but IDC
54 this.dbObj.executeSimpleSQL(
55 "CREATE TABLE IF NOT EXISTS scriptvals ("+
56 " name TEXT PRIMARY KEY NOT NULL"+
57 " , value TEXT"+
58 ")"
60 // run vacuum once manually to switch to the correct auto_vacuum mode for
61 // databases that were created with incorrect auto_vacuum
62 //this.dbObj.executeSimpleSQL("VACUUM;");
64 return this.dbObj;
65 });
68 ScriptStorage.prototype.__defineGetter__("opened", function () {
69 return (this.dbObj != null);
70 });
73 ScriptStorage.prototype.close = function () {
74 if (this.dbObj) {
75 this.dbObj.close();
76 this.dbObj = null;
81 ScriptStorage.prototype.setValue = function (name, val) {
82 if (arguments.length != 2) {
83 logError("invalid number of arguments to `GM_setValue()`");
84 throw new Error("invalid number of arguments to `GM_setValue()`");
86 if (val === null) {
87 this.deleteValue(name);
88 return;
90 //if (name === undefined) return;
91 //if (typeof(name) === "Object") return;
92 let stmt = this.db.createStatement("INSERT OR REPLACE INTO scriptvals (name, value) VALUES (:name, :value)");
93 try {
94 stmt.params.name = name;
95 stmt.params.value = JSON.stringify(val);
96 stmt.execute();
97 } finally {
98 stmt.reset();
103 ScriptStorage.prototype.getValue = function (name, defval) {
104 if (arguments.length < 1 || arguments.length > 2) {
105 logError("invalid number of arguments to `GM_getValue()`");
106 throw new Error("invalid number of arguments to `GM_getValue()`");
108 if (typeof(defval) === "undefined") defval = null;
109 let value = null;
110 let stmt = this.db.createStatement("SELECT value FROM scriptvals WHERE name = :name");
111 try {
112 stmt.params.name = name;
113 while (stmt.step()) value = stmt.row.value;
114 } catch (e) {
115 logException("GM_getValue", e);
116 } finally {
117 stmt.reset();
119 if (typeof(value) !== "string") return defval;
120 // parse json
121 try {
122 let exposedProps = "__exposedProps__";
123 value = JSON.parse(value);
124 if (typeof(value) == "object") {
125 if (!(exposedProps in value)) value[exposedProps] = {};
126 for (let prop in value) {
127 if (prop !== exposedProps && Object.prototype.hasOwnProperty.call(value, prop)) {
128 value[exposedProps][prop] = "rw"; // there is nothing wrong in making this r/w
132 return value;
133 } catch (e) {
134 //logError("JSON parse error: "+e.name+": "+e.message);
135 return defval;
140 ScriptStorage.prototype.deleteValue = function (name) {
141 if (arguments.length != 1) {
142 logError("invalid number of arguments to `GM_deleteValue()`");
143 throw new Error("invalid number of arguments to `GM_deleteValue()`");
145 let stmt = this.db.createStatement("DELETE FROM scriptvals WHERE name = :name");
146 try {
147 stmt.params.name = name;
148 stmt.execute();
149 } finally {
150 stmt.reset();
155 ScriptStorage.prototype.listValues = function () {
156 if (arguments.length != 0) {
157 logError("invalid number of arguments to `GM_listValues()`");
158 throw new Error("invalid number of arguments to `GM_listValues()`");
160 //let count = 0;
161 //let eprp = {length: "r"};
162 //let vals = {};
163 let valueNames = [];
164 let stmt = this.db.createStatement("SELECT name FROM scriptvals");
165 try {
166 while (stmt.executeStep()) {
167 valueNames.push(stmt.row.name);
168 //vals[count++] = stmt.row.name;
170 } finally {
171 stmt.reset();
173 //let vals = Array.prototype.slice.call(valueNames);
174 //vals.__exposedProps__ = {length: "r"};
175 //!vals.length = count;
176 //!vals.__exposedProps__ = eprp;
177 let vals = valueNames;
178 return vals;
182 ////////////////////////////////////////////////////////////////////////////////
183 exports.ScriptStorage = ScriptStorage;