Skip to content

Commit bc8588c

Browse files
committed
Working on sqlite library
1 parent 58b1867 commit bc8588c

File tree

9 files changed

+171
-53
lines changed

9 files changed

+171
-53
lines changed

content/bookmarks.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div id="request-history-dropdown" class="container-fluid" style="display: none;">
1+
<div id="request-history-dropdown" class="container-fluid">
22
<div class="span3">
33
<h1><i class="icon-time"></i> Recent Requests</h1>
44
<ol class="request-history-recent">

content/js/bootstrap-tagsinput.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//http://timschlechter.github.io/bootstrap-tagsinput/examples/#accordion_example_markup
12
(function ($) {
23
"use strict";
34

content/js/restclient.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ if (typeof (restclient) === "undefined") {
6767
try{
6868
console.error(o);
6969
}catch(e){}
70+
},
71+
generateUUID: function (){
72+
try{
73+
var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"]
74+
.getService(Components.interfaces.nsIUUIDGenerator);
75+
var uuid = uuidGenerator.generateUUID();
76+
uuid = uuid.toString();
77+
return uuid.substring(1,uuid.length-1);
78+
}catch(e){
79+
restclient.error(e.message);
80+
}
7081
}
7182
}
7283
}

content/js/restclient.main.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ restclient.main = {
5151
init: function () {
5252
restclient.init();
5353
restclient.sqlite.open();
54-
restclient.sqlite.initTables();
5554

5655
this.initSkin();
5756

@@ -168,23 +167,6 @@ restclient.main = {
168167
window.scrollTo(0,0);
169168
}
170169
},
171-
clearCachedRequests: function() {
172-
var num = restclient.sqlite.getRequestCount();
173-
174-
if(num > 0)
175-
restclient.sqlite.initTables(true);
176-
restclient.message.show({
177-
id: 'alertCachedRequestCleared',
178-
type: 'message',
179-
title: 'Cached requests are cleared!',
180-
message: num + ' records have been removed.',
181-
buttons: [
182-
{title: 'Okay', class: 'btn-danger', callback: function () { $('#alertCachedRequestCleared').alert('close'); }}
183-
],
184-
parent: $('#request-error'),
185-
exclude: true
186-
});
187-
},
188170
changeSkin: function (theme) {
189171
restclient.log("css/themes/" + theme + "/bootstrap.css");
190172
$("link").remove();
@@ -368,13 +350,15 @@ restclient.main = {
368350
if($('#curl').is(':visible')) {
369351
$('.enable-curl').attr('data-curl', 'enable');
370352
$('.enable-curl').text('Disable Curl');
353+
$('.curl-menu').show();
371354
restclient.main.updateCurlCommand();
372355
restclient.setPref('enableCurl', true);
373356
}
374357
else
375358
{
376359
$('.enable-curl').attr('data-curl', 'disabled');
377360
$('.enable-curl').text('Enable Curl');
361+
$('.curl-menu').hide();
378362
restclient.setPref('enableCurl', false);
379363
}
380364
});
@@ -607,6 +591,7 @@ restclient.main = {
607591
}).on('hidden', function () {
608592
$(this).data('source', null);
609593
});
594+
610595
//TODO update bookmark modal to add label
611596
$('#modal-bookmark-request').on('show', function () {
612597
var savedRequest = restclient.getPref('savedRequest', '');
@@ -1052,13 +1037,12 @@ restclient.main = {
10521037
return false;
10531038
},
10541039
showResponse: function () {
1055-
10561040
$("#response").show();
1057-
1058-
//document.getElementById('response').scrollIntoView(true);
1059-
//alert(top);
10601041
document.getElementById('response').scrollIntoView(true);
1061-
//$('html, body').animate({scrollTop: top}, 1000);
1042+
return false;
1043+
},
1044+
showCurl: function() {
1045+
document.getElementById('curl').scrollIntoView(true);
10621046
return false;
10631047
},
10641048
clearResult: function () {
@@ -1948,14 +1932,16 @@ restclient.main = {
19481932
});
19491933
return false;
19501934
}
1951-
restclient.sqlite.saveRequest(request, null, null, null, function() {
1935+
restclient.sqlite.saveHistory(request, function() {
19521936
if(arguments.length > 0)
19531937
{
19541938
if(location.hash.substr(1) === arguments[0])
19551939
return false;
19561940
restclient.main.ignoreHashChange = true;
19571941
location.hash = arguments[0];
19581942
}
1943+
}, function() {
1944+
//TODO alert unable to save history to sqlite
19591945
});
19601946
restclient.http.sendRequest(request.method, request.url, request.headers, request.overrideMimeType, request.body);
19611947
},

content/js/restclient.overlay.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ restclient.overlay = {
5555
firstRun : function() {
5656
var firstRunPref = "firstRunDone",
5757
versionPref = "version",
58-
versionNumber = "2.0.4",
58+
versionNumber = "2.1.0",
5959
browser = restclient.overlay.getBrowser();
6060

6161
/*try{
@@ -75,8 +75,9 @@ restclient.overlay = {
7575
}
7676
if(restclient.getPref(versionPref, '') != versionNumber) {
7777
browser.selectedTab = browser.addTab("http://www.restclient.net/?browser=firefox&version=" + versionNumber);
78-
//restclient.setPref(versionPref, versionNumber);
78+
restclient.setPref(versionPref, versionNumber);
7979
restclient.setPref('defaultSkin', 'simplex');
80+
//Migrate favorite requests from preference to sqlite.
8081
restclient.sqlite.migrateFavoriteRequest();
8182
}
8283
},

content/js/restclient.sqlite.js

Lines changed: 128 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,44 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
restclient.sqlite = {
3232
db: null,
3333
tables: {
34-
requests: "id TEXT PRIMARY KEY, name TEXT, favorite INT default 0, request_method TEXT, request_url TEXT, request_body TEXT, request TEXT,created_datetime TEXT,last_executed TEXT"
34+
requests: " uuid TEXT NOT NULL PRIMARY KEY, \
35+
requestName TEXT NOT NULL, \
36+
requestUrl TEXT NOT NULL, \
37+
requestMethod TEXT NOT NULL, \
38+
request TEXT NOT NULL, \
39+
creationTime INTEGER NOT NULL, \
40+
lastAccess INTEGER NOT NULL",
41+
labels: " labelName TEXT NOT NULL, \
42+
uuid TEXT NOT NULL",
43+
history: "requestId TEXT PRIMARY KEY, \
44+
request TEXT NOT NULL, \
45+
lastAccess INTEGER NOT NULL"
46+
},
47+
sql: {
48+
queryHistory: 'SELECT request FROM history WHERE requestId = :requestId',
49+
updateHistory: 'UPDATE history SET lastAccess = :lastAccess WHERE requestId = :requestId',
50+
newHistory: 'INSERT INTO history (requestId, request, lastAccess) VALUES (:requestId, :request, :lastAccess)',
51+
removeHistory: 'DELETE FROM history WHERE lastAccess < :lastAccess',
52+
53+
queryLabels: 'SELECT count(labelName),labelName FROM labels GROUP BY labelName',
54+
newLabels: 'INSERT INTO labels (labelName, uuid) VALUES (:labelName, :uuid)',
55+
removeLabels: 'DELETE FROM labels WHERE uuid = :uuid',
56+
57+
queryRequests: 'SELECT * FROM requests WHERE uuid = :uuid',
58+
queryRequestsByLabel: 'SELECT * FROM requests WHERE uuid IN (SELECT uuid FROM labels WHERE labelName = :labelName)',
59+
newRequests: 'INSERT INTO requests (uuid, requestName, requestUrl, requestMethod, request, creationTime, LastAccess) VALUES (:uuid, :requestName, :requestUrl, :requestMethod, :request, :creationTime, :LastAccess)',
60+
removeRequests: 'DELETE FROM requests WHERE uuid = :uuid'
3561
},
3662
open: function() {
3763
try{
3864
var file = restclient.FileUtils.getFile("ProfD", ["restclient.sqlite"]);
39-
//console.log(file.path);
65+
restclient.log(file.path);
4066
restclient.sqlite.db = restclient.Services.storage.openDatabase(file);
67+
restclient.sqlite.initStatements();
4168
return true;
4269
}
4370
catch(e) {
44-
console.error(e);
71+
restclient.error(e);
4572
}
4673
return false;
4774
},
@@ -50,22 +77,104 @@ restclient.sqlite = {
5077
restclient.db.asyncClose();
5178
}
5279
catch(e) {
53-
console.error(e);
80+
restclient.error(e);
5481
}
5582
},
56-
initTables: function(force) {
57-
if(restclient.sqlite.db.tableExists('requests'))
58-
{
59-
if (force)
60-
{
61-
restclient.sqlite.db.executeSimpleSQL('DELETE FROM requests WHERE favorite = 0');
62-
}
63-
}
64-
else
83+
initTables: function() {
84+
try{
6585
restclient.sqlite.db.createTable('requests', restclient.sqlite.tables['requests']);
86+
restclient.sqlite.db.createTable('labels', restclient.sqlite.tables['labels']);
87+
restclient.sqlite.db.createTable('history', restclient.sqlite.tables['history']);
88+
}
89+
catch(e) {
90+
restclient.error(e);
91+
}
92+
},
93+
getStatement: function(sqlName) {
94+
return restclient.sqlite.db.createStatement(restclient.sqlite.sql[sqlName]);
6695
},
67-
saveRequest: function(request, name, favorite, labels, callback) {
68-
var id = "request-" + restclient.helper.sha1(JSON.stringify(request));
96+
getHistory: function(requestId){
97+
if(typeof requestId !== 'string' || requestId === '')
98+
return false;
99+
var stmt = restclient.sqlite.getStatement('queryHistory');
100+
var params = stmt.newBindingParamsArray(),
101+
binding = params.newBindingParams();
102+
103+
binding.bindByName("requestId", requestId);
104+
params.addParams(binding);
105+
stmt.bindParameters(params);
106+
107+
while (stmt.executeStep()) {
108+
return stmt.row.request;
109+
}
110+
return false;
111+
},
112+
saveHistory: function(request, success, handleError) {
113+
var requestStr = JSON.stringify(request);
114+
var requestId = "r-" + restclient.helper.sha1(requestStr);
115+
var lastAccess = new Date().valueOf();
116+
var exists = restclient.sqlite.getHistory(requestId);
117+
118+
var sqlName = (exists === false) ? "newHistory" : "updateHistory";
119+
var stmt = restclient.sqlite.getStatement(sqlName);
120+
try{
121+
var params = stmt.newBindingParamsArray(),
122+
binding = params.newBindingParams();
123+
124+
binding.bindByName("requestId", requestId);
125+
if (exists === false)
126+
binding.bindByName("request", requestStr);
127+
binding.bindByName("lastAccess", lastAccess);
128+
params.addParams(binding);
129+
stmt.bindParameters(params);
130+
stmt.executeAsync({
131+
handleError: function(aError) {
132+
restclient.error(aError);
133+
if(typeof handleError === 'function')
134+
handleError.apply(restclient.main, [request]);
135+
},
136+
handleCompletion: function(aReason) {
137+
if (aReason == Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED
138+
&& typeof success === 'function')
139+
success.apply(restclient.main, [requestId]);
140+
}
141+
});
142+
}finally{
143+
stmt.reset();
144+
}
145+
},
146+
removeHistory: function(days, success, handleError) {
147+
var lastAccess = new Date();
148+
lastAccess.setDate(date.getDate() - days);
149+
lastAccess = lastAccess.valueOf();
150+
var stmt = restclient.sqlite.getStatement('removeHistory');
151+
try{
152+
var params = stmt.newBindingParamsArray(),
153+
binding = params.newBindingParams();
154+
155+
binding.bindByName("lastAccess", lastAccess);
156+
157+
params.addParams(binding);
158+
stmt.bindParameters(params);
159+
stmt.executeAsync({
160+
handleError: function(aError) {
161+
restclient.error(aError);
162+
if(typeof handleError === 'function')
163+
handleError.apply(restclient.main, [request]);
164+
},
165+
handleCompletion: function(aReason) {
166+
if (aReason == Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED
167+
&& typeof success === 'function')
168+
success.apply(restclient.main, [requestId]);
169+
}
170+
});
171+
}finally{
172+
stmt.reset();
173+
}
174+
},
175+
saveRequest: function(request, name, labels, success, handleError) {
176+
return false;
177+
var id = "r-" + restclient.helper.sha1(JSON.stringify(request));
69178
var savedRequest = restclient.sqlite.getRequest(id);
70179
var last_executed = new Date().valueOf();
71180
name = name || '';
@@ -127,6 +236,7 @@ restclient.sqlite = {
127236
});
128237
}
129238
},
239+
130240
getRequest: function(id){
131241
if(typeof id !== 'string' || id === '')
132242
return false;
@@ -142,8 +252,8 @@ restclient.sqlite = {
142252
}
143253
return false;
144254
},
145-
getRequestCount: function(){
146-
var stmt = restclient.sqlite.db.createStatement("SELECT count(id) as num FROM requests");
255+
getLabels: function(){
256+
var stmt = restclient.sqlite.db.createStatement("SELECT sum(label) as num FROM requests");
147257
while (stmt.executeStep()) {
148258
return stmt.row.num;
149259
}
@@ -165,7 +275,7 @@ restclient.sqlite = {
165275
dump('savedRequest:' + requests);
166276
if( requests === '')
167277
return false;
168-
278+
169279
restclient.sqlite.open();
170280
restclient.sqlite.initTables();
171281
restclient.sqlite.importRequestFromJSON(JSON.parse(requests));

content/restclient.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<ul class="dropdown-menu">
6262
<li><a style="cursor:pointer" onclick="restclient.main.showRequest();return false;" class="request-menu" >Request</a></li>
6363
<li><a style="cursor:pointer" onclick="restclient.main.showResponse();return false;">Response</a></li>
64+
<li><a style="cursor:pointer" onclick="restclient.main.showCurl();return false;" class="curl-menu">Curl</a></li>
6465
</ul>
6566
</li>
6667
</ul>

content/tests/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html>
33
<head>
44
<title>RESTClient Test Suite</title>
5+
<meta charset="utf-8" />
56
<script src="../js/jquery.js"></script>
67
<!-- qunit -->
78
<link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
@@ -13,8 +14,8 @@
1314
<script src="../js/restclient.sqlite.js"></script>
1415

1516
<!-- unit tests -->
16-
<script src="unit/restclient.helper.js"></script>
17-
<script src="unit/restclient.oauth.js"></script>
17+
<!--<script src="unit/restclient.helper.js"></script>
18+
<script src="unit/restclient.oauth.js"></script>//-->
1819
<script src="unit/restclient.sqlite.js"></script>
1920
<body>
2021
<div>

0 commit comments

Comments
 (0)