You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1018 B
Go
50 lines
1018 B
Go
10 years ago
|
package main
|
||
|
|
||
|
import (
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"regexp"
|
||
|
)
|
||
|
|
||
|
var namePattern = regexp.MustCompile(`^[[:alpha:]][[:alnum:]-_]{0,19}$`)
|
||
|
|
||
|
func ValidName(name string) bool {
|
||
|
return namePattern.MatchString(name)
|
||
|
}
|
||
|
|
||
10 years ago
|
type Profile struct {
|
||
10 years ago
|
id int
|
||
10 years ago
|
name string
|
||
|
}
|
||
|
|
||
10 years ago
|
func (p *Profile) Create() error {
|
||
10 years ago
|
_, err := db.Exec(`
|
||
10 years ago
|
insert into profiles
|
||
10 years ago
|
(name)
|
||
|
values
|
||
|
(?)
|
||
|
;`, p.name)
|
||
|
if err != nil {
|
||
10 years ago
|
return fmt.Errorf("unable to create profile: %v", err)
|
||
10 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func profilesTable() {
|
||
|
stmnt := `create table if not exists profiles (
|
||
10 years ago
|
id integer not null primary key autoincrement,
|
||
|
name text unique
|
||
|
);`
|
||
|
if _, err := db.Exec(stmnt); err != nil {
|
||
10 years ago
|
log_error("couldn't create profiles table: %v", err)
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
func loadProfile(name string) (*Profile, error) {
|
||
|
row := db.QueryRow(`select * from profiles where name = ?`, name)
|
||
|
var p Profile
|
||
10 years ago
|
if err := row.Scan(&p.id, &p.name); err != nil {
|
||
10 years ago
|
return nil, fmt.Errorf("unable to fetch profile from database: %v", err)
|
||
10 years ago
|
}
|
||
|
return &p, nil
|
||
10 years ago
|
}
|