Skip to content

Commit fc3cc07

Browse files
committed
2 parents b831fe2 + f17f995 commit fc3cc07

File tree

11 files changed

+2569
-205
lines changed

11 files changed

+2569
-205
lines changed

package-lock.json

Lines changed: 1652 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tablet/Database.js

Lines changed: 30 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,48 @@
11
import WebUtils from './WebUtils';
2+
import WebSQL from './WebSQL';
3+
import WebForage from './WebForage'
24

35
let dataBase = null;
46
export default class Database {
5-
static initTables() {
6-
if (window.Settings.enableLog)
7-
WebUtils.log("Database.initTables()");
8-
let stmt = "CREATE TABLE IF NOT EXISTS PROJECTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, CTIME DATETIME DEFAULT CURRENT_TIMESTAMP, MTIME DATETIME, ALTMD5 TEXT, POS INTEGER, NAME TEXT, JSON TEXT, THUMBNAIL TEXT, OWNER TEXT, GALLERY TEXT, DELETED TEXT, VERSION TEXT, ISGIFT INTEGER DEFAULT 0)";
9-
this._exec(stmt);
10-
stmt = "CREATE TABLE IF NOT EXISTS USERSHAPES (ID INTEGER PRIMARY KEY AUTOINCREMENT, CTIME DATETIME DEFAULT CURRENT_TIMESTAMP, MD5 TEXT, ALTMD5 TEXT, WIDTH TEXT, HEIGHT TEXT, EXT TEXT, NAME TEXT, OWNER TEXT, SCALE TEXT, VERSION TEXT)";
11-
this._exec(stmt);
12-
stmt = "CREATE TABLE IF NOT EXISTS USERBKGS (ID INTEGER PRIMARY KEY AUTOINCREMENT, CTIME DATETIME DEFAULT CURRENT_TIMESTAMP, MD5 TEXT, ALTMD5 TEXT, WIDTH TEXT, HEIGHT TEXT, EXT TEXT, OWNER TEXT, VERSION TEXT)";
13-
this._exec(stmt);
14-
stmt = "CREATE TABLE IF NOT EXISTS USERFILES (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, CONTEXT TEXT)";
15-
this._exec(stmt);
16-
}
17-
187
//re 0:表示成功 其他表示:Error
198
static _fcnF(fcn, re, data = null) {
209
WebUtils._fcnF(fcn, re, data);
2110
}
2211

23-
static open(body) {
12+
static open(userName) {
2413
if (dataBase != null)
2514
return;
2615
if (window.Settings.enableLog)
27-
WebUtils.log("Database.open({0})".format(body));
28-
dataBase = openDatabase(body, "1.0", "ScratchJr web database", 1024 * 1024, function () {});
29-
this.initTables();
16+
WebUtils.log("Database.open({0})".format(userName));
17+
dataBase = new WebForage(userName);
3018
return "0";
3119
}
3220

3321
static close(str) {
3422
if (window.Settings.enableLog)
3523
WebUtils.log("Database.close({0})".format(str));
24+
if (dataBase != null)
25+
dataBase.close(str);
3626
return "0";
3727
}
3828

3929
static _exec(body, fcn) {
4030
//if (window.Settings.enableLog)
4131
// WebUtils.log("Database._exec({0})".format(body));
42-
dataBase.transaction(function (tx) {
43-
tx.executeSql(body, [],
44-
function (tx, results) {
45-
//if (window.Settings.enableLog)
46-
// WebUtils.log("Database._exec({0} - success.)".format(body));
47-
Database._fcnF(fcn, true);
48-
},
49-
function (tx, error) {
50-
//if (window.Settings.enableLog)
51-
// WebUtils.log("Database._exec({0} - Error:{1})".format(body, error.message));
52-
Database._fcnF(fcn, false);
53-
}
54-
);
55-
});
32+
dataBase._exec(body, fcn);
5633
return "success";
5734
}
5835

59-
//executeSql函数有四个参数,其意义分别是:
60-
// 1)表示查询的字符串,使用的SQL语言是SQLite 3.6.19。(必选)
61-
// 2)插入到查询中问号所在处的字符串数据。(可选)
62-
// 3)成功时执行的回调函数。返回两个参数:tx和执行的结果。(可选)
63-
// 4)一个失败时执行的回调函数。返回两个参数:tx和失败的错误信息。(可选)
6436
static stmt(body, fcn) {
6537
if (window.Settings.enableLog)
6638
WebUtils.log("Database.stmt({0})".format(body));
67-
let jsonData = JSON.parse(body);
68-
//"update db set fieldname = ?,mtime = ? where id = id"
69-
let stmtstr = jsonData["stmt"];
70-
let values = jsonData["values"];
71-
72-
dataBase.transaction(function (tx) {
73-
tx.executeSql(stmtstr, values,
74-
function (tx, results) {
75-
if (window.Settings.enableLog)
76-
WebUtils.log("Database.stmt({0} - success.)".format(body));
77-
try {
78-
Database._fcnF(fcn, results.insertId);
79-
} catch (error) {
80-
Database._fcnF(fcn, true);
81-
}
82-
},
83-
function (tx, error) {
84-
if (window.Settings.enableLog)
85-
WebUtils.log("Database.stmt({0} - Error:{1})".format(body, error.message));
86-
Database._fcnF(fcn, null);
87-
}
88-
);
89-
});
39+
dataBase.stmt(body, fcn)
9040
}
9141

9242
static _stmtBool(body, fcn) {
9343
if (window.Settings.enableLog)
9444
WebUtils.log("Database._stmtBool({0})".format(body));
95-
let jsonData = JSON.parse(body);
96-
//"update db set fieldname = ?,mtime = ? where id = id"
97-
let stmtstr = jsonData["stmt"];
98-
let values = jsonData["values"];
99-
100-
dataBase.transaction(function (tx) {
101-
tx.executeSql(stmtstr, values,
102-
function (tx, results) {
103-
if (window.Settings.enableLog)
104-
WebUtils.log("Database._stmtBool({0} - success.)".format(body));
105-
Database._fcnF(fcn, true);
106-
},
107-
function (tx, error) {
108-
if (window.Settings.enableLog)
109-
WebUtils.log("Database._stmtBool({0} - Error:{1})".format(body, error.message));
110-
Database._fcnF(fcn, false);
111-
}
112-
);
113-
});
45+
dataBase._stmtBool(body, fcn);
11446
}
11547

11648
//{"stmt":"select name,thumbnail,id,isgift from projects where deleted = ? AND version = ? AND gallery IS NULL order by ctime desc",
@@ -135,30 +67,7 @@ export default class Database {
13567
static _queryData(body, fcn) {
13668
if (window.Settings.enableLog)
13769
WebUtils.log("Database.queryData({0})".format(body));
138-
let jsonData = JSON.parse(body);
139-
let stmtstr = jsonData["stmt"];
140-
let values = jsonData["values"];
141-
dataBase.transaction(function (tx) {
142-
tx.executeSql(stmtstr, values,
143-
function (tx, results) {
144-
if (window.Settings.enableLog)
145-
WebUtils.log("Database.queryData({0} - success.)".format(body));
146-
let resArray = [];
147-
let len = results.rows.length;
148-
for (let i = 0; i < len; i++) {
149-
let rowData = results.rows.item(i);
150-
if (window.Settings.enableLog)
151-
WebUtils.log("Database.queryData() - results:{0})".format(JSON.stringify(rowData)));
152-
resArray.push(rowData);
153-
}
154-
Database._fcnF(fcn, resArray);
155-
},
156-
function (tx, error) {
157-
if (window.Settings.enableLog)
158-
WebUtils.log("Database.queryData({0} - Error:{1})".format(body, error.message));
159-
Database._fcnF(fcn, null);
160-
});
161-
});
70+
dataBase._queryData(body, fcn);
16271
}
16372

16473
static _findDataIn(stmtstr, values, fcn) {
@@ -169,38 +78,10 @@ export default class Database {
16978
}
17079

17180
//--------------test--------------------------
172-
static _setfield(db, id, fieldname, val, fcn) {
173-
var json = {};
174-
var keylist = [fieldname + ' = ?', ];
175-
json.values = [val];
176-
json.stmt = 'update ' + db + ' set ' + keylist.toString() + ' where id = ' + id;
177-
return Database._stmtBool(JSON.stringify(json), fcn);
178-
}
179-
18081
static test() {
18182
if (window.Settings.enableLog)
18283
WebUtils.log("Database.test()");
183-
dataBase.transaction(function (tx) {
184-
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
185-
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "foobar")');
186-
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
187-
});
188-
189-
Database._setfield("LOGS", 1, "log", "--foobar2--");
190-
// dataBase.transaction(function (tx) {
191-
// tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
192-
// let len = results.rows.length;
193-
// for (let i = 0; i < len; i++) {
194-
// let rowData = results.rows.item(i);
195-
// WebUtils.log("Database.test() - id:{0},log:{1}".format(rowData.id, rowData.log));
196-
// }
197-
// }, null);
198-
// });
199-
200-
let queryJson = {};
201-
queryJson["stmt"] = "select log from LOGS where id = ?";
202-
queryJson["values"] = [1];
203-
Database.query(JSON.stringify(queryJson));
84+
dataBase.test();
20485
}
20586

20687
//----------File--------------
@@ -210,7 +91,7 @@ export default class Database {
21091

21192
static _getAllFiles(fcn) {
21293
let queryJson = {};
213-
queryJson["stmt"] = "SELECT NAME FROM USERFILES";
94+
queryJson["stmt"] = "select name from userfiles";
21495
queryJson["values"] = [];
21596
Database._queryData(JSON.stringify(queryJson), function (listUserFiles) {
21697
let allFiles = [];
@@ -222,7 +103,7 @@ export default class Database {
222103
let dictData = null;
223104
for (let i = 0; i < listUserFiles.length; i++) {
224105
dictData = listUserFiles[i];
225-
allFiles.push(dictData.NAME);
106+
allFiles.push(dictData.name);
226107
}
227108
Database._fcnF(fcn, allFiles);
228109
});
@@ -257,9 +138,10 @@ export default class Database {
257138
Database._hasFile(url, function (bHas) {
258139
let queryJson = {};
259140
if (bHas)
260-
queryJson["stmt"] = "UPDATE USERFILES SET CONTEXT = ? WHERE NAME = ?";
141+
queryJson["stmt"] = "update userfiles set context = ? where name = ?";
261142
else
262-
queryJson["stmt"] = "INSERT INTO USERFILES (CONTEXT,NAME) VALUES (?, ?)";
143+
queryJson["stmt"] = "insert into userfiles (context,name) values (?, ?)";
144+
263145
queryJson["values"] = [plaindata, url];
264146
Database._stmtBool(JSON.stringify(queryJson), function (ok) {
265147
let re = (!ok) ? "-1" : url;
@@ -278,7 +160,7 @@ export default class Database {
278160
}
279161

280162
let queryJson = {};
281-
queryJson["stmt"] = "SELECT CONTEXT FROM USERFILES WHERE NAME = ?";
163+
queryJson["stmt"] = "select context from userfiles where name = ?";
282164
queryJson["values"] = [url];
283165
Database._queryData(JSON.stringify(queryJson), function (listData) {
284166
if (listData == null) {
@@ -289,7 +171,7 @@ export default class Database {
289171
return;
290172
}
291173

292-
let strContent = listData[0].CONTEXT;
174+
let strContent = listData[0].context;
293175
Database._fcnF(fcn, strContent);
294176
});
295177
});
@@ -305,21 +187,21 @@ export default class Database {
305187
let key = Database.getMD5(contents);
306188
let md5 = "{0}.{1}".format(key, ext);
307189
if (window.Settings.enableLog)
308-
WebUtils.log("Database.setmedia({0}) - {1}".format(ext,md5));
190+
WebUtils.log("Database.setmedia({0}) - {1}".format(ext, md5));
309191
Database._writeToURL(md5, contents, fcn);
310192
}
311193

312194
static getmedia(filename, fcn) {
313195
if (window.Settings.enableLog)
314196
WebUtils.log("Database.getmedia({0})".format(filename));
315-
let url = Database._getDocumentPath(filename);
316-
Database._initWithContentsOfURL(url, function (data) {
317-
if (data == null) {
318-
Database._fcnF(fcn, null);
319-
return;
320-
}
321-
Database._fcnF(fcn, data);
322-
});
197+
let url = Database._getDocumentPath(filename);
198+
Database._initWithContentsOfURL(url, function (data) {
199+
if (data == null) {
200+
Database._fcnF(fcn, null);
201+
return;
202+
}
203+
Database._fcnF(fcn, data);
204+
});
323205
}
324206

325207
static getMD5(str) {
@@ -338,7 +220,7 @@ export default class Database {
338220
}
339221

340222
let queryJson = {};
341-
queryJson["stmt"] = "DELETE FROM USERFILES WHERE NAME = ?";
223+
queryJson["stmt"] = "delete from userfiles where name = ?";
342224
queryJson["values"] = [str];
343225
Database._stmtBool(JSON.stringify(queryJson), function (ok) {
344226
Database._fcnF(fcn, ok);
@@ -350,7 +232,7 @@ export default class Database {
350232
if (window.Settings.enableLog)
351233
WebUtils.log("Database.removeAllFiles()");
352234
let queryJson = {};
353-
queryJson["stmt"] = "DELETE FROM USERFILES";
235+
queryJson["stmt"] = "delete from userfiles";
354236
queryJson["values"] = [];
355237
Database._stmtBool(JSON.stringify(queryJson), function (ok) {
356238
Database._fcnF(fcn, ok);

src/tablet/IO.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ export default class IO {
177177
}
178178
return res;
179179
}
180-
static getImageDataURL (md5, data) {
180+
static getImageDataURL(md5, data) {
181+
if (data == null)
182+
console.log("Error: getImageDataURL(" + md5 + ",null) -- Not exist.");
181183
var header = '';
182184
switch (IO.getExtension(md5)) {
183185
case 'svg': header = 'data:image/svg+xml;base64,';
@@ -265,7 +267,7 @@ export default class IO {
265267
json.values = [obj.version, obj.deleted, obj.name, JSON.stringify(obj.json),
266268
JSON.stringify(obj.thumbnail), (new Date()).getTime().toString()];
267269
json.stmt = 'update ' + database + ' set ' + keylist.toString() + ' where id = ' + obj.id;
268-
console.log("saveProject:"+JSON.stringify(json));
270+
// console.log("saveProject:"+JSON.stringify(json));
269271
OS.stmt(json, fcn);
270272
}
271273

@@ -369,8 +371,8 @@ export default class IO {
369371
}
370372
}
371373

372-
console.log("projectMetadata");
373-
console.log(projectMetadata);
374+
//console.log("projectMetadata");
375+
//console.log(projectMetadata);
374376

375377
// Get the media in projectMetadata and add it to a zip file
376378
zipFile = new JSZip();

src/tablet/Web.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ String.prototype.format = function () {
1414

1515
export default class Web {
1616
static init() {
17-
Database.open("ScratchJr");
18-
// Database.test();
17+
Database.open("Guest");
1918
SoundPlayer.init(function (nRe) {
2019
if (nRe == 0)
2120
RecordSound.loadAllSound()
2221
});
23-
// WebUtils.test()
2422
}
2523

2624
static database_stmt(json, fcn) {
@@ -40,7 +38,7 @@ export default class Web {
4038
WebUtils.log("Web.io_cleanassets({0})".format(fileType));
4139
Database.cleanassets(fileType, fcn);
4240
}
43-
41+
4442
static io_getsettings() {
4543
if (window.Settings.enableLog)
4644
WebUtils.log("Web.io_getsettings()");
@@ -151,15 +149,15 @@ export default class Web {
151149
}
152150

153151
static scratchjr_cameracheck() {
154-
WebUtils.log("scratchjr_cameracheck()");
152+
WebUtils.log("Web.scratchjr_cameracheck()");
155153
}
156154

157155
static scratchjr_startfeed(str) {
158-
WebUtils.log("scratchjr_startfeed({0})".format(str));
156+
WebUtils.log("Web.scratchjr_startfeed({0})".format(str));
159157
}
160158

161159
static scratchjr_stopfeed() {
162-
WebUtils.log("scratchjr_stopfeed()");
160+
WebUtils.log("Web.scratchjr_stopfeed()");
163161
}
164162

165163
static scratchjr_choosecamera(body) {
@@ -171,7 +169,7 @@ export default class Web {
171169
}
172170

173171
static hideSplash() {
174-
WebUtils.log("Web.hideSplash()".format());
172+
WebUtils.log("Web.hideSplash()");
175173
}
176174

177175
static sendSjrUsingShareDialog(fileName, emailSubject, emailBody, shareType, b64data) {

0 commit comments

Comments
 (0)