sqlgen

package module
v1.0.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 0 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const Version = "v1.0.0-alpha.5"

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
sqlgen
Typically this process would be run using go generate, like this:
Typically this process would be run using go generate, like this:
examples module
internal
cmd
types
The `types` package is reference from :
The `types` package is reference from :

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL