File: _query.lua

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (246 lines) | stat: -rw-r--r-- 5,759 bytes parent folder | download | duplicates (4)
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
-- ----------------------------------------------------------------------------------------
-- Copyright (C) 2005, 2006 MySQL AB
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-- ----------------------------------------------------------------------------------------


-- ----------------------------------------------------------------------------------------
-- @file _query.lua
-- @brief This library contains the query class
-- ----------------------------------------------------------------------------------------

Query= {
    grtQuery= nil,
    grtRes= nil
  }
  
function Query:new(args)
  qryObj= {}
  
  -- set meta table to inherit parent class behaviour
  setmetatable(qryObj, self)
  self.__index = self
  
  -- check arguments
  local name= "Query"
  local argCount= 0

  if type(args) == "string" then
    qryObj.grtQuery= grtV.newObj("db.query.Query", name, "", "")
    qryObj:setSql(args)
  else
    if args ~= nil then
      argCount= table.getn(args)
      if argCount >= 2 then
        name= args[2]
      end
    end
    
    if argCount >= 1 then
      if type(args[1]) == "string" then
        qryObj.grtQuery= grtV.newObj("db.query.Query", name, "", "")
        qryObj:setSql(args[1])
      else
        qryObj.grtQuery= args[1]
      end
    else
      qryObj.grtQuery= grtV.newObj("db.query.Query", name, "", "")
    end
  end
  
  return qryObj
end

function Query:close()
  RdbmsResultSet:close({self.grtRes})
end

function Query:setSql(sql)
  self.grtQuery.sql= sql
end


function Query:getSql()
  return self.grtQuery.sql
end


function Query:setConnection(conn)
  if (conn ~= nil) and (grtS.get(conn) ~= "db.mgmt.Connection") then
    return grt.error("This function takes a db.mgmt.Connection as parameter.")
  end
  
  if conn._id == nil then
    conn._id= grt.newGuid()
  end

  -- make sure the conn is in the lookup cache
  grtV.lookupAdd(conn)
  
  self.grtQuery.connection= conn
  
  -- get query module
  local queryModule= conn.modules.QueryModule
  if queryModule == nil then
    queryModule= self:getConnection().driver.defaultModules.QueryModule
  end
  
  self.grtQuery.moduleName= queryModule
end


function Query:getConnection()
  return self.grtQuery.connection
end


function Query:print(conn)
  if conn ~= nil then
    self:setConnection(conn)
  end
  
  if self.grtQuery.moduleName == nil then
    return grt.error("The connection does not have a query module.")
  end
  
  grtM.callFunction(grtV.toLua(self.grtQuery.moduleName), "queryPrint", 
    {self:getConnection(), self:getSql()})
end


function Query:execute(conn, bufferSize)
  if conn ~= nil then
    self:setConnection(conn)
  end
  
  if self.grtQuery.moduleName == nil then
    return grt.error("The connection does not have a query module.")
  end
  
  self.grtRes= RdbmsResultSet:open({self.grtQuery, bufferSize})
  print(self.grtQuery.moduleName)
  grtM.callFunction(grtV.toLua(self.grtQuery.moduleName), "queryFetchResultSet", {self.grtRes})
  
  return self.grtRes
end


function Query:executeAndFetch(conn, bufferSize)
  self:execute(conn, bufferSize)
  
  while(qry2:fetchNextBlock()) do 
  end
end

function Query:fetchNextBlock()
  if self.grtRes ~= nil then
    grtM.callFunction(grtV.toLua(self.grtQuery.moduleName), "queryFetchResultSet", {self.grtRes})
  end
  
  if (self:status() == 2) then
    return true
  else
    return false
  end
end


function Query:currentRowCount()
  if self.grtRes ~= nil then
    return grtV.toLua(RdbmsResultSet:currentRowCount({self.grtRes}))
  else
    return 0
  end
end


function Query:status()
  if self.grtRes ~= nil then
    return grtV.toLua(RdbmsResultSet:status({self.grtRes}))
  else
    return 0
  end
end


function Query:fieldAsString(index)
  if self.grtRes ~= nil then
    return grtV.toLua(RdbmsResultSet:fieldAsString({self.grtRes, index - 1}))
  else
    return ""
  end
end


function Query:moveNext()
  if self.grtRes ~= nil then
    if grtV.toLua(RdbmsResultSet:moveNext({self.grtRes})) == 1 then
      return true
    else
      return false
    end
  else
    return false
  end
end


function Query:movePrior()
  if self.grtRes ~= nil then
    if grtV.toLua(RdbmsResultSet:movePrior({self.grtRes})) == 1 then
      return true
    else
      return false
    end
  else
    return false
  end
end


function Query:moveFirst()
  if self.grtRes ~= nil then
    if grtV.toLua(RdbmsResultSet:moveFirst({self.grtRes})) == 1 then
      return true
    else
      return false
    end
  else
    return false
  end
end


function Query:moveLast()
  if self.grtRes ~= nil then
    if grtV.toLua(RdbmsResultSet:moveLast({self.grtRes})) == 1 then
      return true
    else
      return false
    end
  else
    return false
  end
end


function Query:columnCount()
  if self.grtRes ~= nil then
    return grtV.getn(self.grtRes.columns)
  else
    return 0
  end
end