Skip to content

Commit 64107d4

Browse files
committed
add session support to the db connection
1 parent 62c0498 commit 64107d4

31 files changed

+504
-425
lines changed

api/db.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (C) 2007-2024 by Ubaldo Porcheddu <[email protected]>
2+
3+
package api
4+
5+
import (
6+
"github.com/eja/tibula/db"
7+
)
8+
9+
type dbTypeLink = db.TypeLink
10+
type dbTypeModule = db.TypeModule
11+
type dbTypeSession = db.TypeSession
12+
13+
var dbSession = db.Session

api/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package api
55
import (
66
"encoding/base64"
77
"errors"
8-
"github.com/eja/tibula/db"
8+
99
"github.com/eja/tibula/sys"
1010
)
1111

@@ -16,13 +16,15 @@ func Set() TypeApi {
1616
DefaultSearchOrder: "ejaLog DESC",
1717
Values: make(map[string]string),
1818
SearchOrder: make(map[string]string),
19-
Link: db.TypeLink{},
19+
Link: dbTypeLink{},
2020
}
2121
}
2222

2323
func Run(eja TypeApi, sessionSave bool) (result TypeApi, err error) {
2424
var user map[string]string
2525

26+
db := dbSession()
27+
2628
//open db connection
2729
if err = db.Open(sys.Options.DbType, sys.Options.DbName, sys.Options.DbUser, sys.Options.DbPass, sys.Options.DbHost, sys.Options.DbPort); err != nil {
2830
return
@@ -119,7 +121,7 @@ func Run(eja TypeApi, sessionSave bool) (result TypeApi, err error) {
119121
if eja.ModuleId == eja.Link.ModuleId && eja.Id == eja.Link.FieldId && eja.Id > 0 {
120122
db.SessionCleanLink(eja.Owner)
121123
db.SessionCleanSearch(eja.Owner)
122-
eja.Link = db.TypeLink{}
124+
eja.Link = dbTypeLink{}
123125
eja.Action = "edit"
124126
eja.Linking = false
125127
}
@@ -344,7 +346,7 @@ func Run(eja TypeApi, sessionSave bool) (result TypeApi, err error) {
344346
}
345347

346348
if Plugins[eja.ModuleName] != nil {
347-
eja = Plugins[eja.ModuleName](eja)
349+
eja = Plugins[eja.ModuleName](eja, db)
348350
}
349351

350352
if eja.Owner > 0 && !sessionSave {

api/plugins.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ package api
44

55
import (
66
"encoding/json"
7-
"github.com/eja/tibula/db"
87
)
98

10-
type TypePlugins map[string]func(TypeApi) TypeApi
9+
type TypePlugins map[string]func(TypeApi, dbTypeSession) TypeApi
1110

1211
var Plugins = TypePlugins{
13-
"ejaProfile": func(eja TypeApi) TypeApi {
12+
"ejaProfile": func(eja TypeApi, db dbTypeSession) TypeApi {
1413
eja.Alert = nil
1514
if eja.Action == "run" {
1615
if eja.Values["passwordOld"] == "" || eja.Values["passwordNew"] == "" || eja.Values["passwordRepeat"] == "" {
@@ -32,13 +31,13 @@ var Plugins = TypePlugins{
3231
}
3332
return eja
3433
},
35-
"ejaImport": func(eja TypeApi) TypeApi {
34+
"ejaImport": func(eja TypeApi, db dbTypeSession) TypeApi {
3635
if eja.Action == "run" {
3736
moduleName := eja.Values["moduleName"]
3837
moduleData := eja.Values["import"]
3938
dataImport := db.Number(eja.Values["dataImport"])
4039
if moduleData != "" {
41-
var module db.TypeModule
40+
var module dbTypeModule
4241
if err := json.Unmarshal([]byte(moduleData), &module); err != nil {
4342
alert(&eja.Alert, db.Translate("ejaImportJsonError", eja.Owner))
4443
} else {
@@ -62,7 +61,7 @@ var Plugins = TypePlugins{
6261
}
6362
return eja
6463
},
65-
"ejaExport": func(eja TypeApi) TypeApi {
64+
"ejaExport": func(eja TypeApi, db dbTypeSession) TypeApi {
6665
if eja.Action == "run" {
6766
moduleId := db.Number(eja.Values["ejaModuleId"])
6867
dataExport := db.Number(eja.Values["dataExport"]) > 0

db/command.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ type TypeCommand struct {
1616

1717
// Commands retrieves a list of commands based on user and module information.
1818
// It checks permissions and builds the command list accordingly.
19-
func Commands(userId int64, moduleId int64, actionType string) ([]TypeCommand, error) {
19+
func (session *TypeSession) Commands(userId int64, moduleId int64, actionType string) ([]TypeCommand, error) {
2020
commandList := []TypeCommand{}
2121
actionTypeSql := ""
2222

23-
moduleName := ModuleGetNameById(moduleId)
23+
moduleName := session.ModuleGetNameById(moduleId)
2424
if moduleName == "" {
2525
return nil, errors.New("module does not exist")
2626
}
2727

2828
if moduleName == "ejaLogin" {
29-
commandList = append(commandList, TypeCommand{Name: "login", Label: Translate("login", userId)})
29+
commandList = append(commandList, TypeCommand{Name: "login", Label: session.Translate("login", userId)})
3030
}
3131

3232
if actionType != "" {
@@ -48,28 +48,28 @@ func Commands(userId int64, moduleId int64, actionType string) ([]TypeCommand, e
4848
)
4949
)
5050
) %s`,
51-
UserGroupCsv(userId), actionTypeSql)
51+
session.UserGroupCsv(userId), actionTypeSql)
5252

53-
rows, err := Rows(
53+
rows, err := session.Rows(
5454
query,
5555
moduleId,
56-
ModuleGetIdByName("ejaPermissions"),
57-
ModuleGetIdByName("ejaUsers"),
56+
session.ModuleGetIdByName("ejaPermissions"),
57+
session.ModuleGetIdByName("ejaUsers"),
5858
userId,
59-
ModuleGetIdByName("ejaGroups"),
59+
session.ModuleGetIdByName("ejaGroups"),
6060
)
6161
if err != nil {
6262
return nil, err
6363
}
6464

6565
for _, row := range rows {
66-
commandList = append(commandList, TypeCommand{Name: row["name"], Label: Translate(row["name"], userId), Linker: Number(row["linking"]) > 0})
66+
commandList = append(commandList, TypeCommand{Name: row["name"], Label: session.Translate(row["name"], userId), Linker: session.Number(row["linking"]) > 0})
6767
}
6868
return commandList, nil
6969
}
7070

7171
// CommandExists checks if a given command exists in the provided list of commands.
72-
func CommandExists(commands []TypeCommand, commandName string) bool {
72+
func (session *TypeSession) CommandExists(commands []TypeCommand, commandName string) bool {
7373
for _, row := range commands {
7474
if row.Name == commandName {
7575
return true

db/crud.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
)
99

1010
// New creates a new entry in the specified module table with the given ownerId and moduleId.
11-
func New(ownerId int64, moduleId int64) (int64, error) {
12-
moduleName := ModuleGetNameById(moduleId)
11+
func (session *TypeSession) New(ownerId int64, moduleId int64) (int64, error) {
12+
moduleName := session.ModuleGetNameById(moduleId)
1313

14-
check, err := TableExists(moduleName)
14+
check, err := session.TableExists(moduleName)
1515
if err != nil {
1616
return 0, err
1717
}
@@ -20,84 +20,84 @@ func New(ownerId int64, moduleId int64) (int64, error) {
2020
}
2121

2222
query := fmt.Sprintf("INSERT INTO %s (ejaOwner, ejaLog) VALUES (?,?)", moduleName)
23-
run, err := Run(query, ownerId, Now())
23+
run, err := session.Run(query, ownerId, session.Now())
2424
if err != nil {
2525
return 0, err
2626
}
2727
return run.LastId, nil
2828
}
2929

3030
// Get retrieves a row from the specified module table based on ownerId, moduleId, and ejaId.
31-
func Get(ownerId int64, moduleId int64, ejaId int64) (TypeRow, error) {
32-
moduleName := ModuleGetNameById(moduleId)
31+
func (session *TypeSession) Get(ownerId int64, moduleId int64, ejaId int64) (TypeRow, error) {
32+
moduleName := session.ModuleGetNameById(moduleId)
3333

34-
check, err := TableExists(moduleName)
34+
check, err := session.TableExists(moduleName)
3535
if err != nil {
3636
return nil, err
3737
}
3838
if !check {
3939
return nil, errors.New("table does not exist")
4040
}
4141

42-
query := fmt.Sprintf("SELECT * FROM %s WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, OwnersCsv(ownerId, moduleId))
43-
return Row(query, ejaId)
42+
query := fmt.Sprintf("SELECT * FROM %s WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, session.OwnersCsv(ownerId, moduleId))
43+
return session.Row(query, ejaId)
4444
}
4545

4646
// Put updates a specific field in a row of the specified module table based on ownerId, moduleId, ejaId, fieldName, and fieldValue.
47-
func Put(ownerId int64, moduleId int64, ejaId int64, fieldName string, fieldValue string) error {
48-
moduleName := ModuleGetNameById(moduleId)
47+
func (session *TypeSession) Put(ownerId int64, moduleId int64, ejaId int64, fieldName string, fieldValue string) error {
48+
moduleName := session.ModuleGetNameById(moduleId)
4949

50-
check, err := TableExists(moduleName)
50+
check, err := session.TableExists(moduleName)
5151
if err != nil {
5252
return err
5353
}
5454
if !check {
5555
return errors.New("table not found")
5656
}
5757

58-
check, err = FieldExists(moduleName, fieldName)
58+
check, err = session.FieldExists(moduleName, fieldName)
5959
if err != nil {
6060
return err
6161
}
6262
if !check {
6363
return errors.New("field not found")
6464
}
6565

66-
query := fmt.Sprintf("UPDATE %s SET %s=? WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, fieldName, OwnersCsv(ownerId, moduleId))
67-
_, err = Run(query, fieldValue, ejaId)
66+
query := fmt.Sprintf("UPDATE %s SET %s=? WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, fieldName, session.OwnersCsv(ownerId, moduleId))
67+
_, err = session.Run(query, fieldValue, ejaId)
6868
if err != nil {
6969
return err
7070
}
7171
return nil
7272
}
7373

7474
// Del deletes a row from the specified module table based on ownerId, moduleId, and ejaId.
75-
func Del(ownerId int64, moduleId, ejaId int64) error {
76-
owners := Owners(ownerId, moduleId)
77-
csv := NumbersToCsv(owners)
78-
moduleName := ModuleGetNameById(moduleId)
75+
func (session *TypeSession) Del(ownerId int64, moduleId, ejaId int64) error {
76+
owners := session.Owners(ownerId, moduleId)
77+
csv := session.NumbersToCsv(owners)
78+
moduleName := session.ModuleGetNameById(moduleId)
7979

8080
if moduleName == "ejaModules" {
81-
ejaModulesOwnersCsv := OwnersCsv(ownerId, moduleId)
82-
tableName, err := Value("SELECT name FROM ejaModules WHERE ejaId=? AND ejaOwner IN ("+ejaModulesOwnersCsv+")", ejaId)
81+
ejaModulesOwnersCsv := session.OwnersCsv(ownerId, moduleId)
82+
tableName, err := session.Value("SELECT name FROM ejaModules WHERE ejaId=? AND ejaOwner IN ("+ejaModulesOwnersCsv+")", ejaId)
8383
if err == nil && tableName != "" {
84-
TableDel(tableName)
85-
Run("DELETE FROM ejaFields WHERE ejaModuleId=?", ejaId)
86-
Run("DELETE FROM ejaPermissions WHERE ejaModuleId=?", ejaId)
87-
Run("DELETE FROM ejaTranslations WHERE ejaModuleId=?", ejaId)
88-
Run("DELETE FROM ejaModuleLinks WHERE dstModuleId=?", ejaId)
84+
session.TableDel(tableName)
85+
session.Run("DELETE FROM ejaFields WHERE ejaModuleId=?", ejaId)
86+
session.Run("DELETE FROM ejaPermissions WHERE ejaModuleId=?", ejaId)
87+
session.Run("DELETE FROM ejaTranslations WHERE ejaModuleId=?", ejaId)
88+
session.Run("DELETE FROM ejaModuleLinks WHERE dstModuleId=?", ejaId)
8989
}
9090
}
9191

9292
// Delete the entry from the module table
9393
query := fmt.Sprintf("DELETE FROM %s WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, csv)
94-
if _, err := Run(query, ejaId); err != nil {
94+
if _, err := session.Run(query, ejaId); err != nil {
9595
return err
9696
}
9797

9898
// Delete related entries from 'ejaLinks' table
9999
query = fmt.Sprintf("DELETE FROM ejaLinks WHERE (dstModuleId=? AND dstFieldId=?) OR (srcModuleId=? AND srcFieldId=?) AND ejaOwner IN (%s)", csv)
100-
if _, err := Run(query, moduleId, ejaId, moduleId, ejaId); err != nil {
100+
if _, err := session.Run(query, moduleId, ejaId, moduleId, ejaId); err != nil {
101101
return err
102102
}
103103

db/export.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,43 @@
33
package db
44

55
// ModuleExport exports a module.
6-
func ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
6+
func (session *TypeSession) ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
77
var row TypeRow
88
var rows TypeRows
9-
moduleName := ModuleGetNameById(moduleId)
9+
moduleName := session.ModuleGetNameById(moduleId)
1010
module.Name = moduleName
11-
row, err = Row("SELECT a.searchLimit, a.sqlCreated, a.power, a.sortList, (SELECT x.name FROM ejaModules AS x WHERE x.ejaId=a.parentId) AS parentName FROM ejaModules AS a WHERE ejaId=?", moduleId)
11+
row, err = session.Row("SELECT a.searchLimit, a.sqlCreated, a.power, a.sortList, (SELECT x.name FROM ejaModules AS x WHERE x.ejaId=a.parentId) AS parentName FROM ejaModules AS a WHERE ejaId=?", moduleId)
1212
if err != nil {
1313
return
1414
}
1515
module.Module = TypeModuleModule{
1616
ParentName: row["parentName"],
17-
Power: Number(row["power"]),
18-
SearchLimit: Number(row["searchLimit"]),
19-
SqlCreated: Number(row["sqlCreated"]),
17+
Power: session.Number(row["power"]),
18+
SearchLimit: session.Number(row["searchLimit"]),
19+
SqlCreated: session.Number(row["sqlCreated"]),
2020
SortList: row["sortList"],
2121
}
2222

23-
rows, err = Rows("SELECT * FROM ejaFields WHERE ejaModuleId=?", moduleId)
23+
rows, err = session.Rows("SELECT * FROM ejaFields WHERE ejaModuleId=?", moduleId)
2424
if err != nil {
2525
return
2626
}
2727
for _, row := range rows {
2828
module.Field = append(module.Field, TypeModuleField{
2929
Name: row["name"],
3030
Value: row["value"],
31-
PowerSearch: Number(row["powerSearch"]),
32-
PowerList: Number(row["powerList"]),
33-
PowerEdit: Number(row["powerEdit"]),
34-
SizeSearch: Number(row["sizeSearch"]),
35-
SizeList: Number(row["sizeList"]),
36-
SizeEdit: Number(row["sizeEdit"]),
31+
PowerSearch: session.Number(row["powerSearch"]),
32+
PowerList: session.Number(row["powerList"]),
33+
PowerEdit: session.Number(row["powerEdit"]),
34+
SizeSearch: session.Number(row["sizeSearch"]),
35+
SizeList: session.Number(row["sizeList"]),
36+
SizeEdit: session.Number(row["sizeEdit"]),
3737
Type: row["type"],
38-
Translate: Number(row["translate"]),
38+
Translate: session.Number(row["translate"]),
3939
})
4040
}
4141

42-
rows, err = Rows(`
42+
rows, err = session.Rows(`
4343
SELECT ejaLanguage, word, translation, (SELECT ejaModules.name FROM ejaModules WHERE ejaModules.ejaId=ejaModuleId) AS ejaModuleName
4444
FROM ejaTranslations
4545
WHERE ejaModuleId=? OR word=?
@@ -57,7 +57,7 @@ func ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
5757
}
5858

5959
module.Command = []string{}
60-
rows, err = Rows("SELECT name from ejaCommands WHERE ejaId IN (SELECT ejaCommandId FROM ejaPermissions WHERE ejaModuleId=?)", moduleId)
60+
rows, err = session.Rows("SELECT name from ejaCommands WHERE ejaId IN (SELECT ejaCommandId FROM ejaPermissions WHERE ejaModuleId=?)", moduleId)
6161
if err != nil {
6262
return
6363
}
@@ -66,7 +66,7 @@ func ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
6666
}
6767

6868
if data {
69-
rows, err = Rows("SELECT * FROM " + moduleName)
69+
rows, err = session.Rows("SELECT * FROM " + moduleName)
7070
if err != nil {
7171
return
7272
}

0 commit comments

Comments
 (0)