MacFastLookup/service/service.go

112 lines
2.5 KiB
Go
Raw Normal View History

2024-07-15 16:15:43 +08:00
package service
import (
"database/sql"
"fmt"
2024-07-23 16:32:49 +08:00
"os"
"path/filepath"
2024-07-15 16:15:43 +08:00
_ "github.com/mattn/go-sqlite3"
)
type MacAddress struct {
Id string
Prefix string
VendorName string
Private bool
BlockType string
LastUpdate string
}
type SQLiteHelper struct {
2024-07-23 16:32:49 +08:00
db *sql.DB
2024-07-15 16:15:43 +08:00
}
2024-07-23 16:32:49 +08:00
// create the instance of SQLiteHelper but not initialize the database connection
func NewSQLiteHelper() *SQLiteHelper {
return &SQLiteHelper{
db: nil,
}
}
// check if the directory and file for the database exist
func (helper *SQLiteHelper) CheckBeforeInit() bool {
userHomeDir, _ := os.UserHomeDir()
dbFileDir := filepath.Join(userHomeDir, ".macfastlookup")
dbFileName := "mac_vendors.sqlite3"
dbFilePath := filepath.Join(dbFileDir, dbFileName)
if _, err := os.Stat(dbFileDir); os.IsNotExist(err) {
os.Mkdir(dbFileDir, 0755)
}
if _, err := os.Stat(dbFilePath); os.IsNotExist(err) {
return false
}
return true
}
// initialize the database connection
func (helper *SQLiteHelper) InitSQLiteHelper() bool {
dbFileDir := ".macfastlookup"
userHomeDir, _ := os.UserHomeDir()
dbFileName := "mac_vendors.sqlite3"
dbFilePath := filepath.Join(userHomeDir, dbFileDir, dbFileName)
2024-07-15 16:15:43 +08:00
connectionString := fmt.Sprintf("file:%s?cache=shared&mode=rwc", dbFilePath)
db, err := sql.Open("sqlite3", connectionString)
2024-07-23 16:32:49 +08:00
2024-07-15 16:15:43 +08:00
if err != nil {
2024-07-23 16:32:49 +08:00
return false
2024-07-15 16:15:43 +08:00
}
2024-07-23 16:32:49 +08:00
helper.db = db
return true
2024-07-15 16:15:43 +08:00
}
func (helper *SQLiteHelper) Close() {
2024-07-24 16:33:05 +08:00
if helper.db == nil {
return
}
2024-07-15 16:15:43 +08:00
helper.db.Close()
}
2024-07-16 10:21:51 +08:00
func (helper *SQLiteHelper) ReloadDatabase() bool {
2024-07-23 16:32:49 +08:00
return helper.InitSQLiteHelper()
2024-07-16 10:21:51 +08:00
}
2024-07-15 16:15:43 +08:00
func (helper *SQLiteHelper) QueryMacAddressByPrefix(prefix string) (*MacAddress, error) {
const tableName = "MacAddresses"
query := fmt.Sprintf("SELECT * FROM %s WHERE Prefix = ?", tableName)
row := helper.db.QueryRow(query, prefix)
var macAddress MacAddress
err := row.Scan(&macAddress.Id, &macAddress.Prefix, &macAddress.VendorName, &macAddress.Private, &macAddress.BlockType, &macAddress.LastUpdate)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return &macAddress, nil
}
func (helper *SQLiteHelper) querySingleColumn(tableName, columnName string) (string, error) {
query := fmt.Sprintf("SELECT %s FROM %s LIMIT 1", columnName, tableName)
row := helper.db.QueryRow(query)
var result string
err := row.Scan(&result)
if err != nil {
return "", err
}
return result, nil
}
func (helper *SQLiteHelper) QueryVersion() (string, error) {
return helper.querySingleColumn("Config", "Version")
}