| Property | Default | Required | Description |
| ------------------------- | ------------------------------------------------------------ | :-------: | ------------------------------------------------------------ |
-| url | | Yes | JDBC URL to establish a connection to your database. Examples:<br/><br/>`jdbc:derby:./testDerby;create=true`<br/>`jdbc:h2:./testH2`<br/>`jdbc:hsqldb:./testHsqlDb`<br/>`jdbc:mariadb://192.168.0.1:3306/testMariadb`<br/>`jdbc:mysql://192.168.0.1:3306/testMysql?serverTimezone=UTC`<br/>`jdbc:postgresql://192.168.0.1:5432/testPostgresql`<br/>`jdbc:sqlite:./testSqlite.db`.<br/><br/>If no database is available it will be created; for example the url `jdbc:h2:./testH2` creates a new H2 database in openHAB folder. Example to create your own MySQL database directly:<br/><br/>`CREATE DATABASE 'yourDB' CHARACTER SET utf8 COLLATE utf8_general_ci;` |
+| url | | Yes | JDBC URL to establish a connection to your database. Examples:<br/><br/>`jdbc:derby:./testDerby;create=true`<br/>`jdbc:h2:./testH2`<br/>`jdbc:hsqldb:./testHsqlDb`<br/>`jdbc:mariadb://192.168.0.1:3306/testMariadb`<br/>`jdbc:mysql://192.168.0.1:3306/testMysql?serverTimezone=UTC`<br/>`jdbc:postgresql://192.168.0.1:5432/testPostgresql`<br/>`jdbc:timescaledb://192.168.0.1:5432/testPostgresql`<br/>`jdbc:sqlite:./testSqlite.db`.<br/><br/>If no database is available it will be created; for example the url `jdbc:h2:./testH2` creates a new H2 database in openHAB folder. Example to create your own MySQL database directly:<br/><br/>`CREATE DATABASE 'yourDB' CHARACTER SET utf8 COLLATE utf8_general_ci;` |
| user | | if needed | database user name |
| password | | if needed | database user password |
| errReconnectThreshold | 0 | No | when the service is deactivated (0 means ignore) |
* supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
*
* @author Riccardo Nimser-Joseph - Initial contribution
+ * @author Dan Cunningham - Fixes and refactoring
*/
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
- protected String sqlCreateHypertable;
-
- public JdbcTimescaledbDAO() {
- super();
-
- initSqlQueries();
- }
-
- public Properties getDatabaseProperties() {
- Properties properties = new Properties(this.databaseProps);
+ private final String sqlCreateHypertable = "SELECT created from create_hypertable('#tableName#', 'time')";
+ @Override
+ public Properties getConnectionProperties() {
+ Properties properties = (Properties) this.databaseProps.clone();
// Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
if (properties.containsKey("jdbcUrl")) {
properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
}
-
return properties;
}
+ @Override
public void doCreateItemTable(ItemVO vo) {
- String sql;
-
- sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateItemTable,
- new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
- new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
- this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
- Yank.execute(sql, null);
-
- sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
+ super.doCreateItemTable(vo);
+ String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
new String[] { vo.getTableName() });
this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
- Yank.execute(sql, null);
- }
-
- private void initSqlQueries() {
- this.logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
-
- this.sqlCreateHypertable = "SELECT create_hypertable('#tableName#', 'time')";
+ Yank.queryScalar(sql, Boolean.class, null);
}
}