-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwebsqldump.js
156 lines (147 loc) · 4.47 KB
/
websqldump.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
/*
* websqldump.js v1.0.0
* Copyright 2016 Steven de Salas
* http://github.com/sdesalas/websqldump
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function(window, undefined) {
var wd = {};
// Default config
wd.config = {
database: undefined,
table: undefined, // undefined to export all tables
version: '1.0',
info: '',
dbsize: 5 * 1024 * 1024, // 5MB
linebreaks: false,
schemaonly: false,
dataonly: false,
success: function(sql) {
console.log(sql);
},
error: function(message) {
throw new Error(message);
}
}
wd.exportTable = function(config) {
// Use closure to avoid overwrites
var table = config.table;
// Export schema
if (!config.dataonly) {
wd.execute({
db: config.db,
sql: "SELECT sql FROM sqlite_master WHERE type='table' AND tbl_name=?;",
params: [table],
success: function(results) {
if (!results.rows || !results.rows.length) {
if (typeof(config.error) === "function") config.error('No such table: ' + table);
return;
}
config.exportSql.push(results.rows.item(0)["sql"]);
if (config.schemaonly) {
if (typeof(config.success) === "function") config.success(config.exportSql.toString());
return;
}
}
});
}
// Export data
if (!config.schemaonly) {
wd.execute({
db: config.db,
sql: "SELECT * FROM '" + table + "';",
success: function(results) {
if (results.rows && results.rows.length) {
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var _fields = [];
var _values = [];
for (var col in row) {
_fields.push(col);
_values.push('"' + row[col] + '"');
}
config.exportSql.push("INSERT INTO " + table + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")");
}
}
if (typeof(config.success) === "function") config.success(config.exportSql.toString());
},
error: function(err) {
if (typeof(config.error) === "function") config.error(err);
}
});
}
}
wd.export = function(config) {
// Apply defaults
for (var prop in wd.config) {
if (typeof config[prop] === 'undefined') config[prop] = wd.config[prop];
}
config.db = wd.open(config);
config.exportSql = config.exportSql || [];
config.exportSql.toString = function() {return this.join(config.linebreaks ? ';\n' : '; ') + ';';}
if (config.table) {
wd.exportTable(config);
} else {
config.exported = []; // list of exported tables
config.outstanding = []; // list of outstanding tables
var success = config.success; config.success = function() {
config.exported.push(config.table);
// Check if its all done
if (config.exported.length >= config.outstanding.length) {
if (typeof(success) === "function") success(config.exportSql.toString());
}
}
// Export all tables in db
wd.execute({
db: config.db,
sql: "SELECT tbl_name FROM sqlite_master WHERE type='table';",
success: function(results) {
if (results.rows) {
// First count the outstanding tables
var tbl_name;
for (var i = 0; i < results.rows.length; i++) {
tbl_name = results.rows.item(i)["tbl_name"];
if (tbl_name.indexOf('__WebKit') !== 0) // skip webkit internals
config.outstanding.push(tbl_name);
}
// Then export them
for (var i = 0; i < config.outstanding.length; i++) {
config.table = config.outstanding[i];
wd.exportTable(config);
}
}
},
error: function(err) {
if (typeof(error) === "function") error(transaction, err);
}
});
}
};
wd.open = function(config) {
if (!config) throw new Error('Please use a config object');
if (!config.database) throw new Error('Please define a config database name.');
return window.openDatabase(config.database, config.version || '1.0', config.info || '', config.dbsize || 512000);
};
// Helper method for executing SQL code in DB
wd.execute = function(config) {
if (!config) throw new Error('Please use a config object');
if (!config.db) throw new Error('Please define a db obj to execute against');
if (!config.sql) throw new Error('Please define some sql to execute.');
config.db.transaction(function(transaction){
transaction.executeSql(config.sql, config.params || [],
function(transaction, results) {
if (typeof(config.success) === "function") config.success(results);
},
function(transaction, err) {
if (typeof(config.error) === "function") config.error(err);
}
);
});
};
window.websqldump = {
export: function() {
wd.export.apply(wd, arguments);
}
};
})(this);