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 package fr.paris.lutece.plugins.adminquery.business;
35
36 import fr.paris.lutece.portal.service.plugin.Plugin;
37 import fr.paris.lutece.portal.service.util.AppException;
38 import fr.paris.lutece.util.date.DateUtil;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.sql.ResultSetMetaData;
42 import java.sql.SQLException;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47
48
49
50
51 public final class AdminQueryDAO implements IAdminqueryDAO
52 {
53 private static final String STR_SQL_ERROR = "SQL Error executing command : ";
54
55
56
57
58
59
60
61 public List<String> selectColumnNames( String strRequest, Plugin plugin )
62 {
63 List<String> listNames = new ArrayList<String>( );
64 DAOUtil daoUtil = new DAOUtil( strRequest, plugin );
65 daoUtil.executeQuery( );
66
67 for ( int i = 1; i <= getColumnCount( daoUtil ); i++ )
68 {
69 String columnName = getColumnName( i, daoUtil );
70 listNames.add( columnName );
71 }
72
73 daoUtil.free( );
74
75 return listNames;
76 }
77
78
79
80
81
82
83 public List<List<String>> selectRows( String strRequest, Plugin plugin )
84 {
85 List<List<String>> listRows = new ArrayList<List<String>>( );
86 DAOUtil daoUtil = new DAOUtil( strRequest, plugin );
87 daoUtil.executeQuery( );
88
89 while ( daoUtil.next( ) )
90 {
91 String strValue = null;
92 List<String> listLine = new ArrayList<String>( );
93
94 for ( int i = 1; i <= getColumnCount( daoUtil ); i++ )
95 {
96 switch ( getColumnType( i, daoUtil ) )
97 {
98 case java.sql.Types.CHAR:
99 case java.sql.Types.VARCHAR:
100 case java.sql.Types.LONGVARCHAR:
101 strValue = daoUtil.getString( getColumnName( i, daoUtil ) );
102
103 break;
104
105 case java.sql.Types.INTEGER:
106 case java.sql.Types.BIGINT:
107 case java.sql.Types.SMALLINT:
108 strValue = "" + daoUtil.getInt( getColumnName( i, daoUtil ) );
109
110 break;
111
112 case java.sql.Types.TIMESTAMP:
113 strValue = DateUtil.getDateString( daoUtil.getTimestamp( getColumnName( i, daoUtil ) ) );
114
115 break;
116
117 case java.sql.Types.DATE:
118 strValue = DateUtil.getDateString( daoUtil.getDate( getColumnName( i, daoUtil ) ) );
119
120 break;
121
122 default:
123 break;
124 }
125
126 listLine.add( strValue );
127 }
128
129 listRows.add( listLine );
130 }
131
132 daoUtil.free( );
133
134 return listRows;
135 }
136
137
138
139
140
141
142
143 private int getColumnCount( DAOUtil daoUtil )
144 {
145 try
146 {
147 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
148
149 return rsmd.getColumnCount( );
150 }
151 catch ( SQLException e )
152 {
153 daoUtil.free( );
154 throw new AppException( STR_SQL_ERROR + e.toString( ) );
155 }
156 }
157
158
159
160
161
162
163
164
165 private String getColumnName( int nColumn, DAOUtil daoUtil )
166 {
167 try
168 {
169 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
170
171 return rsmd.getColumnName( nColumn );
172 }
173 catch ( SQLException e )
174 {
175 daoUtil.free( );
176 throw new AppException( STR_SQL_ERROR + e.toString( ) );
177 }
178 }
179
180
181
182
183
184
185
186
187 private int getColumnType( int nColumn, DAOUtil daoUtil )
188 {
189 try
190 {
191 ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( );
192
193 return rsmd.getColumnType( nColumn );
194 }
195 catch ( SQLException e )
196 {
197 daoUtil.free( );
198 throw new AppException( STR_SQL_ERROR + e.toString( ) );
199 }
200 }
201 }