PostgresSQL integration
Kugelschieber opened this issue · 1 comments
Kugelschieber commented
Add Postgres as database backend. To do this, a new environment variable must be added, all SQL statements must be translated to Postgres and acweb must decide which one to use.
Example:
export ACWEB_DB_TYPE=postgres
In model/user.go:
import (
// [...]
// import os to read the env variable
"os"
// [...]
)
// [...]
// add database type specific SQL statements as constants
const (
user_mysql_delete = "DELETE FROM user WHERE id = :id"
user_postgres_delete = "DELETE FROM \"user\" WHERE id = $1"
)
// [...]
func (m *User) Remove() error {
if m.Id == 0 {
return errors.New("ID must be set")
}
// select the query by database type
// os.Getenv("ACWEB_DB_TYPE") should be outsourced to make sure it's in lowercase
// and there should be a check on startup a known DB type is set
var sql string
if os.Getenv("ACWEB_DB_TYPE") == "mysql" {
sql = user_mysql_delete
} else {
sql = user_postgres_delete
}
_, err := session.NamedExec(sql, m)
return err
}
The main difference between MySQL and Postgres is that table names in Postgres must be surrounded by quotes and variables are passed by $1, $2, ... $n
instead of a :name
.
Kugelschieber commented
Implemented.