Skip to content

Commit

Permalink
Add sql module
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Feb 23, 2024
1 parent 7c72785 commit 32ab137
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 11 deletions.
25 changes: 14 additions & 11 deletions kvstore/database/engine.go → db/engine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package database
package db

import (
"os"
Expand All @@ -12,11 +12,13 @@ import (
type Engine string

const (
EngineUnknown Engine = "unknown"
EngineAuto Engine = "auto"
EngineDebug Engine = "debug"
EngineMapDB Engine = "mapdb"
EngineRocksDB Engine = "rocksdb"
EngineUnknown Engine = "unknown"
EngineAuto Engine = "auto"
EngineDebug Engine = "debug"
EngineMapDB Engine = "mapdb"
EngineRocksDB Engine = "rocksdb"
EngineSQLite Engine = "sqlite"
EnginePostgreSQL Engine = "postgresql"
)

var (
Expand All @@ -37,8 +39,8 @@ func engineFromString(engineStr string) Engine {
return Engine(strings.ToLower(engineStr))
}

// getSupportedEnginesString returns a string containing all supported engines separated by "/".
func getSupportedEnginesString(supportedEngines []Engine) string {
// GetSupportedEnginesString returns a string containing all supported engines separated by "/".
func GetSupportedEnginesString(supportedEngines []Engine) string {
supportedEnginesStr := ""
for i, allowedEngine := range supportedEngines {
if i != 0 {
Expand All @@ -59,7 +61,7 @@ func EngineAllowed(dbEngine Engine, allowedEngines []Engine) (Engine, error) {
}
}

return EngineUnknown, ierrors.Errorf("unknown database engine: %s, supported engines: %s", dbEngine, getSupportedEnginesString(allowedEngines))
return EngineUnknown, ierrors.Errorf("unknown database engine: %s, supported engines: %s", dbEngine, GetSupportedEnginesString(allowedEngines))
}

// EngineFromStringAllowed parses an engine from a string and checks if the database engine is allowed.
Expand All @@ -83,8 +85,9 @@ func CheckEngine(dbPath string, createDatabaseIfNotExists bool, dbEngine Engine,
case EngineUnknown:
return EngineUnknown, ierrors.New("the database engine must not be EngineUnknown")

case EngineMapDB:
// no need to create or access a "database info file" in case of mapdb (in-memory)
// TODO: add an interface with a flag that indicates if the database needs the file system or not.
case EngineMapDB, EnginePostgreSQL:
// no need to create or access a "database info file" in case of mapdb (in-memory) or postgres (external database)
return EngineMapDB, nil
}

Expand Down
13 changes: 13 additions & 0 deletions db/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/iotaledger/hive.go/db

go 1.22.0

require (
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e
)

require (
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 23 additions & 0 deletions db/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e h1:SRT3fEbDVM+uZtH+lqN1o5e3VZvJJv2KspbhuSa1lTM=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e/go.mod h1:GQY0/35sjgT9Poi1Vrs9kFVvAkuKzGXfVh4j6CBXsAA=
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e h1:NLuBZrIWe3ScuTHWv0zZF9UnViLC2/C4KrvlxhxCKQg=
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e/go.mod h1:lAR3vbqE9g65M9VwWHiaXab2q+d89dBOFjhKgnDfK7c=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 36 additions & 0 deletions sql/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/iotaledger/hive.go/sql

go 1.22.0

require (
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e
github.com/iotaledger/hive.go/log v0.0.0-20240222083732-602e8e1b2f7e
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e
gorm.io/driver/postgres v1.5.6
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.7
)

require (
github.com/ethereum/go-ethereum v1.13.12 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/hive.go/constraints v0.0.0-20240216125747-dc3943c569bf // indirect
github.com/iotaledger/hive.go/ds v0.0.0-20240216125747-dc3943c569bf // indirect
github.com/iotaledger/hive.go/lo v0.0.0-20240216125747-dc3943c569bf // indirect
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216125747-dc3943c569bf // indirect
github.com/iotaledger/hive.go/stringify v0.0.0-20240216125747-dc3943c569bf // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
86 changes: 86 additions & 0 deletions sql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
github.com/btcsuite/btcd/btcec/v2 v2.2.1 h1:xP60mv8fvp+0khmrN0zTdPC3cNm24rfeE6lh2R/Yv3E=
github.com/btcsuite/btcd/btcec/v2 v2.2.1/go.mod h1:9/CSmJxmuvqzX9Wh2fXMWToLOHhPd11lSPuIupwTkI8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
github.com/ethereum/go-ethereum v1.13.12 h1:iDr9UM2JWkngBHGovRJEQn4Kor7mT4gt9rUZqB5M29Y=
github.com/ethereum/go-ethereum v1.13.12/go.mod h1:hKL2Qcj1OvStXNSEDbucexqnEt1Wh4Cz329XsjAalZY=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/iotaledger/hive.go/constraints v0.0.0-20240216125747-dc3943c569bf h1:LkZq4DwIjEflSiNiLnCZdwfFTBxYWIMeOAQNFJQtFfE=
github.com/iotaledger/hive.go/constraints v0.0.0-20240216125747-dc3943c569bf/go.mod h1:JF7jjkL6tSUOXm23SWadBzBrl7eJk1DQRLc/fNoVZ+o=
github.com/iotaledger/hive.go/ds v0.0.0-20240216125747-dc3943c569bf h1:bhBynZBxq76sQaFoBlpqOmFT20AyaLItkW5A/2cjY+w=
github.com/iotaledger/hive.go/ds v0.0.0-20240216125747-dc3943c569bf/go.mod h1:WVG1fdVj0b47NcRo1iTNT5CKvjQDvORgU9lD3epcSHM=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e h1:SRT3fEbDVM+uZtH+lqN1o5e3VZvJJv2KspbhuSa1lTM=
github.com/iotaledger/hive.go/ierrors v0.0.0-20240222083732-602e8e1b2f7e/go.mod h1:GQY0/35sjgT9Poi1Vrs9kFVvAkuKzGXfVh4j6CBXsAA=
github.com/iotaledger/hive.go/lo v0.0.0-20240216125747-dc3943c569bf h1:5NndLgYJludTyghx2FVAgFHe+7hAqbbR/3epu+/zQvg=
github.com/iotaledger/hive.go/lo v0.0.0-20240216125747-dc3943c569bf/go.mod h1:0ycE1W59kkNjQ87odL6H4x5Ik9bXhbNk88wmo+mIYt0=
github.com/iotaledger/hive.go/log v0.0.0-20240222083732-602e8e1b2f7e h1:g03c/P+yXscbY+K42F1pon4x0KK/+1q/0g3bpy3akJc=
github.com/iotaledger/hive.go/log v0.0.0-20240222083732-602e8e1b2f7e/go.mod h1:JCv/LVVTAMoCPLzou534RoIif7CAezcWZlGYabrHsck=
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e h1:NLuBZrIWe3ScuTHWv0zZF9UnViLC2/C4KrvlxhxCKQg=
github.com/iotaledger/hive.go/runtime v0.0.0-20240222083732-602e8e1b2f7e/go.mod h1:lAR3vbqE9g65M9VwWHiaXab2q+d89dBOFjhKgnDfK7c=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216125747-dc3943c569bf h1:8f7+Nv96LycYGhGgYXaoym47Ki+CkzbweaNlsQ0zUPo=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20240216125747-dc3943c569bf/go.mod h1:yXQNGZUz++dB1T9RAHhWSresuTLzTC856ntdins9ivo=
github.com/iotaledger/hive.go/stringify v0.0.0-20240216125747-dc3943c569bf h1:wYuDOU4lg1T145OfRJsryLaCF1Jxt1HFjeyNOUaWl6c=
github.com/iotaledger/hive.go/stringify v0.0.0-20240216125747-dc3943c569bf/go.mod h1:O4p7UmsfoeLqtAUwrKbq0lXMxjY/MLQSpZSavvvvGig=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY=
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 h1:jik8PHtAIsPlCRJjJzl4udgEf7hawInF9texMeO2jrU=
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0=
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.6 h1:ydr9xEd5YAM0vxVDY0X139dyzNz10spDiDlC7+ibLeU=
gorm.io/driver/postgres v1.5.6/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/driver/sqlite v1.5.5 h1:7MDMtUZhV065SilG62E0MquljeArQZNfJnjd9i9gx3E=
gorm.io/driver/sqlite v1.5.5/go.mod h1:6NgQ7sQWAIFsPrJJl1lSNSu2TABh0ZZ/zm5fosATavE=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
19 changes: 19 additions & 0 deletions sql/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sql

import (
"github.com/iotaledger/hive.go/log"
)

type sqlLogger struct {
log.Logger
}

func newLogger(logger log.Logger) *sqlLogger {
return &sqlLogger{
Logger: logger,
}
}

func (l *sqlLogger) Printf(t string, args ...interface{}) {
l.LogWarnf(t, args...)
}
95 changes: 95 additions & 0 deletions sql/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package sql

import (
"fmt"
"path/filepath"
"time"

"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"

"github.com/iotaledger/hive.go/db"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/hive.go/runtime/options"
)

type DatabaseParameters struct {
Engine db.Engine

// SQLite
Path string
Filename string

// PostgreSQL
Host string
Port uint
Database string
Username string
Password string
}

type gormDatabaseOptions struct {
gormConfig *gorm.Config
gormLoggerConfig gormLogger.Config
}

// WithGormConfig allows to set the gorm config.
// HINT: The Logger setting will be overwritten by the internal default value or by the value set by WithGormLoggerConfig.
func WithGormConfig(config *gorm.Config) options.Option[gormDatabaseOptions] {
return func(o *gormDatabaseOptions) {
o.gormConfig = config
}
}

// WithGormLoggerConfig allows to set the gorm logger config.
func WithGormLoggerConfig(config gormLogger.Config) options.Option[gormDatabaseOptions] {
return func(o *gormDatabaseOptions) {
o.gormLoggerConfig = config
}
}

// New creates a new gorm database instance with the given parameters and options.
func New(logger log.Logger, dbParams DatabaseParameters, createDatabaseIfNotExists bool, allowedEngines []db.Engine, opts ...options.Option[gormDatabaseOptions]) (*gorm.DB, db.Engine, error) {
targetEngine, err := db.CheckEngine(dbParams.Path, createDatabaseIfNotExists, dbParams.Engine, allowedEngines)
if err != nil {
return nil, db.EngineUnknown, err
}

var dbDialector gorm.Dialector

//nolint:exhaustive // false positive
switch targetEngine {
case db.EngineSQLite, db.EngineAuto:
dbDialector = sqlite.Open(fmt.Sprintf("file:%s?&_journal_mode=WAL&_busy_timeout=60000", filepath.Join(dbParams.Path, dbParams.Filename)))
case db.EnginePostgreSQL:
dsn := fmt.Sprintf("host='%s' user='%s' password='%s' dbname='%s' port=%d", dbParams.Host, dbParams.Username, dbParams.Password, dbParams.Database, dbParams.Port)
dbDialector = postgres.Open(dsn)
default:
return nil, db.EngineUnknown, ierrors.Errorf("unknown database engine: %s, supported engines: %s", dbParams.Engine, db.GetSupportedEnginesString(allowedEngines))
}

gormDBOptions := options.Apply(&gormDatabaseOptions{
gormConfig: &gorm.Config{},
gormLoggerConfig: gormLogger.Config{
SlowThreshold: 100 * time.Millisecond,
LogLevel: gormLogger.Warn,
IgnoreRecordNotFoundError: true,
Colorful: false,
},
}, opts,
func(o *gormDatabaseOptions) {
// overwrite the logger in the gorm config to initialize the logger with the given hive logger from the outside and the given settings.
o.gormConfig.Logger = gormLogger.New(newLogger(logger), o.gormLoggerConfig)
},
)

database, err := gorm.Open(dbDialector, gormDBOptions.gormConfig)
if err != nil {
return nil, db.EngineUnknown, err
}

return database, targetEngine, nil
}

0 comments on commit 32ab137

Please sign in to comment.