sql/

directory
v0.0.0-...-115e584 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: Apache-2.0

README

快速开始

  • 通过ddl生成

    $ k2ctl model mysql ddl -src="./*.sql" -dir="./sql/model" -c
    
  • 通过datasource生成

    k2ctl model mysql datasource -url="user:password@tcp(127.0.0.1:3306)/database" -table="*"  -dir="./model"
    

用法

$ k2ctl model mysql -h
NAME:
   k2ctl model mysql - generate mysql model"

USAGE:
   k2ctl model mysql command [command options] [arguments...]

COMMANDS:
   ddl         generate mysql model from ddl"
   datasource  generate model from datasource"

OPTIONS:
   --help, -h  show help

生成规则

  • 默认规则

    我们默认用户在建表时会创建createTime、updateTime字段(忽略大小写、下划线命名风格)且默认值均为CURRENT_TIMESTAMP,而updateTime支持ON UPDATE CURRENT_TIMESTAMP,对于这两个字段生成insertupdate时会被移除,不在赋值范畴内,当然,如果你不需要这两个字段那也无大碍。

  • 带缓存模式

    • ddl

      k2ctl model mysql -src={patterns} -dir={dir} -cache
      

      help

      NAME:
         k2ctl model mysql ddl - generate mysql model from ddl
      
      USAGE:
         k2ctl model mysql ddl [command options] [arguments...]
      
      OPTIONS:
         --src value, -s value  the path or path globbing patterns of the ddl
         --dir value, -d value  the target dir
         --style value          the file naming format, see [https://github.com/k2let/tree/master/k2ctl/config/readme.md]
         --cache, -c            generate code with cache [optional]
         --idea                 for idea plugin [optional]
      
    • datasource

      k2ctl model mysql datasource -url={datasource} -table={patterns}  -dir={dir} -cache=true
      

      help

      NAME:
         k2ctl model mysql datasource - generate model from datasource
      
      USAGE:
         k2ctl model mysql datasource [command options] [arguments...]
      
      OPTIONS:
         --url value              the data source of database,like "root:password@tcp(127.0.0.1:3306)/database
         --table value, -t value  the table or table globbing patterns in the database
         --cache, -c              generate code with cache [optional]
         --dir value, -d value    the target dir
         --style value            the file naming format, see [https://github.com/k2let/tree/master/k2ctl/config/readme.md]
         --idea                   for idea plugin [optional]
      
      
      

      示例用法请参考用法

      NOTE: k2ctl model mysql ddl/datasource 均新增了一个--style参数,用于标记文件命名风格。

    目前仅支持redis缓存,如果选择带缓存模式,即生成的FindOne(ByXxx)&Delete代码会生成带缓存逻辑的代码,目前仅支持单索引字段(除全文索引外),对于联合索引我们默认认为不需要带缓存,且不属于通用型代码,因此没有放在代码生成行列,如example中user表中的idnamemobile字段均属于单字段索引。

  • 不带缓存模式

    • ddl

        k2ctl model -src={patterns} -dir={dir}
      
    • datasource

        k2ctl model mysql datasource -url={datasource} -table={patterns}  -dir={dir}
      

    or

    • ddl

        k2ctl model -src={patterns} -dir={dir}
      
    • datasource

        k2ctl model mysql datasource -url={datasource} -table={patterns}  -dir={dir}
      

生成代码仅基本的CURD结构。

缓存

对于缓存这一块我选择用一问一答的形式进行罗列。我想这样能够更清晰的描述model中缓存的功能。

  • 缓存会缓存哪些信息?

    对于主键字段缓存,会缓存整个结构体信息,而对于单索引字段(除全文索引)则缓存主键字段值。

  • 数据有更新(update)操作会清空缓存吗?

    会,但仅清空主键缓存的信息,why?这里就不做详细赘述了。

  • 为什么不按照单索引字段生成updateByXxxdeleteByXxx的代码?

    理论上是没任何问题,但是我们认为,对于model层的数据操作均是以整个结构体为单位,包括查询,我不建议只查询某部分字段(不反对),否则我们的缓存就没有意义了。

  • 为什么不支持findPageLimitfindAll这么模式代码生层?

    目前,我认为除了基本的CURD外,其他的代码均属于业务型代码,这个我觉得开发人员根据业务需要进行编写更好。

类型转换规则

mysql dataType golang dataType golang dataType(if null&&default null)
bool int64 sql.NullInt64
boolean int64 sql.NullInt64
tinyint int64 sql.NullInt64
smallint int64 sql.NullInt64
mediumint int64 sql.NullInt64
int int64 sql.NullInt64
integer int64 sql.NullInt64
bigint int64 sql.NullInt64
float float64 sql.NullFloat64
double float64 sql.NullFloat64
decimal float64 sql.NullFloat64
date time.Time sql.NullTime
datetime time.Time sql.NullTime
timestamp time.Time sql.NullTime
time string sql.NullString
year time.Time sql.NullInt64
char string sql.NullString
varchar string sql.NullString
binary string sql.NullString
varbinary string sql.NullString
tinytext string sql.NullString
text string sql.NullString
mediumtext string sql.NullString
longtext string sql.NullString
enum string sql.NullString
set string sql.NullString
json string sql.NullString

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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