55*/
66package edu .ie3 .datamodel .io .source .sql ;
77
8- import edu .ie3 .datamodel .exceptions .InvalidWeatherColumnNameException ;
98import edu .ie3 .datamodel .io .connectors .SqlConnector ;
109import edu .ie3 .datamodel .io .factory .timeseries .TimeBasedWeatherValueData ;
1110import edu .ie3 .datamodel .io .factory .timeseries .TimeBasedWeatherValueFactory ;
1413import edu .ie3 .datamodel .models .timeseries .individual .IndividualTimeSeries ;
1514import edu .ie3 .datamodel .models .timeseries .individual .TimeBasedValue ;
1615import edu .ie3 .datamodel .models .value .WeatherValue ;
17- import edu .ie3 .util .StringUtils ;
1816import edu .ie3 .util .interval .ClosedInterval ;
1917import edu .ie3 .util .naming .NamingConvention ;
2018import java .sql .*;
@@ -34,12 +32,8 @@ public class SqlWeatherSource implements WeatherSource {
3432 private static final String WHERE = " WHERE " ;
3533 private static final NamingConvention DEFAULT_NAMING_CONVENTION = NamingConvention .SNAKE ;
3634
37- private final NamingConvention namingConvention ;
3835 private final SqlConnector connector ;
3936 private final IdCoordinateSource idCoordinateSource ;
40- /* The column name represents the name of the column in the chosen naming convention. However, the field name is in
41- * flat case always, as this is, what the factory expects. */
42- private final String coordinateIdColumnName ;
4337 private final String coordinateIdFieldName ;
4438 private final TimeBasedWeatherValueFactory weatherFactory ;
4539
@@ -99,19 +93,9 @@ public SqlWeatherSource(
9993 this .connector = connector ;
10094 this .idCoordinateSource = idCoordinateSource ;
10195 this .weatherFactory = weatherFactory ;
102- this .namingConvention = namingConvention ;
103- this .coordinateIdColumnName = weatherFactory .getCoordinateIdFieldString (namingConvention );
10496 this .coordinateIdFieldName = weatherFactory .getCoordinateIdFieldString ();
105-
106- String dbTimeColumnName =
107- getDbColumnName (weatherFactory .getTimeFieldString (), connector , weatherTableName )
108- .orElseThrow (
109- () ->
110- new InvalidWeatherColumnNameException (
111- "Cannot find column for '"
112- + weatherFactory .getTimeFieldString ()
113- + "' in provided weather data configuration."
114- + "Please ensure that the database connection is working and the column names are correct!" ));
97+ String coordinateIdColumnName = weatherFactory .getCoordinateIdFieldString (namingConvention );
98+ String dbTimeColumnName = weatherFactory .getTimeFieldString ();
11599
116100 // setup queries
117101 this .queryTimeInterval =
@@ -186,43 +170,6 @@ public Optional<TimeBasedValue<WeatherValue>> getWeather(ZonedDateTime date, Poi
186170 return Optional .of (timeBasedValues .get (0 ));
187171 }
188172
189- /**
190- * Determine the corresponding database column name based on the provided factory field parameter
191- * name. Needed to support camel as well as snake case database column names.
192- *
193- * @param factoryColumnName the name of the field parameter set in the entity factory
194- * @param connector the sql connector of this source
195- * @param weatherTableName the table name where the weather is stored
196- * @return the column name that corresponds to the provided field parameter or an empty optional
197- * if no matching column can be found
198- */
199- private Optional <String > getDbColumnName (
200- String factoryColumnName , SqlConnector connector , String weatherTableName ) {
201-
202- // get the column names from the database
203- Optional <String > dbColumnName = Optional .empty ();
204- try {
205- ResultSet rs =
206- connector .getConnection ().getMetaData ().getColumns (null , null , weatherTableName , null );
207-
208- while (rs .next ()) {
209- String databaseColumnName = rs .getString ("COLUMN_NAME" );
210- if (StringUtils .snakeCaseToCamelCase (databaseColumnName )
211- .equalsIgnoreCase (factoryColumnName )) {
212- dbColumnName = Optional .of (databaseColumnName );
213- break ;
214- }
215- }
216- } catch (SQLException ex ) {
217- logger .error (
218- "Cannot connect to database to retrieve db column name for factory column name '{}' in weather table '{}'" ,
219- factoryColumnName ,
220- weatherTableName ,
221- ex );
222- }
223- return dbColumnName ;
224- }
225-
226173 /**
227174 * Creates a base query string without closing semicolon of the following pattern: <br>
228175 * {@code SELECT * FROM <schema>.<table>}
0 commit comments