-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebdb.js
319 lines (300 loc) · 9.11 KB
/
webdb.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
function init_web_database()
{
mbdatabase = new mbdata();
mbdatabase.open_database();
if( mbdatabase.db != false )
{
mbdatabase.init_table();
}
}
mbdata = function()
{
var self = this;
}
mbdata.prototype =
{
get db ()
{
if (!("_db" in this))
this._db = {};
return this._db;
},
set db ( x )
{
this._db = x;
},
}
mbdata.prototype.open_database = function( )
{
try
{
if ( window.openDatabase )
{
if( mbtweet.debug )
{
mbtweet.database.name += "_dev";
}
this.db = openDatabase( mbtweet.database.name, mbtweet.database.version , "database pbedit image datarray" , 200000 );
if (!this.db)
{
alert("Some problem occurs. It may your database storage size limitation is too small for this application\nデータベースストレージ用の容量が不足しているなどの問題が発生しました。");
this.db = false;
}
}
else //not in case that not support Web Database API
{
alert("Your browser does not support Web SQL Database API.");
this.db = false;
}
}
catch(error)
{
if( mbtweet.debug ) window.console.log( error.message );
}
}
mbdata.prototype.init_table = function()
{
this.db.transaction(
function( tx )
{
tx.executeSql(
"SELECT user_data.screen_name , status_data.status_id FROM user_data , status_data LIMIT 1",
[],
function( tx , result )
{
reduce_old_rows( tx );
},
function( tx , error )
{
tx.executeSql(
"CREATE TABLE status_data (status_id TEXT ,created_at TEXT ,in_reply_to_screen_name TEXT ,in_reply_to_status_id TEXT ,in_reply_to_user_id TEXT ,favorited BOOL ,geo TEXT ,source TEXT ,text TEXT ,truncated BOOL, screen_name TEXT, profile_image_url TEXT)",
[],
function( tx ){},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on creating user table" , error );
}
);
tx.executeSql(
"CREATE TABLE user_data (created_at TEXT , description TEXT , favourites_count NUMBER , followers_count NUMBER , following NUMBER , friends_count NUMBER , geo_enabled BOOL , user_id TEXT , location TEXT , name TEXT , notifications BOOL , user_protected BOOL , screen_name TEXT , statuses_count NUMBER , time_zone TEXT , utc_offset TEXT , url TEXT , verified NUMBER , profile_background_color TEXT , profile_background_image_url TEXT , profile_background_tile TEXT , profile_image_url TEXT , profile_link_color TEXT , profile_sidebar_border_color TEXT , profile_sidebar_fill_color TEXT , profile_text_color TEXT)",
[],
function( tx ){},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on creating user table" , error );
}
);
}
);
}
);
}
mbdata.prototype.save_status = function( status_json )
{
this.db.transaction(
function( tx )
{
var status = eval('(' + status_json + ')');
// window.console.log( status );
tx.executeSql(
"SELECT status_id FROM status_data WHERE status_id = ?",
[
status._status_id + ""
],
function( tx , result )
{
if( result.rows.length == 0 )
{
( function( tx , status ){ insert_status( tx , status ) } )( tx , status );
}
else if( result.rows.length > 0 )
{
tx.executeSql(
"DELETE FROM status_data WHERE status_id = ?",
[
status._status_id + ""
],
function( tx , result ){
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on remove_user(): " , error );
}
);
( function( tx , status ){ insert_status( tx , status ) } )( tx , status );
}
},
function( tx , error)
{
//mbdatabase.save( user , "new");
if( mbtweet.debug )window.console.log( "Error on save_user(): " , error );
}
);
}
);
}
mbdata.prototype.save_user = function( user_json )
{
this.db.transaction(
function( tx )
{
var user = eval('(' + user_json + ')');
tx.executeSql(
"SELECT user_id FROM user_data WHERE screen_name = ?",
[
user._screen_name
] ,
function( tx , result )
{
//window.console.log( result.rows );
if( result.rows.length == 0 )
{
( function( tx , user ){ insert_user( tx , user ) } )( tx , user );
//mbdatabase.save( user_json , "new" );
}
else if( result.rows.length > 0 )
{
//window.console.log( user._screen_name , result.rows.length );
tx.executeSql(
"DELETE FROM user_data WHERE screen_name = ?",
[
user._screen_name
],
function( tx , result ){
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on remove_user(): " , error );
}
);
// mbdatabase.save( user_json , "new" );
( function( tx , user ){ insert_user( tx , user ) } )( tx , user );
}
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on save_user(): " , error );
}
);
}
);
}
insert_status = function( tx , status )
{
tx.executeSql(
"INSERT INTO status_data ( status_id , created_at , in_reply_to_screen_name , in_reply_to_status_id , in_reply_to_user_id , favorited , geo , source , text , truncated , screen_name , profile_image_url ) VALUES (? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,?, ?)" ,
[
status._status_id + "",
status._created_at ,
status._in_reply_to_screen_name ,
status._in_reply_to_status_id + "",
status._in_reply_to_user_id + "",
status._favorited ,
JSON.stringify( status._geo ) ,
status._source ,
status._text ,
status._truncated ,
status._screen_name ,
status._profile_image_url
],
function( tx )
{
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on insert tweet status(): " , error );
}
);
}
insert_user = function( tx , user )
{
tx.executeSql(
"INSERT INTO user_data (created_at , description , favourites_count , followers_count , following , friends_count , geo_enabled , user_id , location , name , notifications , user_protected , screen_name , statuses_count , time_zone , utc_offset , url , verified , profile_background_color , profile_background_image_url , profile_background_tile , profile_image_url , profile_link_color , profile_sidebar_border_color , profile_sidebar_fill_color , profile_text_color ) VALUES (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)" ,
[
"" , //user._created_at ,
"" , //user._description ,
"" , //user._favourites_count ,
user._followers_count ,
user._following ,
user._friends_count ,
user._geo_enabled ,
(user._user_id + "") ,
user._location ,
user._name ,
user._notifications ,
user._user_protected ,
user._screen_name ,
user._statuses_count ,
"" , //user._time_zone ,
"" , //user._utc_offset ,
user._url ,
user._verified ,
"" , //user._profile_background_color ,
"" , //user._profile_background_image_url ,
"" , //user._profile_background_tile ,
user._profile_image_url ,
"" , //user._profile_link_color ,
"" , //user._profile_sidebar_border_color ,
"" , //user._profile_sidebar_fill_color ,
"" //user._profile_text_color
],
function( tx )
{
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on save_asnew_user(): " , error );
}
);
}
mbdata.prototype.maintain_tables = function()
{
this.db.transaction(
function( tx )
{
tx.executeSql(
"SELECT user_id FROM user_data LIMIT 1",
[],
function( tx , result )
{
},
function( tx , error )
{
tx.executeSql(
"CREATE TABLE status_data (status_id TEXT ,created_at TEXT ,in_reply_to_screen_name TEXT ,in_reply_to_status_id TEXT ,in_reply_to_user_id TEXT ,favorited BOOL ,geo TEXT ,source TEXT ,text TEXT ,truncated BOOL, screen_name TEXT, profile_image_url TEXT)",
[],
function( tx ){},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on creating user table" , error );
}
);
tx.executeSql(
"CREATE TABLE user_data (created_at TEXT , description TEXT , favourites_count NUMBER , followers_count NUMBER , following NUMBER , friends_count NUMBER , geo_enabled BOOL , user_id TEXT , location TEXT , name TEXT , notifications BOOL , user_protected BOOL , screen_name TEXT , statuses_count NUMBER , time_zone TEXT , utc_offset TEXT , url TEXT , verified NUMBER , profile_background_color TEXT , profile_background_image_url TEXT , profile_background_tile TEXT , profile_image_url TEXT , profile_link_color TEXT , profile_sidebar_border_color TEXT , profile_sidebar_fill_color TEXT , profile_text_color TEXT)",
[],
function( tx ){},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error on creating user table" , error );
}
);
}
);
}
);
}
reduce_old_rows = function( tx )
{
var_sql_string = "DELETE FROM status_data WHERE status_id NOT IN (SELECT status_id FROM status_data ORDER BY status_id DESC LIMIT 5000)";
tx.executeSql(
var_sql_string ,
[],
function( tx )
{
},
function( tx , error)
{
if( mbtweet.debug )window.console.log( "Error in reduce_old_rows(): " , error );
}
);
}