File: FetchInfo.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (341 lines) | stat: -rw-r--r-- 11,311 bytes parent folder | download
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
330
331
332
333
334
335
336
337
338
339
340
341
/*


    ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    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.
    ========== licence end


*/


package com.sap.dbtech.jdbc;

import java.sql.*;
import java.util.*;
import com.sap.dbtech.jdbc.translators.*;
import com.sap.dbtech.jdbc.packet.*;
import com.sap.dbtech.vsp001.*;

/**
 *
 */
public class FetchInfo
{

    private ConnectionSapDB     connection;            // current connection
    private String              cursorName;            // cursor
    private DBTechTranslator[]  columnInfo;            // short info of all columns
    private int                 recordSize;            // physical row size
    private Hashtable           columnMapping = null;  // mapping from column names to short infos
    private String              _fetchparamstring;     // cache for fetch parameters
    private boolean             packetEncodingUnicode = false;
//    private String[]            columnNames;


    /**
     * Constructor. Creates a new fetch info for a specific
     * previously issued command that will produce a result set.
     * If <code>infos</code> and/or <code>columnNames</code> is
     * null, a describe on the result table (<code>cursorName</code>)
     * is executed to retrieve the information.
     * @param connection the connection.
     * @param cursorName the name of the result table.
     * @param infos the short infos of the rows in the result.
     * @param columnNames the names of the rows in the result.
     * @exception java.sql.SQLException if a database error occurs.
     */
    public FetchInfo(ConnectionSapDB connection,
                     String cursorName,
                     DBTechTranslator[] infos,
                     String[] columnNames,
                     boolean apacketEncodingUnicode)
        throws SQLException
    {
        this.connection=connection;
        this.cursorName=cursorName;
        this.packetEncodingUnicode = apacketEncodingUnicode;
        if(infos==null || columnNames==null) {
//            describe();
          this.columnInfo = null;
          return;
        } else {
            setMetaData(infos, columnNames);
        }
    }


    /**
     * Executes a describe operation on the result table.
     * @exception java.sql.SQLException if a database error occurs.
     */
    private void describe()
        throws SQLException
    {
        ConnectionSapDB c=this.connection;
        DBTechTranslator[] infos=null;
        String[] columnNames=null;

        RequestPacket request=c.getRequestPacket(this.packetEncodingUnicode);
        request.initDbsCommand(false, "DESCRIBE \""+this.cursorName+"\"", ResultSet.TYPE_FORWARD_ONLY);
        ReplyPacket reply=c.execute(request, this, ConnectionSapDB.GC_ALLOWED);
        PartEnumeration parts=reply.partEnumeration();
        while(parts.hasMoreElements()) {
            parts.nextElement();
            int partKind=parts.partKind();
            if(partKind==PartKind.Columnnames_C) {
                columnNames=reply.parseColumnNames();
            } else if(partKind==PartKind.Shortinfo_C) {
                infos=reply.parseShortFields(this.connection.isSpaceoptionSet,false, null, false);
	        } else if(partKind==PartKind.Vardata_Shortinfo_C) {
	            infos=reply.parseShortFields(this.connection.isSpaceoptionSet,false, null, true);
	        }
        }
        this.setMetaData(infos, columnNames);
    }

    /**
     *
     * @exception java.sql.SQLException The exception description.
     */
    private void setMetaData (
            DBTechTranslator [] info,
            String [] colName)
            throws SQLException
    {
        int colCount = info.length;
        DBTechTranslator currentInfo;
        int currentFieldEnd;

        this.recordSize = 0;

        if (colCount == colName.length) {
            this.columnInfo = info;
            for (int i = 0; i < colCount; ++i) {
                currentInfo = info [i];
                currentInfo.setColName (colName [i]);
                currentInfo.setColIndex (i);
                currentFieldEnd = currentInfo.getPhysicalLength () + currentInfo.getBufpos () - 1;
                this.recordSize = Math.max (this.recordSize, currentFieldEnd);
            }
        }
        else {
          int outputColCnt = 0;
          this.columnInfo = new DBTechTranslator [colName.length];
          for (int i = 0; i < colCount; ++i) {
            if (info [i].isOutput()){
                currentInfo = this.columnInfo[outputColCnt] = info [i];
                currentInfo.setColName (colName [outputColCnt]);
                currentInfo.setColIndex (outputColCnt++);
              currentFieldEnd = currentInfo.getPhysicalLength () + currentInfo.getBufpos () - 1;
              this.recordSize = Math.max (this.recordSize, currentFieldEnd);
            }
          }
        }
    }


    private void setColMapping ()
    {
        int colCnt = this.columnInfo.length;
        this.columnMapping = new java.util.Hashtable (2 * colCnt);
        DBTechTranslator currentInfo;

        for (int i = 0; i < colCnt; i++) {
          currentInfo = this.columnInfo [i];
          this.columnMapping.put(currentInfo.getColumnName(), currentInfo);
        }
    }

    /**
     * Gets the cursor name.
     */
    public String getCursorName()
    {
        return this.cursorName;
    }


    /**
     * Executes an FETCH NEXT.
     * @param fetchSize The number of records to include in this fetch.
     * @return the reply packet returned from the database.
     */
    public ReplyPacket executeFetchNext(int fetchSize)
        throws SQLException
    {
        String cmd="FETCH NEXT \"" + this.cursorName +
            "\" INTO " + getFetchParamString();
        ReplyPacket r = executeFetchCommand(cmd, fetchSize);
        return r;
    }

    /**
     * Executes an FETCH ABSOLUTE.
     * @param position The absolute position.
     * @param fetchSize The number of records to include in this fetch.
     * @return the reply packet returned from the database.
     */
    public ReplyPacket executeFetchAbsolute(long position, int fetchSize)
        throws SQLException
    {
        String cmd="FETCH ABSOLUTE " + position + " \"" + this.cursorName +
            "\" INTO " + getFetchParamString();
        ReplyPacket r = executeFetchCommand(cmd, fetchSize);
        return r;
    }

     /**
      * Executes an FETCH RELATIVE.
      * @param position The relative position.
      * @param fetchSize The number of records to include in this fetch.
      * @return the reply packet returned from the database.
      */
     public ReplyPacket executeFetchRelative(long position, int fetchSize)
         throws SQLException
     {
         String cmd="FETCH RELATIVE " + position + " \"" + this.cursorName +
             "\" INTO " + getFetchParamString();
         return executeFetchCommand(cmd, fetchSize);
     }

    /**
     * Executes an FETCH FIRST.
     * @param fetchSize The number of records to include in this fetch.
     * @return the reply packet returned from the database.
     */
    public ReplyPacket executeFetchFirst(int fetchSize)
        throws SQLException
    {
        String cmd="FETCH FIRST \"" + this.cursorName +
            "\" INTO " + getFetchParamString();
        return executeFetchCommand(cmd, fetchSize);
    }


    /**
     * Executes an FETCH LAST.
     * @param fetchSize The number of records to include in this fetch.
     * @return the reply packet returned from the database.
     */
    public ReplyPacket executeFetchLast(int fetchSize)
        throws SQLException
    {
        String cmd="FETCH LAST \"" + this.cursorName +
            "\" INTO " + getFetchParamString();
        return executeFetchCommand(cmd, fetchSize);
    }

    boolean marker=false;

    /**
     * Executes a given FETCH command.
     * @param command the command.
     * @param fetchSize The number of records to include in this fetch.
     * @return the reply packet returned from the database.
     */
    private ReplyPacket executeFetchCommand(String command, int fetchSize)
        throws SQLException
    {
        // System.err.println("FETCH COMMAND: " + command + " USING FETCH SIZE " + fetchSize);
        RequestPacket request=this.connection.getRequestPacket(this.packetEncodingUnicode);
        int currentSQLMode=request.switchSqlMode(SqlMode.Internal_C);
        request.initDbsCommand(this.connection.getAutoCommit(), command, ResultSet.TYPE_FORWARD_ONLY);
        if(fetchSize > 1) {
            request.setMassCommand();
        } else {
            fetchSize = 1;
        }
        request.addResultCount(fetchSize);
        try {
            return this.connection.execute(request, this, ConnectionSapDB.GC_DELAYED);
        } finally {
            request.switchSqlMode(currentSQLMode);
        }
    }


    /**
     * Retrieves a string that contains comma separated '?' for
     * use in the fetch command.
     * @return a string containing comma separated '?', in the number
     *          of columns returned.
     */
    private String getFetchParamString() throws SQLException
    {
        if (this.columnInfo == null){
          this.describe();
        }
        if(_fetchparamstring==null) {
            StringBuffer tmp=new StringBuffer("?");
            for(int i=1; i<this.columnInfo.length; ++i) {
                tmp.append(", ?");
            }
            _fetchparamstring=tmp.toString();
        }
        return _fetchparamstring;
    }

    /**
     * Retrieves the short infos of a named column.
     * @param name The name of the column. It is treated
     *   case-insensitive.
     * @return the short info of the column, or <code>null</code> if
     *   nothing was found.
     */
    public final DBTechTranslator getColumnInfo(String name)
    throws SQLException
    {
        if (this.columnInfo == null){
          this.describe();
        }
        if (this.columnMapping == null)
          this.setColMapping();
        Object o=columnMapping.get(name);
        if(o==null) {
            String uc=name.toUpperCase();
            o=columnMapping.get(uc);
            if(o!=null) {
                columnMapping.put(uc, o);
            }
        }
        return (DBTechTranslator)o;
    }

    /**
     * @deprecated
     */
    public DBTechTranslator[] getColInfo()
        throws SQLException
    {
        if (this.columnInfo == null){
          this.describe();
        }
        return this.columnInfo;
    }

    int numberOfColumns()
    {
        return this.columnInfo.length;
    }

    int getRecordSize()
    {
        return recordSize;
    }

}