GO Online Toolset
home
search
To Boss

Convert sql to custom model
135  |   |   |  11

Feature Introduction

  1. Supports converting table creation SQL into custom models for multiple databases, including PostgreSQL, Oracle, and MySQL. Table creation statements, Table creation statements, Table creation statements (important things are repeated three times).
  2. When converting SQL, ensure whether to switch processing types. Currently, three processing modes are supported: normal, postgresql, and oracle. Default: normal.
  3. Supports filtering out unnecessary fields. Multiple fields can be separated by ,.
    Note: SQL should not contain comments (i.e., -- xxx or # xxx). Since this tool uses ; to split statements, normal mode can process multiple table creation statements at once. However, postgresql and oracle require comment parsing and only support a single table creation statement.

Template Guide

Syntax Guide
  • if Condition
{{if condition}}
  // Executes when the condition is met
{{else if another_condition}}
  // Executes when another condition is met
{{else}}
  // Executes when no condition is met
{{end}}

// Comparison operators
eq      Returns true if arg1 == arg2
ne      Returns true if arg1 != arg2
lt      Returns true if arg1 < arg2
le      Returns true if arg1 <= arg2
gt      Returns true if arg1 > arg2
ge      Returns true if arg1 >= arg2
  • Example:
{{if .IsAdmin}}
  <p>Welcome, Admin</p>
{{else}}
  <p>Welcome, User</p>
{{end}}
  • for Loop
// Loop through a variable
{{- range variable }}
  // Process each element
{{- end }}

// Loop with index
{{- range $index, $user := .Users }}
  <p>{{$index}}: {{$user.Name}} ({{$user.Age}} years old)</p>
{{- end }}
  • Note: The - in {{- range }} removes extra spaces or newlines.
  • Example:
<ul>
  {{- range .Users }}
    <li>{{.Name}} - {{.Age}} years old</li>
  {{- end }}
</ul>

Template Explanation

  • Field Descriptions:
Field NameDescriptionType
TableNameTable namestring
FieldsField detailsobjects
    NoField numberint
    NotNullNot nullbool
    AutoIncrementAuto incrementbool
    PrimaryKeyPrimary keybool
    NameField namestring
    ModelTypeModel field typestring
    DbTypeDatabase typestring
    CommentCommentstring
    DefaultDefault valuestring
  • Function Descriptions:
Function NameDescriptionExample
addLineAppend a newline
toFirstUpperConvert first letter to uppercase
toFirstLowerConvert first letter to lowercase
subCompute difference
humpConvert DB field to camel casehump test_aTestA
lenGet length

Example Template

  • SQL to Struct
type {{ hump .TableName }} struct {
{{- range .Fields }}
    {{ hump .Name }} {{ .ModelType }} `json:"{{ .Name }}"`  {{ if .Comment }} // {{ .Comment }} {{ end }}
{{- end }}
}

Example

MySQL Table Creation SQL
CREATE TABLE if not exists user (
  id int NOT NULL AUTO_INCREMENT,
  name varchar(10) NOT NULL COMMENT 'Name',
  gender tinyint NOT NULL DEFAULT 0 COMMENT 'Gender: 0-Unknown, 1-Male, 2-Female',
  age int NOT NULL COMMENT 'Age',
  created_date datetime DEFAULT CURRENT_TIMESTAMP,
  updated_date datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

Processed Output:

type User struct {
    Id int `json:"id"`  
    Name string `json:"name"`   // Name 
    Gender int `json:"gender"`   // Gender: 0-Unknown, 1-Male, 2-Female 
    Age int `json:"age"`   // Age 
    CreatedDate string `json:"created_date"`  
    UpdatedDate string `json:"updated_date"`  
}