PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
2.1 KiB

package Database
import (
"database/sql"
"os"
"path/filepath"
"time"
_ "github.com/mattn/go-sqlite3"
bolt "go.etcd.io/bbolt"
"PackageManager/Variables"
)
var (
DB *sql.DB
FsDB *bolt.DB
)
func InitConfigDir() error {
var (
e error
)
_, e = os.Stat(Variables.ConfigDir)
if os.IsNotExist(e) {
return os.MkdirAll(Variables.ConfigDir, 0644)
}
return e
}
func InitDatabaseFiles() error {
var e error
// Initialise sqlite3 database for package versioning
DB, e = sql.Open(
"sqlite3",
filepath.Join(Variables.ConfigDir, Variables.DatabaseName),
)
if e != nil {
return e
}
// Initialise bolt db for filesystem hashing
FsDB, e = bolt.Open(
filepath.Join(Variables.ConfigDir, Variables.FsHashDatabaseName),
0600,
&bolt.Options{
Timeout: 5 * time.Second,
})
return e
}
func init() {
var e error
e = InitConfigDir()
if e != nil {
panic(e)
}
e = InitDatabaseFiles()
if e != nil {
panic(e)
}
}
func InitBoltDB() error {
var (
tx *bolt.Tx
e error
)
tx, e = FsDB.Begin(true)
if e != nil {
return e
}
defer tx.Rollback()
_, e = tx.CreateBucketIfNotExists(Variables.FsHashIndexBucket)
if e != nil {
return e
}
_, e = tx.CreateBucketIfNotExists(Variables.FsHashPicksBucket)
if e != nil {
return e
}
e = tx.Commit()
return e
}
func InitSqlite3DB() error {
var (
stmt *sql.Stmt
e error
)
stmt, e = DB.Prepare(`
CREATE TABLE IF NOT EXISTS installed_packages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL,
version VARCHAR(64) NOT NULL,
installed_at INTEGER NOT NULL
)
`)
if e != nil {
return e
}
_, e = stmt.Exec()
if e != nil {
return e
}
stmt, e = DB.Prepare(`
CREATE TABLE IF NOT EXISTS dependancy_linker (
id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER,
dependancy_id INTEGER,
FOREIGN KEY(package_id) REFERENCES installed_packages(id),
FOREIGN KEY(dependancy_id) REFERENCES installed_packages(id)
)
`)
if e != nil {
return e
}
_, e = stmt.Exec()
return e
}
func InitDB() error {
var e error
e = InitBoltDB()
if e != nil {
return e
}
return InitSqlite3DB()
}