-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRLOfflineDatabaseStore.j
120 lines (101 loc) · 3.63 KB
/
RLOfflineDatabaseStore.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* RLOfflineDataBaseStore.j
* AppKit
*
* Created by Randall Luecke.
* Copyright 20010, Randall Luecke
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@implementation RLOfflineDatabaseStore : CPObject
{
CPString _name;
double _size;
id _db;
id _delegate;
}
+ (BOOL)offlineDataStoreIsAvailable
{
return !!window.openDatabase
}
- (id)initWithName:(CPString)aName delegate:(id)anObject
{
_delegate = anObject;
if (![RLOfflineDataStore offlineDataStoreIsAvailable] && [_delegate respondsToSelector:@selector(dataStoreIsNotSupported)])
{
[_delegate dataStoreIsNotSupported];
return;
}
self = [super init];
if (self)
{
_name = aName;
_size = 1024 * 2000;
_db = openDatabase('RCOfflineDataStore-' + _name, '1.0', _name, _size);
if(!_db && [_delegate respondsToSelector:@selector(userDidRejectDatabase)])
{
[_delegate userDidRejectDatabase];
return;
}
else if(!_db)
{
[CPException exceptionWithName:@"RLOfflineDataStore" reason:@"Offline storage was rejected by the user." userInfo:nil];
return;
}
_db.transaction(function(_db){
_db.executeSql( 'CREATE TABLE IF NOT EXISTS RLOfflineDataStore (key TEXT UNIQUE NOT NULL PRIMARY KEY, value TEXT NOT NULL)' );
});
}
return self;
}
- (void)blahsetValue:(CPString)aValue forKey:(CPString)aKey
{
localStorage.setItem(aKey, aValue);
}
- (CPString)blahgetValueForKey:(CPString)aKey
{
return localStorage.getItem(aKey);
}
- (void)setValue:(CPString)aValue forKey:(CPString)aKey
{
//there is probably a better way to do this
_db.transaction(function(db){
//if the key exists update it, if not it will fail quietly.
db.executeSql("UPDATE RLOfflineDataStore SET value = ? WHERE key = ?",[aValue, aKey], function result(tx, rs){}, function anError(tx, err){});
//if the key exists this will fail quietyl, if not it will add the key to the db.
db.executeSql("INSERT INTO RLOfflineDataStore (key, value) VALUES (?, ?)", [aKey, aValue], function result(db, rs){}, function error(db, err){});
});
}
- (void)getValueForKey:(CPString)aKey
{
_db.transaction(function(db){
db.executeSql("SELECT value FROM RLOfflineDataStore WHERE key=?",[aKey], function result(text, result){[self _parseResults:result]; [[CPRunLoop currentRunLoop] limitDateForMode:nil]}, function anError(text, theError){});
});
}
- (void)removeValueForKey:(CPString)aKey
{
_db.transaction(function(db){
db.executeSql("DELETE FROM RLOfflineDataStore WHERE key=?",[aKey], function result(text, result){}, function anError(text, theError){});
});
}
- (void)_parseResults:(id)results
{
if(results.rows.length > 0)
var returnValue = results.rows.item(0).value;
else
var returnValue = nil;
[_delegate didReciveData:returnValue];
}
@end