Skip to content

Commit

Permalink
add session support to the db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaldus committed Jun 28, 2024
1 parent 62c0498 commit 64107d4
Show file tree
Hide file tree
Showing 31 changed files with 504 additions and 425 deletions.
13 changes: 13 additions & 0 deletions api/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2007-2024 by Ubaldo Porcheddu <[email protected]>

package api

import (
"github.com/eja/tibula/db"
)

type dbTypeLink = db.TypeLink
type dbTypeModule = db.TypeModule
type dbTypeSession = db.TypeSession

var dbSession = db.Session
10 changes: 6 additions & 4 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package api
import (
"encoding/base64"
"errors"
"github.com/eja/tibula/db"

"github.com/eja/tibula/sys"
)

Expand All @@ -16,13 +16,15 @@ func Set() TypeApi {
DefaultSearchOrder: "ejaLog DESC",
Values: make(map[string]string),
SearchOrder: make(map[string]string),
Link: db.TypeLink{},
Link: dbTypeLink{},
}
}

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

db := dbSession()

//open db connection
if err = db.Open(sys.Options.DbType, sys.Options.DbName, sys.Options.DbUser, sys.Options.DbPass, sys.Options.DbHost, sys.Options.DbPort); err != nil {
return
Expand Down Expand Up @@ -119,7 +121,7 @@ func Run(eja TypeApi, sessionSave bool) (result TypeApi, err error) {
if eja.ModuleId == eja.Link.ModuleId && eja.Id == eja.Link.FieldId && eja.Id > 0 {
db.SessionCleanLink(eja.Owner)
db.SessionCleanSearch(eja.Owner)
eja.Link = db.TypeLink{}
eja.Link = dbTypeLink{}
eja.Action = "edit"
eja.Linking = false
}
Expand Down Expand Up @@ -344,7 +346,7 @@ func Run(eja TypeApi, sessionSave bool) (result TypeApi, err error) {
}

if Plugins[eja.ModuleName] != nil {
eja = Plugins[eja.ModuleName](eja)
eja = Plugins[eja.ModuleName](eja, db)
}

if eja.Owner > 0 && !sessionSave {
Expand Down
11 changes: 5 additions & 6 deletions api/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ package api

import (
"encoding/json"
"github.com/eja/tibula/db"
)

type TypePlugins map[string]func(TypeApi) TypeApi
type TypePlugins map[string]func(TypeApi, dbTypeSession) TypeApi

var Plugins = TypePlugins{
"ejaProfile": func(eja TypeApi) TypeApi {
"ejaProfile": func(eja TypeApi, db dbTypeSession) TypeApi {
eja.Alert = nil
if eja.Action == "run" {
if eja.Values["passwordOld"] == "" || eja.Values["passwordNew"] == "" || eja.Values["passwordRepeat"] == "" {
Expand All @@ -32,13 +31,13 @@ var Plugins = TypePlugins{
}
return eja
},
"ejaImport": func(eja TypeApi) TypeApi {
"ejaImport": func(eja TypeApi, db dbTypeSession) TypeApi {
if eja.Action == "run" {
moduleName := eja.Values["moduleName"]
moduleData := eja.Values["import"]
dataImport := db.Number(eja.Values["dataImport"])
if moduleData != "" {
var module db.TypeModule
var module dbTypeModule
if err := json.Unmarshal([]byte(moduleData), &module); err != nil {
alert(&eja.Alert, db.Translate("ejaImportJsonError", eja.Owner))
} else {
Expand All @@ -62,7 +61,7 @@ var Plugins = TypePlugins{
}
return eja
},
"ejaExport": func(eja TypeApi) TypeApi {
"ejaExport": func(eja TypeApi, db dbTypeSession) TypeApi {
if eja.Action == "run" {
moduleId := db.Number(eja.Values["ejaModuleId"])
dataExport := db.Number(eja.Values["dataExport"]) > 0
Expand Down
20 changes: 10 additions & 10 deletions db/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ type TypeCommand struct {

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

moduleName := ModuleGetNameById(moduleId)
moduleName := session.ModuleGetNameById(moduleId)
if moduleName == "" {
return nil, errors.New("module does not exist")
}

if moduleName == "ejaLogin" {
commandList = append(commandList, TypeCommand{Name: "login", Label: Translate("login", userId)})
commandList = append(commandList, TypeCommand{Name: "login", Label: session.Translate("login", userId)})
}

if actionType != "" {
Expand All @@ -48,28 +48,28 @@ func Commands(userId int64, moduleId int64, actionType string) ([]TypeCommand, e
)
)
) %s`,
UserGroupCsv(userId), actionTypeSql)
session.UserGroupCsv(userId), actionTypeSql)

rows, err := Rows(
rows, err := session.Rows(
query,
moduleId,
ModuleGetIdByName("ejaPermissions"),
ModuleGetIdByName("ejaUsers"),
session.ModuleGetIdByName("ejaPermissions"),
session.ModuleGetIdByName("ejaUsers"),
userId,
ModuleGetIdByName("ejaGroups"),
session.ModuleGetIdByName("ejaGroups"),
)
if err != nil {
return nil, err
}

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

// CommandExists checks if a given command exists in the provided list of commands.
func CommandExists(commands []TypeCommand, commandName string) bool {
func (session *TypeSession) CommandExists(commands []TypeCommand, commandName string) bool {
for _, row := range commands {
if row.Name == commandName {
return true
Expand Down
56 changes: 28 additions & 28 deletions db/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

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

check, err := TableExists(moduleName)
check, err := session.TableExists(moduleName)
if err != nil {
return 0, err
}
Expand All @@ -20,84 +20,84 @@ func New(ownerId int64, moduleId int64) (int64, error) {
}

query := fmt.Sprintf("INSERT INTO %s (ejaOwner, ejaLog) VALUES (?,?)", moduleName)
run, err := Run(query, ownerId, Now())
run, err := session.Run(query, ownerId, session.Now())
if err != nil {
return 0, err
}
return run.LastId, nil
}

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

check, err := TableExists(moduleName)
check, err := session.TableExists(moduleName)
if err != nil {
return nil, err
}
if !check {
return nil, errors.New("table does not exist")
}

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

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

check, err := TableExists(moduleName)
check, err := session.TableExists(moduleName)
if err != nil {
return err
}
if !check {
return errors.New("table not found")
}

check, err = FieldExists(moduleName, fieldName)
check, err = session.FieldExists(moduleName, fieldName)
if err != nil {
return err
}
if !check {
return errors.New("field not found")
}

query := fmt.Sprintf("UPDATE %s SET %s=? WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, fieldName, OwnersCsv(ownerId, moduleId))
_, err = Run(query, fieldValue, ejaId)
query := fmt.Sprintf("UPDATE %s SET %s=? WHERE ejaId=? AND ejaOwner IN (%s)", moduleName, fieldName, session.OwnersCsv(ownerId, moduleId))
_, err = session.Run(query, fieldValue, ejaId)
if err != nil {
return err
}
return nil
}

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

if moduleName == "ejaModules" {
ejaModulesOwnersCsv := OwnersCsv(ownerId, moduleId)
tableName, err := Value("SELECT name FROM ejaModules WHERE ejaId=? AND ejaOwner IN ("+ejaModulesOwnersCsv+")", ejaId)
ejaModulesOwnersCsv := session.OwnersCsv(ownerId, moduleId)
tableName, err := session.Value("SELECT name FROM ejaModules WHERE ejaId=? AND ejaOwner IN ("+ejaModulesOwnersCsv+")", ejaId)
if err == nil && tableName != "" {
TableDel(tableName)
Run("DELETE FROM ejaFields WHERE ejaModuleId=?", ejaId)
Run("DELETE FROM ejaPermissions WHERE ejaModuleId=?", ejaId)
Run("DELETE FROM ejaTranslations WHERE ejaModuleId=?", ejaId)
Run("DELETE FROM ejaModuleLinks WHERE dstModuleId=?", ejaId)
session.TableDel(tableName)
session.Run("DELETE FROM ejaFields WHERE ejaModuleId=?", ejaId)
session.Run("DELETE FROM ejaPermissions WHERE ejaModuleId=?", ejaId)
session.Run("DELETE FROM ejaTranslations WHERE ejaModuleId=?", ejaId)
session.Run("DELETE FROM ejaModuleLinks WHERE dstModuleId=?", ejaId)
}
}

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

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

Expand Down
34 changes: 17 additions & 17 deletions db/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,43 @@
package db

// ModuleExport exports a module.
func ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
func (session *TypeSession) ModuleExport(moduleId int64, data bool) (module TypeModule, err error) {
var row TypeRow
var rows TypeRows
moduleName := ModuleGetNameById(moduleId)
moduleName := session.ModuleGetNameById(moduleId)
module.Name = moduleName
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)
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)
if err != nil {
return
}
module.Module = TypeModuleModule{
ParentName: row["parentName"],
Power: Number(row["power"]),
SearchLimit: Number(row["searchLimit"]),
SqlCreated: Number(row["sqlCreated"]),
Power: session.Number(row["power"]),
SearchLimit: session.Number(row["searchLimit"]),
SqlCreated: session.Number(row["sqlCreated"]),
SortList: row["sortList"],
}

rows, err = Rows("SELECT * FROM ejaFields WHERE ejaModuleId=?", moduleId)
rows, err = session.Rows("SELECT * FROM ejaFields WHERE ejaModuleId=?", moduleId)
if err != nil {
return
}
for _, row := range rows {
module.Field = append(module.Field, TypeModuleField{
Name: row["name"],
Value: row["value"],
PowerSearch: Number(row["powerSearch"]),
PowerList: Number(row["powerList"]),
PowerEdit: Number(row["powerEdit"]),
SizeSearch: Number(row["sizeSearch"]),
SizeList: Number(row["sizeList"]),
SizeEdit: Number(row["sizeEdit"]),
PowerSearch: session.Number(row["powerSearch"]),
PowerList: session.Number(row["powerList"]),
PowerEdit: session.Number(row["powerEdit"]),
SizeSearch: session.Number(row["sizeSearch"]),
SizeList: session.Number(row["sizeList"]),
SizeEdit: session.Number(row["sizeEdit"]),
Type: row["type"],
Translate: Number(row["translate"]),
Translate: session.Number(row["translate"]),
})
}

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

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

if data {
rows, err = Rows("SELECT * FROM " + moduleName)
rows, err = session.Rows("SELECT * FROM " + moduleName)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 64107d4

Please sign in to comment.