Skip to content

si3nloong/sqlgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

sqlgen

Build Go Report Go Coverage LICENSE

sqlgen is not an ORM, it's a code generator instead. It parse the go struct and generate the necessary struct methods for you.

What is sqlgen?

  • sqlgen is based on a Code first approach β€” You don't require to write SQL first, but Go code instead.
  • sqlgen enables Codegen β€” We generate the boring bits, so you can focus on building your app quickly.
  • sqlgen prioritizes Performance β€” Most of the things will define in compile time instead of runtime.
  • sqlgen embrace Generics β€” We use generics to eliminate runtime reflection costs and reduce memory allocation.
  • sqlgen eliminates Side Effects - You will get expected results instead of side effects when mutate your models.

SQL driver support

Driver Support
mysql βœ…
postgres βœ…
sqlite βœ…

Quick start

  1. Install sqlgen.

    go install github.com/si3nloong/sqlgen/cmd/sqlgen@main
  2. Define your model struct.

    model/user.go
    package model
    
    import "time"
    
    type LongText string
    
    type User struct {
        ID      int64 `sql:",auto_increment"`
        Name    string
        Age     uint8
        Address LongText
        Created time.Time
    }
  3. Generate the output files.

    # sqlgen generate <source_file>
    sqlgen generate model/user.go
  4. Generated code will be as follow.

    model/generated.go
    // Code generated by sqlgen. DO NOT EDIT.
    
    package model
    
    import (
        "database/sql/driver"
        "time"
    
        "github.com/si3nloong/sqlgen/sequel/types"
    )
    
    func (User) CreateTableStmt() string {
        return "CREATE TABLE IF NOT EXISTS `user` (`id` BIGINT NOT NULL AUTO_INCREMENT,`name` VARCHAR(255) NOT NULL,`age` TINYINT UNSIGNED NOT NULL,`address` VARCHAR(255) NOT NULL,`created` DATETIME NOT NULL,PRIMARY KEY (`id`));"
    }
    func (User) AlterTableStmt() string {
        return "ALTER TABLE `user` MODIFY `id` BIGINT NOT NULL AUTO_INCREMENT,MODIFY `name` VARCHAR(255) NOT NULL AFTER `id`,MODIFY `age` TINYINT UNSIGNED NOT NULL AFTER `name`,MODIFY `address` VARCHAR(255) NOT NULL AFTER `age`,MODIFY `created` DATETIME NOT NULL AFTER `address`;"
    }
    func (User) TableName() string {
        return "`user`"
    }
    func (User) Columns() []string {
        return []string{"`id`", "`name`", "`age`", "`address`", "`created`"}
    }
    func (v User) IsAutoIncr() {}
    func (v User) PK() (columnName string, pos int, value driver.Value) {
        return "`id`", 0, int64(v.ID)
    }
    func (v User) Values() []any {
        return []any{int64(v.ID), string(v.Name), int64(v.Age), string(v.Address), time.Time(v.Created)}
    }
    func (v *User) Addrs() []any {
        return []any{types.Integer(&v.ID), types.String(&v.Name), types.Integer(&v.Age), types.String(&v.Address), (*time.Time)(&v.Created)}
    }

More help to get started:

Benchmark

Reporting Issues

If you think you've found a bug, or something isn't behaving the way you think it should, please raise an issue on GitHub.

Contributing

We welcome contributions, Read our Contribution Guidelines to learn more about contributing to sqlgen

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❀

GitHub

Inspired By

License

MIT

Copyright (c) 2023-present, SianLoong Lee