diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..03a396a --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod index e42a7b5..8c4ca59 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/gin-gonic/gin v1.9.1 github.com/jmoiron/sqlx v1.3.5 + github.com/mattn/go-sqlite3 v1.14.19 // only needed for tests -.- github.com/rs/xid v1.5.0 github.com/rs/zerolog v1.31.0 go.mongodb.org/mongo-driver v1.13.1 diff --git a/go.sum b/go.sum index e6d0a69..a0af01f 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,6 @@ github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= -github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= @@ -14,8 +13,6 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV 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/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= -github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -23,12 +20,11 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24= -github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= @@ -36,7 +32,6 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -49,19 +44,9 @@ github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Cc github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= -github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= -github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4= -github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/klauspost/compress v1.17.3 h1:qkRjuerhUU1EmXLYGkSH6EZL+vPSxIrYjLNAK4slzwA= -github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= -github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= @@ -72,12 +57,13 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= +github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -86,8 +72,6 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= -github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= 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/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -110,8 +94,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= -github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= -github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= @@ -124,27 +106,15 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.12.1 h1:nLkghSU8fQNaK7oUmDhQFsnrtcoNy7Z6LVFKsEecqgE= -go.mongodb.org/mongo-driver v1.12.1/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= -go.mongodb.org/mongo-driver v1.13.0 h1:67DgFFjYOCMWdtTEmKFpV3ffWlFnh+CYZ8ZS/tXWUfY= -go.mongodb.org/mongo-driver v1.13.0/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.5.0 h1:jpGode6huXQxcskEIpOCvrU+tzo81b6+oFLUYXWtH/Y= -golang.org/x/arch v0.5.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= -golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= -golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= 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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= @@ -153,18 +123,10 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= -golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= -golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= -golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= -golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -178,18 +140,10 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= -golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= -golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8= -golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -198,8 +152,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -208,9 +160,6 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/goextVersion.go b/goextVersion.go index 6d19037..d35eba3 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.350" +const GoextVersion = "0.0.351" -const GoextVersionTimestamp = "2023-12-29T18:06:45+0100" +const GoextVersionTimestamp = "2023-12-29T19:25:36+0100" diff --git a/sq/converter.go b/sq/converter.go index a3d2352..069ed3c 100644 --- a/sq/converter.go +++ b/sq/converter.go @@ -4,17 +4,11 @@ import ( "errors" "fmt" "gogs.mikescher.com/BlackForestBytes/goext/langext" + "gogs.mikescher.com/BlackForestBytes/goext/rfctime" + "reflect" "time" ) -//TODO UNFINISHED -// this is not finished -// idea was that we can register converter in the database struct -// they get inherited from the transactions -// and when marshallingunmarshaling (sq.Query | sq.QueryAll) -// or marshaling (sq.InsertSingle) -// the types get converter automatically... - type DBTypeConverter interface { ModelTypeString() string DBTypeString() string @@ -40,16 +34,42 @@ var ConverterTimeToUnixMillis = NewDBTypeConverter[time.Time, int64](func(v time return time.UnixMilli(v), nil }) -var ConverterOptTimeToUnixMillis = NewDBTypeConverter[*time.Time, *int64](func(v *time.Time) (*int64, error) { - if v == nil { - return nil, nil +var ConverterRFCUnixMilliTimeToUnixMillis = NewDBTypeConverter[rfctime.UnixMilliTime, int64](func(v rfctime.UnixMilliTime) (int64, error) { + return v.UnixMilli(), nil +}, func(v int64) (rfctime.UnixMilliTime, error) { + return rfctime.NewUnixMilli(time.UnixMilli(v)), nil +}) + +var ConverterRFCUnixNanoTimeToUnixNanos = NewDBTypeConverter[rfctime.UnixNanoTime, int64](func(v rfctime.UnixNanoTime) (int64, error) { + return v.UnixNano(), nil +}, func(v int64) (rfctime.UnixNanoTime, error) { + return rfctime.NewUnixNano(time.Unix(0, v)), nil +}) + +var ConverterRFCUnixTimeToUnixSeconds = NewDBTypeConverter[rfctime.UnixTime, int64](func(v rfctime.UnixTime) (int64, error) { + return v.Unix(), nil +}, func(v int64) (rfctime.UnixTime, error) { + return rfctime.NewUnix(time.Unix(v, 0)), nil +}) + +var ConverterRFC339TimeToString = NewDBTypeConverter[rfctime.RFC3339Time, string](func(v rfctime.RFC3339Time) (string, error) { + return v.Format(time.RFC3339), nil +}, func(v string) (rfctime.RFC3339Time, error) { + t, err := time.Parse(time.RFC3339Nano, v) + if err != nil { + return rfctime.RFC3339Time{}, err } - return langext.Ptr(v.UnixMilli()), nil -}, func(v *int64) (*time.Time, error) { - if v == nil { - return nil, nil + return rfctime.NewRFC3339(t), nil +}) + +var ConverterRFC339NanoTimeToString = NewDBTypeConverter[rfctime.RFC3339NanoTime, string](func(v rfctime.RFC3339NanoTime) (string, error) { + return v.Format(time.RFC3339Nano), nil +}, func(v string) (rfctime.RFC3339NanoTime, error) { + t, err := time.Parse(time.RFC3339Nano, v) + if err != nil { + return rfctime.RFC3339NanoTime{}, err } - return langext.Ptr(time.UnixMilli(*v)), nil + return rfctime.NewRFC3339Nano(t), nil }) type dbTypeConverterImpl[TModelData any, TDBData any] struct { @@ -89,3 +109,36 @@ func NewDBTypeConverter[TModelData any, TDBData any](todb func(v TModelData) (TD tomodel: tomodel, } } + +func convertValueToDB(q Queryable, value any) (any, error) { + modelTypeStr := fmt.Sprintf("%T", value) + + for _, conv := range q.ListConverter() { + if conv.ModelTypeString() == modelTypeStr { + return conv.ModelToDB(value) + } + } + + if value != nil && reflect.TypeOf(value).Kind() == reflect.Ptr { + vof := reflect.ValueOf(value) + if vof.IsNil() { + return nil, nil + } else { + return convertValueToDB(q, vof.Elem().Interface()) + } + } + + return value, nil +} + +func convertValueToModel(q Queryable, value any, destinationType string) (any, error) { + dbTypeString := fmt.Sprintf("%T", value) + + for _, conv := range q.ListConverter() { + if conv.ModelTypeString() == destinationType && conv.DBTypeString() == dbTypeString { + return conv.DBToModel(value) + } + } + + return value, nil +} diff --git a/sq/database.go b/sq/database.go index 3cefdea..529c8cf 100644 --- a/sq/database.go +++ b/sq/database.go @@ -4,16 +4,19 @@ import ( "context" "database/sql" "github.com/jmoiron/sqlx" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "sync" ) type DB interface { - Exec(ctx context.Context, sql string, prep PP) (sql.Result, error) - Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error) + Queryable + Ping(ctx context.Context) error BeginTransaction(ctx context.Context, iso sql.IsolationLevel) (Tx, error) AddListener(listener Listener) Exit() error + RegisterConverter(DBTypeConverter) + RegisterDefaultConverter() } type database struct { @@ -21,6 +24,7 @@ type database struct { txctr uint16 lock sync.Mutex lstr []Listener + conv []DBTypeConverter } func NewDB(db *sqlx.DB) DB { @@ -120,9 +124,28 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel v.PostTxBegin(txid, err) } - return NewTransaction(xtx, txid, db.lstr), nil + return NewTransaction(xtx, txid, db), nil } func (db *database) Exit() error { return db.db.Close() } + +func (db *database) ListConverter() []DBTypeConverter { + return db.conv +} + +func (db *database) RegisterConverter(conv DBTypeConverter) { + db.conv = langext.ArrFilter(db.conv, func(v DBTypeConverter) bool { return v.ModelTypeString() != conv.ModelTypeString() }) + db.conv = append(db.conv, conv) +} + +func (db *database) RegisterDefaultConverter() { + db.RegisterConverter(ConverterBoolToBit) + db.RegisterConverter(ConverterTimeToUnixMillis) + db.RegisterConverter(ConverterRFCUnixMilliTimeToUnixMillis) + db.RegisterConverter(ConverterRFCUnixNanoTimeToUnixNanos) + db.RegisterConverter(ConverterRFCUnixTimeToUnixSeconds) + db.RegisterConverter(ConverterRFC339TimeToString) + db.RegisterConverter(ConverterRFC339NanoTimeToString) +} diff --git a/sq/hasher.go b/sq/hasher.go index 3bd0019..678e79f 100644 --- a/sq/hasher.go +++ b/sq/hasher.go @@ -121,7 +121,7 @@ func CreateSqliteDatabaseSchemaString(ctx context.Context, db Queryable) (string if err != nil { return "", err } - tableList, err := ScanAll[tabInfo](rowsTableList, SModeFast, Unsafe, true) + tableList, err := ScanAll[tabInfo](ctx, db, rowsTableList, SModeFast, Unsafe, true) if err != nil { return "", err } @@ -143,7 +143,7 @@ func CreateSqliteDatabaseSchemaString(ctx context.Context, db Queryable) (string return "", err } - columnList, err := ScanAll[colInfo](rowsColumnList, SModeFast, Unsafe, true) + columnList, err := ScanAll[colInfo](ctx, db, rowsColumnList, SModeFast, Unsafe, true) if err != nil { return "", err } @@ -158,7 +158,7 @@ func CreateSqliteDatabaseSchemaString(ctx context.Context, db Queryable) (string if err != nil { return "", err } - idxList, err := ScanAll[idxInfo](rowsIdxList, SModeFast, Unsafe, true) + idxList, err := ScanAll[idxInfo](ctx, db, rowsIdxList, SModeFast, Unsafe, true) if err != nil { return "", err } @@ -173,7 +173,7 @@ func CreateSqliteDatabaseSchemaString(ctx context.Context, db Queryable) (string if err != nil { return "", err } - fkyList, err := ScanAll[fkyInfo](rowsIdxList, SModeFast, Unsafe, true) + fkyList, err := ScanAll[fkyInfo](ctx, db, rowsIdxList, SModeFast, Unsafe, true) if err != nil { return "", err } diff --git a/sq/queryable.go b/sq/queryable.go index 07aee31..fbf063f 100644 --- a/sq/queryable.go +++ b/sq/queryable.go @@ -9,4 +9,5 @@ import ( type Queryable interface { Exec(ctx context.Context, sql string, prep PP) (sql.Result, error) Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error) + ListConverter() []DBTypeConverter } diff --git a/sq/scanner.go b/sq/scanner.go index 6544fdf..949c20a 100644 --- a/sq/scanner.go +++ b/sq/scanner.go @@ -13,8 +13,8 @@ import ( type StructScanMode string const ( - SModeFast StructScanMode = "FAST" - SModeExtended StructScanMode = "EXTENDED" + SModeFast StructScanMode = "FAST" // Use default sq.Scan, does not work with joined/resolved types and/or custom value converter + SModeExtended StructScanMode = "EXTENDED" // Fully featured perhaps (?) a tiny bit slower - default ) type StructScanSafety string @@ -51,7 +51,13 @@ func InsertSingle[TData any](ctx context.Context, q Queryable, tableName string, columns = append(columns, "\""+columnName+"\"") params = append(params, ":"+paramkey) - pp[paramkey] = rvfield.Interface() + + val, err := convertValueToDB(q, rvfield.Interface()) + if err != nil { + return nil, err + } + + pp[paramkey] = val } @@ -71,7 +77,7 @@ func QuerySingle[TData any](ctx context.Context, q Queryable, sql string, pp PP, return *new(TData), err } - data, err := ScanSingle[TData](rows, mode, sec, true) + data, err := ScanSingle[TData](ctx, q, rows, mode, sec, true) if err != nil { return *new(TData), err } @@ -85,7 +91,7 @@ func QueryAll[TData any](ctx context.Context, q Queryable, sql string, pp PP, mo return nil, err } - data, err := ScanAll[TData](rows, mode, sec, true) + data, err := ScanAll[TData](ctx, q, rows, mode, sec, true) if err != nil { return nil, err } @@ -93,7 +99,7 @@ func QueryAll[TData any](ctx context.Context, q Queryable, sql string, pp PP, mo return data, nil } -func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) (TData, error) { +func ScanSingle[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) (TData, error) { if rows.Next() { var strscan *StructScanner @@ -123,7 +129,7 @@ func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanS return *new(TData), err } } else if mode == SModeExtended { - err := strscan.StructScanExt(&data) + err := strscan.StructScanExt(q, &data) if err != nil { return *new(TData), err } @@ -149,6 +155,10 @@ func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanS return *new(TData), err } + if err := ctx.Err(); err != nil { + return *new(TData), err + } + return data, nil } else { @@ -159,7 +169,7 @@ func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanS } } -func ScanAll[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) ([]TData, error) { +func ScanAll[TData any](ctx context.Context, q Queryable, rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) ([]TData, error) { var strscan *StructScanner if sec == Safe { @@ -182,6 +192,11 @@ func ScanAll[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafe res := make([]TData, 0) for rows.Next() { + + if err := ctx.Err(); err != nil { + return nil, err + } + if mode == SModeFast { var data TData err := strscan.StructScanBase(&data) @@ -191,7 +206,7 @@ func ScanAll[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafe res = append(res, data) } else if mode == SModeExtended { var data TData - err := strscan.StructScanExt(&data) + err := strscan.StructScanExt(q, &data) if err != nil { return nil, err } diff --git a/sq/sq_test.go b/sq/sq_test.go new file mode 100644 index 0000000..0c7e2a5 --- /dev/null +++ b/sq/sq_test.go @@ -0,0 +1,89 @@ +package sq + +import ( + "context" + "fmt" + "github.com/jmoiron/sqlx" + "github.com/mattn/go-sqlite3" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "gogs.mikescher.com/BlackForestBytes/goext/rfctime" + "gogs.mikescher.com/BlackForestBytes/goext/tst" + "os" + "path/filepath" + "testing" + "time" +) + +func TestTypeConverter1(t *testing.T) { + type RequestData struct { + ID string `db:"id"` + Timestamp time.Time `db:"timestamp"` + } + + ctx := context.Background() + + dbdir := t.TempDir() + dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3") + + sqlite3.Version() // ensure loaded + + tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm)) + + url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000) + + xdb := tst.Must(sqlx.Open("sqlite3", url))(t) + + db := NewDB(xdb) + db.RegisterDefaultConverter() + + _, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NOT NULL, PRIMARY KEY (id) ) STRICT", PP{}) + tst.AssertNoErr(t, err) + + _, err = InsertSingle(ctx, db, "requests", RequestData{ + ID: "001", + Timestamp: time.Date(2000, 06, 15, 12, 0, 0, 0, time.UTC), + }) + tst.AssertNoErr(t, err) +} + +func TestTypeConverter2(t *testing.T) { + type RequestData struct { + ID string `db:"id"` + Timestamp rfctime.UnixMilliTime `db:"timestamp"` + } + + ctx := context.Background() + + dbdir := t.TempDir() + dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3") + + sqlite3.Version() // ensure loaded + + tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm)) + + url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000) + + xdb := tst.Must(sqlx.Open("sqlite3", url))(t) + + db := NewDB(xdb) + db.RegisterDefaultConverter() + + _, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NOT NULL, PRIMARY KEY (id) ) STRICT", PP{}) + tst.AssertNoErr(t, err) + + t0 := rfctime.NewUnixMilli(time.Date(2012, 03, 01, 16, 0, 0, 0, time.UTC)) + + _, err = InsertSingle(ctx, db, "requests", RequestData{ + ID: "002", + Timestamp: t0, + }) + tst.AssertNoErr(t, err) + + r, err := QuerySingle[RequestData](ctx, db, "SELECT * FROM requests WHERE id = '002'", PP{}, SModeExtended, Safe) + tst.AssertNoErr(t, err) + + fmt.Printf("%+v\n", r) + + tst.AssertEqual(t, "002", r.ID) + tst.AssertEqual(t, t0.UnixNano(), r.Timestamp.UnixNano()) +} diff --git a/sq/structscanner.go b/sq/structscanner.go index 758eb94..ce15d4e 100644 --- a/sq/structscanner.go +++ b/sq/structscanner.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx/reflectx" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "reflect" ) @@ -15,9 +16,10 @@ type StructScanner struct { Mapper *reflectx.Mapper unsafe bool - fields [][]int - values []any - columns []string + fields [][]int + values []any + converter []DBTypeConverter + columns []string } func NewStructScanner(rows *sqlx.Rows, unsafe bool) *StructScanner { @@ -47,13 +49,15 @@ func (r *StructScanner) Start(dest any) error { return fmt.Errorf("missing destination name %s in %T", columns[f], dest) } r.values = make([]interface{}, len(columns)) + r.converter = make([]DBTypeConverter, len(columns)) return nil } // StructScanExt forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go -// does also wok with nullabel structs (from LEFT JOIN's) -func (r *StructScanner) StructScanExt(dest any) error { +// does also work with nullabel structs (from LEFT JOIN's) +// does also work with custom value converters +func (r *StructScanner) StructScanExt(q Queryable, dest any) error { v := reflect.ValueOf(dest) if v.Kind() != reflect.Ptr { @@ -64,7 +68,7 @@ func (r *StructScanner) StructScanExt(dest any) error { v = v.Elem() - err := fieldsByTraversalExtended(v, r.fields, r.values) + err := fieldsByTraversalExtended(q, v, r.fields, r.values, r.converter) if err != nil { return err } @@ -131,7 +135,6 @@ func (r *StructScanner) StructScanExt(dest any) error { val1 := reflect.ValueOf(r.values[i]) val2 := val1.Elem() - val3 := val2.Elem() if val2.IsNil() { if f.Kind() != reflect.Pointer { @@ -140,7 +143,16 @@ func (r *StructScanner) StructScanExt(dest any) error { f.Set(reflect.Zero(f.Type())) // set to nil } else { - f.Set(val3) + if r.converter[i] != nil { + val3 := val2.Elem().Interface() + conv3, err := r.converter[i].DBToModel(val3) + if err != nil { + return err + } + f.Set(reflect.ValueOf(conv3)) + } else { + f.Set(val2.Elem()) + } } } @@ -172,7 +184,7 @@ func (r *StructScanner) StructScanBase(dest any) error { } // fieldsByTraversal forked from github.com/jmoiron/sqlx@v1.3.5/sqlx.go -func fieldsByTraversalExtended(v reflect.Value, traversals [][]int, values []interface{}) error { +func fieldsByTraversalExtended(q Queryable, v reflect.Value, traversals [][]int, values []interface{}, converter []DBTypeConverter) error { v = reflect.Indirect(v) if v.Kind() != reflect.Struct { return errors.New("argument not a struct") @@ -185,7 +197,23 @@ func fieldsByTraversalExtended(v reflect.Value, traversals [][]int, values []int } f := reflectx.FieldByIndexes(v, traversal) - values[i] = reflect.New(reflect.PointerTo(f.Type())).Interface() + typeStr := f.Type().String() + + foundConverter := false + for _, conv := range q.ListConverter() { + if conv.ModelTypeString() == typeStr { + _v := langext.Ptr[any](nil) + values[i] = _v + foundConverter = true + converter[i] = conv + break + } + } + + if !foundConverter { + values[i] = reflect.New(reflect.PointerTo(f.Type())).Interface() + converter[i] = nil + } } return nil } diff --git a/sq/transaction.go b/sq/transaction.go index dd0433d..a6306cc 100644 --- a/sq/transaction.go +++ b/sq/transaction.go @@ -17,35 +17,35 @@ const ( ) type Tx interface { + Queryable + Rollback() error Commit() error Status() TxStatus - Exec(ctx context.Context, sql string, prep PP) (sql.Result, error) - Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error) } type transaction struct { tx *sqlx.Tx id uint16 - lstr []Listener status TxStatus execCtr int queryCtr int + db *database } -func NewTransaction(xtx *sqlx.Tx, txid uint16, lstr []Listener) Tx { +func NewTransaction(xtx *sqlx.Tx, txid uint16, db *database) Tx { return &transaction{ tx: xtx, id: txid, - lstr: lstr, status: TxStatusInitial, execCtr: 0, queryCtr: 0, + db: db, } } func (tx *transaction) Rollback() error { - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { err := v.PreTxRollback(tx.id) if err != nil { return err @@ -58,7 +58,7 @@ func (tx *transaction) Rollback() error { tx.status = TxStatusRollback } - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { v.PostTxRollback(tx.id, result) } @@ -66,7 +66,7 @@ func (tx *transaction) Rollback() error { } func (tx *transaction) Commit() error { - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { err := v.PreTxCommit(tx.id) if err != nil { return err @@ -79,7 +79,7 @@ func (tx *transaction) Commit() error { tx.status = TxStatusComitted } - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { v.PostTxRollback(tx.id, result) } @@ -88,7 +88,7 @@ func (tx *transaction) Commit() error { func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Result, error) { origsql := sqlstr - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { err := v.PreExec(ctx, langext.Ptr(tx.id), &sqlstr, &prep) if err != nil { return nil, err @@ -101,7 +101,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re tx.status = TxStatusActive } - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { v.PostExec(langext.Ptr(tx.id), origsql, sqlstr, prep) } @@ -113,7 +113,7 @@ func (tx *transaction) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Re func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Rows, error) { origsql := sqlstr - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { err := v.PreQuery(ctx, langext.Ptr(tx.id), &sqlstr, &prep) if err != nil { return nil, err @@ -126,7 +126,7 @@ func (tx *transaction) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx tx.status = TxStatusActive } - for _, v := range tx.lstr { + for _, v := range tx.db.lstr { v.PostQuery(langext.Ptr(tx.id), origsql, sqlstr, prep) } @@ -140,6 +140,10 @@ func (tx *transaction) Status() TxStatus { return tx.status } +func (tx *transaction) ListConverter() []DBTypeConverter { + return tx.db.conv +} + func (tx *transaction) Traffic() (int, int) { return tx.execCtr, tx.queryCtr }