1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | |
23 | |
24 | */ |
25 | package com.mysql.jdbc.util; |
26 | |
27 | import java.sql.ResultSet; |
28 | import java.sql.ResultSetMetaData; |
29 | import java.sql.SQLException; |
30 | |
31 | /** |
32 | * Utilities for dealing with result sets (used in testcases and profiler). |
33 | * |
34 | * @author Mark Matthews |
35 | * |
36 | * @version $Id: ResultSetUtil.java 3726 2005-05-19 15:52:24Z mmatthews $ |
37 | */ |
38 | public class ResultSetUtil { |
39 | |
40 | public static StringBuffer appendResultSetSlashGStyle( |
41 | StringBuffer appendTo, ResultSet rs) throws SQLException { |
42 | ResultSetMetaData rsmd = rs.getMetaData(); |
43 | |
44 | int numFields = rsmd.getColumnCount(); |
45 | int maxWidth = 0; |
46 | |
47 | String[] fieldNames = new String[numFields]; |
48 | |
49 | for (int i = 0; i < numFields; i++) { |
50 | fieldNames[i] = rsmd.getColumnLabel(i + 1); |
51 | |
52 | if (fieldNames[i].length() > maxWidth) { |
53 | maxWidth = fieldNames[i].length(); |
54 | } |
55 | } |
56 | |
57 | int rowCount = 1; |
58 | |
59 | while (rs.next()) { |
60 | appendTo.append("*************************** "); |
61 | appendTo.append(rowCount++); |
62 | appendTo.append(". row ***************************\n"); |
63 | |
64 | for (int i = 0; i < numFields; i++) { |
65 | int leftPad = maxWidth - fieldNames[i].length(); |
66 | |
67 | for (int j = 0; j < leftPad; j++) { |
68 | appendTo.append(" "); |
69 | } |
70 | |
71 | appendTo.append(fieldNames[i]); |
72 | appendTo.append(": "); |
73 | |
74 | String stringVal = rs.getString(i + 1); |
75 | |
76 | if (stringVal != null) { |
77 | appendTo.append(stringVal); |
78 | } else { |
79 | appendTo.append("NULL"); |
80 | } |
81 | |
82 | appendTo.append("\n"); |
83 | } |
84 | |
85 | appendTo.append("\n"); |
86 | } |
87 | |
88 | return appendTo; |
89 | } |
90 | } |