-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.go
110 lines (93 loc) · 2.42 KB
/
sqlite.go
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
package main
import (
"database/sql"
"os"
_ "github.com/mattn/go-sqlite3" // Import go-sqlite3 library
log "github.com/sirupsen/logrus"
)
var db *sql.DB
func init() {
_, err := os.Stat("./police.db")
creatingDB := os.IsNotExist(err)
if creatingDB {
log.Info("Creating police.db...")
file, err := os.Create("police.db") // Create SQLite file
if err != nil {
log.Error(err)
}
file.Close()
log.Info("police.db created")
}
db, err = sql.Open("sqlite3", "./police.db") // Open the created SQLite File
if err != nil {
log.Error(err)
os.Exit(1)
}
if creatingDB {
createTables() // Create Database Tables
}
}
func createTables() {
createTableViolations()
createTableOfficer()
createTableDecisions()
createTableObjections()
}
func createTable(tableDescription string) {
log.Info("Creating table...")
statement, err := db.Prepare(tableDescription) // Prepare SQL Statement
if err != nil {
log.Error(err)
}
statement.Exec() // Execute SQL Statements
log.Info("Table created")
}
func createTableOfficer() {
createOfficerTableSQL := `CREATE TABLE officer (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"first_name" VARCHAR,
"last_name" VARCHAR,
"rank" VARCHAR,
"commander", INTEGER
);` // SQL Statement for Create Table
createTable(createOfficerTableSQL)
}
func createTableViolations() {
createViolationTableSQL := `CREATE TABLE violations (
"ap" VARCHAR NOT NULL PRIMARY KEY,
"at" VARCHAR,
"violation_number" INTEGER,
"registration_number" VARCHAR,
"first_name_owner" VARCHAR,
"middle_name_owner" VARCHAR,
"last_name_owner" VARCHAR,
"address_owner" VARCHAR,
"document_type" INTEGER
);` // SQL Statement for Create Table
createTable(createViolationTableSQL)
}
func createTableDecisions() {
createDecisionTableSQL := `CREATE TABLE decisions (
"ap" VARCHAR NOT NULL PRIMARY KEY,
"violation_number" INTEGER,
"publish_date" VARCHAR
);` // SQL Statement for Create Table
createTable(createDecisionTableSQL)
}
func createTableObjections() {
createObjectionTableSQL := `CREATE TABLE objections (
"ap" VARCHAR NOT NULL PRIMARY KEY,
"violation_number" INTEGER,
"first_name_driver" VARCHAR,
"last_name_driver" VARCHAR,
"middle_name_driver" VARCHAR,
"gender_driver" VARCHAR,
"objection_date" VARCHAR,
"publish_date" VARCHAR,
"document_type" INTEGER
);` // SQL Statement for Create Table
createTable(createObjectionTableSQL)
}
func GetDB() *sql.DB {
return db
}