-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoedb.go
executable file
·53 lines (43 loc) · 1.4 KB
/
Goedb.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
package goedb
import (
"errors"
"fmt"
"log"
"os"
"github.com/plopezm/goedb/database/dbaccess"
"github.com/plopezm/goedb/config"
"github.com/plopezm/goedb/database"
)
var goedbStandalone *dbm
type dbm struct {
drivers map[string]database.EntityManager
}
func init() {
log.Println("[GOEDB] library version: 1.0.0")
goedbStandalone = new(dbm)
goedbStandalone.drivers = make(map[string]database.EntityManager)
}
// Initialize gets the datasources from persistence.json
func Initialize() {
var persistence config.Persistence
persistence = config.GetPersistenceConfig("persistence.json")
for _, datasource := range persistence.Datasources {
driver := new(database.SQLDatabase)
driver.DBAccess = dbaccess.GetDatabaseAccess(datasource.Driver)
err := driver.Open(datasource.Driver, datasource.URL, datasource.Schema)
if err != nil {
fmt.Fprintf(os.Stdout, "[Connection ERROR for Persistence unit { %s } URL { %s }]: %v\n", datasource.Name, datasource.URL, err)
//os.Exit(1)
continue
}
goedbStandalone.drivers[datasource.Name] = driver
}
}
// GetEntityManager returns a entity manager for the datasource selected.
func GetEntityManager(persistenceUnit string) (database.EntityManager, error) {
entityManager, ok := goedbStandalone.drivers[persistenceUnit]
if !ok {
return nil, errors.New("Persistence unit \"" + persistenceUnit + "\" not found in persistence.json")
}
return entityManager, nil
}