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
- func CamelCaseToLowerSnakeCase(src string) string
- func ClassNameWithoutModel(dtoClassName string) string
- func FileNameSnakeCase(className, ext string) string
- func FindDto(name string, dtos []sdmxml.DtoClass) (*sdmxml.DtoClass, error)
- func FormatJdbcSQLForCpp(sql string) string
- func FormatJdbcSQLForGo(sql string) string
- func FormatJdbcSQLForPython(sql string) string
- func IsEmptyRef(ref string) bool
- func IsJdbcStoredProcCall(sql string) bool
- func IsSQLFileRef(ref string) bool
- func IsSQLShortcutRef(ref string) bool
- func IsTableRef(ref string) bool
- func JdbcSQLByDtoClassRef(ref, sqlRoot string) (string, error)
- func JdbcSQLByExecDmlRef(ref, sqlRoot string) (string, error)
- func JdbcSQLByRef(ref, sqlRoot string) (string, error)
- func ListedItems(list string, allowColon bool) ([]string, error)
- func LowerCamel(s string) string
- func MethodName(name string, mode FieldNamesMode) string
- func Model(dtoClassName string) string
- func PackageName(scope string) (string, error)
- func ParseMethodDeclaration(text string) (name, params string, err error)
- func SplitMappedParam(pd string) []string
- func SplitTargetType(target string) (typ, assign string, ok bool)
- func Title(s string) string
- func ToKebabCase(src string) string
- type Core
- func (c *Core) CrudCreateInfo(table string, dto *sdmxml.DtoClass, generated string) (*DaoSqlInfo, error)
- func (c *Core) CrudDeleteInfo(table string, dto *sdmxml.DtoClass, explicitPK string) (*DaoSqlInfo, error)
- func (c *Core) CrudReadInfo(table string, dto *sdmxml.DtoClass, fetchList bool, explicitPK string) (*DaoSqlInfo, error)
- func (c *Core) CrudUpdateInfo(table string, dto *sdmxml.DtoClass, explicitPK string) (*DaoSqlInfo, error)
- func (c *Core) DtoFieldNamesMode() FieldNamesMode
- func (c *Core) DtoFields(dto *sdmxml.DtoClass) ([]FieldInfo, error)
- func (c *Core) ExecDmlParams(jdbcSQL string, descriptors []string) ([]ParamInfo, error)
- func (c *Core) QueryInfo(ref string, descriptors []string, dtoOrReturnType string, returnTypeIsDto bool, ...) (*DaoSqlInfo, error)
- func (c *Core) SQLRoot() string
- func (c *Core) TargetTypeByTypeMap(detected string) string
- type DaoMethod
- type DaoSqlInfo
- type ExecDmlKind
- type ExecDmlSlot
- type FieldInfo
- func (f FieldInfo) AssignFunc() string
- func (f FieldInfo) ColumnName() string
- func (f FieldInfo) ColumnSize() int
- func (f FieldInfo) Comment() string
- func (f FieldInfo) IsAI() bool
- func (f FieldInfo) IsPK() bool
- func (f FieldInfo) LowerCamelName() string
- func (f FieldInfo) Name() string
- func (f FieldInfo) OriginalScalarType() string
- func (f FieldInfo) OriginalType() string
- func (f FieldInfo) Precision() int
- func (f FieldInfo) ScalarType() string
- func (f FieldInfo) Scale() int
- func (f FieldInfo) Type() string
- func (f FieldInfo) WithAI(b bool) FieldInfo
- func (f FieldInfo) WithAssignFunc(s string) FieldInfo
- func (f FieldInfo) WithColumnSize(n int) FieldInfo
- func (f FieldInfo) WithComment(s string) FieldInfo
- func (f FieldInfo) WithDecimalDigits(n int) FieldInfo
- func (f FieldInfo) WithFK(s string) FieldInfo
- func (f FieldInfo) WithIndexed(b bool) FieldInfo
- func (f FieldInfo) WithName(s string) FieldInfo
- func (f FieldInfo) WithNullable(b bool) FieldInfo
- func (f FieldInfo) WithPK(b bool) FieldInfo
- func (f FieldInfo) WithRaw(s string) FieldInfo
- func (f FieldInfo) WithRenderedType(t string) FieldInfo
- func (f FieldInfo) WithRsMd(precision, scale, displaySize int) FieldInfo
- func (f FieldInfo) WithScalarType(s string) FieldInfo
- func (f FieldInfo) WithUnique(b bool) FieldInfo
- type FieldNamesMode
- type Macros
- type MethodKind
- type ParamInfo
- type TypeMap
- func (m *TypeMap) RawOfJava(javaType string) string
- func (m *TypeMap) Target(detected string) string
- func (m *TypeMap) TargetMapped(target string) string
- func (m *TypeMap) TargetOf(fi FieldInfo) string
- func (m *TypeMap) TargetRaw(detected, raw string) string
- func (m *TypeMap) Unmapped(typ string) bool
Constants ¶
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 ClassNameWithoutModel ¶
func FileNameSnakeCase ¶
FileNameSnakeCase is the file name of a class: "TasksDao" -> "tasks_dao.go".
func FormatJdbcSQLForCpp ¶
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 ¶
FormatJdbcSQLForGo puts SQL in backquotes, lines joined by " \n\t\t".
func FormatJdbcSQLForPython ¶
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 IsJdbcStoredProcCall ¶
func IsSQLFileRef ¶
func IsSQLShortcutRef ¶
func IsTableRef ¶
func JdbcSQLByDtoClassRef ¶
func JdbcSQLByExecDmlRef ¶
func JdbcSQLByRef ¶
func ListedItems ¶
ListedItems: "a, b, [x:X, y:Y]" -> {"a", "b", "[x:X, y:Y]"} with a check of names.
func LowerCamel ¶
func MethodName ¶
func MethodName(name string, mode FieldNamesMode) string
func PackageName ¶
PackageName is the last segment of scope (GoCG._get_package_name).
func ParseMethodDeclaration ¶
ParseMethodDeclaration — "get_something(p1, p2)" -> "get_something", "p1, p2".
func SplitMappedParam ¶
SplitMappedParam: "on_dto:Dto" or "on_dto~Dto" -> {"on_dto", "Dto"}; nil for a plain parameter.
func SplitTargetType ¶
SplitTargetType splits "type->assign_func" of type-map. ok == false if there is no function.
func ToKebabCase ¶
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 (*Core) CrudCreateInfo ¶
func (*Core) CrudDeleteInfo ¶
func (*Core) CrudReadInfo ¶
func (*Core) CrudUpdateInfo ¶
func (*Core) DtoFieldNamesMode ¶
func (c *Core) DtoFieldNamesMode() FieldNamesMode
func (*Core) ExecDmlParams ¶
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) TargetTypeByTypeMap ¶
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 ¶
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 (FieldInfo) ColumnName ¶
func (FieldInfo) ColumnSize ¶
func (FieldInfo) LowerCamelName ¶
func (FieldInfo) OriginalScalarType ¶
func (FieldInfo) OriginalType ¶
func (FieldInfo) ScalarType ¶
func (FieldInfo) WithAssignFunc ¶
func (FieldInfo) WithColumnSize ¶
func (FieldInfo) WithComment ¶
func (FieldInfo) WithDecimalDigits ¶
func (FieldInfo) WithIndexed ¶
func (FieldInfo) WithNullable ¶
func (FieldInfo) WithRenderedType ¶
WithRenderedType takes "type->assign_func" from type-map: the type and the assign function.
func (FieldInfo) WithRsMd ¶
WithRsMd sets the ResultSetMetaData of the column: they always come together.
func (FieldInfo) WithScalarType ¶
func (FieldInfo) WithUnique ¶
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.
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 (ParamInfo) LowerCamelName ¶
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 (*TypeMap) RawOfJava ¶
RawOfJava returns a Java name in the words of the database driver; meta.RawUnknown if the database does not know it.
func (*TypeMap) Target ¶
Target: detected is an already mapped type or one declared explicitly in XML.
func (*TypeMap) TargetMapped ¶
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 ¶
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 ¶
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).