· 8 years ago · Oct 25, 2017, 07:02 PM
1/**
2 * Persistent SqlLite data functions
3 */
4
5 /**
6 * Will initialize the TouchParams (variables within the touch_params table)
7 */
8 $scope.initializeTouchParamVars = function () {
9 var query = 'SELECT id, touchID, showTouchIDOption, touchLogin, showTouchLink FROM touch_params';
10 $cordovaSQLite.execute(db, query).then(function (res) {
11 if(res.rows.length > 0) {
12 $scope.setTouchParamsVariables();
13 $scope.viewTouchParams();
14 } else {
15 var initQuery = 'INSERT INTO touch_params (touchID, showTouchIDOption, touchLogin, showTouchLink) VALUES (?,?,?,?)';
16 $cordovaSQLite.execute(db, initQuery, ['false', 'false', 'false', 'false']).then(function (res) {
17 console.log('initializeTouchParamVars res: ', res);
18 $scope.setTouchParamsVariables();
19 $scope.viewTouchParams();
20 },function (err) {
21 console.log('initializeTouchParamVars error: ', err);
22 })
23 }
24 },function (err) {
25 console.log('initializeTouchParamVars err: ', err);
26 });
27 };
28
29 /**
30 * Will initialize the AuthParams (variables within the auth table)
31 */
32 $scope.initializeAuthParamVars = function () {
33 var query = 'SELECT id, username, password, Authorization FROM auth';
34 $cordovaSQLite.execute(db, query).then(function (res) {
35 if(res.rows.length > 0) {
36 $scope.setAuthVariables();
37 $scope.viewAuth();
38 } else {
39 var initQuery = 'INSERT INTO auth (username, password, Authorization) VALUES (?,?,?)';
40 $cordovaSQLite.execute(db, initQuery, ['null', 'null', 'null']).then(function (res) {
41 console.log('initializeAuthParamVars res:', res);
42 $scope.setAuthVariables();
43 $scope.viewAuth();
44 },function (err) {
45 console.log('initializeAuthParamVars err:', err);
46 })
47 }
48
49 }, function (err) {
50 console.log('initializeAuthParamVars err:', err);
51 })
52 };
53
54 /**
55 * Will set the touch_param scope variables
56 * to whatever is currently in the database
57 */
58 $scope.setTouchParamsVariables = function () {
59 var query = 'SELECT id, touchID, showTouchIDOption, touchLogin, showTouchLink FROM touch_params';
60 $cordovaSQLite.execute(db, query).then(function (res) {
61 console.log('setTouchParamsVariables res', res.rows.item(0).showTouchIDOption);
62 $scope.showTouchIDOption = $scope.textToDataTypeConverter(res.rows.item(0).showTouchIDOption);
63 $scope.showTouchLink = $scope.textToDataTypeConverter(res.rows.item(0).showTouchLink);
64 $scope.touchID = $scope.textToDataTypeConverter(res.rows.item(0).touchID);
65 $scope.touchLogin = $scope.textToDataTypeConverter(res.rows.item(0).touchLogin);
66 // alert($scope.showTouchIDOption);
67 // alert($scope.showTouchLink);
68 // alert($scope.touchID);
69 // alert($scope.touchLogin);
70 console.log('$scope.showTouchIDOption', $scope.showTouchIDOption);
71 console.log('$scope.showTouchLink', $scope.showTouchLink);
72 console.log('$scope.touchID', $scope.touchID);
73 console.log('$scope.touchLogin', $scope.touchLogin);
74 }, function (err) {
75 alert(err);
76 console.log('setTouchParamsVariables err', err);
77 });
78 };
79
80 /**
81 * Will set the auth scope variables
82 * to whatever is currently in the database
83 */
84 $scope.setAuthVariables = function () {
85 var query = 'SELECT id, username, password, Authorization FROM auth';
86 $cordovaSQLite.execute(db, query).then(function (res) {
87 $scope.username = $scope.textToDataTypeConverter(res.rows.item(0).username);
88 $scope.password = $scope.textToDataTypeConverter(res.rows.item(0).password);
89 $scope.Authorization = $scope.textToDataTypeConverter(res.rows.item(0).Authorization);
90 console.log('setAuthVariables $scope.username:', $scope.username);
91 console.log('setAuthVariables $scope.password:', $scope.password);
92 console.log('setAuthVariables $scope.Authorization:', $scope.Authorization);
93 },function (err) {
94 console.log('setAuthVariables err:', err);
95 });
96 };
97
98 /**
99 * Will update the touchID column along with update the touchID scope variable
100 * Note since this is only dealing with one user, we always want to update the
101 * first entry
102 */
103 $scope.updateTouchID = function (touchID) {
104 var query = 'UPDATE touch_params SET touchID=? WHERE id=1';
105 var touchIDInput = $scope.dataTypeToTextConverter(touchID);
106 $cordovaSQLite.execute(db, query, [touchIDInput]).then(function (res) {
107 $scope.viewTouchParams();
108 $scope.setTouchParamsVariables();
109 }, function (err) {
110 console.log('updateTouchID err', err);
111 })
112
113 };
114
115 /**
116 * Will update the touchLogin column along with update the touchLogin scope variable
117 */
118 $scope.updateTouchLogin = function (touchLogin) {
119 var query = 'UPDATE touch_params SET touchLogin=? WHERE id=1';
120 var touchLoginInput = $scope.dataTypeToTextConverter(touchLogin);
121 $cordovaSQLite.execute(db, query, [touchLoginInput]).then(function (res) {
122 $scope.viewTouchParams();
123 $scope.setTouchParamsVariables();
124 },function (err) {
125 console.log('updateTouchLogin err', err)
126 });
127 };
128
129 /**
130 * Will update the showTouchIDOption column along with the showTouchIDOption scope variable
131 */
132 $scope.updateShowTouchIDOption = function (showTouchIDOption) {
133 var query = 'UPDATE touch_params SET showTouchIDOption=? WHERE id=1';
134 var showTouchIDOptionInput = $scope.dataTypeToTextConverter(showTouchIDOption);
135 $cordovaSQLite.execute(db, query, [showTouchIDOptionInput]).then(function (res) {
136 $scope.viewTouchParams();
137 $scope.setTouchParamsVariables();
138 },function (err) {
139 console.log('updateShowTouchIDOption err', err)
140 });
141 };
142
143 /**
144 * Will update the showTouchLink column along with the showTouchLink scope variable
145 */
146 $scope.updateShowTouchLink = function (showTouchLink) {
147 var query = 'UPDATE touch_params SET showTouchIDOption=? WHERE id=1';
148 var showTouchLinkInput = $scope.dataTypeToTextConverter(showTouchLink);
149 $cordovaSQLite.execute(db, query, [showTouchLinkInput]).then(function (res) {
150 $scope.viewTouchParams();
151 $scope.setTouchParamsVariables();
152 },function (err) {
153 console.log('updateShowTouchLink err', err)
154 });
155 };
156
157 /**
158 * Will update the username column along wiht the username scope variable
159 */
160 $scope.updateUsername = function (username) {
161 var query = 'UPDATE auth SET username=? WHERE id=1';
162 var usernameInput = $scope.dataTypeToTextConverter(username);
163 $cordovaSQLite.execute(db, query, [usernameInput]).then(function (res) {
164 $scope.viewAuth();
165 $scope.setAuthVariables();
166 },function (err) {
167 console.log('updateUsername err', err);
168 })
169 };
170
171 /**
172 * Will update the password column along with the password scope variable
173 */
174 $scope.updatePassword = function (password) {
175 var query = 'UPDATE auth SET password=? WHERE id=1';
176 var passwordInput = $scope.dataTypeToTextConverter(password);
177 $cordovaSQLite.execute(db, query, [passwordInput]).then(function (res) {
178 $scope.viewAuth();
179 $scope.setAuthVariables();
180 },function (err) {
181 console.log('updatePassword err', err);
182 })
183 };
184
185 /**
186 * Will update the Authorization column along with Authorization assests
187 */
188 $scope.updateAuthorization = function (Authorization) {
189 var query = 'UPDATE auth SET Authorization=? WHERE id=1';
190 var authorizationInput = $scope.dataTypeToTextConverter(Authorization);
191 $cordovaSQLite.execute(db, query, [authorizationInput]).then(function (res) {
192 $scope.viewAuth();
193 $scope.setAuthVariables();
194 },function (err) {
195 console.log('updateAuthorization err', err);
196 })
197 };
198
199 /**
200 * Mainly for debugging, will console log out the current state of the touch params table
201 */
202 $scope.viewTouchParams = function () {
203 var query = 'SELECT id, touchID, showTouchIDOption, touchLogin, showTouchLink FROM touch_params';
204 $cordovaSQLite.execute(db, query).then(function (res) {
205 console.log('touch_params Table: ', res);
206 }, function (err) {
207 console.log('touch_params Table Error: ', err)
208 })
209 };
210
211 /**
212 * Mainly for debugging, will console log out the current state of the auth table
213 */
214 $scope.viewAuth = function () {
215 var query = 'SELECT id, username, password, Authorization FROM auth';
216 $cordovaSQLite.execute(db, query).then(function (res) {
217 console.log('Auth Table: ', res);
218 }, function (err) {
219 console.log('Auth Table Error: ', err);
220 })
221 };
222
223 /**
224 * Simply converts text data types
225 * into more usable data types
226 * @param variable
227 * @returns {*}
228 */
229 $scope.textToDataTypeConverter = function (variable) {
230 switch (variable) {
231 case 'null':
232 return null;
233 break;
234 case 'true':
235 return true;
236 break;
237 case 'false':
238 return false;
239 break;
240 default:
241 return variable;
242 }
243 };
244
245 /**
246 * Simply converts data types into
247 * text to be inserted into the sqlLite DB
248 * @param variable
249 * @returns {*}
250 */
251 $scope.dataTypeToTextConverter = function (variable) {
252 switch (variable) {
253 case true:
254 return 'true';
255 break;
256 case false:
257 return 'false';
258 break;
259 case null:
260 return 'null';
261 break;
262 default:
263 return variable;
264 }
265 };
266
267 /**
268 * End of Persistent SqlLite data functions
269 */
270
271//initialize the db variable
272var db = null;
273.
274.
275.
276.
277.
278 $ionicPlatform.ready(function() {
279
280 if(window.cordova) {
281 db = $cordovaSQLite.openDB({name: "mmpapp.db", bgType: 1, location:"default"});
282 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS auth (id integer primary key, username text, password text, Authorization text)");
283 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS touch_params (id integer primary key, touchID text, showTouchIDOption text, touchLogin text, showTouchLink text)");
284 } else {
285 db = window.openDatabase("mmpapp.db", '1', 'ES Database', 5 * 1024 * 1024);
286 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS auth (id integer primary key, username text, password text, Authorization text)");
287 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS touch_params (id integer primary key, touchID text, showTouchIDOption text, touchLogin text, showTouchLink text)");
288 }
289.
290
291.
292.
293.