dbCodegen

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 19 Imported by: 0

README

Database Code Generator

Code generators are complex. Hence in this package, we shall be adding more comments than usual to make sure that process is clear about what we are doing.

Documentation

Index

Constants

View Source
const DefaultMagicComment = "// MAGIC COMMENT (DO NOT EDIT): Please write any custom code only below this line.\n"

Variables

This section is empty.

Functions

This section is empty.

Types

type CodegenConfig

type CodegenConfig struct {
	PgDbUrl                  string // DB URL string for PostgreSQL database to which we have to connect
	ModelsContainerPackage   string // Full package name under which the DB and Network packages will fall
	DbModelPackageName       string // Name of the package under which the db related code will be placed
	DbModelPackagePath       string // Full path of the directory where the generated code for db will be placed
	NetworkPackageName       string // Name of the package under which the networking related code for DB tables is gonna be placed
	NetworkPackagePath       string // Full path of the directory where the networking related code for DB tables is gonna be placed
	MagicComment             string // Magic comment which allows the generator to update only the generated portion of code
	ColCommentSeparator      string // The string after which we can place the Properties JSON in column comment
	InsertCreatedAtInCode    bool   // Should the code for inserting `created_at` timestamp column be generated by the code generator?
	InsertUpdatedAtInCode    bool   // Should the code for inserting `updated_at` timestamp column be generated by the code generator?
	UpdateUpdatedAtInCode    bool   // Should the code for updating `updated_at` timestamp column be generated by the code generator?
	BuildUpdateByUniqueIndex bool   // Should we generate the update functions for unique indexes?
	ColumnOrderAlphabetic    bool   // Column order in generated code will be alphabetic if this is set to true, ordinal otherwise
	Enumerations             map[string]EnumDefinition
}

CodegenConfig contains the values and rules using which the code is to be generated

type DbColumn

type DbColumn struct {
	Schema            string           // Schema name in which this column's table resides
	Table             string           // Table name of the table in which this column is
	Name              string           // Column name
	GoName            string           // Name we want to use for Golang code that will be generated
	GoNameSingular    string           // Singular form of the name
	GoNamePlural      string           // Plural form of the name
	DataType          string           // Data type we get from db
	GoDataType        string           // Data type we want to use in go program
	NetworkDataType   string           // Data type we want to use for the network model
	Comment           string           // Column comment
	CharacterLength   int              // Length in case it is varchar
	Nullable          bool             // NOT NULL means it is false
	HasDefaultValue   bool             // Does the column have a default value?
	DefaultValue      string           // If column has default value then what is it
	CommentProperties dbColumnProperty // Properties that will control mostly column value validations
}

DbColumn is the column representation of a table in the database for the generator

type DbFkInfo

type DbFkInfo struct {
	FromSchema     string            // The schema name of the table from which the foreign key reference is being made
	FromTable      string            // The table name which is referencing the target table
	ToSchema       string            // The schema name of the table whose column is being referenced
	ToTable        string            // The table name of the table which is being referenced
	FromColOrder   []string          // The order which the columns appear in the FromTable
	References     map[string]string // The reference map ([from_column]to_column format)
	ConstraintName string            // Name of the foreign key constraint
}

DbFkInfo represents a single foreign key in a table

func (*DbFkInfo) GetReverseRefName

func (fki *DbFkInfo) GetReverseRefName() string

type DbIndex

type DbIndex struct {
	Name       string     // Name of the index
	IsUnique   bool       // Is this a "unique" index?
	IsPrimary  bool       // Does this index correspond to the primary key of the table
	ColumnList []DbColumn // List of columns of this index (ordered the same way as the columns are defined in the index)
}

DbIndex represents an index inside a table

func (*DbIndex) GetFuncNamePart

func (idx *DbIndex) GetFuncNamePart() string

func (*DbIndex) GetSortedColumnNames

func (idx *DbIndex) GetSortedColumnNames() []string

type DbRevFkInfo

type DbRevFkInfo struct {
	DbFkInfo
	UniqueIndex bool // Is there a unique index on the column set pointing to this column
}

type DbSchema

type DbSchema struct {
	Name      string             // Name of the schema
	GoName    string             // The name of table as a go variable that we would use
	Tables    map[string]DbTable // Map of name of table to their DbTable struct values
	TablesA2z []string           // List of Tables in alphabetical order
}

DbSchema represents the schema in the database

type DbTable

type DbTable struct {
	Name           string                 // Table name as got from the query
	GoName         string                 // The name of table as a go variable that we would use
	GoNameSingular string                 // Singular form of GoName
	GoNamePlural   string                 // Plural form of GoName
	Schema         string                 // The name of the schema where this table resides
	Comment        string                 // Comment on the table
	ColumnMap      map[string]DbColumn    // List of columns as a map from the name of the column to the DbColumn type
	ColumnList     []string               // List of columns (ordinal)
	ColumnListA2z  []string               // List of column names (alphabetical)
	PkColumnList   []DbColumn             // List of columns that make the primary key (slice because order matters)
	IndexList      []DbIndex              // List of indexes on this table
	FKeyMap        map[string]DbFkInfo    // List of foreign keys in table as map from constraint name to DbFkInfo type
	RevFKeyMap     map[string]DbRevFkInfo // List of reverse reference in table as map from constraint name to DbRevFkInfo type
}

DbTable represents a table in the database

func (*DbTable) FindIndexByColumnNames

func (table *DbTable) FindIndexByColumnNames(colNames []string) DbIndex

type EnumDefinition

type EnumDefinition struct {
	Name              string           // Name of this enum
	Exported          bool             // Enum to be used outside the DB package
	IsDbType          bool             // Is this enum supposed to be used in the DB?
	Mappings          map[string]int16 // List of enumerations
	DisableGeneration bool             // Disable the generation/update of this type (temporarily?)
	// contains filtered or unexported fields
}

EnumDefinition defines an enumeration in code which would ideally be saved in the DB

type Generator

type Generator struct {
	// More fields to be decided
	Config  CodegenConfig             // The configuration for this generator
	Schemas map[string]DbSchema       // The schemas in the database (will in turn contain tables)
	Enums   map[string]EnumDefinition // The enumerations to be built

	sync.Mutex // To prevent parallel runs
	// contains filtered or unexported fields
}

Generator is the structure we return to a client which needs a generator. It is supposed to contain all the information needed for performing the code generation

func NewCodeGenerator

func NewCodeGenerator(config CodegenConfig) (Generator, appError.Typ)

func (*Generator) Generate

func (g *Generator) Generate() appError.Typ

Jump to

Keyboard shortcuts

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