|
1 |
| -### SQLize |
| 1 | +# SQLize |
2 | 2 |
|
3 | 3 | 
|
4 | 4 |
|
5 | 5 | English | [中文](README_zh.md)
|
6 | 6 |
|
7 |
| -Generate SQL migration schema from Golang models and the current SQL schema, support: |
| 7 | +SQLize is a powerful SQL toolkit for Golang, offering parsing, building, and migration capabilities. |
8 | 8 |
|
9 |
| -- [x] MySQL |
10 |
| -- [x] Postgres |
11 |
| -- [x] Sqlite |
12 |
| -- [ ] Sql Server |
| 9 | +## Features |
13 | 10 |
|
14 |
| -#### Features |
| 11 | +- SQL parsing and building for multiple databases: |
| 12 | + - MySQL |
| 13 | + - PostgreSQL |
| 14 | + - SQLite |
15 | 15 |
|
16 |
| -+ Sql parser (MySQL, Postgres, Sqlite) |
17 |
| -+ Sql builder from objects (MySQL, Postgres, Sqlite) |
18 |
| -+ Generate `sql migration` from Golang models and the current SQL schema |
19 |
| -+ Generate `arvo` schema (Mysql only) |
20 |
| -+ Support embedded struct |
21 |
| -+ Generate migration version - compatible with `golang-migrate/migrate` |
22 |
| -+ Tag options - compatible with `gorm` tag (default tag is `sql`) |
| 16 | +- SQL migration generation: |
| 17 | + - Create migrations from Golang models and current SQL schema |
| 18 | + - Generate migration versions compatible with `golang-migrate/migrate` |
23 | 19 |
|
| 20 | +- Advanced functionalities: |
| 21 | + - Support for embedded structs |
| 22 | + - Avro schema generation (MySQL only) |
| 23 | + - Compatibility with `gorm` tags (default tag is `sql`) |
24 | 24 |
|
25 |
| -### Getting Started |
| 25 | +## Conventions |
| 26 | + |
| 27 | +### Default Behaviors |
| 28 | + |
| 29 | +- Database: `mysql` (use `sql_builder.WithPostgresql()` for PostgreSQL, etc.) |
| 30 | +- SQL syntax: Uppercase (e.g., `"SELECT * FROM user WHERE id = ?"`) |
| 31 | + - For lowercase, use `sql_builder.WithSqlLowercase()` |
| 32 | +- Table naming: Singular |
| 33 | + - For plural (adding 's'), use `sql_builder.WithPluralTableName()` |
| 34 | +- Comment generation: Use `sql_builder.WithCommentGenerate()` |
| 35 | + |
| 36 | +### SQL Tag Options |
| 37 | + |
| 38 | +- Format: Supports both `snake_case` and `camelCase` (e.g., `sql:"primary_key"` equals `sql:"primaryKey"`) |
| 39 | +- Custom column: `sql:"column:column_name"` |
| 40 | +- Primary key: `sql:"primary_key"` |
| 41 | +- Foreign key: `sql:"foreign_key:user_id;references:user_id"` |
| 42 | +- Auto increment: `sql:"auto_increment"` |
| 43 | +- Default value: `sql:"default:CURRENT_TIMESTAMP"` |
| 44 | +- Override datatype: `sql:"type:VARCHAR(64)"` |
| 45 | +- Ignore field: `sql:"-"` |
| 46 | + |
| 47 | +### Indexing |
| 48 | + |
| 49 | +- Basic index: `sql:"index"` |
| 50 | +- Custom index name: `sql:"index:idx_col_name"` |
| 51 | +- Unique index: `sql:"unique"` |
| 52 | +- Custom unique index: `sql:"unique:idx_name"` |
| 53 | +- Composite index: `sql:"index_columns:col1,col2"` (includes unique index and primary key) |
| 54 | +- Index type: `sql:"index_type:btree"` |
| 55 | + |
| 56 | +### Embedded Structs |
| 57 | + |
| 58 | +- Use `sql:"embedded"` or `sql:"squash"` |
| 59 | +- Cannot be a pointer |
| 60 | +- Supports prefix: `sql:"embedded_prefix:base_"` |
| 61 | +- Fields have lowest order, except for primary key (always first) |
| 62 | + |
| 63 | +### Data Types |
| 64 | + |
| 65 | +- MySQL data types are implicitly changed: |
| 66 | + |
| 67 | +```sql |
| 68 | + TINYINT => tinyint(4) |
| 69 | + INT => int(11) |
| 70 | + BIGINT => bigint(20) |
| 71 | +``` |
| 72 | + |
| 73 | +### Important Notes |
| 74 | + |
| 75 | +- Pointer values must be declared in the struct |
| 76 | + |
| 77 | +### Examples |
| 78 | + |
| 79 | +1. Using pointer values: |
| 80 | + |
| 81 | +```golang |
| 82 | +type sample struct { |
| 83 | + ID int32 `sql:"primary_key"` |
| 84 | + DeletedAt *time.Time |
| 85 | +} |
| 86 | + |
| 87 | +now := time.Now() |
| 88 | +newMigration.FromObjects(sample{DeletedAt: &now}) |
| 89 | +``` |
| 90 | + |
| 91 | +2. Embedded struct: |
| 92 | + |
| 93 | +```golang |
| 94 | +type Base struct { |
| 95 | + ID int32 `sql:"primary_key"` |
| 96 | + CreatedAt time.Time |
| 97 | +} |
| 98 | +type sample struct { |
| 99 | + Base `sql:"embedded"` |
| 100 | + User string |
| 101 | +} |
| 102 | + |
| 103 | +newMigration.FromObjects(sample{}) |
| 104 | + |
| 105 | +/* |
| 106 | +CREATE TABLE sample ( |
| 107 | + id int(11) PRIMARY KEY, |
| 108 | + user text, |
| 109 | + created_at datetime |
| 110 | +); |
| 111 | +*/ |
| 112 | +``` |
| 113 | + |
| 114 | +3. Complete example: |
26 | 115 |
|
27 | 116 | ```go
|
28 | 117 | package main
|
@@ -108,67 +197,3 @@ func main() {
|
108 | 197 | _ = newMigration.WriteFiles("demo migration")
|
109 | 198 | }
|
110 | 199 | ```
|
111 |
| - |
112 |
| -### Convention |
113 |
| - |
114 |
| -* `mysql` by default, using options like `sql_builder.WithPostgresql()` for `postgresql`, ... |
115 |
| -* Sql syntax uppercase (Eg: `"SELECT * FROM user WHERE id = ?"`) default, using option `sql_builder.WithSqlLowercase()` for lowercase |
116 |
| -* Support **generate** comment, using option `sql_builder.WithCommentGenerate()` |
117 |
| -* Support automatic addition of `s` to table names (plural naming convention), using option `sql_builder.WithPluralTableName()` |
118 |
| -* Accept tag convention: `snake_case` or `camelCase`, Eg: `sql:"primary_key"` equalize `sql:"primaryKey"` |
119 |
| -* Custom column name: `sql:"column:column_name"` |
120 |
| -* Primary key for this field: `sql:"primary_key"` |
121 |
| -* Foreign key: `sql:"foreign_key:user_id;references:user_id"` |
122 |
| -* Auto increment: `sql:"auto_increment"` |
123 |
| -* Indexing this field: `sql:"index"` |
124 |
| -* Custom index name: `sql:"index:idx_col_name"` |
125 |
| -* Unique indexing this field: `sql:"unique"` |
126 |
| -* Custome unique index name: `sql:"unique:idx_name"` |
127 |
| -* Composite index (include unique index and primary key): `sql:"index_columns:col1,col2"` |
128 |
| -* Index type: `sql:"index_type:btree"` |
129 |
| -* Set default value: `sql:"default:CURRENT_TIMESTAMP"` |
130 |
| -* Override datatype: `sql:"type:VARCHAR(64)"` |
131 |
| -* Ignore: `sql:"-"` |
132 |
| -* Pointer value must be declare in struct |
133 |
| - |
134 |
| -```golang |
135 |
| -type sample struct { |
136 |
| - ID int32 `sql:"primary_key"` |
137 |
| - DeletedAt *time.Time |
138 |
| -} |
139 |
| - |
140 |
| -now := time.Now() |
141 |
| -newMigration.FromObjects(sample{DeletedAt: &now}) |
142 |
| -``` |
143 |
| - |
144 |
| -* `mysql` data type will be changed implicitly: |
145 |
| - |
146 |
| -```sql |
147 |
| -TINYINT => tinyint(4) |
148 |
| -INT => int(11) |
149 |
| -BIGINT => bigint(20) |
150 |
| -``` |
151 |
| - |
152 |
| -* fields belong to embedded struct have the lowest order, except `primary key` always first |
153 |
| -* an embedded struct (`sql:"embedded"` or `sql:"squash"`) can not be pointer, also support prefix: `sql:"embedded_prefix:base_"` |
154 |
| - |
155 |
| -```golang |
156 |
| -type Base struct { |
157 |
| - ID int32 `sql:"primary_key"` |
158 |
| - CreatedAt time.Time |
159 |
| -} |
160 |
| -type sample struct { |
161 |
| - Base `sql:"embedded"` |
162 |
| - User string |
163 |
| -} |
164 |
| - |
165 |
| -newMigration.FromObjects(sample{}) |
166 |
| - |
167 |
| -/* |
168 |
| -CREATE TABLE sample ( |
169 |
| - id int(11) PRIMARY KEY, |
170 |
| - user text, |
171 |
| - created_at datetime |
172 |
| -); |
173 |
| -*/ |
174 |
| -``` |
|
0 commit comments