File: ResultSet.st

package info (click to toggle)
gnu-smalltalk 3.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,688 kB
  • sloc: ansic: 87,424; sh: 22,729; asm: 8,465; perl: 4,513; cpp: 3,548; xml: 1,669; awk: 1,581; yacc: 1,357; makefile: 1,235; lisp: 855; lex: 843; sed: 258; objc: 124
file content (329 lines) | stat: -rw-r--r-- 7,641 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"=====================================================================
|
|   PosgreSQL DBI driver - ResultSet class
|
|
 ======================================================================"

"======================================================================
|
| Written by Mike Anderson gnu-smalltalk@gingerbread.plus.com 2006
| Based on PostgreSQL interface by Thomas Braun shin@shin.homelinux.net
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library 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 Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
 ======================================================================
"



ResultSet subclass: PGResultSet [
    | handle index rowCount columns columnsArray |
    
    <comment: nil>
    <category: 'DBI-Drivers'>

    PGResultSet class >> new: aCObject [
	<category: 'private'>
	^(self basicNew)
	    handle: aCObject;
	    yourself
    ]

    ResultStatus := #(#PGRES_EMPTY_QUERY
	#PGRES_COMMAND_OK
	#PGRES_TUPLES_OK
	#PGRES_COPY_OUT
	#PGRES_COPY_IN
	#PGRES_BAD_RESPONSE
	#PGRES_NONFATAL_ERROR
	#PGRES_FATAL_ERROR).

    PGResultSet class >> resultStatus: aCode [
	<category: 'constants'>
	^ResultStatus at: aCode + 1
    ]

    handle: aCObject [
	<category: 'private'>
	handle := aCObject.
	index := 0.
	rowCount := nil.
	columns := nil.
	self addToBeFinalized
    ]

    finalize [
	<category: 'private'>
	self primClear
    ]

    next [
	<category: 'cursor access'>
	| r |
	self atEnd ifTrue: [self error: 'No more rows'].
	"FIXME - This could be neater"
	r := PGRow in: self at: index.
	index := index + 1.
	^r
    ]

    atEnd [
	<category: 'cursor access'>
	^index >= self rowCount
    ]

    checkStatusForDo [
	<category: 'private'>
	(#(#PGRES_COMMAND_OK #PGRES_TUPLES_OK #PGRES_EMPTY_QUERY) 
	    includes: self resultStatus) ifFalse: [self error: self errorMessage]
    ]

    checkStatusForSelect [
	<category: 'private'>
	| stat |
	stat := self resultStatus.
	stat = #PGRES_TUPLES_OK 
	    ifFalse: 
		[| msg |
		stat = #PGRES_EMPTY_QUERY 
		    ifTrue: [self error: 'Empty query - no result set'].
		stat = #PGRES_COMMAND_OK 
		    ifTrue: [self error: 'Not a SELECT - no result set'].
		msg := self errorMessage.
		msg isEmpty ifTrue: [self error: stat].
		self error: msg]
    ]

    rawValueAtRow: aRowNum column: aColNum [
	"Answer a given result value at row aRowNum and column aColNum.
	 Both values 0-based."

	<category: 'private'>
	| v |
	v := handle
		    row: aRowNum
		    column: aColNum - 1.
	(v isEmpty and: 
		[(handle
		    isNullRow: aRowNum
		    column: aColNum - 1) = 1]) 
	    ifTrue: [v := nil].
	^v
    ]

    valueAtRow: aRowNum column: aColNum [
	<category: 'private'>
	^PGColumnInfo convert: (self rawValueAtRow: aRowNum column: aColNum)
	    type: (self columnsArray at: aColNum) type
    ]

    isSelect [
	<category: 'accessing'>
	^self resultStatus = #PGRES_TUPLES_OK
    ]

    isDML [
	<category: 'accessing'>
	^self resultStatus = #PGRES_COMMAND_OK
    ]

    position [
        <category: 'cursor access'>
        ^index
    ]

    position: anInteger [
        <category: 'cursor access'>
        (anInteger between: 0 and: self size)
            ifTrue: [ index := anInteger ] 
            ifFalse: [ SystemExceptions.IndexOutOfRange signalOn: self withIndex: anInteger ].
        ^index
    ]

    rowCount [
	<category: 'accessing'>
	self isSelect ifFalse: [super rowCount].
	rowCount isNil ifTrue: [rowCount := handle numTuples].
	^rowCount
    ]

    rowsAffected [
	<category: 'accessing'>
	self isDML ifFalse: [super rowsAffected].
	^handle numTuples
    ]

    columnsArray [
	<category: 'accessing'>
	columnsArray isNil 
	    ifTrue: 
		[| n |
		n := handle numFields.
		columnsArray := Array new: n.
		1 to: n do: [:i | columnsArray at: i put: (PGColumnInfo in: self at: i)]].
	^columnsArray
    ]

    columns [
	<category: 'accessing'>
	columns isNil 
	    ifTrue: 
		[| n |
		columns := LookupTable new: self columnsArray size.
		columnsArray do: [:col | columns at: col name put: col]].
	^columns
    ]

    columnNames [
	"Answer the names of the columns in this result set."

	<category: 'accessing'>
	^self columnsArray collect: [:col | col name]
    ]

    columnAt: aIndex [
	"Answer the name of a given column."

	<category: 'accessing'>
	^handle fieldName: aIndex - 1
    ]

    columnCount [
	"Answer the number of columns in the result set."

	<category: 'accessing'>
	^handle numFields
    ]

    rows [
	"This is slightly more efficient than the default method."

	<category: 'accessing'>
	| r n |
	n := self rowCount.
	r := WriteStream on: (Array new: n).
	0 to: n - 1 do: [:i | r nextPut: (PGRow in: self at: i)].
	^r contents
    ]

    resultStatus [
	"Answer the symbolic execution status."

	<category: 'PG specific'>
	^self class resultStatus: handle status
    ]

    errorMessage [
	<category: 'PG specific'>
	^handle errorMessage
    ]

    primClear [
	<category: 'PG specific'>
	handle clear
    ]

    release [
	"Clear the result set."

	<category: 'result set'>
	self removeToBeFinalized.
	self primClear
    ]

    columnTypeAt: aIndex [
	"Used by PGColumnInfo. Prefer (columns at: aName) type or (columnsArray at: aIndex) type"

	<category: 'PG specific'>
	^PGColumnInfo 
	    typeFromOid: (handle fieldType: aIndex - 1)
    ]

    columnSizeAt: aIndex [
	"Used by PGColumnInfo. Prefer (columns at: aName) size or (columnsArray at: aIndex) size"

	<category: 'PG specific'>
	^handle fieldSize: aIndex - 1
    ]
]


CObject subclass: PQResultSet [
    "Results"
    status [
        <cCall: 'PQresultStatus' returning: #int args: #( #self)>
    ]

    errorMessage [
        <cCall: 'PQresultErrorMessage' returning: #string args: #( #self)>
    ]

    clear [
        <cCall: 'PQclear' returning: #void args: #( #self)>
    ]

    "Result sets"
    numTuples [
        <cCall: 'PQntuples' returning: #int args: #( #self)>
    ]

    row: aRowNum column: aColNum [
        <cCall: 'PQgetvalue' returning: #string args: #( #self #int #int)>
    ]

    isNullRow: aRowNum column: aColNum [
        <cCall: 'PQgetisnull' returning: #int args: #( #self #int #int)>
    ]

    "DML results"
    rowsAffected [
        <cCall: 'PQcmdTuples' returning: #int args: #( #self)>
    ]
	
    lastOid [
        <cCall: 'PQoidValue' returning: #uInt args: #( #self)>
    ]

    "Column info"
    numFields [
        <cCall: 'PQnfields' returning: #int args: #( #self)>
    ]

    fieldName: aColNum [
        <cCall: 'PQfname' returning: #string args: #( #self #int)>
    ]

    fieldIsBinary: aColNum [
        <cCall: 'PQfformat' returning: #int args: #( #self #int)>
    ]
    
    fieldType: aColNum [
        <cCall: 'PQftype' returning: #uInt "Oid" args: #( #self #int)>
    ]

    fieldMod: aColNum [
        <cCall: 'PQfmod' returning: #int "eg. precision or size" args: #( #self #int)>
    ]

    fieldSize: aColNum [
        <cCall: 'PQfsize' returning: #int args: #( #self #int)>
    ]

]