cg

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: ISC Imports: 14 Imported by: 0

Documentation

Overview

Package cg is the language-independent core of the generator: a port of com.sqldalmaker.cg of the Java plug-in.

The port is deliberately close to the original (the same names, the same branches, the same error messages): the output is compared with the plug-in byte for byte, and a divergence is easier to find when the code can be put next to the Java source.

Index

Constants

View Source
const GolangScopesErr = "Error in 'settings.xml': \n" +
	"empty values for '<dto scope' and '<dao scope' are not allowed"

Variables

This section is empty.

Functions

func CamelCaseToLowerSnakeCase

func CamelCaseToLowerSnakeCase(src string) string

func ClassNameWithoutModel

func ClassNameWithoutModel(dtoClassName string) string

func FileNameSnakeCase

func FileNameSnakeCase(className, ext string) string

FileNameSnakeCase is the file name of a class: "TasksDao" -> "tasks_dao.go".

func FindDto

func FindDto(name string, dtos []sdmxml.DtoClass) (*sdmxml.DtoClass, error)

FindDto is for the generators.

func FormatJdbcSQLForCpp

func FormatJdbcSQLForCpp(sql string) string

FormatJdbcSQLForCpp is the SQL as a C++ string literal with its quotes (SqlUtils.jdbc_sql_to_cpp_str): each line escaped by escapeSQLLine, lines joined by ` \n\` and a line break. No lines: only `"`, as in Java.

func FormatJdbcSQLForGo

func FormatJdbcSQLForGo(sql string) string

FormatJdbcSQLForGo puts SQL in backquotes, lines joined by " \n\t\t".

func FormatJdbcSQLForPython

func FormatJdbcSQLForPython(sql string) string

FormatJdbcSQLForPython is the SQL inside """...""" (SqlUtils.jdbc_sql_to_python_string): each line is escaped by escapeSQLLine, lines joined by " \n" and 16 spaces.

func IsEmptyRef

func IsEmptyRef(ref string) bool

func IsJdbcStoredProcCall

func IsJdbcStoredProcCall(sql string) bool

func IsSQLFileRef

func IsSQLFileRef(ref string) bool

func IsSQLShortcutRef

func IsSQLShortcutRef(ref string) bool

func IsTableRef

func IsTableRef(ref string) bool

func JdbcSQLByDtoClassRef

func JdbcSQLByDtoClassRef(ref, sqlRoot string) (string, error)

func JdbcSQLByExecDmlRef

func JdbcSQLByExecDmlRef(ref, sqlRoot string) (string, error)

func JdbcSQLByRef

func JdbcSQLByRef(ref, sqlRoot string) (string, error)

func ListedItems

func ListedItems(list string, allowColon bool) ([]string, error)

ListedItems: "a, b, [x:X, y:Y]" -> {"a", "b", "[x:X, y:Y]"} with a check of names.

func LowerCamel

func LowerCamel(s string) string

func MethodName

func MethodName(name string, mode FieldNamesMode) string

func Model

func Model(dtoClassName string) string

Model: "sa-Task" -> "sa"; no model without '-'.

func PackageName

func PackageName(scope string) (string, error)

PackageName is the last segment of scope (GoCG._get_package_name).

func ParseMethodDeclaration

func ParseMethodDeclaration(text string) (name, params string, err error)

ParseMethodDeclaration — "get_something(p1, p2)" -> "get_something", "p1, p2".

func SplitMappedParam

func SplitMappedParam(pd string) []string

SplitMappedParam: "on_dto:Dto" or "on_dto~Dto" -> {"on_dto", "Dto"}; nil for a plain parameter.

func SplitTargetType

func SplitTargetType(target string) (typ, assign string, ok bool)

SplitTargetType splits "type->assign_func" of type-map. ok == false if there is no function.

func Title

func Title(s string) string

func ToKebabCase

func ToKebabCase(src string) string

Types

type Core

type Core struct {
	// contains filtered or unexported fields
}

Core is what the Java plug-in keeps in JdbcUtils for one generation run: metadata, type-map, macros and the naming mode of DTO fields.

func NewCore

func NewCore(md meta.Metadata, s *sdmxml.Settings, dtoMode, paramMode FieldNamesMode, sqlRoot string) *Core

func (*Core) CrudCreateInfo

func (c *Core) CrudCreateInfo(table string, dto *sdmxml.DtoClass, generated string) (*DaoSqlInfo, error)

func (*Core) CrudDeleteInfo

func (c *Core) CrudDeleteInfo(table string, dto *sdmxml.DtoClass, explicitPK string) (*DaoSqlInfo, error)

func (*Core) CrudReadInfo

func (c *Core) CrudReadInfo(table string, dto *sdmxml.DtoClass, fetchList bool, explicitPK string) (*DaoSqlInfo, error)

func (*Core) CrudUpdateInfo

func (c *Core) CrudUpdateInfo(table string, dto *sdmxml.DtoClass, explicitPK string) (*DaoSqlInfo, error)

func (*Core) DtoFieldNamesMode

func (c *Core) DtoFieldNamesMode() FieldNamesMode

func (*Core) DtoFields

func (c *Core) DtoFields(dto *sdmxml.DtoClass) ([]FieldInfo, error)

DtoFields returns the fields of a DTO class.

func (*Core) ExecDmlParams

func (c *Core) ExecDmlParams(jdbcSQL string, descriptors []string) ([]ParamInfo, error)

func (*Core) QueryInfo

func (c *Core) QueryInfo(ref string, descriptors []string, dtoOrReturnType string, returnTypeIsDto bool, dtos []sdmxml.DtoClass) (*DaoSqlInfo, error)

QueryInfo — <query...>, <query-list...>, <query-dto...>, <query-dto-list...>.

func (*Core) SQLRoot

func (c *Core) SQLRoot() string

func (*Core) TargetTypeByTypeMap

func (c *Core) TargetTypeByTypeMap(detected string) string

TargetTypeByTypeMap returns the type without the assign function.

type DaoMethod

type DaoMethod struct {
	Kind         MethodKind
	ErrorContext string // "<query method="..." ref="..."...\n"
	MethodName   string
	XMLNodeName  string

	// query, exec-dml
	ParamDescriptors []string
	Ref              string
	ExternalSQL      bool
	// query
	ReturnTypeIsDto bool
	DtoOrReturnType string
	FetchList       bool // and CRUD read: "read all" or "read one"

	// CRUD
	DtoClassName   string
	TableName      string
	ExplicitPK     string // "" for "read all"
	AutoColumn     string
	FetchGenerated bool
}

func PlanDaoMethods

func PlanDaoMethods(items []sdmxml.DaoItem, dtos []sdmxml.DtoClass, mode FieldNamesMode) ([]DaoMethod, error)

type DaoSqlInfo

type DaoSqlInfo struct {
	SQL       string
	HasSQL    bool
	Fields    []FieldInfo // query, CRUD read: result columns; CRUD create/update: assigned ones
	Params    []ParamInfo // query: parameters; create: values; read/update/delete: PK
	Generated []FieldInfo // CRUD create: generated columns
}

DaoSqlInfo is the SQL of a method, the returned fields and the parameters (JdbcUtils.DaoSqlInfo). HasSQL == false: the method is not rendered, a warning comment is rendered instead.

type ExecDmlKind

type ExecDmlKind int
const (
	Plain       ExecDmlKind = iota // "p_id"
	Mapped                         // "on_dto:Dto" or "on_dto~Dto": one implicit cursor
	CursorArray                    // "[on_dto_1:Dto1, on_dto_2:Dto2]"
)

type ExecDmlSlot

type ExecDmlSlot struct {
	Kind           ExecDmlKind
	Index          int        // position in the declaration
	Mapping        []string   // Mapped: {"on_dto", "Dto"}
	CursorMappings [][]string // CursorArray: the same for each element
}

ExecDmlSlot is one exec-dml parameter (ExecDmlParamSlot.java).

func ParseExecDmlParams

func ParseExecDmlParams(descriptors []string) ([]ExecDmlSlot, error)

type FieldInfo

type FieldInfo struct {
	// contains filtered or unexported fields
}

FieldInfo is a table column, a result column or a DTO field (FieldInfo.java). It is copied by value, and each With... returns a modified copy: the holder of a FieldInfo can be sure nobody changes it behind its back.

func NewFieldInfo

func NewFieldInfo(mode FieldNamesMode, originalType, colName, comment string) (FieldInfo, error)

func (FieldInfo) AssignFunc

func (f FieldInfo) AssignFunc() string

func (FieldInfo) ColumnName

func (f FieldInfo) ColumnName() string

func (FieldInfo) ColumnSize

func (f FieldInfo) ColumnSize() int

func (FieldInfo) Comment

func (f FieldInfo) Comment() string

func (FieldInfo) IsAI

func (f FieldInfo) IsAI() bool

func (FieldInfo) IsPK

func (f FieldInfo) IsPK() bool

func (FieldInfo) LowerCamelName

func (f FieldInfo) LowerCamelName() string

func (FieldInfo) Name

func (f FieldInfo) Name() string

For the templates.

func (FieldInfo) OriginalScalarType

func (f FieldInfo) OriginalScalarType() string

func (FieldInfo) OriginalType

func (f FieldInfo) OriginalType() string

func (FieldInfo) Precision

func (f FieldInfo) Precision() int

func (FieldInfo) ScalarType

func (f FieldInfo) ScalarType() string

func (FieldInfo) Scale

func (f FieldInfo) Scale() int

func (FieldInfo) Type

func (f FieldInfo) Type() string

func (FieldInfo) WithAI

func (f FieldInfo) WithAI(b bool) FieldInfo

func (FieldInfo) WithAssignFunc

func (f FieldInfo) WithAssignFunc(s string) FieldInfo

func (FieldInfo) WithColumnSize

func (f FieldInfo) WithColumnSize(n int) FieldInfo

func (FieldInfo) WithComment

func (f FieldInfo) WithComment(s string) FieldInfo

func (FieldInfo) WithDecimalDigits

func (f FieldInfo) WithDecimalDigits(n int) FieldInfo

func (FieldInfo) WithFK

func (f FieldInfo) WithFK(s string) FieldInfo

func (FieldInfo) WithIndexed

func (f FieldInfo) WithIndexed(b bool) FieldInfo

func (FieldInfo) WithName

func (f FieldInfo) WithName(s string) FieldInfo

func (FieldInfo) WithNullable

func (f FieldInfo) WithNullable(b bool) FieldInfo

func (FieldInfo) WithPK

func (f FieldInfo) WithPK(b bool) FieldInfo

func (FieldInfo) WithRaw

func (f FieldInfo) WithRaw(s string) FieldInfo

func (FieldInfo) WithRenderedType

func (f FieldInfo) WithRenderedType(t string) FieldInfo

WithRenderedType takes "type->assign_func" from type-map: the type and the assign function.

func (FieldInfo) WithRsMd

func (f FieldInfo) WithRsMd(precision, scale, displaySize int) FieldInfo

WithRsMd sets the ResultSetMetaData of the column: they always come together.

func (FieldInfo) WithScalarType

func (f FieldInfo) WithScalarType(s string) FieldInfo

func (FieldInfo) WithUnique

func (f FieldInfo) WithUnique(b bool) FieldInfo

type FieldNamesMode

type FieldNamesMode int

FieldNamesMode is how fields and parameters are named (Names.java, FieldNamesMode.java).

const (
	AsIs FieldNamesMode = iota
	LowerCamelCase
	SnakeCase
	TitleCase
)

type Macros

type Macros struct {
	// contains filtered or unexported fields
}

Macros are <macros> (JaxbMacros.java). <vm-xml> is executed (vmxml.go), text Velocity (<vm>) is not in the Go version.

func NewMacros

func NewMacros(list []sdmxml.Macro) *Macros

func (*Macros) ParseTargetType

func (m *Macros) ParseTargetType(target string, fi FieldInfo) (string, error)

ParseTargetType substitutes macros into the target type of a field.

type MethodKind

type MethodKind int
const (
	QueryMethod MethodKind = iota
	ExecDmlMethod
	CrudCreate
	CrudRead
	CrudUpdate
	CrudDelete
)

type ParamInfo

type ParamInfo struct {
	// contains filtered or unexported fields
}

ParamInfo is a method parameter (ParamInfo.java).

func NewParamInfo

func NewParamInfo(mode FieldNamesMode, typ, name string) ParamInfo

func ParamOf

func ParamOf(f FieldInfo) ParamInfo

ParamOf returns a column as a parameter: the same name and type.

func ParamsOf

func ParamsOf(fields []FieldInfo) []ParamInfo

func (ParamInfo) LowerCamelName

func (p ParamInfo) LowerCamelName() string

func (ParamInfo) Name

func (p ParamInfo) Name() string

func (ParamInfo) Type

func (p ParamInfo) Type() string

func (ParamInfo) WithType

func (p ParamInfo) WithType(t string) ParamInfo

WithType: a parameter needs only the type from "type->assign_func".

type TypeMap

type TypeMap struct {
	// contains filtered or unexported fields
}

TypeMap — <type-map> (JaxbTypeMap.java).

<type detected="...">: two vocabularies on a par: Java names, as in the IDE plug-in (java.lang.Integer), and raw driver names (pg: int4, MySQL: INT, SQLite: INTEGER). The keys are one map, as in the plug-in. The raw metadata name is used only on the first pass (TargetRaw); on a repeated pass of the core the type string itself is looked up, as in Java.

func NewTypeMap

func NewTypeMap(tm sdmxml.TypeMap) *TypeMap

func (*TypeMap) RawOfJava

func (m *TypeMap) RawOfJava(javaType string) string

RawOfJava returns a Java name in the words of the database driver; meta.RawUnknown if the database does not know it.

func (*TypeMap) Target

func (m *TypeMap) Target(detected string) string

Target: detected is an already mapped type or one declared explicitly in XML.

func (*TypeMap) TargetMapped

func (m *TypeMap) TargetMapped(target string) string

TargetMapped maps a type that is a target already: only by its key, "default" is not for it (a non-empty "default" made every mapped field the default; JaxbTypeMap.get_target_type_name_no_default).

func (*TypeMap) TargetOf

func (m *TypeMap) TargetOf(fi FieldInfo) string

TargetOf maps the type of a field (JaxbTypeMap.get_target_type_name(FieldInfo)): one from metadata as usual, with the raw name of the driver; a mapped one by TargetMapped.

func (*TypeMap) TargetRaw

func (m *TypeMap) TargetRaw(detected, raw string) string

TargetRaw maps a type from metadata: the Java name detected and the raw driver name raw ("" if none). An empty type-map gives the raw name as is: it shows which keys are needed (misc.html#type-map).

func (*TypeMap) Unmapped

func (m *TypeMap) Unmapped(typ string) bool

Unmapped reports a type declared in XML by a Java name of the plug-in (java.lang.Integer) that has no key in type-map: the column type from metadata is used instead, and without metadata the same Java name in the driver's words (RawOfJava).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL