Add --version flag and display version on startup

This commit is contained in:
2026-03-29 18:56:03 +02:00
parent 894cd458f5
commit 67240d9998
6 changed files with 94 additions and 96 deletions

View File

@@ -4,46 +4,48 @@ Type-safe GORM column descriptors and model generation utilities.
## Library
The library provides [Field](#field) for type-safe column references and helper functions to extract column/table names.
The library provides [Field](#field) for type-safe column references and methods to extract column/table names.
### Field
`Field` represents a GORM column descriptor with table context. Define package-level variables to get type-safe column references:
`Field` represents a GORM column descriptor with table context. Generated code creates package-level variables with typed field references:
```go
var PsAccess = struct {
IDProfile gormcol.Field
IDAuthorizationRole gormcol.Field
var ProductCols = struct {
ID gormcol.Field
Name gormcol.Field
Price gormcol.Field
}{
IDProfile: gormcol.Field{Table: "ps_access", Column: "id_profile"},
IDAuthorizationRole: gormcol.Field{Table: "ps_access", Column: "id_authorization_role"},
ID: gormcol.Field{}.Set((&Product{}).TableName(), "id_product"),
Name: gormcol.Field{}.Set((&Product{}).TableName(), "name"),
Price: gormcol.Field{}.Set((&Product{}).TableName(), "price"),
}
```
### Helper Functions
### Field Methods
#### Column
#### TabCol
Returns just the column name from a Field descriptor.
Returns "table.column" format for use in SQL with table qualification.
```go
gormcol.Column(dbmodel.PsAccess.IDAuthorizationRole) // "id_authorization_role"
model.ProductCols.Price.TabCol() // "ps_product.price"
```
#### ColumnOnTable
#### Col
Returns "table.column" format from a Field descriptor.
Returns just the column name.
```go
gormcol.ColumnOnTable(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access.id_authorization_role"
model.ProductCols.Price.Col() // "price"
```
#### TableField
#### Tab
Returns the table name from a Field descriptor.
Returns the table name.
```go
gormcol.TableField(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access"
model.ProductCols.Price.Tab() // "ps_product"
```
## CLI
@@ -78,13 +80,14 @@ DSN can be provided via `--dsn` flag or `DSN` env var (from `.env` file).
- **Interactive (default)**: select tables with fzf
- **Batch (`--filter` or `--all`)**: generate matching tables with confirmation
| Flag | Default | Description |
|----------|------------------------|--------------------------------------------------|
| `--dsn` | *(from DSN env)* | MySQL/MariaDB DSN, e.g. `user:pass@tcp(localhost:3306)/dbname` |
| `--filter` | *(interactive)* | Regex matching table names to generate (triggers batch mode) |
| `--all` | *(interactive)* | Generate all tables matching filter (shows confirmation) |
| `--out` | `./app/model/dbmodel` | Output directory for generated files |
| `--pkg` | `dbmodel` | Go package name for generated files |
| Flag | Default | Description |
|-----------|------------------------|---------------------------------------------------|
| `--dsn` | *(from DSN env)* | MySQL/MariaDB DSN, e.g. `user:pass@tcp(localhost:3306)/dbname` |
| `--filter`| *(interactive)* | Regex matching table names to generate (triggers batch mode) |
| `--all` | *(interactive)* | Generate all tables matching filter (shows confirmation) |
| `--out` | `./app/model/dbmodel` | Output directory for generated files |
| `--pkg` | `dbmodel` | Go package name for generated files |
| `--clean` | false | Remove existing model files before generation |
### Interactive Mode (Default)
@@ -189,6 +192,18 @@ ctx := context.Background()
err := gg.GenModels(ctx)
```
## GenConfig
Configuration for model generation.
| Field | Default | Description |
|-----------------|--------------------------|---------------------------------------------------|
| `OutputDir` | `./app/model/dbmodel` | Directory for generated files |
| `PkgName` | `dbmodel` | Go package name |
| `TableFilter` | `ps_.*` | Regex pattern to match table names |
| `SelectedTables`| *(none)* | Specific tables to generate (overrides filter) |
| `Clean` | `true` | Remove existing files before generation |
## Generated Models
After generation, each model file contains a struct and a `Cols` variable:
@@ -202,57 +217,57 @@ type Product struct {
}
var ProductCols = struct {
ID Field
Name Field
Price Field
ID gormcol.Field
Name gormcol.Field
Price gormcol.Field
}{
ID: Field{Table: "ps_product", Column: "id_product"},
Name: Field{Table: "ps_product", Column: "name"},
Price: Field{Table: "ps_product", Column: "price"},
ID: gormcol.Field{}.Set((&Product{}).TableName(), "id_product"),
Name: gormcol.Field{}.Set((&Product{}).TableName(), "name"),
Price: gormcol.Field{}.Set((&Product{}).TableName(), "price"),
}
```
## Using Generated Models
### GORM queries with type-safe columns
### Table-qualified columns
Use `ColumnOnTable` for table-qualified column references in GORM clauses:
Use `TabCol()` for table-qualified column references:
```go
import "git.ma-al.com/goc_marek/gormcol"
// Where clauses
db.Where(
gormcol.ColumnOnTable(model.ProductCols.Price) + " > ?",
model.ProductCols.Price.TabCol() + " > ?",
100.0,
).Find(&products)
// Order
db.Order(gormcol.ColumnOnTable(model.ProductCols.Name) + " ASC").Find(&products)
db.Order(model.ProductCols.Name.TabCol() + " ASC").Find(&products)
// Joins
db.Joins("JOIN ps_category ON " +
gormcol.ColumnOnTable(model.ProductCols.ID) + " = ps_category.id_product",
model.ProductCols.ID.TabCol() + " = ps_category.id_product",
).Find(&products)
```
### Unqualified column names
Use `Column` when the table is already scoped:
Use `Col()` when the table is already scoped:
```go
db.Select(gormcol.Column(model.ProductCols.Name)).Find(&products)
db.Select(model.ProductCols.Name.Col()).Find(&products)
// Raw queries
db.Raw("SELECT " + gormcol.Column(model.ProductCols.Name) + " FROM ps_product").Scan(&names)
db.Raw("SELECT " + model.ProductCols.Name.Col() + " FROM ps_product").Scan(&names)
```
### Table name
Use `TableField` to get the table name from a column descriptor:
Use `Tab()` to get the table name from a column descriptor:
```go
table := gormcol.TableField(model.ProductCols.ID) // "ps_product"
table := model.ProductCols.ID.Tab() // "ps_product"
```
## Dependencies