Merge pull request #2 from joohoi/extend-db

Extend the database for a upcoming feature before release
This commit is contained in:
Joona Hoikkala 2016-11-29 15:42:15 +02:00 committed by GitHub
commit 8ffed7a3d7
2 changed files with 24 additions and 11 deletions

34
db.go
View File

@ -17,7 +17,8 @@ var recordsTable = `
Password TEXT UNIQUE NOT NULL, Password TEXT UNIQUE NOT NULL,
Subdomain TEXT UNIQUE NOT NULL, Subdomain TEXT UNIQUE NOT NULL,
Value TEXT, Value TEXT,
LastActive INT LastActive INT,
AllowFrom TEXT
);` );`
// getSQLiteStmt replaces all PostgreSQL prepared statement placeholders (eg. $1, $2) with SQLite variant "?" // getSQLiteStmt replaces all PostgreSQL prepared statement placeholders (eg. $1, $2) with SQLite variant "?"
@ -54,8 +55,9 @@ func (d *acmedb) Register() (ACMETxt, error) {
Password, Password,
Subdomain, Subdomain,
Value, Value,
LastActive) LastActive,
values($1, $2, $3, '', $4)` AllowFrom)
values($1, $2, $3, '', $4, $5)`
if DNSConf.Database.Engine == "sqlite3" { if DNSConf.Database.Engine == "sqlite3" {
regSQL = getSQLiteStmt(regSQL) regSQL = getSQLiteStmt(regSQL)
} }
@ -64,7 +66,7 @@ func (d *acmedb) Register() (ACMETxt, error) {
return a, errors.New("SQL error") return a, errors.New("SQL error")
} }
defer sm.Close() defer sm.Close()
_, err = sm.Exec(a.Username.String(), passwordHash, a.Subdomain, timenow) _, err = sm.Exec(a.Username.String(), passwordHash, a.Subdomain, timenow, a.AllowFrom)
if err != nil { if err != nil {
return a, err return a, err
} }
@ -76,7 +78,7 @@ func (d *acmedb) GetByUsername(u uuid.UUID) (ACMETxt, error) {
defer d.Unlock() defer d.Unlock()
var results []ACMETxt var results []ACMETxt
getSQL := ` getSQL := `
SELECT Username, Password, Subdomain, Value, LastActive SELECT Username, Password, Subdomain, Value, LastActive, AllowFrom
FROM records FROM records
WHERE Username=$1 LIMIT 1 WHERE Username=$1 LIMIT 1
` `
@ -97,12 +99,11 @@ func (d *acmedb) GetByUsername(u uuid.UUID) (ACMETxt, error) {
// It will only be one row though // It will only be one row though
for rows.Next() { for rows.Next() {
a := ACMETxt{} txt, err := getModelFromRow(rows)
err = rows.Scan(&a.Username, &a.Password, &a.Subdomain, &a.Value, &a.LastActive)
if err != nil { if err != nil {
return ACMETxt{}, err return ACMETxt{}, err
} }
results = append(results, a) results = append(results, txt)
} }
if len(results) > 0 { if len(results) > 0 {
return results[0], nil return results[0], nil
@ -116,7 +117,7 @@ func (d *acmedb) GetByDomain(domain string) ([]ACMETxt, error) {
domain = sanitizeString(domain) domain = sanitizeString(domain)
var a []ACMETxt var a []ACMETxt
getSQL := ` getSQL := `
SELECT Username, Password, Subdomain, Value SELECT Username, Password, Subdomain, Value, LastActive, AllowFrom
FROM records FROM records
WHERE Subdomain=$1 LIMIT 1 WHERE Subdomain=$1 LIMIT 1
` `
@ -136,8 +137,7 @@ func (d *acmedb) GetByDomain(domain string) ([]ACMETxt, error) {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
txt := ACMETxt{} txt, err := getModelFromRow(rows)
err = rows.Scan(&txt.Username, &txt.Password, &txt.Subdomain, &txt.Value)
if err != nil { if err != nil {
return a, err return a, err
} }
@ -171,6 +171,18 @@ func (d *acmedb) Update(a ACMETxt) error {
return nil return nil
} }
func getModelFromRow(r *sql.Rows) (ACMETxt, error) {
txt := ACMETxt{}
err := r.Scan(
&txt.Username,
&txt.Password,
&txt.Subdomain,
&txt.Value,
&txt.LastActive,
&txt.AllowFrom)
return txt, err
}
func (d *acmedb) Close() { func (d *acmedb) Close() {
d.DB.Close() d.DB.Close()
} }

View File

@ -72,6 +72,7 @@ type ACMETxt struct {
Password string Password string
ACMETxtPost ACMETxtPost
LastActive int64 LastActive int64
AllowFrom string
} }
// ACMETxtPost holds the DNS part of the ACMETxt struct // ACMETxtPost holds the DNS part of the ACMETxt struct