Skip to content

Commit

Permalink
change configuration format for functions
Browse files Browse the repository at this point in the history
- unify ignoredFunctions and Functions to one field
- Force specification of mapped named for overloaded functions
  • Loading branch information
ZelvaMan committed Apr 22, 2024
1 parent 8539ad0 commit b967dd7
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 168 deletions.
1 change: 1 addition & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/keenmate/db-gen/common"
dbGen "github.com/keenmate/db-gen/src"
dbGen "github.com/keenmate/db-gen/dbGen"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand Down Expand Up @@ -85,7 +85,7 @@ func doGenerate() error {
}

log.Printf("Preprocessing...")
processedFunctions, err := dbGen.Preprocess(routines, config)
processedFunctions, err := dbGen.Process(routines, config)
if err != nil {
return fmt.Errorf("error preprocessing: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
dbGen "github.com/keenmate/db-gen/src"
dbGen "github.com/keenmate/db-gen/dbGen"
"github.com/spf13/cobra"
"os"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/keenmate/db-gen/common"
dbGen "github.com/keenmate/db-gen/src"
dbGen "github.com/keenmate/db-gen/dbGen"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
_ "embed"
dbGen "github.com/keenmate/db-gen/src"
dbGen "github.com/keenmate/db-gen/dbGen"
"github.com/spf13/cobra"
)

Expand Down
107 changes: 65 additions & 42 deletions database/testing-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,55 @@ create extension if not exists ltree schema "ext";

create table example_table
(
number int primary key,
string text,
json json,
jsonb jsonb,
ltree ext.ltree
number int primary key,
string text,
json json,
jsonb jsonb,
ltree ext.ltree
);

insert into example_table (number, string, json, jsonb, ltree)
values (1, 'Hello world', json_build_object('key', 'value', 'number', 12),
json_build_object('key', 'value', 'number', 12)::jsonb, '1.2.3');
json_build_object('key', 'value', 'number', 12)::jsonb, '1.2.3');

create function sum(a int, b int) returns int
language plpgsql
language plpgsql
as
$$
BEGIN
return a + b;
return a + b;

end;
$$;

create function return_custom_type()
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
as
$$
begin

return query select 1, 'Hello from custom type', json_build_object('key', 'value');
return query select 1, 'Hello from custom type', json_build_object('key', 'value');
end;
$$;

create or replace function return_setof()
returns setof example_table
language plpgsql
returns setof example_table
language plpgsql
as
$$
begin
return query select * from example_table;
return query select * from example_table;
end;
$$;

create function return_void(a int, b int) returns void
language plpgsql
language plpgsql
as
$$
BEGIN
Expand All @@ -68,7 +68,7 @@ $$;


create procedure procedure(a int, b int)
language plpgsql
language plpgsql
as
$$
begin
Expand All @@ -79,19 +79,19 @@ $$;
select return_void(1, 2);

create or replace function new_function(name text) returns text
language plpgsql
language plpgsql
as
$$
begin
return 'Hello ' || name;
return 'Hello ' || name;
end
$$;

select new_function('Honza');
select new_function(null);


create or replace function ignored() returns void
language plpgsql as
language plpgsql as
$$
begin

Expand All @@ -100,34 +100,57 @@ $$;

create schema if not exists test;
create function test.explicitly_included()
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
as
$$
begin

return query select 1, 'Hello from custom type', json_build_object('key', 'value');
return query select 1, 'Hello from custom type', json_build_object('key', 'value');
end;
$$;

create schema if not exists test;
create function test.implicitly_ignored()
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
returns table
(
__number int,
__string text,
__json json
)
language plpgsql
as
$$
begin

return query select 1, 'Hello from custom type', json_build_object('key', 'value');
return query select 1, 'Hello from custom type', json_build_object('key', 'value');
end;
$$;
$$;



create or replace function overloaded_function(name text) returns text
language plpgsql
as
$$
begin
return 'Hello ' || name;
end
$$;

create or replace function overloaded_function(name int) returns text
language plpgsql
as
$$
begin
return 'Hello ' || name;
end
$$;

select overloaded_function('franta');
select overloaded_function(1);
40 changes: 29 additions & 11 deletions src/config.go → dbGen/config.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,32 @@ type Config struct {
}

type SchemaConfig struct {
Schema string `mapstructure:"Schema"`
AllFunctions bool `mapstructure:"AllFunctions"`
Functions []string `mapstructure:"Functions"`
IgnoredFunctions []string `mapstructure:"IgnoredFunctions"`
Schema string `mapstructure:"Schema"`
AllFunctions bool `mapstructure:"AllFunctions"`
Functions map[string]FunctionMapping `mapstructure:"Functions"`
}

type FunctionMapping struct {
Generate bool
MappedName string `mapstructure:"MappedName"`
DontSelectValue bool `mapstructure:"DontSelectValue"`
SelectAllColumns bool `mapstructure:"SelectAllColumns"`
Model map[string]ColumnMapping `mapstructure:"Model"`
Parameters map[string]ParamMapping `mapstructure:"Parameters"`
}

type ColumnMapping struct {
SelectColumn bool
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`
MappingFunction string `mapstructure:"MappingFunction"`
IsNullable bool `mapstructure:"IsNullable"`
}

type ParamMapping struct {
IsNullable bool `mapstructure:"IsNullable"`
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`
}

type Mapping struct {
Expand All @@ -49,8 +71,6 @@ type Mapping struct {
MappingFunction string `mapstructure:"MappingFunction"`
}

var CurrentConfig *Config = nil

// set in ReadConfig
var loadedConfigLocation = ""

Expand All @@ -75,11 +95,12 @@ func GetAndValidateConfig() (*Config, error) {
Generate: nil,
Mappings: nil,
RoutinesFile: "./db-gen-routines.json",
UseRoutinesFile: false,
}

err := viper.Unmarshal(config)
err := getConfigFromViper(config)
if err != nil {
return nil, fmt.Errorf("error mapping configuration: %s", err)
return nil, fmt.Errorf("error processing configuration: %s", err)
}

// no configuration file loaded
Expand All @@ -101,9 +122,6 @@ func GetAndValidateConfig() (*Config, error) {
return nil, fmt.Errorf(" '%s' is not valid case (maybe GeneratedFileCase is missing)", config.GeneratedFileCase)
}

// used by debug helpers
CurrentConfig = config

common.LogDebug("Loaded configuration: \n%+v", config)
return config, nil
}
Expand Down
Loading

0 comments on commit b967dd7

Please sign in to comment.