forked from cfadmin-cn/mongo
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
Latest commit
247 lines (227 loc) · 10.9 KB
/
Copy pathinit.lua
File metadata and controls
247 lines (227 loc) · 10.9 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
localprotocol=require"mongo.protocol"
-- 协议交互
localrequest_count=protocol.request_count
localrequest_query=protocol.request_query
localrequest_update=protocol.request_update
localrequest_insert=protocol.request_insert
localrequest_delete=protocol.request_delete
localrequest_mapreduce=protocol.request_mapreduce
localrequest_aggregate=protocol.request_aggregate
localrequest_getindexes=protocol.request_getindexes
localrequest_dropindexes=protocol.request_dropindexes
localrequest_createindex=protocol.request_createindex
-- 握手
localrequest_auth=protocol.request_auth
localrequest_handshake=protocol.request_handshake
localtoint=math.tointeger
localtonumber=tonumber
localfmt=string.format
localtcp=require"internal.TCP"
localgridfs=require"mongo.gridfs"
localclass=require"class"
---@classMongoDB @`MongoDB`对象
localmongo=class("MongoDB")
functionmongo:ctor(opt)
self.reqid=1
self.SSL=opt.SSL
self.db=opt.dbor"admin"
self.host=opt.hostor"localhost"
self.port=opt.portor27017
self.username=opt.username
self.password=opt.password
self.auth_mode=opt.auth_modeor"SCRAM-SHA-1"
self.sock=tcp:new()
self.gridfs=gridfs:new({ctx=self})
self.have_transaction=false
self.connected=false
end
functionmongo:set_timeout(timeout)
ifself.sockandtonumber(timeout) then
self.sock._timeout=timeout
end
end
---comment 连接服务器
functionmongo:connect()
ifself.connectedthen
returntrue, "already connected."
end
ifnotself.sockthen
self.sock=tcp:new()
end
localok, err
ok, err=self.sock:connect(self.host, self.port)
ifnotokthen
returnfalse, err
end
ifself.SSLthen
ok, err=self.sock:ssl_handshake()
ifnotokthen
returnfalse, error"Mongo SSL handshake failed."
end
end
ok, err=request_handshake(self)
ifnotokthen
returnfalse, err
end
ok, err=request_auth(self)
ifnotokthen
returnfalse, err
end
self.reqid=self.reqid+1
self.connected=true
returntrue
end
---comment 查询数据
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@paramfiltertable @需要执行查询的条件
---@paramoptiontable @需要查询的可选参数(`cursor`/`limit`/`skip`/`sort`)
---@returntable, integer | nil, string @成功返回结果数据与游标`id`, 失败返回`false`与出错信息;
functionmongo:find(database, collect, filter, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid find collect or database.")
localtab, err=request_query(self, database, collect, filter, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.cursor.firstBatchortab.cursor.nextBatch, tab.cursor.id
end
---comment 插入数据
---@paramdatabasestring @需要插入的数据库名称
---@paramcollectstring @需要插入的集合名称
---@paramdocumentstable @需要插入的文档数组
---@paramoptiontable @需要插入的文档可选参数(`ordered`)
functionmongo:insert(database, collect, documents, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid insert collect or database.")
assert(type(documents) =='table' and#documents>0andtype(documents[1]) =="table", "Invalid insert documents.")
localtab, err=request_insert(self, database, collect, documents, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
return { acknowledged= (tab['ok'] ==1ortab['ok'] ==true) andtrueorfalse, insertedCount=toint(tab['n']) }
end
---comment 修改数据
---@paramdatabasestring @需要修改的数据库名称
---@paramcollectstring @需要修改的集合名称
---@paramfiltertable @需要更新的过滤条件
---@paramupdatetable @需要更新的文档结果
---@paramoptiontable @需要更新的可选参数(`upsert`/`multi`)
functionmongo:update(database, collect, filter, update, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid update collect or database.")
assert(type(filter) =='table', "Invalid update filter.")
localtab, err=request_update(self, database, collect, filter, update, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
return{ acknowledged= (tab['ok'] ==1ortab['ok'] ==true) andtrueorfalse, matchedCount=toint(tab['n']), modifiedCount=toint(tab['nModified']) }
end
---comment 删除数据
---@paramdatabasestring @需要删除的数据库名称
---@paramcollectstring @需要删除的集合名称
---@paramfiltertable @需要删除的过滤条件
---@paramoptiontable @需要删除的可选参数(`one`)
functionmongo:delete(database, collect, filter, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid delete collect or database.")
assert(type(filter) =='table', "Invalid delete filter.")
localtab, err=request_delete(self, database, collect, filter, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
return { acknowledged= (tab['ok'] ==1ortab['ok'] ==true) andtrueorfalse, deletedCount=toint(tab['n']), }
end
---comment COUNT - 聚合函数
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@paramfiltertable @需要执行查询的条件
---@paramoptiontable @需要查询的可选参数
---@returntable, integer | nil, string @成功返回结果数据与游标`id`, 失败返回`false`与出错信息;
functionmongo:count(database, collect, filter, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid count collect or database.")
localtab, err=request_count(self, database, collect, filter, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
return { acknowledged=true , count=tab.cursor.firstBatch[1].n }, tab.cursor.id
end
---comment AGGREGATE - 聚合函数
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@paramfiltertable @需要执行查询的条件
---@paramoptiontable @需要查询的可选参数
---@returntable, integer | nil, string @成功返回结果数据与游标`id`, 失败返回`false`与出错信息;
functionmongo:aggregate(database, collect, filter, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid aggregate collect or database.")
localtab, err=request_aggregate(self, database, collect, filter, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.cursor.firstBatchortab.cursor.nextBatch, tab.cursor.id
end
---comment MapReduce - 计算函数
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@parammapstring @需要查询的映射函数(`javascript function`)
---@paramreducestring @需要查询的统计函数(`javascript function`)
---@paramoptiontable @需要查询的条件(`query`/`limit`/`sort`/`out`)
---@returntable | nil, string @成功返回结果数据与游标`id`, 失败返回`false`与出错信息;
functionmongo:mapreduce(database, collect, map, reduce, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid mapreduce collect or database.")
assert(type(map) =='string' andmap~='' andtype(reduce) =='string' andreduce~='', "Invalid Map or Reduce function.")
localtab, err=request_mapreduce(self, database, collect, map, reduce, option)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.results
end
---comment 创建索引
---@paramdatabasestring @需要指定的数据库名称
---@paramcollectstring @需要指定的集合名称
---@paramindexestable @索引名称与内容
---@paramoptiontable @索引的额外参数(`background`/`unique`等)
---@returntable, nil | nil, string @成功返回结果创建内容, 失败返回`false`与出错信息;
functionmongo:create_indexes(database, collect, indexes, option)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid Indexed collect or database.")
assert(type(indexes) =='table', "Invalid Index type.")
localtab, err=request_createindex(self, database, collect, indexes, optionor {})
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab
end
---comment 获取索引
---@paramdatabasestring @需要指定的数据库名称
---@paramcollectstring @需要指定的集合名称
---@returntable, nil | nil, string @成功返回结果数据, 失败返回`false`与出错信息;
functionmongo:get_indexes(database, collect)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid Indexed collect or database.")
localtab, err=request_getindexes(self, database, collect)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.cursor.firstBatch
end
---comment 删除索引
---@paramdatabasestring @需要指定的数据库名称
---@paramcollectstring @需要指定的集合名称
---@paramindexnamestring @需要删除的索引名称
functionmongo:drop_indexes(database, collect, indexname)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid Indexed collect or database.")
assert(type(indexname) =='string', "Invalid Index name.")
localtab, err=request_dropindexes(self, database, collect, indexname)
ifnottabortab.errmsgthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab
end
---comment 关闭连接
functionmongo:close()
self.connected=false
ifself.sockthen
self.sock:close()
self.sock=nil
end
ifself.gridfsthen
self.gridfs:close()
self.gridfs=nil
end
end
returnmongo