View Javadoc

1   package salto.tool.jdo.util;
2   
3   import java.sql.Types;
4   
5   import salto.tool.jdo.data.JdoColInfo;
6   import salto.tool.sql.data.TableColInfo;
7   
8   /***
9    * Ins�rez la description du type � cet endroit.
10   *  Date de cr�ation : (20/10/01 09:09:16)
11   * @author : Administrator
12   */
13  public class DoJdbcConvert implements IJdbcConvert {
14  	
15  /***
16   * Commentaire relatif au constructeur DoJdbcConvert.
17   */
18  public DoJdbcConvert() {
19  	super();
20  }
21  /***
22   * Cette m�thode permet de retourner la fonction du ResultSet qui sera utilis�
23   pour r�cup�rer les donn�es.
24   Cette m�thode a un impact sur les performances de la base de donn�e et cela d�pend
25   de la mani�re dont est cr�� le driver JDBC
26   Voici quelques informations concernant les diff�rentes fonctions possibles
27   	//non g�r� par les do
28  	Array getArray(int i) throws SQLException;
29  	Ref getRef(int i) throws SQLException;
30  
31  	les fonctions suivantes ne seront � priori jamais g�r� par les do car il est 
32  	important de fermer les flux or ceci ne peut pas �tre fait automatiquement.
33  	Ces m�thodes sont donc r�serv�s � du travail d'expertise, l'invers des DO!!!!
34  	java.io.InputStream getAsciiStream(int columnIndex) throws SQLException;
35  	java.io.InputStream getBinaryStream(int columnIndex)
36  		throws SQLException;
37  	java.io.Reader getCharacterStream(int columnIndex) throws SQLException;
38  	java.io.InputStream getUnicodeStream(int columnIndex) throws SQLException;
39  
40  	
41  	boolean getBoolean(int columnIndex) throws SQLException;
42  	byte getByte(int columnIndex) throws SQLException;
43  
44  	//stockage d'objet voir le package 
45  	byte[] getBytes(int columnIndex) throws SQLException;
46  	Blob getBlob(int i) throws SQLException;
47  	Clob getClob(int i) throws SQLException;
48  
49  	//type primitif
50  	Ces m�thodes sont en r�gle g�n�rale les plus performantes
51  	short getShort(int columnIndex) throws SQLException;
52  	int getInt(int columnIndex) throws SQLException;
53  	long getLong(int columnIndex) throws SQLException;
54  	double getDouble(int columnIndex) throws SQLException;
55  	float getFloat(int columnIndex) throws SQLException;
56  	
57  	String getString(int columnIndex) throws SQLException;
58  	
59  	Seul un nombre exceptionnel justifie l'utilisation de ces m�thodes
60  	D'une mani�re g�n�rale, il faut essayer d'�viter ces m�thodes
61  	car 
62  	1 - Ces nombres sont peu pratiques � g�rer pour les d�veloppeurs
63  	2 - ce n'est pas, en r�gle g�n�ral, les m�thodes les plus performantes pour les drivers
64  	
65  	BigDecimal getBigDecimal(int columnIndex) throws SQLException;
66  	BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException;
67  
68  	A �viter pour des probl�mes de performance
69  	Cette m�thode est justement cr�� pour �viter l'utilisation de cette M�thode
70  	
71  	Object getObject(int columnIndex) throws SQLException;
72  
73  	
74  	
75  	Attention aux types Time et Date qui comportent �norm�ment de m�thodes d�pr�ci�s
76  	Il est donc pr�f�rable de g�rer toutes les dates de mani�re uniforme avec le type
77  	Timestamp
78  	
79  	java.sql.Time getTime(int columnIndex) throws SQLException;
80  	java.sql.Timestamp getTimestamp(int columnIndex) throws SQLException;
81  	java.sql.Date getDate(int columnIndex) throws SQLException;
82  
83   *  Date de cr�ation : (20/10/01 09:10:54)
84   * @return java.lang.String
85   * @param attInfo salto.tool.jdo.JdoInfo
86   */
87  public String convert(JdoColInfo attInfo) 
88  {
89  /*	
90  	BIT 		=  -7;
91  	TINYINT 	=  -6;
92  	SMALLINT	=   5;
93  	INTEGER 	=   4;
94  	BIGINT 		=  -5;
95  	FLOAT 		=   6;
96  	REAL 		=   7;
97  	DOUBLE 		=   8;
98  	NUMERIC 	=   2;
99  	DECIMAL		=   3;
100 	CHAR		=   1;
101 	VARCHAR 	=  12;
102 	LONGVARCHAR 	=  -1;
103 	DATE 		=  91;
104 	TIME 		=  92;
105 	TIMESTAMP 	=  93;
106 	BINARY		=  -2;
107 	VARBINARY 	=  -3;
108 	LONGVARBINARY 	=  -4;
109 
110 	NULL		=   0;
111 
112 	OTHER		= 1111;
113 	JAVA_OBJECT         = 2000;
114 	DISTINCT            = 2001;
115 	STRUCT              = 2002;
116 	ARRAY               = 2003;
117 	BLOB                = 2004;
118 	CLOB                = 2005;
119 	REF                 = 2006;
120 	
121 */	
122 	String result = null;
123 	switch (attInfo.getColTyp()){
124 		case Types.TINYINT:
125 			result = "Byte";
126 			break;
127 		case Types.SMALLINT:
128 			result = "Short";
129 			break;
130 		case Types.INTEGER:
131 			result = "Integer";
132 			break;
133 		case Types.BIGINT:
134 			result = "Long";
135 			break;
136 		case Types.REAL:
137 			result = "Float";
138 			break;
139 		case Types.FLOAT:
140 		case Types.DOUBLE:
141 			result = "Double";
142 			break;
143 		case Types.DECIMAL:
144 		case Types.NUMERIC:
145 			if (attInfo.getDecDigit() == 0) {
146 				if (attInfo.getColSize() < 4) result = "Short";
147 				else if (attInfo.getColSize() < 10) result = "Integer";
148 				//else if (attInfo.getColSize() < 19) result = "Long";
149 				else result = "Long";
150 				
151 			} else {
152 				result = "Double";
153 			}
154 			break;
155 		case Types.BIT:
156 			result = "Boolean";
157 			break;
158 		case Types.CHAR:
159 		case Types.VARCHAR:
160 		case Types.LONGVARCHAR:
161 			result = "String";
162 			break;
163 		case Types.BINARY:
164 		case Types.VARBINARY:
165 		case Types.LONGVARBINARY:
166 		case Types.BLOB:
167 			result = "byte[]";
168 			break;
169 		case Types.DATE:
170 		case Types.TIME:
171 		case Types.TIMESTAMP:
172 			result = "Timestamp";
173 			break;
174 	}
175 	if (result == null) result = "Object";
176 	if (attInfo.getColNullable() == 0) {
177 		if ("Integer".equals(result)) {
178 			result = "int";
179 		} else if ("String".equals(result)) {
180 			//ne rien faire
181 		} else {
182 			try {
183 				Class.forName("java.lang."+result);
184 				result = result.toLowerCase();
185 			} catch (Exception e) {}		
186 		}
187 	}
188 	return result;
189 }
190 /* (non-Javadoc)
191  * @see salto.tool.jdo.util.IJdbcConvert#getJavaPossTyp(salto.tool.sql.data.TableColInfo)
192  */
193 public String[] getJavaPossTyp(TableColInfo attInfo) {
194 	switch (attInfo.getColTyp()) {
195 		case Types.TINYINT :
196 		case Types.SMALLINT :
197 		case Types.INTEGER :
198 		case Types.BIGINT:
199 		case Types.REAL:
200 		case Types.FLOAT:
201 		case Types.DOUBLE:
202 		case Types.DECIMAL:
203 		case Types.NUMERIC:
204 		case Types.BIT:
205 			if (booleanValue(attInfo.getColIsNullable())) {
206 				return new String[] {"byte","short","int","long","float","double","BigDecimal","boolean","String"};
207 			} else {
208 				return new String[] {"Byte","Short","Integer","Long","Float","Double","BigDecimal","Boolean","String"};
209 			}
210 		case Types.CHAR:
211 		case Types.VARCHAR :
212 		case Types.LONGVARCHAR :
213 			if (booleanValue(attInfo.getColIsNullable())) {
214 				return new String[] {"byte","short","int","long","float","double","BigDecimal","boolean","String","Timestamp"};
215 			} else {
216 				return new String[] {"Byte","Short","Integer","Long","Float","Double","BigDecimal","Boolean","String","Timestamp"};
217 			}
218 		case Types.BINARY:
219 		case Types.VARBINARY:
220 		case Types.LONGVARBINARY:
221 			return new String[] {"String","byte[]"};
222 		case Types.DATE:
223 		case Types.TIME:
224 		case Types.TIMESTAMP:
225 			return new String[] {"Time","Date","Timestamp"};
226 	}
227 	return null;
228 }
229 
230 	public static boolean booleanValue(String val) {
231 		if (val == null) return false;
232 		if (val.equalsIgnoreCase("Y") || val.equalsIgnoreCase("O") 
233 			|| val.equalsIgnoreCase("true") || val.equalsIgnoreCase("1")) {
234 			return true;
235 		}
236 		return false;
237 	}
238 
239 }