1
1
local request = require (" drive.postgres.request" )
2
2
3
- local postgresDrive = {}
3
+ local postgresDrive = {
4
+ threads = {},
5
+ currentJobs = {}
6
+ }
4
7
5
- postgresDrive .threads = {}
8
+ --
9
+ -- private
10
+ --
6
11
7
- postgresDrive .currentJobs = {}
8
-
9
- function postgresDrive .ThreadWork (input , output )
12
+ local function ThreadWork (input , output )
10
13
local Run = require (" drive.postgres.thread" )
11
14
Run (input , output )
12
15
end
13
16
14
- function postgresDrive . Initiate ()
15
- for i = 1 , config .postgres .threadCount do
16
- local threadId = threadHandler .CreateThread (postgresDrive . ThreadWork )
17
+ local function Initiate ()
18
+ for _ = 1 , config .postgres .threadCount do
19
+ local threadId = threadHandler .CreateThread (ThreadWork )
17
20
table.insert (postgresDrive .threads , threadId )
18
21
postgresDrive .currentJobs [threadId ] = 0
19
22
end
@@ -41,7 +44,10 @@ function postgresDrive.Initiate()
41
44
local doneMigrations = postgresDrive .QueryAsync ([[ SELECT * FROM migrations]] )
42
45
local doneTable = {}
43
46
if doneMigrations .error then
44
- tes3mp .LogMessage (enumerations .log .INFO , " [Postgres] Seeding database for the first time, ignore the SQL error above!" )
47
+ tes3mp .LogMessage (
48
+ enumerations .log .INFO ,
49
+ " [Postgres] Seeding database for the first time, ignore the SQL error above!"
50
+ )
45
51
ProcessMigration (0 , " 0000_migrations" )
46
52
else
47
53
for i = 1 , doneMigrations .count do
@@ -57,15 +63,15 @@ function postgresDrive.Initiate()
57
63
end
58
64
end
59
65
60
- function postgresDrive . ProcessResponse (res )
66
+ local function ProcessResponse (res )
61
67
if res .error then
62
68
tes3mp .LogMessage (enumerations .log .ERROR , " [Postgres] [[" .. res .error .. " ]]" )
63
69
elseif res .log then
64
70
tes3mp .LogMessage (enumerations .log .VERBOSE , " [Postgres] [[" .. res .log .. " ]]" )
65
71
end
66
72
end
67
73
68
- function postgresDrive . ChooseThread ()
74
+ local function ChooseThread ()
69
75
local minThread = postgresDrive .threads [1 ]
70
76
local min = postgresDrive .currentJobs [minThread ]
71
77
for _ , thread in pairs (postgresDrive .threads ) do
@@ -80,49 +86,53 @@ function postgresDrive.ChooseThread()
80
86
return minThread
81
87
end
82
88
83
- function postgresDrive . StartJob (thread )
89
+ local function StartJob (thread )
84
90
postgresDrive .currentJobs [thread ] = postgresDrive .currentJobs [thread ] + 1
85
91
end
86
92
87
- function postgresDrive . FinishJob (thread )
93
+ local function FinishJob (thread )
88
94
postgresDrive .currentJobs [thread ] = postgresDrive .currentJobs [thread ] - 1
89
95
if postgresDrive .currentJobs [thread ] < 0 then
90
96
postgresDrive .currentJobs [thread ] = 0
91
97
end
92
98
end
93
99
94
- function postgresDrive . Send (thread , req , callback )
95
- postgresDrive . StartJob (thread )
100
+ local function Send (thread , req , callback )
101
+ StartJob (thread )
96
102
threadHandler .Send (
97
103
thread ,
98
104
req ,
99
105
function (res )
100
- postgresDrive . FinishJob (thread )
101
- postgresDrive . ProcessResponse (res )
106
+ FinishJob (thread )
107
+ ProcessResponse (res )
102
108
if callback ~= nil then
103
109
callback (res )
104
110
end
105
111
end
106
112
)
107
113
end
108
114
109
- function postgresDrive . SendAsync (thread , req )
110
- postgresDrive . StartJob (thread )
115
+ local function SendAsync (thread , req )
116
+ StartJob (thread )
111
117
local res = threadHandler .SendAsync (
112
118
thread ,
113
119
req
114
120
)
115
- postgresDrive . FinishJob (thread )
116
- postgresDrive . ProcessResponse (res )
121
+ FinishJob (thread )
122
+ ProcessResponse (res )
117
123
return res
118
124
end
119
125
126
+ --
127
+ -- public
128
+ --
129
+
120
130
function postgresDrive .Connect (connectString )
121
131
local results = {}
122
132
for _ , thread in pairs (postgresDrive .threads ) do
123
133
table.insert (
124
134
results ,
125
- postgresDrive . SendAsync (thread , request .Connect (connectString ))
135
+ SendAsync (thread , request .Connect (connectString ))
126
136
)
127
137
end
128
138
return results
@@ -132,7 +142,7 @@ function postgresDrive.ConnectAsync(connectString, timeout)
132
142
local tasks = {}
133
143
for _ , thread in pairs (postgresDrive .threads ) do
134
144
table.insert (tasks , function ()
135
- return postgresDrive . SendAsync (thread , request .Connect (connectString ))
145
+ return SendAsync (thread , request .Connect (connectString ))
136
146
end )
137
147
end
138
148
return async .WaitAll (tasks , timeout )
@@ -143,7 +153,7 @@ function postgresDrive.Disconnect()
143
153
for _ , thread in pairs (postgresDrive .threads ) do
144
154
table.insert (
145
155
results ,
146
- postgresDrive . SendAsync (thread , request .Disconnect ())
156
+ SendAsync (thread , request .Disconnect ())
147
157
)
148
158
end
149
159
return results
@@ -153,31 +163,31 @@ function postgresDrive.DisconnectAsync()
153
163
local tasks = {}
154
164
for _ , thread in pairs (postgresDrive .threads ) do
155
165
table.insert (tasks , function ()
156
- return postgresDrive . SendAsync (thread , request .Disconnect ())
166
+ return SendAsync (thread , request .Disconnect ())
157
167
end )
158
168
end
159
169
return async .WaitAll (tasks )
160
170
end
161
171
162
172
function postgresDrive .Query (sql , parameters , callback , numericalIndices )
163
- local thread = postgresDrive . ChooseThread ()
173
+ local thread = ChooseThread ()
164
174
if numericalIndices then
165
- postgresDrive . Send (thread , request .QueryNumerical (sql , parameters ), callback )
175
+ Send (thread , request .QueryNumerical (sql , parameters ), callback )
166
176
else
167
- postgresDrive . Send(thread , request .Query (sql , parameters ), callback )
177
+ Send (thread , request .Query (sql , parameters ), callback )
168
178
end
169
179
end
170
180
171
181
function postgresDrive .QueryAsync (sql , parameters , numericalIndices )
172
- local thread = postgresDrive . ChooseThread ()
182
+ local thread = ChooseThread ()
173
183
if numericalIndices then
174
- return postgresDrive . SendAsync (thread , request .QueryNumerical (sql , parameters ))
184
+ return SendAsync (thread , request .QueryNumerical (sql , parameters ))
175
185
else
176
- return postgresDrive . SendAsync (thread , request .Query (sql , parameters ))
186
+ return SendAsync (thread , request .Query (sql , parameters ))
177
187
end
178
188
end
179
189
180
- postgresDrive . Initiate ()
190
+ Initiate ()
181
191
182
192
-- make sure the disconnect handler is the every last
183
193
customEventHooks .registerHandler (" OnServerPostInit" , function (eventStatus )
0 commit comments