KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > tools > shell > DisplayOutput


1 package com.daffodilwoods.tools.shell;
2
3 import java.sql.*;
4
5 import com.daffodilwoods.daffodildb.client.*;
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
9 import com.daffodilwoods.daffodildb.utils.parser.*;
10
11 public class DisplayOutput {
12
13   public static void displayOutput(String JavaDoc query, Object JavaDoc executeresult,
14                                    int queryType) throws
15       Exception JavaDoc {
16     switch (queryType) {
17
18       case 0:
19         _SelectIterator iter = (_SelectIterator) executeresult;
20         showRetriever(iter);
21         displayLn("");
22         break;
23       case 1:
24         displayLn(" \n\n " + Integer.parseInt(executeresult.toString()) +
25                   " row inserted\n");
26         break;
27       case 2:
28         displayLn(" \n\n " + Integer.parseInt(executeresult.toString()) +
29                   " row(s) updated\n");
30         break;
31       case 3:
32         displayLn(" \n\n " + Integer.parseInt(executeresult.toString()) +
33                   " row(s) deleted\n");
34         break;
35       default:
36         try {
37           query = query.substring(0, query.indexOf(" "));
38         }
39         catch (Exception JavaDoc e) {}
40         displayLn("\n " + query.toUpperCase() + " SUCCESSFUL\n");
41         break;
42     }
43   }
44
45   private static void showRetriever(_SelectIterator iter) {
46     try {
47       RecordSet recordSetBuffer = new RecordSet();
48       recordSetBuffer.setSelectIterator(iter);
49       _ColumnCharacteristics columnCharacteristics = recordSetBuffer.
50           getColumnCharacteristics();
51       String JavaDoc[] columns = columnCharacteristics.getColumnNames();
52       int len = columns.length;
53       int[] size = new int[len];
54       int total = 0;
55       for (int i = 0; i < len; i++) {
56         int columnPrecision = columnCharacteristics.getPrecision(i + 1);
57         int type = columnCharacteristics.getColumnType(i + 1);
58         size[i] = getSize(type, columnPrecision);
59         total += size[i];
60       }
61       printInRow(columns, size);
62       printChar('-', total, size);
63       int columnCount = columnCharacteristics.getColumnCount();
64       _RecordSetBufferIterator iterator = recordSetBuffer.getIterator();
65       int rowCount = 0;
66       if (iterator.top()) {
67         do {
68           showRecord(iterator.getRecord(), columnCount, size);
69           rowCount++;
70         }
71         while (iterator.next());
72       }
73       displayLn(" \n\n " + rowCount + " row(s) selected");
74     }
75     catch (Exception JavaDoc e) {
76       printError("", e);
77     }
78   }
79
80   private static int getSize(int type, int columnPrecision) {
81     int size = 0;
82     try {
83       switch (type) {
84         case Datatypes.BLOB:
85           size = 15;
86           break;
87         case Datatypes.CLOB:
88           size = 15;
89           break;
90         case Datatypes.CHARACTERLARGEOBJECT:
91           size = 15;
92           break;
93         case Datatypes.CHARLARGEOBJECT:
94           size = 15;
95           break;
96         case Datatypes.BINARYLARGEOBJECT:
97           size = 15;
98           break;
99         case Datatypes.LONGVARCHAR:
100           size = 15;
101           break;
102         case Datatypes.LONGVARBINARY:
103           size = 15;
104           break;
105         default:
106           if (columnPrecision < 5) {
107             size = 5;
108           }
109           else {
110             size = columnPrecision;
111           }
112           break;
113       }
114     }
115     catch (Exception JavaDoc ex) {
116     }
117     return size;
118   }
119
120   private static void printInRow(Object JavaDoc[] value, int[] size) {
121     int len = value.length;
122     DisplayOutput.displayLn("");
123     for (int i = 0; i < len; i++) {
124       Object JavaDoc val = value[i] == null ? "null" : value[i];
125       display("" + val.toString());
126       printSpace(size[i] - val.toString().length() + 1);
127     }
128   }
129
130    private static void printChar(char ch, int count, int[] size) {
131      DisplayOutput.displayLn("");
132      int j = 0, k = 0;
133      for (int i = 0; i < count; i++) {
134        if (k == size[j]) {
135          j++;
136          k = 0;
137          printSpace(1);
138        }
139        DisplayOutput.display(ch);
140        k++;
141      }
142    }
143
144    private static void printSpace(int len) {
145      for (int i = 0; i < len; i++) {
146        DisplayOutput.display(" ");
147      }
148    }
149
150    public static void showRecord(_Record record, int columnCount, int[] sizes) throws
151        Exception JavaDoc {
152      Object JavaDoc[] values = new Object JavaDoc[columnCount];
153      for (int j = 0; j < columnCount; j++) {
154        values[j] = record.getColumnValue(j + 1);
155        if (values[j] instanceof Blob || values[j] instanceof Clob) {
156          values[j] = "<binary>";
157        }
158      }
159      printInRow(values, sizes);
160    }
161
162     public static void printError(String JavaDoc message, Exception JavaDoc ex) {
163       if (DaffodilDBShell.trace) {
164         ex.printStackTrace();
165       }
166       else {
167         try {
168           System.out.println(ex.getCause().getMessage());
169         }
170         catch (Throwable JavaDoc ex1) {
171           System.out.println(ex.getMessage());
172         }
173       }
174     }
175
176   public static void print(String JavaDoc message) {
177     System.out.println(message);
178   }
179
180   public static void locateSyntaxError(String JavaDoc query, ParseException pe) {
181     int pos = pe.getErrorPosition();
182     System.out.println(query.trim());
183     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
184     for (int i = 0; i < pos + 1; i++) {
185       str.append(" ");
186     }
187     str.append('^');
188     System.out.println(str.toString());
189   }
190
191   public static void display(String JavaDoc str) {
192     System.out.print(str);
193   }
194
195   public static void display(char str) {
196     System.out.print(str);
197   }
198
199   public static void displayLn(String JavaDoc str) {
200     System.out.println(str);
201   }
202
203   public static void displayLn(char str) {
204     System.out.println(str);
205   }
206
207 }
208
Popular Tags