-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
61 lines (54 loc) · 1.25 KB
/
client.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
package sqlite_db
import (
"database/sql"
"errors"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/parvez0/whatsapp-provisioner/custom_logger"
"os"
)
// custom wrapper for sqlite db object
type SQLDB struct {
Db *sql.DB
}
// initialize global logger
var clog = custom_logger.NewLogger()
// CreateDbConnection initializes the sqlite
func CreateDbConnection() *SQLDB {
dbpath := "/data"
if path := os.Getenv("SQLITE_DB_PATH"); path != ""{
dbpath = path
}
//
db, err := sql.Open("sqlite3", dbpath + "/wa.sqlite")
if err != nil{
clog.Panicf("failed to initialize sqlite - %+v", err)
}
return &SQLDB{
Db: db,
}
}
// PopulateDB initializes the db with table structure and initial values
func (db *SQLDB) PopulateDB(table string) error {
if table == ""{
table = "infra_queue"
}
query := fmt.Sprintf(`create table %s(
wa_id varchar(255),
number varchar(255),
status varchar(255),
ip varchar(255),
tier int
)`, table)
_, err := db.Db.Exec(query)
return err
}
// DropTable drops the table after testing
func (db *SQLDB) DropTable(table string) error {
if table == ""{
return errors.New("table name not provided")
}
clog.Warn("dropping table - ", table)
_, err := db.Db.Exec("drop table " + table)
return err
}