dev-centralize #1
@ -12,7 +12,7 @@ type Command struct {
|
|||||||
Configure func(c *cobra.Command)
|
Configure func(c *cobra.Command)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Command) Initialize(f func(c *cobra.Command)) *cobra.Command {
|
func (c Command) Initialize(f func(cmd *cobra.Command)) *cobra.Command {
|
||||||
if f != nil {
|
if f != nil {
|
||||||
f(c.Command)
|
f(c.Command)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ type Config struct {
|
|||||||
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
||||||
ShutdownSignals []os.Signal
|
ShutdownSignals []os.Signal
|
||||||
ShutdownTimeout time.Duration
|
ShutdownTimeout time.Duration
|
||||||
SubCommands []Command
|
SubCommands []Commander
|
||||||
SubCommandInitializeFunc func(cmd *cobra.Command)
|
SubCommandInitializeFunc func(cmd *cobra.Command)
|
||||||
ValidArgs []string
|
ValidArgs []string
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
Long: long,
|
Long: long,
|
||||||
PersistentPreRunE: persistentPreRunFuncE,
|
PersistentPreRunE: persistentPreRunFuncE,
|
||||||
PersistentPostRunE: persistentPostRunFuncE,
|
PersistentPostRunE: persistentPostRunFuncE,
|
||||||
RunE: runFuncE,
|
RunE: RunCatchFuncE,
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
@ -69,9 +69,9 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) ParseVersion() (semver.Version, error) {
|
// func (c Config) ParseVersion() (semver.Version, error) {
|
||||||
return semver.Parse(c.Version.Full)
|
// return semver.Parse(c.Version.Full)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
||||||
appCmd.AddCommand(cmd.Initialize(f))
|
appCmd.AddCommand(cmd.Initialize(f))
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
@ -24,7 +25,11 @@ var (
|
|||||||
outWriter io.Writer = os.Stdout
|
outWriter io.Writer = os.Stdout
|
||||||
)
|
)
|
||||||
|
|
||||||
func helpFuncE(cmd *cobra.Command, args []string) error {
|
func SubCommandInitializePrintNameFunc(cmd *cobra.Command) {
|
||||||
|
fmt.Println("Initializing:", cmd.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
func HelpFuncE(cmd *cobra.Command, args []string) error {
|
||||||
return cmd.Help()
|
return cmd.Help()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,10 +56,10 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that we show the app help if no commands or flags are passed
|
// Make sure that we show the app help if no commands or flags are passed
|
||||||
if cmd.CalledAs() == appName && runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name() == runtime.FuncForPC(reflect.ValueOf(runFuncE).Pointer()).Name() {
|
if cmd.CalledAs() == appName && runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name() == runtime.FuncForPC(reflect.ValueOf(RunCatchFuncE).Pointer()).Name() {
|
||||||
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "overriding command", slog.String("old_function", runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name()), slog.String("new_function", runtime.FuncForPC(reflect.ValueOf(helpFuncE).Pointer()).Name()))
|
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "overriding command", slog.String("old_function", runtime.FuncForPC(reflect.ValueOf(cmd.RunE).Pointer()).Name()), slog.String("new_function", runtime.FuncForPC(reflect.ValueOf(HelpFuncE).Pointer()).Name()))
|
||||||
|
|
||||||
cmd.RunE = helpFuncE
|
cmd.RunE = HelpFuncE
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +101,7 @@ func persistentPostRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// appRunE is an empty catch function to allow overrides through persistentPreRunE
|
// RunCatchFuncE is an empty catch function to allow overrides through persistentPreRunE
|
||||||
func runFuncE(cmd *cobra.Command, args []string) error {
|
func RunCatchFuncE(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.flexabyte.io/flexabyte/go-kit/semver"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -48,10 +46,10 @@ func configureVersionFlag(cmd *cobra.Command, v Version) {
|
|||||||
addVersionFlag(cmd)
|
addVersionFlag(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printVersion(v semver.Version) string {
|
func printVersion(v Version) string {
|
||||||
var output string
|
var output string
|
||||||
if !verboseFlag {
|
if !verboseFlag {
|
||||||
output = v.String()
|
output = v.Full
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonOutputFlag {
|
if jsonOutputFlag {
|
||||||
@ -65,27 +63,20 @@ func printVersion(v semver.Version) string {
|
|||||||
}
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Full: %s\nBranch: %s\nTag: %s\nCommit: %s\nCommit date: %s\nBuild date: %s\nMajor: %s\nMinor: %s\nPatch: %s\nPreRelease: %s\n",
|
"Full: %s\nBranch: %s\nTag: %s\nCommit: %s\nCommit date: %s\nBuild date: %s\nMajor: %s\nMinor: %s\nPatch: %s\nPreRelease: %s\n",
|
||||||
version.Full,
|
v.Full,
|
||||||
version.Branch,
|
v.Branch,
|
||||||
version.Tag,
|
v.Tag,
|
||||||
version.Commit,
|
v.Commit,
|
||||||
version.CommitDate,
|
v.CommitDate,
|
||||||
version.BuildDate,
|
v.BuildDate,
|
||||||
version.Major,
|
v.Major,
|
||||||
version.Minor,
|
v.Minor,
|
||||||
version.Patch,
|
v.Patch,
|
||||||
version.PreRelease,
|
v.PreRelease,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
||||||
var v semver.Version
|
_, err := fmt.Fprintln(outWriter, printVersion(version))
|
||||||
var err error
|
return err
|
||||||
if v, err = semver.Parse(version.Full); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if _, err = fmt.Fprintln(outWriter, printVersion(v)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS demo
|
||||||
|
(
|
||||||
|
id INTEGER NOT NULL
|
||||||
|
CONSTRAINT p_id
|
||||||
|
PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL
|
||||||
|
CONSTRAINT u_name
|
||||||
|
UNIQUE
|
||||||
|
ON CONFLICT FAIL
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO demo (name)
|
||||||
|
VALUES ('item1')
|
@ -0,0 +1,3 @@
|
|||||||
|
DELETE
|
||||||
|
FROM demo
|
||||||
|
WHERE id == ?
|
@ -0,0 +1,3 @@
|
|||||||
|
DELETE
|
||||||
|
FROM demo
|
||||||
|
WHERE name == ?
|
@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO demo (name)
|
||||||
|
VALUES (?)
|
||||||
|
RETURNING id
|
@ -0,0 +1,2 @@
|
|||||||
|
SELECT *
|
||||||
|
FROM demo
|
@ -0,0 +1,2 @@
|
|||||||
|
SELECT *
|
||||||
|
FROM test
|
27
examples/sqr/embeddedSqlWithRepository/go.mod
Normal file
27
examples/sqr/embeddedSqlWithRepository/go.mod
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
module embeddedSql
|
||||||
|
|
||||||
|
go 1.24.2
|
||||||
|
|
||||||
|
toolchain go1.24.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.2
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect
|
||||||
|
golang.org/x/sys v0.30.0 // indirect
|
||||||
|
modernc.org/libc v1.61.13 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.8.2 // indirect
|
||||||
|
modernc.org/sqlite v1.35.0 // indirect
|
||||||
|
)
|
68
examples/sqr/embeddedSqlWithRepository/go.sum
Normal file
68
examples/sqr/embeddedSqlWithRepository/go.sum
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985 h1:Q9mYadYgRyb0i89T2fvzJlViGMiKosUvBrBpK8pd/6w=
|
||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985/go.mod h1:dZ04ub68ACYiSHUrF+7mq4xg4xL/0C3MvnIUEfahdiU=
|
||||||
|
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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.2 h1:2VSCMz7x7mjyTXx3m2zPokOY82LTRgxK1yQYKo6wWQ8=
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.2/go.mod h1:2CM6tJvn2kqPXwnXO/d3rAQYiyoIm180VsO8PRX6Rpk=
|
||||||
|
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
|
||||||
|
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
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/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||||
|
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
|
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
|
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac h1:l5+whBCLH3iH2ZNHYLbAe58bo7yrN4mVcnkHDYz5vvs=
|
||||||
|
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac/go.mod h1:hH+7mtFmImwwcMvScyxUhjuVHR3HGaDPMn9rMSUUbxo=
|
||||||
|
golang.org/x/mod v0.23.0 h1:Zb7khfcRGKk+kqfxFaP5tZqCnDZMjC5VtUBs87Hr6QM=
|
||||||
|
golang.org/x/mod v0.23.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
||||||
|
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
|
||||||
|
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||||
|
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
|
||||||
|
golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
modernc.org/cc/v4 v4.24.4 h1:TFkx1s6dCkQpd6dKurBNmpo+G8Zl4Sq/ztJ+2+DEsh0=
|
||||||
|
modernc.org/cc/v4 v4.24.4/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||||
|
modernc.org/ccgo/v4 v4.23.16 h1:Z2N+kk38b7SfySC1ZkpGLN2vthNJP1+ZzGZIlH7uBxo=
|
||||||
|
modernc.org/ccgo/v4 v4.23.16/go.mod h1:nNma8goMTY7aQZQNTyN9AIoJfxav4nvTnvKThAeMDdo=
|
||||||
|
modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
|
||||||
|
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
|
||||||
|
modernc.org/gc/v2 v2.6.3 h1:aJVhcqAte49LF+mGveZ5KPlsp4tdGdAOT4sipJXADjw=
|
||||||
|
modernc.org/gc/v2 v2.6.3/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
|
modernc.org/libc v1.61.13 h1:3LRd6ZO1ezsFiX1y+bHd1ipyEHIJKvuprv0sLTBwLW8=
|
||||||
|
modernc.org/libc v1.61.13/go.mod h1:8F/uJWL/3nNil0Lgt1Dpz+GgkApWh04N3el3hxJcA6E=
|
||||||
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
|
modernc.org/memory v1.8.2 h1:cL9L4bcoAObu4NkxOlKWBWtNHIsnnACGF/TbqQ6sbcI=
|
||||||
|
modernc.org/memory v1.8.2/go.mod h1:ZbjSvMO5NQ1A2i3bWeDiVMxIorXwdClKE/0SZ+BMotU=
|
||||||
|
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||||
|
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
|
modernc.org/sqlite v1.35.0 h1:yQps4fegMnZFdphtzlfQTCNBWtS0CZv48pRpW3RFHRw=
|
||||||
|
modernc.org/sqlite v1.35.0/go.mod h1:9cr2sicr7jIaWTBKQmAxQLfBv9LL0su4ZTEV+utt3ic=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
124
examples/sqr/embeddedSqlWithRepository/main.go
Normal file
124
examples/sqr/embeddedSqlWithRepository/main.go
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/sqr"
|
||||||
|
"github.com/golang-migrate/migrate/v4"
|
||||||
|
"github.com/golang-migrate/migrate/v4/database"
|
||||||
|
"github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||||
|
"github.com/golang-migrate/migrate/v4/source"
|
||||||
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed assets/migrations/*
|
||||||
|
var migrationFs embed.FS
|
||||||
|
|
||||||
|
//go:embed assets/statements/*
|
||||||
|
var statementsFS embed.FS
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
var r *sqr.Repository
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
fmt.Println("Create in-memory database and run migrations:")
|
||||||
|
// Create in-memory demo database
|
||||||
|
if err = initialize(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create query repository from embedded files
|
||||||
|
if r, err = sqr.NewFromFs(statementsFS, "assets/statements"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
fmt.Println("Fetch statements from repository:")
|
||||||
|
var query string
|
||||||
|
if query, err = r.Get("demo", "list"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Query:", query)
|
||||||
|
|
||||||
|
if query, err = r.Get("demo", "insert"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Query:", query)
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
fmt.Println("Run insert statement:")
|
||||||
|
var stmtInsert *sql.Stmt
|
||||||
|
if stmtInsert, err = sqr.Prepare(db, r, "demo", "insert"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var res int
|
||||||
|
if err = stmtInsert.QueryRow("item2").Scan(&res); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully inserted into database, returned id:", res)
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("Run list statement:")
|
||||||
|
var stmtQuery *sql.Stmt
|
||||||
|
if stmtQuery, err = sqr.Prepare(db, r, "demo", "list"); err != nil {
|
||||||
|
fmt.Println("Error preparing statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
if stmtQuery == nil {
|
||||||
|
panic(errors.New("statement not found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows *sql.Rows
|
||||||
|
if rows, err = stmtQuery.Query(); err != nil {
|
||||||
|
fmt.Println("Error querying statement")
|
||||||
|
}
|
||||||
|
if rows != nil {
|
||||||
|
for rows.Next() {
|
||||||
|
var id int
|
||||||
|
var name string
|
||||||
|
if err = rows.Scan(&id, &name); err != nil {
|
||||||
|
fmt.Println("Error scanning row")
|
||||||
|
}
|
||||||
|
fmt.Println("Output", id, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialize() error {
|
||||||
|
var err error
|
||||||
|
db, err = sql.Open("sqlite", ":memory:")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error opening database")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var src source.Driver
|
||||||
|
if src, err = iofs.New(migrationFs, "assets/migrations"); err != nil {
|
||||||
|
fmt.Println("Error opening migrations source")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var driver database.Driver
|
||||||
|
if driver, err = sqlite.WithInstance(db, &sqlite.Config{}); err != nil {
|
||||||
|
fmt.Println("Error opening migrations destination")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var m *migrate.Migrate
|
||||||
|
if m, err = migrate.NewWithInstance("fs", src, "sqlite", driver); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS demo
|
||||||
|
(
|
||||||
|
id INTEGER NOT NULL
|
||||||
|
CONSTRAINT p_id
|
||||||
|
PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL
|
||||||
|
CONSTRAINT u_name
|
||||||
|
UNIQUE
|
||||||
|
ON CONFLICT FAIL
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO demo (name)
|
||||||
|
VALUES ('item1')
|
@ -0,0 +1,3 @@
|
|||||||
|
DELETE
|
||||||
|
FROM demo
|
||||||
|
WHERE id == ?
|
@ -0,0 +1,3 @@
|
|||||||
|
DELETE
|
||||||
|
FROM demo
|
||||||
|
WHERE name == ?
|
@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO demo (name)
|
||||||
|
VALUES (?)
|
||||||
|
RETURNING id
|
@ -0,0 +1,2 @@
|
|||||||
|
SELECT *
|
||||||
|
FROM demo
|
@ -0,0 +1,2 @@
|
|||||||
|
SELECT *
|
||||||
|
FROM test
|
27
examples/sqr/embeddedSqlWithoutRepository/go.mod
Normal file
27
examples/sqr/embeddedSqlWithoutRepository/go.mod
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
module embeddedSql
|
||||||
|
|
||||||
|
go 1.24.2
|
||||||
|
|
||||||
|
toolchain go1.24.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.3
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
|
||||||
|
golang.org/x/sys v0.33.0 // indirect
|
||||||
|
modernc.org/libc v1.65.7 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.11.0 // indirect
|
||||||
|
modernc.org/sqlite v1.37.1 // indirect
|
||||||
|
)
|
68
examples/sqr/embeddedSqlWithoutRepository/go.sum
Normal file
68
examples/sqr/embeddedSqlWithoutRepository/go.sum
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985 h1:Q9mYadYgRyb0i89T2fvzJlViGMiKosUvBrBpK8pd/6w=
|
||||||
|
git.flexabyte.io/flexabyte/go-kit v0.0.0-20250521190905-eff54c777985/go.mod h1:dZ04ub68ACYiSHUrF+7mq4xg4xL/0C3MvnIUEfahdiU=
|
||||||
|
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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
|
||||||
|
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||||
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||||
|
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
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/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||||
|
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
|
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/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
|
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 h1:y5zboxd6LQAqYIhHnB48p0ByQ/GnQx2BE33L8BOHQkI=
|
||||||
|
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
|
||||||
|
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
|
||||||
|
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||||
|
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||||
|
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||||
|
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
|
||||||
|
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
modernc.org/cc/v4 v4.26.1 h1:+X5NtzVBn0KgsBCBe+xkDC7twLb/jNVj9FPgiwSQO3s=
|
||||||
|
modernc.org/cc/v4 v4.26.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||||
|
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
|
||||||
|
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
|
||||||
|
modernc.org/fileutil v1.3.1 h1:8vq5fe7jdtEvoCf3Zf9Nm0Q05sH6kGx0Op2CPx1wTC8=
|
||||||
|
modernc.org/fileutil v1.3.1/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||||
|
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||||
|
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
|
modernc.org/libc v1.65.7 h1:Ia9Z4yzZtWNtUIuiPuQ7Qf7kxYrxP1/jeHZzG8bFu00=
|
||||||
|
modernc.org/libc v1.65.7/go.mod h1:011EQibzzio/VX3ygj1qGFt5kMjP0lHb0qCW5/D/pQU=
|
||||||
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
|
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||||
|
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||||
|
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||||
|
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
|
modernc.org/sqlite v1.37.1 h1:EgHJK/FPoqC+q2YBXg7fUmES37pCHFc97sI7zSayBEs=
|
||||||
|
modernc.org/sqlite v1.37.1/go.mod h1:XwdRtsE1MpiBcL54+MbKcaDvcuej+IYSMfLN6gSKV8g=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
116
examples/sqr/embeddedSqlWithoutRepository/main.go
Normal file
116
examples/sqr/embeddedSqlWithoutRepository/main.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/sqr"
|
||||||
|
"github.com/golang-migrate/migrate/v4"
|
||||||
|
"github.com/golang-migrate/migrate/v4/database"
|
||||||
|
"github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||||
|
"github.com/golang-migrate/migrate/v4/source"
|
||||||
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed assets/migrations/*
|
||||||
|
var migrationFs embed.FS
|
||||||
|
|
||||||
|
//go:embed assets/statements/*
|
||||||
|
var statementsFS embed.FS
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
fmt.Println("Create in-memory database and run migrations:")
|
||||||
|
// Create in-memory demo database
|
||||||
|
if err = initialize(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Fetch demo list query from filesystem:")
|
||||||
|
var query string
|
||||||
|
if query, err = sqr.LoadQueryFromFs(statementsFS, "assets/statements", "demo", "list"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Query:", query)
|
||||||
|
|
||||||
|
if query, err = sqr.LoadQueryFromFs(statementsFS, "assets/statements", "demo", "insert"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Query:", query)
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
fmt.Println("Run insert statement:")
|
||||||
|
var stmtInsert *sql.Stmt
|
||||||
|
if stmtInsert, err = sqr.PrepareFromFs(db, statementsFS, "assets/statements", "demo", "insert"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var res int
|
||||||
|
if err = stmtInsert.QueryRow("item2").Scan(&res); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully inserted into database, returned id:", res)
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
fmt.Println("Run list statement:")
|
||||||
|
var stmtQuery *sql.Stmt
|
||||||
|
if stmtQuery, err = sqr.PrepareFromFs(db, statementsFS, "assets/statements", "demo", "list"); err != nil {
|
||||||
|
fmt.Println("Error preparing statement")
|
||||||
|
}
|
||||||
|
|
||||||
|
if stmtQuery == nil {
|
||||||
|
panic(errors.New("statement not found"))
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows *sql.Rows
|
||||||
|
if rows, err = stmtQuery.Query(); err != nil {
|
||||||
|
fmt.Println("Error querying statement")
|
||||||
|
}
|
||||||
|
if rows != nil {
|
||||||
|
for rows.Next() {
|
||||||
|
var id int
|
||||||
|
var name string
|
||||||
|
if err = rows.Scan(&id, &name); err != nil {
|
||||||
|
fmt.Println("Error scanning row")
|
||||||
|
}
|
||||||
|
fmt.Println("Output", id, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialize() error {
|
||||||
|
var err error
|
||||||
|
db, err = sql.Open("sqlite", ":memory:")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error opening database")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var src source.Driver
|
||||||
|
if src, err = iofs.New(migrationFs, "assets/migrations"); err != nil {
|
||||||
|
fmt.Println("Error opening migrations source")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var driver database.Driver
|
||||||
|
if driver, err = sqlite.WithInstance(db, &sqlite.Config{}); err != nil {
|
||||||
|
fmt.Println("Error opening migrations destination")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var m *migrate.Migrate
|
||||||
|
if m, err = migrate.NewWithInstance("fs", src, "sqlite", driver); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
2
go.mod
2
go.mod
@ -12,5 +12,5 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/samber/lo v1.50.0 // indirect
|
github.com/samber/lo v1.50.0 // indirect
|
||||||
golang.org/x/text v0.24.0 // indirect
|
golang.org/x/text v0.26.0 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -20,6 +20,10 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
|
|||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||||
|
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||||
|
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||||
|
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||||
|
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
135
sqr/README.md
Normal file
135
sqr/README.md
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
# go-sql-sqr - SQL Query Repository for Go
|
||||||
|
|
||||||
|
This library enables the use of centralized storage for all SQL queries used in an application.
|
||||||
|
You can either choose to load all queries into a repository, or load them from a filesystem as necessary.
|
||||||
|
|
||||||
|
The main intention is to embed a directory structure into the binary as ```embed.FS```, which can then used in the
|
||||||
|
application.
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/github.com/jantytgat/go-sql-sqr)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Basics
|
||||||
|
|
||||||
|
### Add the package to your project
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go get git.flexabyte.io/flexabyte/go-kit
|
||||||
|
```
|
||||||
|
|
||||||
|
### Import
|
||||||
|
Next, you can manually add the import statement to your ```.go```-file, or have it added automatically when using it.
|
||||||
|
|
||||||
|
```text
|
||||||
|
import git.flexabyte.io/flexabyte/go-kit/sqr
|
||||||
|
```
|
||||||
|
|
||||||
|
### Embed assets containing queries.
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> The root folder embedded in the application cannot have nested directories.
|
||||||
|
> This means that the directory structure is limited to 1 level of collections, each containing a set of text files with
|
||||||
|
> a ```.sql``` extension.
|
||||||
|
>
|
||||||
|
> Files with another extension will fail to load!
|
||||||
|
|
||||||
|
Let's assume to following directory structure in an embedded filesystem:
|
||||||
|
|
||||||
|
```
|
||||||
|
/assets
|
||||||
|
|-- /queries
|
||||||
|
|-- collection1
|
||||||
|
| |-- create.sql
|
||||||
|
| |-- read.sql
|
||||||
|
| |-- update.sql
|
||||||
|
| |-- delete.sql
|
||||||
|
|-- collection2
|
||||||
|
|-- list.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
You can now embed the statement files as follows:
|
||||||
|
|
||||||
|
```go
|
||||||
|
//go:embed assets/queries/*
|
||||||
|
var f embed.FS
|
||||||
|
```
|
||||||
|
|
||||||
|
### With repository
|
||||||
|
|
||||||
|
#### Create a new repository
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Create query repository from embedded files
|
||||||
|
var r *sqr.Repository
|
||||||
|
if r, err = sqr.NewFromFs(f, "assets/queries"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Load a query from the repository
|
||||||
|
|
||||||
|
Now the repository has been initialized, we can get a query from a collection:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var query string
|
||||||
|
if query, err = r.Get("collection1", "create"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println("Query:", query)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Without repository
|
||||||
|
|
||||||
|
If you don't want to initialize a repository, but rather choose to load SQL queries straight from the filesystem, you
|
||||||
|
can do so as follows:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var query string
|
||||||
|
if query, err = sqr.LoadQueryFromFs(f, "assets/queries", "collection2", "list"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Prepared statements
|
||||||
|
|
||||||
|
We also provide the means to create prepared statements for the queries, either with or without using a repository, as
|
||||||
|
long as a ```Preparer``` is passed into the functions.
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Preparer interface {
|
||||||
|
Prepare(query string) (*sql.Stmt, error)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
For example:
|
||||||
|
|
||||||
|
- *sql.Db
|
||||||
|
- *sql.Tx
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Prepare[T Preparer](t T, r *Repository, collectionName, queryName string) (*sql.Stmt, error) {}
|
||||||
|
func PrepareFromFs[T Preparer](t T, f fs.FS, rootPath, collectionName, queryName string) (*sql.Stmt, error) {}
|
||||||
|
```
|
||||||
|
|
||||||
|
### With repository
|
||||||
|
|
||||||
|
```go
|
||||||
|
var createStmt *sql.Stmt
|
||||||
|
if createStmt, err = sqr.Prepare(db, r, "collection1", "create"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Without repository
|
||||||
|
|
||||||
|
```go
|
||||||
|
var createStmt *sql.Stmt
|
||||||
|
if createStmt, err = sqr.PrepareFromFs(db, f, "assets/queries", "collection1", "create"); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
Two examples are available:
|
||||||
|
- embbededSqlWithRepository
|
||||||
|
- embeddedSqlWithoutRepository
|
36
sqr/collection.go
Normal file
36
sqr/collection.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package sqr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// newCollection creates a new collection with the supplied name and returns it to the caller.
|
||||||
|
func newCollection(name string) collection {
|
||||||
|
return collection{
|
||||||
|
name: name,
|
||||||
|
queries: make(map[string]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type collection struct {
|
||||||
|
name string
|
||||||
|
queries map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// add adds a query to the collection.
|
||||||
|
func (c *collection) add(name, query string) error {
|
||||||
|
if _, ok := c.queries[name]; ok {
|
||||||
|
return fmt.Errorf("query %s already exists", name)
|
||||||
|
}
|
||||||
|
c.queries[name] = query
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// get retrieves a query from the collection by name.
|
||||||
|
// If the query name cannot be found, get() returns an empty string and an error.
|
||||||
|
func (c *collection) get(name string) (string, error) {
|
||||||
|
if _, ok := c.queries[name]; !ok {
|
||||||
|
return "", fmt.Errorf("query %s not found in collection %s", name, c.name)
|
||||||
|
}
|
||||||
|
return c.queries[name], nil
|
||||||
|
}
|
162
sqr/collection_test.go
Normal file
162
sqr/collection_test.go
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
package sqr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_collection_add(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
name string
|
||||||
|
queries map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
name string
|
||||||
|
query string
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "good",
|
||||||
|
fields: fields{
|
||||||
|
name: "test1",
|
||||||
|
queries: map[string]string{
|
||||||
|
"query1": "queryString1",
|
||||||
|
"query2": "queryString2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
name: "query3",
|
||||||
|
query: "queryString3",
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad",
|
||||||
|
fields: fields{
|
||||||
|
name: "test1",
|
||||||
|
queries: map[string]string{
|
||||||
|
"query1": "queryString1",
|
||||||
|
"query2": "queryString2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
name: "query2",
|
||||||
|
query: "queryString2",
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := &collection{
|
||||||
|
name: tt.fields.name,
|
||||||
|
queries: tt.fields.queries,
|
||||||
|
}
|
||||||
|
if err := c.add(tt.args.name, tt.args.query); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("add() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_collection_get(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
name string
|
||||||
|
queries map[string]string
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "good",
|
||||||
|
fields: fields{
|
||||||
|
name: "test1",
|
||||||
|
queries: map[string]string{
|
||||||
|
"query1": "queryString1",
|
||||||
|
"query2": "queryString2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
name: "query1",
|
||||||
|
},
|
||||||
|
want: "queryString1",
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad",
|
||||||
|
fields: fields{
|
||||||
|
name: "test1",
|
||||||
|
queries: map[string]string{
|
||||||
|
"query1": "queryString1",
|
||||||
|
"query2": "queryString2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
name: "query3",
|
||||||
|
},
|
||||||
|
want: "",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := &collection{
|
||||||
|
name: tt.fields.name,
|
||||||
|
queries: tt.fields.queries,
|
||||||
|
}
|
||||||
|
got, err := c.get(tt.args.name)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("get() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("get() got = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_newCollection(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want collection
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test1",
|
||||||
|
args: args{
|
||||||
|
name: "test1",
|
||||||
|
},
|
||||||
|
want: collection{
|
||||||
|
name: "test1",
|
||||||
|
queries: make(map[string]string),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := newCollection(tt.args.name); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("newCollection() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
30
sqr/loader.go
Normal file
30
sqr/loader.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package sqr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LoadQueryFromFs retrieves a query from a filesystem.
|
||||||
|
// It needs the root path to start the search from, as well as a collection name and a query name.
|
||||||
|
// The collection name equals to a direct directory name in the root path.
|
||||||
|
// The query name is the file name (without extension) to load the contents from.
|
||||||
|
// It returns and empty string and an error if the file cannot be found.
|
||||||
|
func LoadQueryFromFs(f fs.FS, rootPath, collectionName, queryName string) (string, error) {
|
||||||
|
var err error
|
||||||
|
var contents []byte
|
||||||
|
switch f.(type) {
|
||||||
|
case embed.FS:
|
||||||
|
if contents, err = fs.ReadFile(f, path.Join(rootPath, collectionName, queryName)+".sql"); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to read file %s: %w", path.Join(rootPath, collectionName, queryName)+".sql", err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if contents, err = fs.ReadFile(f, filepath.Join(rootPath, collectionName, queryName)+".sql"); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to read file %s: %w", filepath.Join(rootPath, collectionName, queryName)+".sql", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(contents), nil
|
||||||
|
}
|
76
sqr/preparer.go
Normal file
76
sqr/preparer.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package sqr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Preparer defines the interface to create a prepared statement.
|
||||||
|
type Preparer interface {
|
||||||
|
Prepare(query string) (*sql.Stmt, error)
|
||||||
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare creates a prepared statement for the supplied Preparer by looking up a query in the supplied repository.
|
||||||
|
// It returns an nil pointer and an error if either the query cannot be found in the supplied repository, or the statement preparation fails.
|
||||||
|
func Prepare[T Preparer](t T, r *Repository, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if r == nil {
|
||||||
|
return nil, errors.New("repository is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.Prepare(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare creates a prepared statement for the supplied Preparer by looking up a query in the supplied repository using a context.
|
||||||
|
// It returns an nil pointer and an error if either the query cannot be found in the supplied repository, or the statement preparation fails.
|
||||||
|
func PrepareContext[T Preparer](ctx context.Context, t T, r *Repository, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if r == nil {
|
||||||
|
return nil, errors.New("repository is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.PrepareContext(ctx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrepareFromFs creates a prepared statement for the supplied Preparer by looking up a query in the supplied filesystem.
|
||||||
|
// It returns an nil pointer and an error if either the query cannot be found in the supplied filesystem, or the statement preparation fails.
|
||||||
|
func PrepareFromFs[T Preparer](t T, f fs.FS, rootPath, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if f == nil {
|
||||||
|
return nil, errors.New("invalid filesystem")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = LoadQueryFromFs(f, rootPath, collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.Prepare(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrepareFromFs creates a prepared statement for the supplied Preparer by looking up a query in the supplied filesystem using a context.
|
||||||
|
// It returns an nil pointer and an error if either the query cannot be found in the supplied filesystem, or the statement preparation fails.
|
||||||
|
func PrepareFromFsContext[T Preparer](ctx context.Context, t T, f fs.FS, rootPath, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if f == nil {
|
||||||
|
return nil, errors.New("invalid filesystem")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = LoadQueryFromFs(f, rootPath, collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.PrepareContext(ctx, query)
|
||||||
|
}
|
191
sqr/repository.go
Normal file
191
sqr/repository.go
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
// Package queryrepo enables the use of centralized storage for all SQL queries used in an application.
|
||||||
|
package sqr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewFromFs creates a new repository using a filesystem.
|
||||||
|
// It takes a filesystem and a root path to start loading files from and returns an error if files cannot be loaded.
|
||||||
|
func NewFromFs(f fs.FS, rootPath string) (*Repository, error) {
|
||||||
|
repo := &Repository{
|
||||||
|
queries: make(map[string]collection),
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, loadFromFs(repo, f, rootPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Repository stores multiple collections of queries in a map for later use.
|
||||||
|
// Queries can either be retrieved by their name, or be used to create a prepared statement.
|
||||||
|
type Repository struct {
|
||||||
|
queries map[string]collection
|
||||||
|
mux sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// add adds the supplied collection to the repository.
|
||||||
|
// It returns an error if the collection already exists.
|
||||||
|
func (r *Repository) add(c collection) error {
|
||||||
|
r.mux.Lock()
|
||||||
|
defer r.mux.Unlock()
|
||||||
|
|
||||||
|
if _, ok := r.queries[c.name]; ok {
|
||||||
|
return fmt.Errorf("collection %s already exists", c.name)
|
||||||
|
}
|
||||||
|
r.queries[c.name] = c
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DbPrepare creates a prepared statement for the supplied database handle.
|
||||||
|
// It takes a collection name and query name to look up the query to create the prepared statement.
|
||||||
|
func (r *Repository) DbPrepare(db *sql.DB, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if db == nil {
|
||||||
|
return nil, errors.New("db is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return db.Prepare(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DbPrepareContext creates a prepared statement for the supplied database handle using a context.
|
||||||
|
// It takes a collection name and query name to look up the query to create the prepared statement.
|
||||||
|
func (r *Repository) DbPrepareContext(ctx context.Context, db *sql.DB, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if db == nil {
|
||||||
|
return nil, errors.New("db is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var query string
|
||||||
|
|
||||||
|
if query, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db.PrepareContext(ctx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves the supplied query from the repository.
|
||||||
|
// It takes a collection name and a query name to perform the lookup and returns an empty string and an error if the query cannot be found
|
||||||
|
// in the collection.
|
||||||
|
func (r *Repository) Get(collectionName, queryName string) (string, error) {
|
||||||
|
r.mux.Lock()
|
||||||
|
defer r.mux.Unlock()
|
||||||
|
|
||||||
|
if s, ok := r.queries[collectionName]; ok {
|
||||||
|
return s.get(queryName)
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("collection %s not found", collectionName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxPrepare creates a prepared statement for the supplied in-progress database transaction.
|
||||||
|
// It takes a collection name and query name to look up the query to create the prepared statement.
|
||||||
|
func (r *Repository) TxPrepare(tx *sql.Tx, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if tx == nil {
|
||||||
|
return nil, errors.New("tx is nil")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
var statement string
|
||||||
|
|
||||||
|
if statement, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Prepare(statement)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxPrepare creates a prepared statement for the supplied in-progress database transaction using a context.
|
||||||
|
// It takes a collection name and query name to look up the query to create the prepared statement.
|
||||||
|
func (r *Repository) TxPrepareContext(ctx context.Context, tx *sql.Tx, collectionName, queryName string) (*sql.Stmt, error) {
|
||||||
|
if tx == nil {
|
||||||
|
return nil, errors.New("tx is nil")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
var statement string
|
||||||
|
|
||||||
|
if statement, err = r.Get(collectionName, queryName); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.PrepareContext(ctx, statement)
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadFromFs looks for directories in the root path to create collections for.
|
||||||
|
// If a directory is found, it loads all the files in the subdirectory and adds the returned collection to the repository.
|
||||||
|
func loadFromFs(r *Repository, f fs.FS, rootPath string) error {
|
||||||
|
if r == nil {
|
||||||
|
return errors.New("repository is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if f == nil {
|
||||||
|
return errors.New("filesystem is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var files []fs.DirEntry
|
||||||
|
if files, err = fs.ReadDir(f, rootPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if file.IsDir() {
|
||||||
|
var c collection
|
||||||
|
if c, err = loadFilesFromDir(f, rootPath, file.Name()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = r.add(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadFilesFromDir loads all the files in the directory and returns a collection of queries.
|
||||||
|
func loadFilesFromDir(f fs.FS, rootPath, dirName string) (collection, error) {
|
||||||
|
var err error
|
||||||
|
var c = newCollection(dirName)
|
||||||
|
var fullPath string
|
||||||
|
|
||||||
|
switch f.(type) {
|
||||||
|
case embed.FS:
|
||||||
|
fullPath = path.Join(rootPath, dirName)
|
||||||
|
default:
|
||||||
|
fullPath = filepath.Join(rootPath, dirName)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var files []fs.DirEntry
|
||||||
|
if files, err = fs.ReadDir(f, fullPath); err != nil {
|
||||||
|
return c, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if file.IsDir() {
|
||||||
|
return c, fmt.Errorf("nested directories are not supported, %s is a directory in %s", file.Name(), fullPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
var contents string
|
||||||
|
if contents, err = LoadQueryFromFs(f, rootPath, dirName, strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))); err != nil {
|
||||||
|
return c, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = c.add(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())), contents); err != nil {
|
||||||
|
return c, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
Reference in New Issue
Block a user