This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
118 lines (104 loc) · 2.84 KB
/
database.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
111
112
113
114
115
116
117
118
package simpledb
import (
"database/sql"
"errors"
"fmt"
"github.com/Nigel2392/simplelog"
"github.com/go-sql-driver/mysql"
)
// ENVIRONMENT VARIABLES:
// These can be set with a .env file in the root of the project,
// or by setting them in the environment variables of the system.
// DB_HOST
// DB_PORT
// DB_USERNAME
// DB_PASSWORD (optional)
// DB_NAME (database name)
// DB_SSLMODE (disable, prefer, require, verify-ca, verify-full)
// Equality operators for use in filters
const (
IN = "IN"
EQ = "="
NE = "!="
GT = ">"
GTE = ">="
LT = "<"
LTE = "<="
LIKE = "LIKE"
)
// Database is the main struct for the database
type Database struct {
Host string
Port any
Username string
Password string `json:"-"`
Database string
SSL_MODE string
LIMIT int
conn *sql.DB `json:"-"`
models []Model `json:"-"`
LatestMigration *Migration `json:"-"`
Logger simplelog.Logger `json:"-"`
}
// Connect to the database
func NewDatabase(loglevel ...string) *Database {
latest_migration, _ := Migration{}.GetLatestMigration()
var logger simplelog.Logger
if len(loglevel) > 0 {
logger = simplelog.NewLogger(loglevel[0])
} else {
logger = simplelog.NewLogger("info")
}
return &Database{
Host: "",
Port: "",
Username: "",
Password: "",
Database: "",
LIMIT: 1000,
LatestMigration: &latest_migration,
Logger: logger,
}
}
// Database string method
func (db *Database) String() string {
return fmt.Sprintf("Database: %s", db.Database)
}
// Register a model with the database
// This will be used to create the table if it doesn't exist,
// and to create the migration if it doesn't exist
func (db *Database) Register(model Model) {
db.Logger.Debug("Registering model: " + model.TableName())
db.models = append(db.models, model)
}
// Migrate the database to the latest version
func (db *Database) Migrate() error {
db.Logger.Info("Initializing migration")
migration := NewMigration(db)
migration.CreateFromModels(db.models)
return migration.Run()
}
// Shorthand for a queryset
func (db *Database) NewQS(mdl Model) *QuerySet {
return NewQuerySet(db, mdl)
}
// Model interface
type Model interface {
TableName() string
}
// Check for SQL errors
func (db *Database) CheckError(err error, number int) bool {
return CheckSQLError(err, number)
}
// Check for SQL errors
func CheckSQLError(err error, number int) bool {
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == uint16(number) {
return true
}
return false
}
// Return all the models registered with the database
func (db *Database) AllModels() []Model {
return db.models
}