Files
old-riskletpy/db/db.go
2024-10-27 17:54:12 +01:00

97 lines
1.8 KiB
Go

package db
import (
"database/sql"
"log"
"sync"
_ "github.com/mattn/go-sqlite3"
)
var (
db *sql.DB
once sync.Once
)
func InitDB() {
once.Do(func() {
var err error
db, err = sql.Open("sqlite3", "./risklet.db")
if err != nil {
log.Fatal(err)
}
createTables()
})
}
func createTables() {
companyTable := `
CREATE TABLE IF NOT EXISTS Company (
id INTEGER PRIMARY KEY AUTOINCREMENT,
UUID TEXT NOT NULL,
Name TEXT NOT NULL,
TaxId TEXT NOT NULL,
Email TEXT NOT NULL,
Password TEXT NOT NULL
);`
basicProfileTable := `
CREATE TABLE IF NOT EXISTS BasicProfile (
id INTEGER PRIMARY KEY AUTOINCREMENT,
CompanyId INTEGER,
Employees TEXT,
Revenue TEXT,
Applications TEXT,
Compliance TEXT,
Industry TEXT,
ITDependency TEXT,
DataSensitivity TEXT,
DataVolume TEXT,
NetworkSegmentation TEXT,
LegacySystems TEXT,
IoTIntegration TEXT,
RemoteWork TEXT,
BYOD TEXT,
VPN TEXT,
API TEXT,
VendorAccess TEXT,
InternalDev TEXT,
FOREIGN KEY (CompanyId) REFERENCES Company(id)
);`
advancedProfileTable := `
CREATE TABLE IF NOT EXISTS AdvancedProfile (
id INTEGER PRIMARY KEY AUTOINCREMENT,
CompanyId INTEGER,
GeographicDistribution TEXT,
CustomerConcentration TEXT,
ProductServicePortfolio TEXT,
OrganizationalCulture TEXT,
SupplierDiversity TEXT,
TechnologicalInfrastructure TEXT,
IntellectualProperty TEXT,
ManagementTeamExperience TEXT,
FOREIGN KEY (CompanyId) REFERENCES Company(id)
);`
_, err := db.Exec(companyTable)
if err != nil {
log.Fatalf("Error creating Company table: %v", err)
}
_, err = db.Exec(basicProfileTable)
if err != nil {
log.Fatalf("Error creating BasicProfile table: %v", err)
}
_, err = db.Exec(advancedProfileTable)
if err != nil {
log.Fatalf("Error creating AdvancedProfile table: %v", err)
}
}
func GetDB() *sql.DB {
return db
}