| | |
Feature Introduction
- 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).
- When converting SQL, ensure whether to switch processing types. Currently, three processing modes are supported: normal, postgresql, and oracle. Default: normal.
- 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 Name | Description | Type |
---|---|---|
TableName | Table name | string |
Fields | Field details | objects |
No | Field number | int |
NotNull | Not null | bool |
AutoIncrement | Auto increment | bool |
PrimaryKey | Primary key | bool |
Name | Field name | string |
ModelType | Model field type | string |
DbType | Database type | string |
Comment | Comment | string |
Default | Default value | string |
- Function Descriptions:
Function Name | Description | Example |
---|---|---|
addLine | Append a newline | |
toFirstUpper | Convert first letter to uppercase | |
toFirstLower | Convert first letter to lowercase | |
sub | Compute difference | |
hump | Convert DB field to camel case | hump test_a → TestA |
len | Get 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"`
}