Skip to content

Commit c29c693

Browse files
committed
Create even table automatically
1 parent 58ff65e commit c29c693

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/stat/impl/JDBCCollector.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.sql.Connection;
2525
import java.sql.PreparedStatement;
2626
import java.sql.SQLException;
27+
import java.sql.Statement;
2728
import java.sql.Timestamp;
2829

2930
import org.springframework.core.env.Environment;
@@ -63,7 +64,7 @@ public class JDBCCollector extends AbstractDbCollector {
6364
private Environment environment;
6465

6566
@PostConstruct
66-
public void init() {
67+
public void init() throws IOException {
6768
String baseURL = environment.getProperty("proxy.usage-stats-url");
6869
String username = environment.getProperty("proxy.usage-stats-username", "monetdb");
6970
String password = environment.getProperty("proxy.usage-stats-password", "monetdb");
@@ -96,7 +97,29 @@ public void init() {
9697
if (maximumPoolSize != null) {
9798
ds.setMaximumPoolSize(maximumPoolSize);
9899
}
99-
100+
101+
// create table if not already exists
102+
try (Connection con = ds.getConnection()) {
103+
Statement statement = con.createStatement();
104+
if (con.getMetaData().getDatabaseProductName().equals("Microsoft SQL Server")) {
105+
statement.execute(
106+
"IF OBJECT_ID('event', 'U') IS NULL" +
107+
" create table event(" +
108+
" event_time datetime," +
109+
" username varchar(128)," +
110+
" type varchar(128)," +
111+
" data text)");
112+
} else {
113+
statement.execute(
114+
"create table if not exists event(" +
115+
" event_time timestamp," +
116+
" username varchar(128)," +
117+
" type varchar(128)," +
118+
" data text)");
119+
}
120+
} catch (SQLException e) {
121+
throw new IOException("Exception while logging stats", e);
122+
}
100123
}
101124

102125
@Override

0 commit comments

Comments
 (0)