Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-orusapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orus-io
go-orusapi
Commits
2c836efb6791
Commit
2c836efb6791
authored
4 years ago
by
Christophe de Vienne
Browse files
Options
Downloads
Patches
Plain Diff
generate_models: add helpers to easily write sql columns and table aliases and joins
parent
e174902909bd
No related branches found
No related tags found
No related merge requests found
Pipeline
#6861
passed
4 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
scripts/generate_db_helpers.go
+119
-0
119 additions, 0 deletions
scripts/generate_db_helpers.go
with
119 additions
and
0 deletions
scripts/generate_db_helpers.go
+
119
−
0
View file @
2c836efb
...
@@ -257,6 +257,8 @@
...
@@ -257,6 +257,8 @@
// This file contains constants for all db names involved in a mapped struct
// This file contains constants for all db names involved in a mapped struct
// It also add some accessors on the struct types so they implement a 'Mapped' interface
// It also add some accessors on the struct types so they implement a 'Mapped' interface
import "github.com/Masterminds/squirrel"
// Mapped is the common interface of all structs that are mapped in the database
// Mapped is the common interface of all structs that are mapped in the database
type Mapped interface {
type Mapped interface {
Table() string
Table() string
...
@@ -265,6 +267,45 @@
...
@@ -265,6 +267,45 @@
Values(columns ...string) []interface{}
Values(columns ...string) []interface{}
}
}
type TableSchema interface {
GetName() string
Sql() string
}
type Column struct {
table TableSchema
name string
}
func (c Column) Sql() string {
return c.table.GetName()+"."+c.name
}
func (c Column) ToSql() (string, []interface{}, error ) {
return c.Sql(), nil, nil
}
func (c Column) Join(col Column) Join {
return Join{
from: c,
to: col,
}
}
func (c Column) Eq(value interface{}) squirrel.Eq{
return squirrel.Eq{c.Sql(): value}
}
type Join struct {
from Column
to Column
}
func (j Join) Sql() string {
return j.to.table.Sql() + " ON " + j.to.Sql() + " = " + j.from.Sql()
}
const (
const (
// table and column names
// table and column names
{{- range $i, $dbstruct := .DBStructs}}
{{- range $i, $dbstruct := .DBStructs}}
...
@@ -378,4 +419,11 @@
...
@@ -378,4 +419,11 @@
}
}
return values
return values
}
}
{{- if .HasTable}}
func New{{.Name}}TableSchema() *{{.Name}}TableSchema {
t := {{.Name}}TableSchema{}
{{- range .Fields}}
t.{{.Name}} = Column{&t, "{{.Column}}"}
{{- end}}
{{- end}}
...
@@ -381,3 +429,74 @@
...
@@ -381,3 +429,74 @@
{{- end}}
{{- end}}
return &t
}
type {{.Name}}TableSchema struct {
alias string
{{- range .Fields}}
{{.Name}} Column
{{- end}}
}
// Columns returns the database table column names
func (t {{.Name}}TableSchema) Columns(withPKey bool) []string {
if withPKey {
return {{.Name}}Columns
}
return {{.Name}}DataColumns
}
func (t {{.Name}}TableSchema) As(name string) *{{.Name}}TableSchema {
t.alias = name
{{- range .Fields}}
t.{{.Name}} = Column{&t, "{{.Column}}"}
{{- end}}
return &t
}
func (t {{.Name}}TableSchema) GetName() string {
if t.alias == "" {
return {{.Name}}Table
}
return t.alias
}
func (t {{.Name}}TableSchema) Sql() string {
if t.alias == "" {
return {{.Name}}Table
}
return {{.Name}}Table + " AS " + t.alias
}
func (t {{.Name}}TableSchema) ToSql() (string, []interface{}, error) {
return t.Sql(), nil, nil
}
{{- end}}
{{- end}}
func NewDBSchema() *DBSchema {
return &DBSchema{
{{- range $dbstruct := .DBStructs}}
{{- if .HasTable}}
{{.Name}}: New{{.Name}}TableSchema(),
{{- end}}
{{- end}}
}
}
type DBSchema struct {
{{- range $dbstruct := .DBStructs}}
{{- if .HasTable}}
{{.Name}} *{{.Name}}TableSchema
{{- end}}
{{- end}}
}
var Schema = NewDBSchema()
`
))
`
))
)
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment