View difference between Paste ID: zL7r6UQY and FH0fj30W
SHOW: | | - or go back to the newest paste.
1
require("CLRPackage")
2
--
3
import "System.Collections"
4
import "System.Collections.Generic"
5
import "System.Data"
6
import "System.Data.SqlClient"
7
--
8
sqlServer = {} -- CLASS OBJECT
9
--
10
function sqlServer.ctor(instance, database, modeFlag) -- Constructor
11
--
12
-- NOTE: modeFlag is not implemented at the moment.
13-
--       It's purpose will be to forced the connection to close
13+
--       It's purpose will be to force the connection to close
14
--       after each transaction. Right now the connection must
15
--       be opened and closed manually.
16
--
17
	local self = {} -- PUBLIC FUNCTIONS CONTAINER
18
	
19
    -- #PRIVATE FIELDS
20
	cmd      = nil
21
	conn     = nil
22
	connStr  = ""
23
	database = database or ""
24
	dQuote   = "\""	
25
	errormsg = ""	
26
	instance = instance or ""
27
	mode     = modeFlag or false
28
	null     = "\"\""
29
	provider = "sqlServer"	
30
	useTable = true
31
	version  = "N/A"
32
	--
33
	csPattern = "Server=%s;Trusted_Connection=yes;connection timeout=30;Initial Catalog=%s"
34
	--
35
	-- #PRIVATE METHODS
36
	--
37
	function setConnStr(instance, database)
38
		
39
		if instance ~= nil then self.instance = instance end
40
		
41
		if database ~= nil then self.database = database end
42
43
	end	
44
	
45
	function setParams(	args)
46
47
		cmd.Parameters:Clear()
48
49
		local ok = true
50
51
		if type(args) == "table" then
52
			for k, v in pairs(args)
53
			do
54
				if type(v) ~= "string" then v = ""..v end			
55
				ok, errormsg = pcall(function() cmd.Parameters:AddWithValue(k, value) end)
56
			end
57
		end
58
59
		return ok
60
61
	end
62
	
63
	function getData(dr)
64
65
		local record, index, h = {}, 0 
66
	
67
		if dr.HasRows == true then
68
			repeat
69
				while (dr:Read() == true)
70
					do
71
						h = {}
72
						for i = 0, dr.FieldCount - 1 
73
							do
74
								if useTable then h[dr:GetName(i)] = dr:GetValue(i)
75
									else h[i] = dr:GetValue(i)
76
								end
77
							end
78
						record[index] = h
79
						index = index + 1
80
					end		
81
			until (dr:NextResult() == false)	
82
		end
83
	
84
		_, errormsg = pcall(function() dr:Close() end)
85
	
86
		return record
87
88
	end
89
	--
90
	-- #PUBLIC METHODS
91
	--	
92
	self.isOpen = function()
93
	
94
		if type(conn) == "userdata" and conn.State == ConnectionState.Open then
95
			return true
96
		end
97
			
98
		return false
99
		
100
	end
101
	
102
	self.ErrorMsg = function() return errormsg end
103
	
104
	self.Instance = function() return instance end
105
	
106
	self.Database = function() return database end
107
	
108
	self.ConnStr  = function() return connStr  end
109
	
110
	self.Version  = function() return version  end
111
	
112
	self.Provider = function() return provider end
113
	
114
	self.setDatabase = function(d)
115
	
116
		database = ""
117
		
118
		if type(d) == "string" then database = d end
119
		
120
		self.Close()
121
		
122
	end
123
	
124
	self.setInstance = function(i)
125
	
126
		instance = ""
127
		
128
		if type(i) == "string" then instance = i end
129
		
130
		self.Close()
131
		
132
	end
133
	
134
	self.UseTable = function(flag)
135
	
136
		if flag == nil then return useTable end
137
		
138
		if type(flag) == "boolean" then useTable = flag end
139
		
140
	end
141
	
142
	self.Null = function(str)
143
	
144
		if str == nil then return null end
145
		
146
		if type(flag) == "string" then null = str end
147
		
148
	end	
149
	
150
	function self.Open()
151
	
152
		mode = modeFlag or mode
153
	
154
		self.Close()
155
156
		connStr = csPattern:format(instance, database)
157
		
158
		local ok
159
	
160
		ok, errormsg = pcall(function() conn = SqlConnection(connStr) end)
161
		
162
		if ok ~= true then return ok end
163
		
164
		ok, errormsg = pcall(function() conn:Open() end)
165
		
166
		if ok then version = conn.ServerVersion
167
			else version = "N/A"			
168
		end
169
		
170
		return ok
171
		
172
	end
173
	
174
	function self.Close()
175
	
176
		local ok
177
		
178
		if type(conn) == "userdata" and conn.State == ConnectionState.Open then
179
			ok, errormsg = pcall(function() conn:Close() end)
180
		end
181
		
182
		return ok
183
		
184
	end
185
	
186
	function self.execNonQuery(sql, args)
187
	
188
		errormsg = ""
189
		
190
		cmd = SqlCommand(sql, conn)
191
		
192
		cmd.CommandType = CommandType.Text
193
		
194
		local rc, ok
195
		
196
		ok = setParams(args)
197
		
198
		if ok == false then return -1 end		
199
		
200
		_, errormsg = pcall(function() rc = cmd:ExecuteNonQuery() end)
201
		
202
		return rc
203
	
204
	end
205
	
206
	function self.execProc(sql, args)
207
	
208
		errormsg = ""
209
		
210
		cmd = SqlCommand(sql, conn)
211
		
212
		cmd.CommandType = CommandType.StoredProcedure
213
		
214
		local dr, ok
215
216
		ok = setParams(args)
217
		
218
		if ok == false then return null end		
219
		
220
		ok, errormsg = pcall(function() dr = cmd:ExecuteReader() end)
221
		
222
		if ok then return getData(dr) end
223
		
224
		return null
225
	
226
	end
227
	
228
	function self.execProcNR(sql, args)
229
	
230
		errormsg = ""
231
		
232
		cmd = SqlCommand(sql, conn)
233
		
234
		cmd.CommandType = CommandType.StoredProcedure
235
		
236
		local rc, ok
237
		
238
		ok = setParams(args)
239
		
240
		if ok == false then return -1 end		
241
		
242
		_, errormsg = pcall(function() rc = cmd:ExecuteNonQuery() end)
243
		
244
		return rc
245
	
246
	end
247
	
248
	function self.execScalar(sql, args)
249
	
250
		errormsg = ""
251
		
252
		cmd = SqlCommand(sql, conn)
253
		
254
		cmd.CommandType = CommandType.Text
255
		
256
		local result, ok
257
		
258
		ok = setParams(args)
259
		
260
		if ok == false then return null end		
261
		
262
		ok, errormsg = pcall(function() result = cmd:ExecuteScalar() end)
263
		
264
		if ok then return result end
265
		
266
		return null
267
	
268
	end
269
	
270
	function self.execSelect(sql, args)
271
	
272
		errormsg = ""
273
		
274
		cmd = SqlCommand(sql, conn)
275
		
276
		cmd.CommandType = CommandType.Text
277
		
278
		local dr, ok
279
		
280
		ok = setParams(args)
281
		
282
		if ok then ok, errormsg = pcall(function() dr = cmd:ExecuteReader() end)
283
			if ok then return getData(dr) end
284
		end
285
		
286
		return null
287
	
288
	end
289
290
	return self -- return public container to caller.
291
--
292
end