Commit 776b85a6 authored by wannes's avatar wannes
Browse files

Add PostgreSQL database type to fix SEQUENCE handling

parent 07f78434
......@@ -24,6 +24,7 @@ import be.neutrinet.ispng.mail.Generator;
import be.neutrinet.ispng.monitoring.Agent;
import be.neutrinet.ispng.util.MariaDBType;
import be.neutrinet.ispng.util.MySQLDBType;
import be.neutrinet.ispng.util.PostgreSQLType;
import be.neutrinet.ispng.util.Zookeeper;
import be.neutrinet.ispng.vpn.Manager;
import be.neutrinet.ispng.vpn.api.FlowServlet;
......@@ -119,6 +120,11 @@ public class VPN implements Daemon {
cfg.getProperty("db.user"),
cfg.getProperty("db.password"),
new MySQLDBType());
} else if (cfg.get("db.uri").toString().contains("postgresql")) {
cs = new JdbcConnectionSource(cfg.getProperty("db.uri"),
cfg.getProperty("db.user"),
cfg.getProperty("db.password"),
new PostgreSQLType());
} else {
cs = new JdbcConnectionSource(cfg.getProperty("db.uri"),
cfg.getProperty("db.user"),
......
package be.neutrinet.ispng.util;
import com.j256.ormlite.db.PostgresDatabaseType;
import com.j256.ormlite.field.FieldType;
import java.util.List;
/**
* Created by wannes on 6/30/15.
* <p>
* Apparently the built-in PostgreSQL class has either not been tested or is abandoned
* This class implements proper SEQUENCE creation and handling
*/
public class PostgreSQLType extends PostgresDatabaseType {
@Override
public void appendSelectNextValFromSequence(StringBuilder sb, String sequenceName) {
sb.append("SELECT NEXTVAL(");
sb.append('\'').append('\"').append(sequenceName).append('\"').append('\'');
sb.append(')');
}
@Override
protected void configureGeneratedIdSequence(StringBuilder sb, FieldType fieldType, List<String> statementsBefore,
List<String> additionalArgs, List<String> queriesAfter) {
String sequenceName = fieldType.getGeneratedIdSequence();
// added existence check
// !! this check will only detect if there is a something with the given name present,
// even though the object in question might not be a SEQUENCE
StringBuilder seqSb = new StringBuilder();
seqSb.append("do $$\nBEGIN\n");
seqSb.append("IF NOT EXISTS (SELECT 0 FROM pg_class where relname = '");
seqSb.append(sequenceName);
seqSb.append("' )\n" +
"THEN\n" +
"CREATE SEQUENCE ");
appendEscapedEntityName(seqSb, sequenceName);
seqSb.append(";\nEND IF;\nEND\n$$");
statementsBefore.add(seqSb.toString());
sb.append("DEFAULT NEXTVAL(");
// postgres needed this special escaping for NEXTVAL('"sequence-name"')
sb.append('\'').append('\"').append(sequenceName).append('\"').append('\'');
sb.append(") ");
// could also be the type serial for auto-generated sequences
// 8.2 also have the returning insert statement
configureId(sb, fieldType, statementsBefore, additionalArgs, queriesAfter);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment