-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
214 lines (182 loc) · 6.84 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2019. Aberic - All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package lily
import (
"context"
"github.com/aberic/lily/api"
)
//var ServerURL = "localhost:19877"
// GetConf 获取数据库集合
func GetConf(serverURL string) (*Conf, error) {
res, err := getConf(serverURL, &api.ReqConf{})
conf := &Conf{}
conf.rpc2Conf(res.(*api.RespConf).Conf)
return conf, err
}
// ObtainDatabases 获取数据库集合
func ObtainDatabases(serverURL string) (*api.RespDatabases, error) {
res, err := obtainDatabases(serverURL, &api.ReqDatabases{})
if nil != err {
return nil, err
}
if nil == res {
return &api.RespDatabases{Code: api.Code_Success, Databases: []*api.Database{}}, nil
}
return res.(*api.RespDatabases), nil
}
// ObtainForms 获取数据库表集合
func ObtainForms(serverURL, dbName string) (*api.RespForms, error) {
res, err := obtainForms(serverURL, &api.ReqForms{DatabaseName: dbName})
if nil != err {
return nil, err
}
if nil == res {
return &api.RespForms{Code: api.Code_Success, Forms: []*api.Form{}}, nil
}
return res.(*api.RespForms), nil
}
// CreateDatabase 创建数据库
func CreateDatabase(serverURL, name, comment string) error {
_, err := createDatabase(serverURL, &api.ReqCreateDatabase{Name: name, Comment: comment})
return err
}
// CreateTable 创建表
func CreateTable(serverURL, dbName, name, comment string) error {
_, err := createForm(serverURL, &api.ReqCreateForm{DatabaseName: dbName, Name: name, Comment: comment, FormType: FormatFormType2API(FormTypeSQL)})
return err
}
// CreateDoc 创建文档
func CreateDoc(serverURL, dbName, name, comment string) error {
_, err := createForm(serverURL, &api.ReqCreateForm{DatabaseName: dbName, Name: name, Comment: comment, FormType: FormatFormType2API(FormTypeDoc)})
return err
}
// PutD 新增数据
func PutD(serverURL, key, value string) (*api.RespPutD, error) {
res, err := putD(serverURL, &api.ReqPutD{Key: key, Value: []byte(value)})
if nil != err {
return nil, err
}
return res.(*api.RespPutD), err
}
// SetD 新增数据
func SetD(serverURL, key, value string) (*api.RespSetD, error) {
res, err := setD(serverURL, &api.ReqSetD{Key: key, Value: []byte(value)})
if nil != err {
return nil, err
}
return res.(*api.RespSetD), err
}
// GetD 获取数据
func GetD(serverURL, key string) (*api.RespGetD, error) {
res, err := getD(serverURL, &api.ReqGetD{Key: key})
if nil != err {
return nil, err
}
return res.(*api.RespGetD), err
}
// Put 新增数据
func Put(serverURL, databaseName, formName, key, value string) (*api.RespPut, error) {
res, err := put(serverURL, &api.ReqPut{DatabaseName: databaseName, FormName: formName, Key: key, Value: []byte(value)})
if nil != err {
return nil, err
}
return res.(*api.RespPut), err
}
// Set 新增数据
func Set(serverURL, databaseName, formName, key, value string) (*api.RespSet, error) {
res, err := set(serverURL, &api.ReqSet{DatabaseName: databaseName, FormName: formName, Key: key, Value: []byte(value)})
if nil != err {
return nil, err
}
return res.(*api.RespSet), err
}
// Get 获取数据
func Get(serverURL, databaseName, formName, key string) (*api.RespGet, error) {
res, err := get(serverURL, &api.ReqGet{DatabaseName: databaseName, FormName: formName, Key: key})
if nil != err {
return nil, err
}
return res.(*api.RespGet), err
}
// Select 获取数据
func Select(serverURL, databaseName, formName string, selector *api.Selector) (*api.RespSelect, error) {
res, err := query(serverURL, &api.ReqSelect{DatabaseName: databaseName, FormName: formName, Selector: selector})
return res.(*api.RespSelect), err
}
// Remove 删除数据
func Remove(serverURL, databaseName, formName, key string) (*api.Resp, error) {
res, err := remove(serverURL, &api.ReqRemove{DatabaseName: databaseName, FormName: formName, Key: key})
return res.(*api.Resp), err
}
// Delete 删除数据
func Delete(serverURL, databaseName, formName string, selector *api.Selector) (*api.Resp, error) {
res, err := del(serverURL, &api.ReqDelete{DatabaseName: databaseName, FormName: formName, Selector: selector})
return res.(*api.Resp), err
}
// getConf 获取数据库引擎对象
func getConf(serverURL string, req *api.ReqConf) (interface{}, error) {
return getClient(serverURL).GetConf(context.Background(), req)
}
// obtainDatabases 获取数据库集合
func obtainDatabases(serverURL string, req *api.ReqDatabases) (interface{}, error) {
return getClient(serverURL).ObtainDatabases(context.Background(), req)
}
// obtainForms 获取数据库表集合
func obtainForms(serverURL string, req *api.ReqForms) (interface{}, error) {
return getClient(serverURL).ObtainForms(context.Background(), req)
}
// createDatabase 创建数据库
func createDatabase(serverURL string, req *api.ReqCreateDatabase) (interface{}, error) {
return getClient(serverURL).CreateDatabase(context.Background(), req)
}
// createForm 创建表
func createForm(serverURL string, req *api.ReqCreateForm) (interface{}, error) {
return getClient(serverURL).CreateForm(context.Background(), req)
}
// putD 新增数据
func putD(serverURL string, req *api.ReqPutD) (interface{}, error) {
return getClient(serverURL).PutD(context.Background(), req)
}
// setD 新增数据
func setD(serverURL string, req *api.ReqSetD) (interface{}, error) {
return getClient(serverURL).SetD(context.Background(), req)
}
// getD 获取数据
func getD(serverURL string, req *api.ReqGetD) (interface{}, error) {
return getClient(serverURL).GetD(context.Background(), req)
}
// put 新增数据
func put(serverURL string, req *api.ReqPut) (interface{}, error) {
return getClient(serverURL).Put(context.Background(), req)
}
// set 新增数据
func set(serverURL string, req *api.ReqSet) (interface{}, error) {
return getClient(serverURL).Set(context.Background(), req)
}
// get 获取数据
func get(serverURL string, req *api.ReqGet) (interface{}, error) {
return getClient(serverURL).Get(context.Background(), req)
}
// query 获取数据
func query(serverURL string, req *api.ReqSelect) (interface{}, error) {
return getClient(serverURL).Select(context.Background(), req)
}
// remove 删除数据
func remove(serverURL string, req *api.ReqRemove) (interface{}, error) {
return getClient(serverURL).Remove(context.Background(), req)
}
// del 删除数据
func del(serverURL string, req *api.ReqDelete) (interface{}, error) {
return getClient(serverURL).Delete(context.Background(), req)
}