Skip to content

Commit

Permalink
add --useRoutinesFile
Browse files Browse the repository at this point in the history
    - instead of loading procedures from database, loads procedures from RoutinesFile
  • Loading branch information
ZelvaMan committed Apr 20, 2024
1 parent dbf9dfa commit 753e41f
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 184 deletions.
11 changes: 5 additions & 6 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
keyDebug = "debug"
keyConnectionString = "connectionString"
keyConfig = "config"
keyUseRoutinesFile = "useRoutinesFile"
)

var generateCmd = &cobra.Command{
Expand All @@ -21,6 +22,7 @@ var generateCmd = &cobra.Command{
Long: "Generate code for calling database stored procedures",
Run: func(cmd *cobra.Command, args []string) {
common.BindBoolFlag(cmd, keyDebug)
common.BindBoolFlag(cmd, keyUseRoutinesFile)
common.BindStringFlag(cmd, keyConnectionString)
common.BindStringFlag(cmd, keyConfig)

Expand All @@ -43,6 +45,7 @@ func init() {

// set cli flags
common.DefineBoolFlag(generateCmd, keyDebug, "d", false, "Print debug logs and create debug files")
common.DefineBoolFlag(generateCmd, keyUseRoutinesFile, "", false, "Use routines file to generate code")
common.DefineStringFlag(generateCmd, keyConnectionString, "s", "", "Connection string used to connect to database")
common.DefineStringFlag(generateCmd, keyConfig, "c", "", "Path to configuration file")
}
Expand All @@ -57,14 +60,10 @@ func doGenerate() error {

common.LogDebug("Debug logging is enabled")

log.Printf("Connecting to database...")
conn, err := dbGen.Connect(config.ConnectionString)
if err != nil {
return fmt.Errorf("error connecting to database: %s", err)
}
var routines []dbGen.DbRoutine

log.Printf("Getting routines...")
routines, err := dbGen.GetRoutines(conn, config)
routines, err = dbGen.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
Expand Down
12 changes: 5 additions & 7 deletions cmd/getRoutines.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var getRoutinesCmd = &cobra.Command{
Long: "Get routines from database to generate later",
Run: func(cmd *cobra.Command, args []string) {
common.BindBoolFlag(cmd, keyDebug)
common.BindBoolFlag(cmd, keyUseRoutinesFile)
common.BindStringFlag(cmd, keyConnectionString)
common.BindStringFlag(cmd, keyConfig)

Expand Down Expand Up @@ -42,6 +43,7 @@ func init() {

// set cli flags
common.DefineBoolFlag(getRoutinesCmd, keyDebug, "d", false, "Print debug logs and create debug files")
common.DefineBoolFlag(getRoutinesCmd, keyUseRoutinesFile, "", false, "Use routines file to generate code")
common.DefineStringFlag(getRoutinesCmd, keyConnectionString, "s", "", "Connection string used to connect to database")
common.DefineStringFlag(getRoutinesCmd, keyConfig, "c", "", "Path to configuration file")
}
Expand All @@ -56,14 +58,10 @@ func doGetRoutines() error {

common.LogDebug("Debug logging is enabled")

log.Printf("Connecting to database...")
conn, err := dbGen.Connect(config.ConnectionString)
if err != nil {
return fmt.Errorf("error connecting to database: %s", err)
}

// because we use shared config, we need to set this
config.UseRoutinesFile = false
log.Printf("Getting routines...")
routines, err := dbGen.GetRoutines(conn, config)
routines, err := dbGen.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
Expand Down
32 changes: 32 additions & 0 deletions common/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package common

import (
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
)

type DbConn struct {
conn *sqlx.DB
}

func Connect(connectionString string) (*DbConn, error) {
connection, err := sqlx.Connect("pgx", connectionString)
if err != nil {
return nil, err
}

err = connection.Ping()

if err != nil {
return nil, err
}

conn := &DbConn{conn: connection}

return conn, nil
}

func (conn DbConn) Select(output interface{}, query string, params ...interface{}) error {
err := conn.conn.Select(output, query, params...)
return err
}
16 changes: 16 additions & 0 deletions common/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,19 @@ func SaveAsJson(path string, data interface{}) error {

return nil
}

func LoadFromJson(path string, data interface{}) error {
LogDebug("Loading data from json file %s", path)

fileContent, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading file: %s", err)
}

err = json.Unmarshal(fileContent, data)
if err != nil {
return fmt.Errorf("parsing json: %s", err)
}
return nil

}
1 change: 1 addition & 0 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Debug bool `mapstructure:"Debug"`
ClearOutputFolder bool `mapstructure:"ClearOutputFolder"`
RoutinesFile string `mapstructure:"RoutinesFile"`
UseRoutinesFile bool `mapstructure:"UseRoutinesFile"`
Generate []SchemaConfig `mapstructure:"Generate"`
Mappings []Mapping `mapstructure:"Mappings"`
}
Expand Down
62 changes: 31 additions & 31 deletions src/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,11 @@ package dbGen

import (
"fmt"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/keenmate/db-gen/common"
"log"
"slices"
)

type DbConn struct {
conn *sqlx.DB
}

func Connect(connectionString string) (*DbConn, error) {
connection, err := sqlx.Connect("pgx", connectionString)
if err != nil {
return nil, err
}

err = connection.Ping()

if err != nil {
return nil, err
}

conn := &DbConn{conn: connection}

return conn, nil
}

func (conn DbConn) Select(output interface{}, query string, params ...interface{}) error {
err := conn.conn.Select(output, query, params...)
return err
}

type DbRoutine struct {
RowNumber int `db:"row_number"`
RoutineSchema string `db:"routine_schema"`
Expand Down Expand Up @@ -61,7 +35,32 @@ const (
Procedure = "procedure"
)

func GetRoutines(conn *DbConn, config *Config) ([]DbRoutine, error) {
func GetRoutines(config *Config) ([]DbRoutine, error) {
if config.UseRoutinesFile {
return LoadRoutinesFromFile(config)
}

return getRoutinesFromDatabase(config)
}

func LoadRoutinesFromFile(config *Config) ([]DbRoutine, error) {
routines := new([]DbRoutine)
err := common.LoadFromJson(config.RoutinesFile, routines)
if err != nil {
return nil, fmt.Errorf("loading routines from file: %s", err)
}

return *routines, nil
}

func getRoutinesFromDatabase(config *Config) ([]DbRoutine, error) {
log.Printf("Connecting to database...")
conn, err := common.Connect(config.ConnectionString)
if err != nil {

return nil, fmt.Errorf("error connecting to database: %s", err)
}

schemas := getSchemas(config)

routines := make([]DbRoutine, 0)
Expand All @@ -86,6 +85,7 @@ func GetRoutines(conn *DbConn, config *Config) ([]DbRoutine, error) {
return routines, nil

}

func getSchemas(config *Config) []string {
schemas := make([]string, 0)
for _, schemaConfig := range config.Generate {
Expand All @@ -97,7 +97,7 @@ func getSchemas(config *Config) []string {
return schemas
}

func getFunctionsInSchema(conn *DbConn, schema string) ([]DbRoutine, error) {
func getFunctionsInSchema(conn *common.DbConn, schema string) ([]DbRoutine, error) {
routines := new([]DbRoutine)

// I am coalescing
Expand Down Expand Up @@ -127,7 +127,7 @@ func getFunctionsInSchema(conn *DbConn, schema string) ([]DbRoutine, error) {
return *routines, nil
}

func addParamsToRoutine(conn *DbConn, routine *DbRoutine) error {
func addParamsToRoutine(conn *common.DbConn, routine *DbRoutine) error {
q := `
select ordinal_position::int,
parameter_name::text,
Expand Down
12 changes: 8 additions & 4 deletions src/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,41 @@ func Generate(routines []Routine, config *Config) error {
}
common.LogDebug("Got %d file hashes", len(*fileHashes))

log.Printf("Ensuring output folder...")

err = ensureOutputFolder(config)
if err != nil {
return fmt.Errorf("ensuring output folder: %s", err)
}
log.Printf("Ensured output folder")

log.Printf("Generating dbcontext...")

err = generateDbContext(routines, fileHashes, config)
if err != nil {
return fmt.Errorf("generating dbcontext: %s", err)

}
log.Printf("Generated dbcontext")

if config.GenerateModels {
log.Printf("Generating models...")

err = generateModels(routines, fileHashes, config)
if err != nil {
return fmt.Errorf("generating models: %s", err)

}
log.Printf("Generated models")
} else {
log.Printf("Skipping generating models")
}

if config.GenerateProcessors {
log.Printf("Generating processors...")

err = generateProcessors(routines, fileHashes, config)
if err != nil {
return fmt.Errorf("generating processors: %s", err)

}
log.Printf("Generated processors")
} else {
log.Printf("Skipping generating processors")
}
Expand Down
Loading

0 comments on commit 753e41f

Please sign in to comment.