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.adminsql.business; |
35 | |
|
36 | |
import fr.paris.lutece.portal.service.database.PluginConnectionService; |
37 | |
import fr.paris.lutece.portal.service.plugin.Plugin; |
38 | |
import fr.paris.lutece.portal.service.plugin.PluginService; |
39 | |
import fr.paris.lutece.portal.service.util.AppException; |
40 | |
import fr.paris.lutece.portal.service.util.AppLogService; |
41 | |
import fr.paris.lutece.util.sql.DAOUtil; |
42 | |
|
43 | |
import java.sql.Connection; |
44 | |
import java.sql.PreparedStatement; |
45 | |
import java.sql.ResultSet; |
46 | |
import java.sql.ResultSetMetaData; |
47 | |
import java.sql.SQLException; |
48 | |
|
49 | |
import java.util.ArrayList; |
50 | |
import java.util.List; |
51 | |
import java.util.StringTokenizer; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 1 | public class DataDAO implements IDataDAO |
58 | |
{ |
59 | |
|
60 | |
private static final String WHERE = " WHERE "; |
61 | |
private static final String VALUES = " VALUES "; |
62 | |
private static final String SELECT = " SELECT "; |
63 | |
private static final String FROM = " FROM "; |
64 | |
private static final String AND = " AND "; |
65 | |
private static final String UPDATE = "UPDATE "; |
66 | |
private static final String SET = " SET "; |
67 | |
private static final String STR_SQL_ERROR = "SQL Error executing command : "; |
68 | |
private static final String DELETE_FROM = "DELETE FROM "; |
69 | |
|
70 | |
|
71 | |
private static final String SQL_QUERY_SELECT_ALL_DATA = "SELECT * FROM "; |
72 | |
private static final String SQL_QUERY_SELECT_A_ROW = "SELECT * FROM "; |
73 | |
private static final String SQL_QUERY_INSERT_VALUES = "INSERT INTO "; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public List<List<String>> selectDataList( String strTableName, PluginConnectionService connectionService ) |
82 | |
{ |
83 | 0 | List<List<String>> listData = new ArrayList<List<String>>( ); |
84 | |
|
85 | |
try |
86 | |
{ |
87 | 0 | listData = selectSqlQuery( SQL_QUERY_SELECT_ALL_DATA + strTableName, connectionService ); |
88 | |
} |
89 | 0 | catch ( SQLException sql ) |
90 | |
{ |
91 | 0 | AppLogService.error( sql.getMessage( ), sql ); |
92 | 0 | } |
93 | |
|
94 | 0 | return listData; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public List<List<String>> selectDataListUserRequest( String strPoolName, String strUserRequest, |
105 | |
PluginConnectionService connectionService ) |
106 | |
{ |
107 | 0 | List<List<String>> listData = new ArrayList<List<String>>( ); |
108 | 0 | connectionService = new PluginConnectionService( strPoolName ); |
109 | 0 | connectionService.setPool( strPoolName ); |
110 | |
|
111 | |
try |
112 | |
{ |
113 | 0 | listData = selectSqlQuery( strUserRequest, connectionService ); |
114 | |
} |
115 | 0 | catch ( SQLException sql ) |
116 | |
{ |
117 | 0 | AppLogService.error( sql.getMessage( ), sql ); |
118 | 0 | } |
119 | |
|
120 | 0 | return listData; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void insert( String strPoolName, String strTableName, List<String> listData, List<String> listFieldsNames, |
132 | |
PluginConnectionService connectionService ) |
133 | |
{ |
134 | 0 | String strFieldsNames = ""; |
135 | 0 | String strDataValues = ""; |
136 | |
|
137 | 0 | for ( String strFieldName : listFieldsNames ) |
138 | |
{ |
139 | 0 | strFieldsNames = strFieldsNames + strFieldName + ", "; |
140 | 0 | } |
141 | |
|
142 | 0 | strFieldsNames = strFieldsNames.substring( 0, strFieldsNames.length( ) - 2 ); |
143 | |
|
144 | 0 | String strFieldsNameFinal = " (" + strFieldsNames + ") "; |
145 | |
|
146 | 0 | for ( String strDataValue : listData ) |
147 | |
{ |
148 | 0 | strDataValues += ( "'" + strDataValue + "'" + ", " ); |
149 | 0 | } |
150 | |
|
151 | 0 | strDataValues = strDataValues.substring( 0, strDataValues.length( ) - 2 ); |
152 | |
|
153 | 0 | String strDataValuesFinal = " (" + strDataValues + ") "; |
154 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_VALUES + strTableName + " " + strFieldsNameFinal + VALUES + |
155 | 0 | strDataValuesFinal, changePool( strPoolName ) ); |
156 | 0 | daoUtil.executeUpdate( ); |
157 | 0 | daoUtil.free( ); |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void store( String strPoolName, String strTableName, List<FieldValues> listFieldValues, |
168 | |
PluginConnectionService connectionService ) |
169 | |
{ |
170 | 0 | String strAllDataToModify = ""; |
171 | 0 | String strAllDataModified = ""; |
172 | |
|
173 | 0 | for ( FieldValues fieldValues : listFieldValues ) |
174 | |
{ |
175 | 0 | strAllDataModified = strAllDataModified + "," + fieldValues.getFieldName( ) + "=" + "'" + |
176 | 0 | fieldValues.getNewValue( ) + "'"; |
177 | 0 | strAllDataToModify = strAllDataToModify + AND + fieldValues.getFieldName( ) + "=" + "'" + |
178 | 0 | fieldValues.getOldValue( ) + "'"; |
179 | 0 | } |
180 | |
|
181 | 0 | strAllDataModified = strAllDataModified.substring( 1 ); |
182 | 0 | strAllDataToModify = strAllDataToModify.substring( 4 ); |
183 | |
|
184 | 0 | DAOUtil daoUtil = new DAOUtil( UPDATE + strTableName + SET + strAllDataModified + WHERE + strAllDataToModify, |
185 | 0 | changePool( strPoolName ) ); |
186 | 0 | daoUtil.executeUpdate( ); |
187 | 0 | daoUtil.free( ); |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
public void delete( String strPoolName, String strTableName, List<String> listConcatFieldNameAndData, |
198 | |
PluginConnectionService connectionService ) |
199 | |
{ |
200 | 0 | String strAllDataToDelete = ""; |
201 | |
|
202 | 0 | for ( String strConcatFieldNameAndData : listConcatFieldNameAndData ) |
203 | |
{ |
204 | 0 | strAllDataToDelete = strAllDataToDelete + AND + strConcatFieldNameAndData; |
205 | 0 | } |
206 | |
|
207 | 0 | strAllDataToDelete = strAllDataToDelete.substring( 4 ); |
208 | |
|
209 | 0 | DAOUtil daoUtil = new DAOUtil( DELETE_FROM + strTableName + WHERE + strAllDataToDelete, |
210 | 0 | changePool( strPoolName ) ); |
211 | 0 | daoUtil.executeUpdate( ); |
212 | 0 | daoUtil.free( ); |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public void insertWithUserRequest( String strUserRequest, PluginConnectionService connectionService, |
222 | |
String strPoolName ) |
223 | |
{ |
224 | 0 | DAOUtil daoUtil = new DAOUtil( strUserRequest, changePool( strPoolName ) ); |
225 | 0 | daoUtil.executeUpdate( ); |
226 | 0 | daoUtil.free( ); |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public List<List<String>> load( String strTableName, String strContatFieldNameAndData, List<Field> listFields, |
238 | |
PluginConnectionService connectionService ) |
239 | |
{ |
240 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_A_ROW + strTableName + WHERE + strContatFieldNameAndData ); |
241 | 0 | daoUtil.executeQuery( ); |
242 | 0 | List<List<String>> listDataValueSelectedByPK = new ArrayList( ); |
243 | 0 | return listDataValueSelectedByPK; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private List<List<String>> selectSqlQuery( String strRequest, PluginConnectionService connectionService ) |
253 | |
throws SQLException |
254 | |
{ |
255 | 0 | Connection connection = null; |
256 | 0 | PreparedStatement statement = null; |
257 | |
|
258 | |
try |
259 | |
{ |
260 | 0 | connection = connectionService.getConnection( ); |
261 | 0 | statement = connection.prepareStatement( strRequest ); |
262 | |
|
263 | 0 | ResultSet resultSet = statement.executeQuery( ); |
264 | 0 | ResultSetMetaData rsmd = resultSet.getMetaData( ); |
265 | |
|
266 | 0 | ArrayList listRow = new ArrayList( ); |
267 | |
|
268 | 0 | while ( resultSet.next( ) ) |
269 | |
{ |
270 | 0 | String strValue = null; |
271 | 0 | ArrayList listLine = new ArrayList( ); |
272 | |
|
273 | 0 | for ( int i = 1; i <= rsmd.getColumnCount( ); i++ ) |
274 | |
{ |
275 | 0 | if ( resultSet.getObject( rsmd.getColumnName( i ) ) != null ) |
276 | |
{ |
277 | 0 | strValue = resultSet.getObject( rsmd.getColumnName( i ) ).toString( ); |
278 | |
} |
279 | |
else |
280 | |
{ |
281 | 0 | strValue = " "; |
282 | |
} |
283 | |
|
284 | 0 | listLine.add( strValue ); |
285 | |
} |
286 | |
|
287 | 0 | listRow.add( listLine ); |
288 | 0 | } |
289 | |
|
290 | 0 | statement.close( ); |
291 | 0 | statement = null; |
292 | |
|
293 | 0 | return listRow; |
294 | |
} |
295 | |
finally |
296 | |
{ |
297 | 0 | try |
298 | |
{ |
299 | 0 | if ( statement != null ) |
300 | |
{ |
301 | 0 | statement.close( ); |
302 | |
} |
303 | |
} |
304 | 0 | catch ( SQLException e ) |
305 | |
{ |
306 | 0 | throw new AppException( "SQL Error executing command : " + e.toString( ) ); |
307 | 0 | } |
308 | |
|
309 | 0 | connectionService.freeConnection( connection ); |
310 | |
} |
311 | |
} |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public List<List<String>> findResultOfUserRequest( String strUserRequest, PluginConnectionService connectionService ) |
320 | |
{ |
321 | 0 | List<List<String>> listData = new ArrayList<List<String>>( ); |
322 | 0 | List<String> listFields = new ArrayList<String>( ); |
323 | |
|
324 | |
try |
325 | |
{ |
326 | 0 | listData = selectSqlQuery( strUserRequest, connectionService ); |
327 | |
} |
328 | 0 | catch ( SQLException sql ) |
329 | |
{ |
330 | 0 | AppLogService.error( sql.getMessage( ), sql ); |
331 | 0 | } |
332 | |
|
333 | 0 | return listData; |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
public List<String> selectColumnNames( String strUserRequest, PluginConnectionService connectionService, |
344 | |
String strPoolName ) |
345 | |
{ |
346 | 0 | List<String> listFields = new ArrayList( ); |
347 | 0 | DAOUtil daoUtil = new DAOUtil( strUserRequest, changePool( strPoolName ) ); |
348 | 0 | daoUtil.executeQuery( ); |
349 | |
|
350 | 0 | for ( int i = 1; i <= getColumnCount( daoUtil ); i++ ) |
351 | |
{ |
352 | 0 | String columnName = getColumnName( i, daoUtil ); |
353 | 0 | listFields.add( columnName ); |
354 | |
} |
355 | |
|
356 | 0 | daoUtil.free( ); |
357 | |
|
358 | 0 | return listFields; |
359 | |
} |
360 | |
|
361 | |
private String getColumnName( int nColumn, DAOUtil daoUtil ) |
362 | |
{ |
363 | |
try |
364 | |
{ |
365 | 0 | ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( ); |
366 | |
|
367 | 0 | return rsmd.getColumnName( nColumn ); |
368 | |
} |
369 | 0 | catch ( SQLException e ) |
370 | |
{ |
371 | 0 | daoUtil.free( ); |
372 | 0 | throw new AppException( STR_SQL_ERROR + e.toString( ) ); |
373 | |
} |
374 | |
} |
375 | |
|
376 | |
private int getColumnCount( DAOUtil daoUtil ) |
377 | |
{ |
378 | |
try |
379 | |
{ |
380 | 0 | ResultSetMetaData rsmd = daoUtil.getResultSet( ).getMetaData( ); |
381 | |
|
382 | 0 | return rsmd.getColumnCount( ); |
383 | |
} |
384 | 0 | catch ( SQLException e ) |
385 | |
{ |
386 | 0 | daoUtil.free( ); |
387 | 0 | throw new AppException( STR_SQL_ERROR + e.toString( ) ); |
388 | |
} |
389 | |
} |
390 | |
|
391 | |
private Plugin changePool( String strPoolName ) |
392 | |
{ |
393 | 0 | Plugin plugin = PluginService.getPlugin( "adminsql" ); |
394 | 0 | PluginConnectionService connectionService = new PluginConnectionService( strPoolName ); |
395 | 0 | connectionService.setPool( strPoolName ); |
396 | 0 | plugin.setConnectionService( connectionService ); |
397 | |
|
398 | 0 | return plugin; |
399 | |
} |
400 | |
} |