init
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
// import for postgres
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
// PHONE struct pg table
|
||||
type PHONE struct {
|
||||
ID int64 `json:"id" db:"id"`
|
||||
MAC string `json:"mac" db:"mac"`
|
||||
IP string `json:"ip,omitempty" db:"ip"`
|
||||
INVENTORY string `json:"inventory,omitempty" db:"inventory"`
|
||||
MODEL string `json:"model,omitempty" db:"model"`
|
||||
LANG string `json:"lang,omitempty" db:"lang"`
|
||||
FIRMWARE string `json:"firmware,omitempty" db:"firmware"`
|
||||
TYPECONN int64 `json:"typeconn" db:"typeconn"`
|
||||
EXTS *ExtSlice `json:"exts" db:"exts"`
|
||||
STATUS int `json:"busy" db:"busy"`
|
||||
}
|
||||
|
||||
// ExtSlice slice of extension
|
||||
type ExtSlice []*EXTS
|
||||
|
||||
// Value return jsonb to json
|
||||
func (e ExtSlice) Value() (driver.Value, error) {
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
// Scan return json to jsonb
|
||||
func (e *ExtSlice) Scan(src interface{}) error {
|
||||
switch v := src.(type) {
|
||||
case []byte:
|
||||
return json.Unmarshal(v, &e)
|
||||
case string:
|
||||
return json.Unmarshal([]byte(v), &e)
|
||||
}
|
||||
return errors.New("type assertion failed")
|
||||
}
|
||||
|
||||
// EXTS struct extension
|
||||
type EXTS struct {
|
||||
NUM int64 `json:"num"`
|
||||
PASSWORD string `json:"password,omitempty"`
|
||||
NAME string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// GetPG return pg sql.db
|
||||
func GetPG() *sql.DB {
|
||||
pgconn, err := sql.Open("postgres", "postgres://<>?sslmode=disable")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := pgconn.Ping(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return pgconn
|
||||
}
|
||||
Reference in New Issue
Block a user