· 8 years ago · Jun 04, 2018, 09:18 AM
1import { Component } from '@angular/core';
2import { NavController } from 'ionic-angular';
3
4import { AcceuilPage } from '../acceuil/acceuil';
5
6//Permet la transmission d'information d'une page à l'autre.
7import { NavParams } from 'ionic-angular';
8
9//permet la gestion de la BDD locale.
10import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
11
12@Component({
13 selector: 'page-chargement',
14 templateUrl: 'chargement.html'
15})
16export class ChargementPage {
17
18 private db: SQLiteObject;
19
20 log: string;
21
22 private testSiTablesEstMonte: number;
23
24 constructor(public navCtrl: NavController, private navParams: NavParams, private sqlite: SQLite) {
25
26 // récuperation d'information transmise par la page d'avant.
27 let nextPage = navParams.get('next');
28
29 this.log = 'lancement de la creation de la bd /';
30
31 this.createDatabaseFile();
32
33 setTimeout(() => { this.navCtrl.push(nextPage) }, 78000);
34
35 }
36
37 private createDatabaseFile(): void {
38
39 this.sqlite.create({
40
41 name: 'gsb.db',
42 location: 'default'
43
44 })
45 .then((db: SQLiteObject) => {
46
47 this.db = db;
48
49 this.verificationSiTableMonter(this.db);
50
51 // if(this.testSiTablesEstMonte != 1){
52 this.createTable(this.db);
53 // }
54
55 })
56 .catch(e => this.log = e);
57
58 }
59
60 private createTable(db: SQLiteObject) {
61
62 db.sqlBatch([
63 ['CREATE TABLE IF NOT EXISTS `visiteur` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `nom` TEXT, `prenom` TEXT, `login` TEXT, `mdp` TEXT, `adresse` TEXT, `cp` INTEGER, `ville` TEXT, `dateEmbauche` TEXT )'],
64 ['CREATE TABLE IF NOT EXISTS `famille` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `libelle` TEXT )'],
65 ['CREATE TABLE IF NOT EXISTS `medecin` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `nom` TEXT, `prenom` TEXT, `tel` INTEGER, `specialiteComplementaire` TEXT, `departement` TEXT )'],
66 ['CREATE TABLE IF NOT EXISTS `medicament` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `nomCommercial` TEXT, `idFamille` INTEGER, `composition` TEXT, `effets` TEXT, `contreIndications` TEXT, FOREIGN KEY(`idFamille`) REFERENCES `famille`(`id`) )'],
67 ['CREATE TABLE IF NOT EXISTS `rapport` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `date` TEXT, `motif` TEXT, `bilan` TEXT, `idVisiteur` INTEGER, `idMedecin` INTEGER, FOREIGN KEY(`idMedecin`) REFERENCES `medecin`(`id`), FOREIGN KEY(`idVisiteur`) REFERENCES `visiteur`(`id`) )'],
68 ['CREATE TABLE IF NOT EXISTS `offrir` ( `idRapport` INTEGER NOT NULL, `idMedicament` INTEGER NOT NULL, `quantite` INTEGER, FOREIGN KEY(`idRapport`) REFERENCES `rapport`(`id`), PRIMARY KEY(`idRapport`,`idMedicament`) )'],
69 ['CREATE TABLE IF NOT EXISTS `suivieApp` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `creationDB` INTEGER DEFAULT 0, `creationTables` INTEGER DEFAULT 0, `dateDernierImportInformationViaApi` TEXT, `dateDerniereConnexionSurApp` TEXT, `modifViaApiEnAttente` INTEGER DEFAULT 0, `versionApp` TEXT DEFAULT 0.1 )'],
70 ['CREATE TABLE IF NOT EXISTS `attenteEnvoieAPI` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `enteteUrl` TEXT NOT NULL, `requete` TEXT NOT NULL )'],
71 ['INSERT INTO suivieApp (creationDB, creationTables) VALUES(1,1)']
72 ])
73 .then( () => this.log = this.log.concat('monter des tables fini'))
74 .catch( () =>this.log = this.log.concat('monter des tables echouer'));
75 }
76
77 public drop(db: SQLiteObject) {
78
79 this.db.sqlBatch([
80 ['DROP TABLE IF EXISTS`visiteur`;'],
81 ['DROP TABLE IF EXISTS`famille`;'],
82 ['DROP TABLE IF EXISTS`medecin`;'],
83 ['DROP TABLE IF EXISTS`medicament`;'],
84 ['DROP TABLE IF EXISTS`rapport`;'],
85 ['DROP TABLE IF EXISTS`offrir`;'],
86 ['DROP TABLE IF EXISTS`suivieApp`;'],
87 ['DROP TABLE IF EXISTS`attenteEnvoieAPI`;']
88 ])
89 .then(() => {
90
91 //affichage en bas de la page de chargement
92 this.log = this.log.concat(' Drop de toutes les tables');
93
94 })
95 .catch(() => this.log = this.log.concat(' Drop de toutes les tables echouer'));
96 }
97
98 private verificationSiTableMonter(db: SQLiteObject) {
99
100 db.executeSql('SELECT * FROM `suivieApp`;', {})
101 .then((data) => {
102
103 if(data == null) {
104 return;
105 }
106
107 if(data.rows){
108 this.testSiTablesEstMonte = data.rows.item(0).creationTables;
109 }
110
111 })
112 .catch(() => this.testSiTablesEstMonte = 0);
113 }
114
115}