[jdbc] Rework TimescaleDB code to actually work (#12525)

Fixes #12513

Signed-off-by: Dan Cunningham <dan@digitaldan.com>
pull/12568/head
Dan Cunningham 2022-04-02 09:11:32 -07:00 committed by GitHub
parent 1239dda691
commit 5712de5e63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 28 deletions

View File

@ -40,7 +40,7 @@ This service can be configured in the file `services/jdbc.cfg`.
| Property | Default | Required | Description | | 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 | | user | | if needed | database user name |
| password | | if needed | database user password | | password | | if needed | database user password |
| errReconnectThreshold | 0 | No | when the service is deactivated (0 means ignore) | | errReconnectThreshold | 0 | No | when the service is deactivated (0 means ignore) |

View File

@ -25,47 +25,29 @@ import org.slf4j.LoggerFactory;
* supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO. * supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
* *
* @author Riccardo Nimser-Joseph - Initial contribution * @author Riccardo Nimser-Joseph - Initial contribution
* @author Dan Cunningham - Fixes and refactoring
*/ */
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO { public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class); private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
protected String sqlCreateHypertable; private final String sqlCreateHypertable = "SELECT created from create_hypertable('#tableName#', 'time')";
public JdbcTimescaledbDAO() {
super();
initSqlQueries();
}
public Properties getDatabaseProperties() {
Properties properties = new Properties(this.databaseProps);
@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 // Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
if (properties.containsKey("jdbcUrl")) { if (properties.containsKey("jdbcUrl")) {
properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql")); properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("timescaledb", "postgresql"));
} }
return properties; return properties;
} }
@Override
public void doCreateItemTable(ItemVO vo) { public void doCreateItemTable(ItemVO vo) {
String sql; super.doCreateItemTable(vo);
String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
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#" },
new String[] { vo.getTableName() }); new String[] { vo.getTableName() });
this.logger.debug("JDBC::doCreateItemTable sql={}", sql); this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
Yank.execute(sql, null); Yank.queryScalar(sql, Boolean.class, null);
}
private void initSqlQueries() {
this.logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
this.sqlCreateHypertable = "SELECT create_hypertable('#tableName#', 'time')";
} }
} }