-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
277 lines (246 loc) · 8.46 KB
/
App.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
import React from 'react';
import { StyleSheet, Text, View, ListView } from 'react-native';
import SQLite from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(true);
let db;
export default class App extends React.Component {
constructor() {
super();
this.progress = [];
this.state = {
progress: [],
ds: new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2}
)
};
}
updateProgress = (text, resetState) => {
let progress = [];
if (!resetState) {
progress = [...this.progress];
}
progress.push(text);
this.progress = progress;
this.setState({
progress
});
}
componentWillUnmount(){
this.closeDatabase();
}
errorCB = (err) => {
console.log("error: ",err);
this.updateProgress("Error " + (err.message || err));
}
populateDatabase = (db) => {
this.updateProgress("Database integrity check")
this.updateProgress("select * from antes")
db.executeSql('select * from sqlite_master').then(([results]) =>{
this.updateProgress("Database is ready ... executing query ...");
this.updateProgress(JSON.stringify(results.rows.length));
}).catch((error) =>{
console.log("Received error: ", error)
this.updateProgress(JSON.stringify(error));
/*db.transaction(this.populateDB).then(() =>{
this.updateProgress("Database populated ... executing query ...")
db.transaction(this.queryEmployees).then((result) => {
console.log("Transaction is now finished");
this.updateProgress("Processing completed");
this.closeDatabase()});
});*/
});
}
populateDB = (tx) => {
this.updateProgress("Executing DROP stmts")
tx.executeSql('DROP TABLE IF EXISTS Employees;');
tx.executeSql('DROP TABLE IF EXISTS Offices;');
tx.executeSql('DROP TABLE IF EXISTS Departments;');
this.updateProgress("Executing CREATE stmts");
tx.executeSql('CREATE TABLE IF NOT EXISTS Version( '
+ 'version_id INTEGER PRIMARY KEY NOT NULL); ').catch((error) => {
this.errorCB(error)
});
tx.executeSql('CREATE TABLE IF NOT EXISTS Departments( '
+ 'department_id INTEGER PRIMARY KEY NOT NULL, '
+ 'name VARCHAR(30) ); ').catch((error) => {
this.errorCB(error)
});
tx.executeSql('CREATE TABLE IF NOT EXISTS Offices( '
+ 'office_id INTEGER PRIMARY KEY NOT NULL, '
+ 'name VARCHAR(20), '
+ 'longtitude FLOAT, '
+ 'latitude FLOAT ) ; ').catch((error) => {
this.errorCB(error)
});
tx.executeSql('CREATE TABLE IF NOT EXISTS Employees( '
+ 'employe_id INTEGER PRIMARY KEY NOT NULL, '
+ 'name VARCHAR(55), '
+ 'office INTEGER, '
+ 'department INTEGER, '
+ 'FOREIGN KEY ( office ) REFERENCES Offices ( office_id ) '
+ 'FOREIGN KEY ( department ) REFERENCES Departments ( department_id ));').catch((error) => {
this.errorCB(error)
});
this.updateProgress("Executing INSERT stmts")
tx.executeSql('INSERT INTO Departments (name) VALUES ("Client Services");');
tx.executeSql('INSERT INTO Departments (name) VALUES ("Investor Services");');
tx.executeSql('INSERT INTO Departments (name) VALUES ("Shipping");');
tx.executeSql('INSERT INTO Departments (name) VALUES ("Direct Sales");');
tx.executeSql('INSERT INTO Offices (name, longtitude, latitude) VALUES ("Denver", 59.8, 34.1);');
tx.executeSql('INSERT INTO Offices (name, longtitude, latitude) VALUES ("Warsaw", 15.7, 54.1);');
tx.executeSql('INSERT INTO Offices (name, longtitude, latitude) VALUES ("Berlin", 35.3, 12.1);');
tx.executeSql('INSERT INTO Offices (name, longtitude, latitude) VALUES ("Paris", 10.7, 14.1);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Sylvester Stallone", 2, 4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Elvis Presley", 2, 4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Leslie Nelson", 3, 4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Fidel Castro", 3, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Bill Clinton", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Margaret Thatcher", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Donald Trump", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Dr DRE", 2, 2);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Samantha Fox", 2, 1);');
console.log("all config SQL done");
}
queryEmployees = (tx) => {
console.log("Executing employee query");
tx.executeSql('SELECT a.name, b.name as deptName FROM Employees a, Departments b WHERE a.department = b.department_id and a.department=?', [3]).then(([tx,results]) => {
this.updateProgress("Query completed")
var len = results.rows.length;
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
this.updateProgress(`Empl Name: ${row.name}, Dept Name: ${row.deptName}`)
}
}).catch((error) => {
console.log(error);
});
}
loadAndQueryDB = () => {
this.updateProgress("Plugin integrity check ...");
SQLite.echoTest().then(() => {
this.updateProgress("Integrity check passed ...")
this.updateProgress("Opening database ...")
SQLite.openDatabase({key:'sameer@saachi123', name : "SQLiteCipher_2048.sqlite", createFromLocation : "~data/SQLiteCipher_2048.sqlite"}).then((DB) => {
db = DB;
this.updateProgress("Database OPEN");
this.populateDatabase(DB);
}).catch((error) => {
this.updateProgress("Error database ...")
console.log(error);
});
}).catch(error => {
this.updateProgress("echoTest failed - plugin not functional");
});
}
closeDatabase = () => {
if (db) {
console.log("Closing database ...");
this.updateProgress("Closing DB")
db.close().then((status) => {
this.updateProgress("Database CLOSED");
}).catch((error) => {
this.errorCB(error);
});
} else {
this.updateProgress("Database was not OPENED")
}
}
deleteDatabase = () => {
this.updateProgress("Deleting database")
SQLite.deleteDatabase(database_name).then(() => {
console.log("Database DELETED");
this.updateProgress("Database DELETED")
}).catch((error) => {
this.errorCB(error);
});
}
runDemo = () => {
console.log('running');
this.updateProgress("Starting SQLite Promise Demo",true);
this.loadAndQueryDB();
}
renderProgressEntry = (entry) => {
return (<View style={listStyles.li}>
<View>
<Text style={listStyles.liText}>{entry}</Text>
</View>
</View>)
}
render(){
let ds = new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2 });
return (<View style={styles.mainContainer}>
<View style={styles.toolbar}>
<Text style={styles.toolbarButton} onPress={this.runDemo}>
Run Demo
</Text>
<Text style={styles.toolbarButton} onPress={this.closeDatabase}>
Close DB
</Text>
<Text style={styles.toolbarButton} onPress={this.deleteDatabase}>
Delete DB
</Text>
</View>
<ListView
enableEmptySections={true}
dataSource={this.state.ds.cloneWithRows(this.state.progress)}
renderRow={this.renderProgressEntry}
style={listStyles.liContainer}/>
</View>);
}
}
var listStyles = StyleSheet.create({
li: {
borderBottomColor: '#c8c7cc',
borderBottomWidth: 0.5,
paddingTop: 15,
paddingRight: 15,
paddingBottom: 15,
},
liContainer: {
backgroundColor: '#fff',
flex: 1,
paddingLeft: 15,
},
liIndent: {
flex: 1,
},
liText: {
color: '#333',
fontSize: 17,
fontWeight: '400',
marginBottom: -3.5,
marginTop: -3.5,
},
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
toolbar: {
backgroundColor: '#51c04d',
paddingTop: 30,
paddingBottom: 10,
flexDirection: 'row'
},
toolbarButton: {
color: 'blue',
textAlign: 'center',
flex: 1
},
mainContainer: {
flex: 1
}
});