Skip to content

Commit bd6f89d

Browse files
committed
Issue #1 - Testing plugin, added rollback method
1 parent 8d1e7e8 commit bd6f89d

File tree

6 files changed

+241
-7
lines changed

6 files changed

+241
-7
lines changed

jSQL.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jSQL.js v1.6
2+
* jSQL.js v1.7
33
* A Javascript Query Language Database Engine
44
* @author Robert Parham
55
* @website https://github.com/Pamblam/jSQL#jsql
@@ -1931,7 +1931,7 @@
19311931
});
19321932
};
19331933

1934-
self.load = function(LoadCallback){
1934+
self.load = function(LoadCallback){
19351935
if("function" !== typeof LoadCallback) LoadCallback = function(){};
19361936
self.loadingCallbacks.push(LoadCallback);
19371937

@@ -1942,16 +1942,24 @@
19421942
if(self.isLoading) return;
19431943
self.isLoading = true;
19441944

1945+
self.rollback(function(){
1946+
self.isLoading = false;
1947+
self.loaded = true;
1948+
while(self.loadingCallbacks.length)
1949+
self.loadingCallbacks.shift()();
1950+
});
1951+
};
1952+
1953+
self.rollback = function(LoadCallback){
1954+
if("function" !== typeof LoadCallback) LoadCallback = function(){};
1955+
19451956
// Wait for the schema to be set up
19461957
(function waitForSchema(tries){
19471958
try{
19481959
self.api.select("jSQL_data_schema", function(r){
19491960
jSQL.tables = {};
19501961
if(r.length === 0){
1951-
self.isLoading = false;
1952-
self.loaded = true;
1953-
while(self.loadingCallbacks.length)
1954-
self.loadingCallbacks.shift()();
1962+
LoadCallback()
19551963
return;
19561964
}
19571965
for(var i=r.length; i--;){
@@ -2146,7 +2154,7 @@
21462154
////////////////////////////////////////////////////////////////////////////
21472155

21482156
return {
2149-
version: 1.6,
2157+
version: 1.7,
21502158
tables: {},
21512159
query: jSQLParseQuery,
21522160
createTable: createTable,
@@ -2160,6 +2168,7 @@
21602168
reset: jSQLReset,
21612169
minify: jSQLMinifier,
21622170
commit: persistenceManager.commit,
2171+
rollback: persistenceManager.rollback,
21632172
// legacy, to be removed at a later date
21642173
persist: persistenceManager.commit
21652174
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>jSQLEverywhere Example Client</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
</head>
8+
<body>
9+
<center>
10+
<h2>Client example 1</h2>
11+
<p style="width:60%;">Any jSQL committed to the server will be visible on any page which connects to the same jSQLEverywhere server</p>
12+
<div id="ServerCount">Connecting to server</div>
13+
<p style="width:60%;">Once the server has connected you open the other example links and you will see that it was loaded on this list.</p>
14+
<a href="example2.html">Example 2</a><br>
15+
<a href="example3.html">Example 3</a>
16+
</center>
17+
18+
<script src="../jSQLe.js"></script>
19+
<script>
20+
21+
// Load jSQLEverywhere which will load jSQL Server
22+
jSQLe.load({
23+
role: "client",
24+
server: "http://localhost/jSQL/plugins/jSQLEverywhere/server.html"
25+
});
26+
27+
// Once jSQL is loaded, we can use it
28+
jSQL.load(function(){
29+
var FileName = window.location.pathname.split("/").pop();
30+
var now = new Date();
31+
32+
// create a table on the serverif it does not exist
33+
jSQL.query("CREATE TABLE IF NOT EXISTS `ServerTest` (`FileName` VARCHAR, `Date` DATE)").execute();
34+
35+
// insert the current time and file name
36+
jSQL.query("INSERT INTO `ServerTest` VALUES (?, ?)").execute([FileName, now]);
37+
jSQL.commit();
38+
39+
drawTable();
40+
});
41+
42+
function drawTable(){
43+
var ServerCount = document.getElementById("ServerCount");
44+
setInterval(function(){
45+
var data = jSQL.query("SELECT * FROM `ServerTest` ORDER BY `Date` DESC").execute().fetchAll("ASSOC");
46+
var html = ["<table>"];
47+
for(var i=0; i<data.length; i++)
48+
html.push("<tr><td>"+i+"</td><td>"+data[i].FileName+"</td><td>"+data[i].Date.getTime()+"</td></tr>");
49+
ServerCount.innerHTML = html.join("")+"</table>";
50+
},1500);
51+
}
52+
</script>
53+
</body>
54+
</html>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>jSQLEverywhere Example Client</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
</head>
8+
<body>
9+
<center>
10+
<h2>Client example 2</h2>
11+
<p style="width:60%;">Any jSQL committed to the server will be visible on any page which connects to the same jSQLEverywhere server</p>
12+
<div id="ServerCount">Connecting to server</div>
13+
<p style="width:60%;">Once the server has connected you open the other example links and you will see that it was loaded on this list.</p>
14+
<a href="example1.html">Example 1</a><br>
15+
<a href="example3.html">Example 3</a>
16+
</center>
17+
18+
<script src="../jSQLe.js"></script>
19+
<script>
20+
21+
// Load jSQLEverywhere which will load jSQL Server
22+
jSQLe.load({
23+
role: "client",
24+
server: "http://localhost/jSQL/plugins/jSQLEverywhere/server.html"
25+
});
26+
27+
// Once jSQL is loaded, we can use it
28+
jSQL.load(function(){
29+
var FileName = window.location.pathname.split("/").pop();
30+
var now = new Date();
31+
32+
// create a table on the serverif it does not exist
33+
jSQL.query("CREATE TABLE IF NOT EXISTS `ServerTest` (`FileName` VARCHAR, `Date` DATE)").execute();
34+
35+
// insert the current time and file name
36+
jSQL.query("INSERT INTO `ServerTest` VALUES (?, ?)").execute([FileName, now]);
37+
jSQL.commit();
38+
39+
drawTable();
40+
});
41+
42+
function drawTable(){
43+
var ServerCount = document.getElementById("ServerCount");
44+
setInterval(function(){
45+
jSQL.rollback(function(){
46+
var data = jSQL.query("SELECT * FROM `ServerTest` ORDER BY `Date` DESC").execute().fetchAll("ASSOC");
47+
var html = ["<table>"];
48+
for(var i=0; i<data.length; i++)
49+
html.push("<tr><td>"+i+"</td><td>"+data[i].FileName+"</td><td>"+data[i].Date.getTime()+"</td></tr>");
50+
ServerCount.innerHTML = html.join("")+"</table>";
51+
});
52+
},1500);
53+
}
54+
</script>
55+
</body>
56+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>jSQLEverywhere Example Client</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
</head>
8+
<body>
9+
<center>
10+
<h2>Client example 3</h2>
11+
<p style="width:60%;">Any jSQL committed to the server will be visible on any page which connects to the same jSQLEverywhere server</p>
12+
<div id="ServerCount">Connecting to server</div>
13+
<p style="width:60%;">Once the server has connected you open the other example links and you will see that it was loaded on this list.</p>
14+
<a href="example1.html">Example 1</a><br>
15+
<a href="example2.html">Example 2</a>
16+
</center>
17+
18+
<script src="../jSQLe.js"></script>
19+
<script>
20+
21+
// Load jSQLEverywhere which will load jSQL Server
22+
jSQLe.load({
23+
role: "client",
24+
server: "http://localhost/jSQL/plugins/jSQLEverywhere/server.html"
25+
});
26+
27+
// Once jSQL is loaded, we can use it
28+
jSQL.load(function(){
29+
var FileName = window.location.pathname.split("/").pop();
30+
var now = new Date();
31+
32+
// create a table on the serverif it does not exist
33+
jSQL.query("CREATE TABLE IF NOT EXISTS `ServerTest` (`FileName` VARCHAR, `Date` DATE)").execute();
34+
35+
// insert the current time and file name
36+
jSQL.query("INSERT INTO `ServerTest` VALUES (?, ?)").execute([FileName, now]);
37+
jSQL.commit();
38+
39+
drawTable();
40+
});
41+
42+
function drawTable(){
43+
var ServerCount = document.getElementById("ServerCount");
44+
setInterval(function(){
45+
var data = jSQL.query("SELECT * FROM `ServerTest` ORDER BY `Date` DESC").execute().fetchAll("ASSOC");
46+
var html = ["<table>"];
47+
for(var i=0; i<data.length; i++)
48+
html.push("<tr><td>"+i+"</td><td>"+data[i].FileName+"</td><td>"+data[i].Date.getTime()+"</td></tr>");
49+
ServerCount.innerHTML = html.join("")+"</table>";
50+
},1500);
51+
}
52+
</script>
53+
</body>
54+
</html>

plugins/jSQLEverywhere/jSQLe.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* jSQLe.js v0.1
3+
* A Plugin that allows use of jSQL anywhere on your domain
4+
* @author Robert Parham
5+
* @website https://github.com/Pamblam/jSQL#jsql
6+
* @license http://www.apache.org/licenses/LICENSE-2.0
7+
*/
8+
9+
;window.jSQLe = (function(){
10+
"use strict";
11+
12+
var Role = "client";
13+
var ServerURL = "";
14+
var callbackStack = [];
15+
16+
// Temporary jSQL placeholder
17+
function createjSQLTemp(){
18+
window.jSQL = {load: function(cb){
19+
callbackStack.push(cb);
20+
}};
21+
}
22+
23+
function loadClient(){
24+
createjSQLTemp();
25+
var i = document.createElement("iframe");
26+
i.onload = function(){
27+
var win = i.contentWindow;
28+
window.jSQL = win.jSQL;
29+
for(var n=0; n<callbackStack.length; n++)
30+
jSQL.load(callbackStack[n]);
31+
};
32+
i.setAttribute("style", "visibility:hidden;display:none");
33+
i.setAttribute("src", ServerURL);
34+
document.getElementsByTagName("body")[0].appendChild(i);
35+
}
36+
37+
function load(params){
38+
if(undefined === params.role) params.role = "client";
39+
if(params.role == "client" && undefined === params.server)
40+
throw "Client side must include server url.";
41+
Role = params.role.toUpperCase();
42+
ServerURL = params.server;
43+
if(Role==="CLIENT") loadClient();
44+
}
45+
46+
////////////////////////////////////////////////////////////////////////////
47+
// Exposed Methods /////////////////////////////////////////////////////////
48+
////////////////////////////////////////////////////////////////////////////
49+
50+
return {
51+
version: 0.1,
52+
load: load
53+
};
54+
55+
}());

plugins/jSQLEverywhere/server.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<script src="../../jSQL.js"></script>
5+
</body>
6+
</html>

0 commit comments

Comments
 (0)