From 2154213518ff5c80b8f056ea44d916079bbcef37 Mon Sep 17 00:00:00 2001 From: xuthus5 Date: Mon, 11 Sep 2023 00:07:03 +0800 Subject: [PATCH] first commit --- .gitignore | 2 + autogen_model_model.go | 35 ++++++++ go.mod | 30 +++++++ go.sum | 58 +++++++++++++ insert_scope.go | 38 ++++++++ insert_scope_test.go | 25 ++++++ model.pb.go | 191 +++++++++++++++++++++++++++++++++++++++++ model.proto | 16 ++++ scope.go | 50 +++++++++++ sdbc.go | 90 +++++++++++++++++++ 10 files changed, 535 insertions(+) create mode 100644 .gitignore create mode 100644 autogen_model_model.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 insert_scope.go create mode 100644 insert_scope_test.go create mode 100644 model.pb.go create mode 100644 model.proto create mode 100644 scope.go create mode 100644 sdbc.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..00ac5fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +test.db diff --git a/autogen_model_model.go b/autogen_model_model.go new file mode 100644 index 0000000..4f27a99 --- /dev/null +++ b/autogen_model_model.go @@ -0,0 +1,35 @@ +// Code generated by protoc-gen-coco. DO NOT EDIT. +// source: model.proto +// generate at: 2023-09-11 00:05:45 + +package sdbc + +const TableNameModelArticles = "articles" + +func (t *ModelArticles) TableName() string { + return "articles" +} + +func (m *ModelArticles) GetIdField() string { + return "_id" +} + +func (m *ModelArticles) GetTitleField() string { + return "title" +} + +func (m *ModelArticles) GetAvatarUrlField() string { + return "avatar_url" +} + +func (m *ModelArticles) GetPhoneField() string { + return "phone" +} + +func (m *ModelArticles) GetCreateTimeField() string { + return "create_time" +} + +func (m *ModelArticles) GetUpdateTimeField() string { + return "update_time" +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b0052c6 --- /dev/null +++ b/go.mod @@ -0,0 +1,30 @@ +module gitter.top/drivers/sdbc + +go 1.21.0 + +require ( + github.com/glebarez/sqlite v1.9.0 + github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.7.0 + gitter.top/common/lormatter v0.0.0-20230910075849-28d49dccd03a + google.golang.org/protobuf v1.31.0 + gorm.io/gorm v1.25.4 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.9.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + modernc.org/libc v1.22.5 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.5.0 // indirect + modernc.org/sqlite v1.23.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..33baf6f --- /dev/null +++ b/go.sum @@ -0,0 +1,58 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.9.0 h1:Aj6bPA12ZEx5GbSF6XADmCkYXlljPNUY+Zf1EQxynXs= +github.com/glebarez/sqlite v1.9.0/go.mod h1:YBYCoyupOao60lzp1MVBLEjZfgkq0tdB1voAQ09K9zw= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gitter.top/common/lormatter v0.0.0-20230910075849-28d49dccd03a h1:bOn73Ju5BmAn+MXT3bukh7fsn9FXGwqHLM03QixCavY= +gitter.top/common/lormatter v0.0.0-20230910075849-28d49dccd03a/go.mod h1:/Zue/gLVDDSvCCRJKytEfpX0LP/JHkIeDzwVE5cA254= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= +gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= +modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= +modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= +modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= diff --git a/insert_scope.go b/insert_scope.go new file mode 100644 index 0000000..ecb98d2 --- /dev/null +++ b/insert_scope.go @@ -0,0 +1,38 @@ +package sdbc + +import "context" + +type insert struct { + scope *scope + sdbc *sdbc + ctx context.Context + selectOpts []interface{} + omitOpts []string +} + +func (i *insert) SetContext(ctx context.Context) { + i.ctx = ctx +} + +func (i *insert) SetSelect(selects ...any) { + i.selectOpts = selects +} + +func (i *insert) SetOmit(omits ...string) { + i.omitOpts = omits +} + +func (i *insert) InsertOne(doc any) error { + db := i.sdbc.client + if len(i.selectOpts) != 0 { + db.Select(i.selectOpts[0], i.selectOpts[1:]...) + } + if len(i.omitOpts) != 0 { + db.Omit(i.omitOpts...) + } + return db.Create(doc).Error +} + +func (i *insert) InsertMany(docs any) error { + return nil +} diff --git a/insert_scope_test.go b/insert_scope_test.go new file mode 100644 index 0000000..6cc66fb --- /dev/null +++ b/insert_scope_test.go @@ -0,0 +1,25 @@ +package sdbc + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func Test_insert_InsertOne(t *testing.T) { + var driver = NewSDBC(&Config{ + Dbname: "test.db", + MaxIdleConn: 10, + MaxOpenConn: 100, + MaxLifetime: time.Hour, + }) + err := driver.GetClient().AutoMigrate(&ModelArticles{}) + assert.NoError(t, err) + var doc = &ModelArticles{ + Title: "hello world1", + CreateTime: time.Now().Unix(), + } + err = driver.BindModel(&ModelArticles{}).Insert().InsertOne(doc) + assert.NoError(t, err) + t.Logf("doc id: %v", doc.Id) +} diff --git a/model.pb.go b/model.pb.go new file mode 100644 index 0000000..b37a270 --- /dev/null +++ b/model.pb.go @@ -0,0 +1,191 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.21.5 +// source: model.proto + +package sdbc + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// @table_name: articles +type ModelArticles struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` // 主键ID + Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"` // 标题 + AvatarUrl string `protobuf:"bytes,5,opt,name=avatar_url,json=avatarUrl,proto3" json:"avatar_url,omitempty"` // 封面 + Phone string `protobuf:"bytes,6,opt,name=phone,proto3" json:"phone,omitempty"` // 手机号码 + CreateTime int64 `protobuf:"varint,12,opt,name=create_time,json=createTime,proto3" json:"create_time,omitempty"` // 创建时间 + UpdateTime int64 `protobuf:"varint,13,opt,name=update_time,json=updateTime,proto3" json:"update_time,omitempty"` // 更新时间 +} + +func (x *ModelArticles) Reset() { + *x = ModelArticles{} + if protoimpl.UnsafeEnabled { + mi := &file_model_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ModelArticles) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ModelArticles) ProtoMessage() {} + +func (x *ModelArticles) ProtoReflect() protoreflect.Message { + mi := &file_model_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ModelArticles.ProtoReflect.Descriptor instead. +func (*ModelArticles) Descriptor() ([]byte, []int) { + return file_model_proto_rawDescGZIP(), []int{0} +} + +func (x *ModelArticles) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *ModelArticles) GetTitle() string { + if x != nil { + return x.Title + } + return "" +} + +func (x *ModelArticles) GetAvatarUrl() string { + if x != nil { + return x.AvatarUrl + } + return "" +} + +func (x *ModelArticles) GetPhone() string { + if x != nil { + return x.Phone + } + return "" +} + +func (x *ModelArticles) GetCreateTime() int64 { + if x != nil { + return x.CreateTime + } + return 0 +} + +func (x *ModelArticles) GetUpdateTime() int64 { + if x != nil { + return x.UpdateTime + } + return 0 +} + +var File_model_proto protoreflect.FileDescriptor + +var file_model_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x73, + 0x64, 0x62, 0x63, 0x22, 0xac, 0x01, 0x0a, 0x0d, 0x4d, 0x6f, 0x64, 0x65, 0x6c, 0x41, 0x72, 0x74, + 0x69, 0x63, 0x6c, 0x65, 0x73, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x61, + 0x76, 0x61, 0x74, 0x61, 0x72, 0x5f, 0x75, 0x72, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x09, 0x61, 0x76, 0x61, 0x74, 0x61, 0x72, 0x55, 0x72, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x70, 0x68, + 0x6f, 0x6e, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x70, 0x68, 0x6f, 0x6e, 0x65, + 0x12, 0x1f, 0x0a, 0x0b, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, + 0x0c, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x54, 0x69, 0x6d, + 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, + 0x18, 0x0d, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x54, 0x69, + 0x6d, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x73, 0x64, 0x62, 0x63, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_model_proto_rawDescOnce sync.Once + file_model_proto_rawDescData = file_model_proto_rawDesc +) + +func file_model_proto_rawDescGZIP() []byte { + file_model_proto_rawDescOnce.Do(func() { + file_model_proto_rawDescData = protoimpl.X.CompressGZIP(file_model_proto_rawDescData) + }) + return file_model_proto_rawDescData +} + +var file_model_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_model_proto_goTypes = []interface{}{ + (*ModelArticles)(nil), // 0: sdbc.ModelArticles +} +var file_model_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_model_proto_init() } +func file_model_proto_init() { + if File_model_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_model_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ModelArticles); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_model_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_model_proto_goTypes, + DependencyIndexes: file_model_proto_depIdxs, + MessageInfos: file_model_proto_msgTypes, + }.Build() + File_model_proto = out.File + file_model_proto_rawDesc = nil + file_model_proto_goTypes = nil + file_model_proto_depIdxs = nil +} diff --git a/model.proto b/model.proto new file mode 100644 index 0000000..01cb6bf --- /dev/null +++ b/model.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package sdbc; + +option go_package = "./;sdbc"; + + +// @table_name: articles +message ModelArticles { + int64 id = 1; // 主键ID + string title = 2; // 标题 + string avatar_url = 5; // 封面 + string phone = 6; // 手机号码 + int64 create_time = 12; // 创建时间 + int64 update_time = 13; // 更新时间 +} diff --git a/scope.go b/scope.go new file mode 100644 index 0000000..e9881a6 --- /dev/null +++ b/scope.go @@ -0,0 +1,50 @@ +package sdbc + +import ( + "reflect" + + "google.golang.org/protobuf/proto" +) + +// model props for protobuf Message +type model struct { + Type proto.Message // model prototype + modelKind reflect.Type // model low level type + modelName string // model struct name + tableName string // model mapping table name +} + +type scope struct { + *model + *sdbc +} + +func (s *scope) Insert() *insert { + return &insert{ + scope: s, + sdbc: s.sdbc, + } + +} + +func (s *scope) Find() { + //TODO implement me + panic("implement me") +} + +func (s *scope) Update() { + //TODO implement me + panic("implement me") +} + +func (s *scope) Delete() { + //TODO implement me + panic("implement me") +} + +type Operator interface { + Insert() *insert + Find() + Update() + Delete() +} diff --git a/sdbc.go b/sdbc.go new file mode 100644 index 0000000..4965da6 --- /dev/null +++ b/sdbc.go @@ -0,0 +1,90 @@ +package sdbc + +import ( + "github.com/glebarez/sqlite" + "github.com/sirupsen/logrus" + "gitter.top/common/lormatter" + "google.golang.org/protobuf/proto" + "gorm.io/gorm" + "reflect" + "time" +) + +func init() { + var formatter = &lormatter.Formatter{ + ShowTime: true, + } + formatter.Register() +} + +type tabler interface { + TableName() string +} + +type Config struct { + Dbname string // 数据库名 + MaxIdleConn int // 最大空闲连接 + MaxOpenConn int // 最大连接 + MaxLifetime time.Duration // 最大生存时长 +} + +type sdbc struct { + dbname string + client *gorm.DB +} + +func NewSDBC(cfg *Config) *sdbc { + if cfg == nil { + logrus.Fatalf("config can not be nil") + } + var ( + driver = new(sdbc) + err error + ) + driver.client, err = gorm.Open(sqlite.Open(cfg.Dbname), &gorm.Config{}) + if err != nil { + logrus.Fatalf("open sqlite file failed: %v", err) + } + rawDB, err := driver.client.DB() + if err != nil { + logrus.Fatalf("get sqlite instance failed: %v", err) + } + if cfg.MaxOpenConn != 0 { + rawDB.SetMaxOpenConns(cfg.MaxOpenConn) + } + if cfg.MaxIdleConn != 0 { + rawDB.SetMaxIdleConns(cfg.MaxIdleConn) + } + if cfg.MaxLifetime != 0 { + rawDB.SetConnMaxLifetime(cfg.MaxLifetime) + } + driver.dbname = cfg.Dbname + return driver +} + +func (s *sdbc) GetClient() *gorm.DB { + return s.client +} + +func (s *sdbc) BindModel(prototype any) Operator { + if prototype == nil { + panic("model can not be nil") + } + if _, impled := prototype.(tabler); !impled { + panic("model does not implement tabler interface") + } + protomsg, impled := prototype.(proto.Message) + if !impled { + panic("model does not implement message interface") + } + scope := &scope{ + &model{ + Type: protomsg, + modelKind: reflect.TypeOf(protomsg), + modelName: string(protomsg.ProtoReflect().Descriptor().FullName()), + tableName: prototype.(tabler).TableName(), + }, + s, + } + return scope +}