-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.go
More file actions
91 lines (83 loc) · 2.28 KB
/
init.go
File metadata and controls
91 lines (83 loc) · 2.28 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright 2022-2024 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package flynn
import (
"os"
"sync/atomic"
"github.com/tknie/errorrepo"
"github.com/tknie/flynn/adabas"
"github.com/tknie/flynn/common"
"github.com/tknie/flynn/mysql"
"github.com/tknie/flynn/oracle"
"github.com/tknie/flynn/postgres"
"github.com/tknie/log"
)
var globalRegID = common.RegDbID(0)
// Handler Handle database driver with a database URL returning a
// reference id for the driver path to database
func Handle(p ...string) (common.RegDbID, error) {
l := len(p) - 1
if l < 0 {
return 0, errorrepo.NewError("DB000012")
}
log.Log.Debugf("Register %v", p[0])
r, passwd, err := common.NewReference(p[l])
if err != nil {
return 0, err
}
if l > 1 {
r.SetType(p[0])
}
if r.Driver == common.NoType {
return 0, errorrepo.NewError("DB000013")
}
return Handler(r, passwd)
}
// Handler Register database driver with a database URL returning a
// reference id for the driver path to database
func Handler(dbref *common.Reference, password string) (common.RegDbID, error) {
if dbref == nil {
return 0, errorrepo.NewError("DB000014")
}
id := common.RegDbID(atomic.AddUint64((*uint64)(&globalRegID), 1))
if log.IsDebugLevel() {
p := "*******"
if os.Getenv("FLYNN_TRACE_PASSWORD") == "TRUE" {
p = password
}
log.Log.Debugf("Register database with passwordx %s", p)
}
var db common.Database
var err error
switch dbref.Driver {
case common.PostgresType:
db, err = postgres.NewInstance(id, dbref, password)
case common.MysqlType:
db, err = mysql.NewInstance(id, dbref, password)
case common.AdabasType:
db, err = adabas.NewInstance(id, dbref, password)
case common.OracleType:
db, err = oracle.NewInstance(id, dbref, password)
default:
return 0, errorrepo.NewError("DB065535")
}
if err != nil {
return 0, err
}
common.RegisterDbClient(db)
log.Log.Debugf("%s Register db type on db=%p driver=%d: %v", db.ID().String(),
db, dbref.Driver, common.DBHelper())
return db.ID(), nil
}
// Maps database tables,views and/or maps usable for queries
func Maps() []string {
return common.Maps()
}