# sql2go **Repository Path**: saulmou/sql2go ## Basic Information - **Project Name**: sql2go - **Description**: No description available - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2022-03-14 - **Last Updated**: 2022-03-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # sql2go tool This tool intend to convert SQL DDL statement to golang struct with table name and String() method. For example, here is a DDL statement: ```sql CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增id', `name` varchar(32) NOT NULL DEFAULT '' COMMENT '用户名', `email` varchar(64) NOT NULL DEFAULT '' COMMENT '邮箱', `phone` varchar(32) NOT NULL DEFAULT '' COMMENT '手机号', `description` varchar(1024) NOT NULL DEFAULT '' COMMENT '用户介绍', `status` int(11) NOT NULL DEFAULT '0' COMMENT '状态0|1|9:初始添加|正常|注销', `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `idx_unique` (`email`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ``` golang struct User is generated When type cmd `sql2go -f user.sql | gofmt`: ```go package model // NOTE: This file is auto-generated. Any modification will be lost after re-generated by sql2go. import ( "encoding/json" "time" ) // columns of table `user` const ( UserID = "id" UserBaiduUID = "baidu_uid" UserBaiduUK = "baidu_uk" UserWxOpenid = "wx_openid" UserUsername = "username" UserPassword = "password" UserMaskedPhone = "masked_phone" UserPhone = "phone" UserEncryptedPhone = "encrypted_phone" UserIDCard = "id_card" UserName = "name" UserSex = "sex" UserAge = "age" UserUserType = "user_type" UserAuthType = "auth_type" UserAgreement = "agreement" UserMedication = "medication" UserRecordRemind = "record_remind" UserIntvRemind = "intv_remind" UserMsgRemind = "msg_remind" UserConsultation = "consultation" UserBindTpCode = "bind_tp_code" UserTpToken = "tp_token" UserStatus = "status" UserBindLung = "bind_lung" UserPushOption = "push_option" UserCreateTime = "create_time" UserUpdateTime = "update_time" ) // User contains enssential properties. type User struct { // ID defines 自增主键 ID *int64 `gorm:"id" comment:"自增主键" json:"id"` // BaiduUID defines 用户在百度的账号 uid BaiduUID *int64 `gorm:"baidu_uid" comment:"用户在百度的账号 uid" json:"baiduUid"` // BaiduUK defines 用户 uk BaiduUK *string `gorm:"baidu_uk" comment:"用户 uk" json:"baiduUk"` // WxOpenid defines 微信open id WxOpenid *string `gorm:"wx_openid" comment:"微信open id" json:"wxOpenid"` // Username defines 登录账户名 Username *string `gorm:"username" comment:"登录账户名" json:"username"` // Password defines 登录密码 Password *string `gorm:"password" comment:"登录密码" json:"password"` // MaskedPhone defines 从 passport 获取的加掩码的手机号 MaskedPhone *string `gorm:"masked_phone" comment:"从 passport 获取的加掩码的手机号" json:"maskedPhone"` // Phone defines 手机号 Phone *string `gorm:"phone" comment:"手机号" json:"phone"` // EncryptedPhone defines 加密的手机号 EncryptedPhone *string `gorm:"encrypted_phone" comment:"加密的手机号" json:"encryptedPhone"` // IDCard defines 身份证号 IDCard *string `gorm:"id_card" comment:"身份证号" json:"idCard"` // Name defines 真实姓名 Name *string `gorm:"name" comment:"真实姓名" json:"name"` // Sex defines 0|1|9: 女|男|未知 Sex *int32 `gorm:"sex" comment:"0|1|9: 女|男|未知" json:"sex"` // Age defines 年龄 Age *int32 `gorm:"age" comment:"年龄" json:"age"` // UserType defines 关联方: 1 浪潮 UserType *int32 `gorm:"user_type" comment:"关联方: 1 浪潮" json:"userType"` // AuthType defines 医保类型 -1 未设置 0 非医保用户 1 成都医保用户 AuthType *int32 `gorm:"auth_type" comment:"医保类型 -1 未设置 0 非医保用户 1 成都医保用户" json:"authType"` // Agreement defines 协议状态, 0|1:未同意|已同意 Agreement *int32 `gorm:"agreement" comment:"协议状态, 0|1:未同意|已同意" json:"agreement"` // Medication defines 用药提醒 0|1:关闭|开启 Medication *int32 `gorm:"medication" comment:"用药提醒 0|1:关闭|开启" json:"medication"` // RecordRemind defines 记录提醒开关,0-关闭 1-开启 RecordRemind *int32 `gorm:"record_remind" comment:"记录提醒开关,0-关闭 1-开启" json:"recordRemind"` // IntvRemind defines 小度用户随访提醒开关 0-关闭 1-打开 IntvRemind *int32 `gorm:"intv_remind" comment:"小度用户随访提醒开关 0-关闭 1-打开" json:"intvRemind"` // MsgRemind defines 小度用户消息提醒开关 0-关闭 1-打开 MsgRemind *int32 `gorm:"msg_remind" comment:"小度用户消息提醒开关 0-关闭 1-打开" json:"msgRemind"` // Consultation defines 复诊提醒 0|1:关闭|开启 Consultation *int32 `gorm:"consultation" comment:"复诊提醒 0|1:关闭|开启" json:"consultation"` // BindTpCode defines 当前绑定的接入方 BindTpCode *string `gorm:"bind_tp_code" comment:"当前绑定的接入方" json:"bindTpCode"` // TpToken defines 当前绑定的接入方token TpToken *string `gorm:"tp_token" comment:"当前绑定的接入方token" json:"tpToken"` // Status defines 0|9:正常|停用 Status *int32 `gorm:"status" comment:"0|9:正常|停用" json:"status"` // BindLung defines 肺结节医生绑定状态:0|未绑定、1|已绑定、2|已解绑 BindLung *int32 `gorm:"bind_lung" comment:"肺结节医生绑定状态:0|未绑定、1|已绑定、2|已解绑" json:"bindLung"` // PushOption defines 提醒选项:0|1, 第二天推送|及时推送提醒 PushOption *int32 `gorm:"push_option" comment:"提醒选项:0|1, 第二天推送|及时推送提醒" json:"pushOption"` // CreateTime defines 创建时间 CreateTime *time.Time `gorm:"create_time" comment:"创建时间" json:"createTime"` // UpdateTime defines 更新时间 UpdateTime *time.Time `gorm:"update_time" comment:"更新时间" json:"updateTime"` } // TableName defines mapping table in database func (instance User) TableName() string { return "user" } // String method ensures that struct property can be logged func (instance User) String() string { bytes, err := json.Marshal(instance) if err != nil { panic(err) } return string(bytes) } // SetDefaultValueWhenNil method set property pointer to default value when it is nil func (instance *User) SetDefaultValueWhenNil() { var defaultInt8 int8 = 0 var defaultInt16 int16 = 0 var defaultInt32 int32 = 0 var defaultInt64 int64 = 0 var defaultStr string = "" var defaultTime time.Time = time.Now() if instance.BaiduUID == nil { instance.BaiduUID = &defaultInt64 } if instance.BaiduUK == nil { instance.BaiduUK = &defaultStr } if instance.WxOpenid == nil { instance.WxOpenid = &defaultStr } if instance.Username == nil { instance.Username = &defaultStr } if instance.Password == nil { instance.Password = &defaultStr } if instance.MaskedPhone == nil { instance.MaskedPhone = &defaultStr } if instance.Phone == nil { instance.Phone = &defaultStr } if instance.EncryptedPhone == nil { instance.EncryptedPhone = &defaultStr } if instance.IDCard == nil { instance.IDCard = &defaultStr } if instance.Name == nil { instance.Name = &defaultStr } if instance.Sex == nil { instance.Sex = &defaultInt32 } if instance.Age == nil { instance.Age = &defaultInt32 } if instance.UserType == nil { instance.UserType = &defaultInt32 } if instance.AuthType == nil { instance.AuthType = &defaultInt32 } if instance.Agreement == nil { instance.Agreement = &defaultInt32 } if instance.Medication == nil { instance.Medication = &defaultInt32 } if instance.RecordRemind == nil { instance.RecordRemind = &defaultInt32 } if instance.IntvRemind == nil { instance.IntvRemind = &defaultInt32 } if instance.MsgRemind == nil { instance.MsgRemind = &defaultInt32 } if instance.Consultation == nil { instance.Consultation = &defaultInt32 } if instance.BindTpCode == nil { instance.BindTpCode = &defaultStr } if instance.TpToken == nil { instance.TpToken = &defaultStr } if instance.Status == nil { instance.Status = &defaultInt32 } if instance.BindLung == nil { instance.BindLung = &defaultInt32 } if instance.PushOption == nil { instance.PushOption = &defaultInt32 } if instance.CreateTime == nil { instance.CreateTime = &defaultTime } if instance.UpdateTime == nil { instance.UpdateTime = &defaultTime } _ = defaultInt8 _ = defaultInt16 } ``` PS: gofmt command is used to make the output go code looks pretty.