[New] Implemented querying of StringParameter
This commit is contained in:
parent
65402e9006
commit
c7ad5d21c2
|
@ -39,6 +39,9 @@ import ch.eitchnet.utils.collections.DateRange;
|
|||
*/
|
||||
public class PostgreSqlAuditDao implements AuditDao {
|
||||
|
||||
public static final String FIELDS = "id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type";
|
||||
public static final String TABLE_NAME = "audits";
|
||||
|
||||
private PostgreSqlStrolchTransaction tx;
|
||||
|
||||
/**
|
||||
|
@ -50,7 +53,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public boolean hasElement(String type, Long id) {
|
||||
String sql = "select count(*) from audits where element_type = ? and id = ?"; //$NON-NLS-1$
|
||||
String sql = "select count(*) from " + TABLE_NAME + " where element_type = ? and id = ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, type);
|
||||
|
@ -75,7 +78,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public long querySize(DateRange dateRange) {
|
||||
String sql = "select count(*) from audits where date between ? and ?"; //$NON-NLS-1$
|
||||
String sql = "select count(*) from " + TABLE_NAME + " where date between ? and ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
statement.setDate(1, new Date(dateRange.getFromDate().getTime()), Calendar.getInstance());
|
||||
|
@ -93,7 +96,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public long querySize(String type, DateRange dateRange) {
|
||||
String sql = "select count(*) from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
String sql = "select count(*) from " + TABLE_NAME + " where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, type);
|
||||
|
@ -114,7 +117,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
public Set<String> queryTypes() {
|
||||
Set<String> keySet = new HashSet<>();
|
||||
|
||||
String sql = "select distinct element_type from audits"; //$NON-NLS-1$
|
||||
String sql = "select distinct element_type from " + TABLE_NAME; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
while (result.next()) {
|
||||
|
@ -131,7 +134,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
@Override
|
||||
public Audit queryBy(String type, Long id) {
|
||||
|
||||
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and id = ?"; //$NON-NLS-1$
|
||||
String sql = "select " + FIELDS + " from " + TABLE_NAME + " where element_type = ? and id = ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, type);
|
||||
|
@ -154,7 +157,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
@Override
|
||||
public List<Audit> queryAll(String type, DateRange dateRange) {
|
||||
List<Audit> list = new ArrayList<>();
|
||||
String sql = "select id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
String sql = "select " + FIELDS + " from " + TABLE_NAME + " where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement statement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, type);
|
||||
|
@ -176,7 +179,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public void save(Audit audit) {
|
||||
String sql = "insert into audits (id, username, firstname, lastname, date, element_type, element_accessed, new_version, action, access_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?::access_type)"; //$NON-NLS-1$
|
||||
String sql = "insert into " + TABLE_NAME + " (" + FIELDS + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?::access_type)"; //$NON-NLS-1$
|
||||
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
setAuditFields(audit, preparedStatement);
|
||||
|
@ -202,7 +205,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public void update(Audit audit) {
|
||||
String sql = "update audits set id = ?, username = ?, firstname = ?, lastname = ?, date = ?, element_type = ?, element_accessed = ?, new_version = ?, action = ?, access_type = ?::access_type where id = ?"; //$NON-NLS-1$
|
||||
String sql = "update " + TABLE_NAME + " set id = ?, username = ?, firstname = ?, lastname = ?, date = ?, element_type = ?, element_accessed = ?, new_version = ?, action = ?, access_type = ?::access_type where id = ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
setAuditFields(audit, preparedStatement);
|
||||
|
@ -229,7 +232,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public void remove(Audit audit) {
|
||||
String sql = "delete from audits where id = ?"; //$NON-NLS-1$
|
||||
String sql = "delete from " + TABLE_NAME + " where id = ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
preparedStatement.setLong(1, audit.getId());
|
||||
|
@ -256,7 +259,7 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public long removeAll(String type, DateRange dateRange) {
|
||||
String sql = "delete from audits where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
String sql = "delete from " + TABLE_NAME + " where element_type = ? and date between ? and ?"; //$NON-NLS-1$
|
||||
try (PreparedStatement preparedStatement = this.tx.getConnection().prepareStatement(sql)) {
|
||||
|
||||
preparedStatement.setString(1, type);
|
||||
|
@ -274,7 +277,9 @@ public class PostgreSqlAuditDao implements AuditDao {
|
|||
|
||||
@Override
|
||||
public <U> List<U> doQuery(AuditQuery query, AuditVisitor<U> auditVisitor) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
PostgreSqlAuditQueryVisitor queryVisitor = new PostgreSqlAuditQueryVisitor(FIELDS);
|
||||
query.accept(queryVisitor);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package li.strolch.persistence.postgresql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ch.eitchnet.utils.StringMatchMode;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class PostgreSqlHelper {
|
||||
|
||||
public static String toSql(String indent, StringMatchMode mm, List<Object> values, String... query) {
|
||||
|
||||
// CS EQ
|
||||
// 1. x x
|
||||
// 2. x o
|
||||
// 3. o x
|
||||
// 4. o o
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (mm.isCaseSensitve() && mm.isEquals()) {
|
||||
if (query.length == 1) {
|
||||
sb.append("name = ?\n");
|
||||
values.add(query[0]);
|
||||
} else {
|
||||
sb.append("name in ( ");
|
||||
for (int i = 0; i < query.length; i++) {
|
||||
sb.append("?");
|
||||
values.add(query[i]);
|
||||
if (i < query.length - 1)
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(" )\n");
|
||||
}
|
||||
} else if (!mm.isCaseSensitve() && mm.isEquals()) {
|
||||
if (query.length == 1) {
|
||||
sb.append("lower(name) = ?\n");
|
||||
values.add(query[0].toLowerCase());
|
||||
} else {
|
||||
sb.append("lower(name) in ( ");
|
||||
for (int i = 0; i < query.length; i++) {
|
||||
sb.append("?");
|
||||
values.add(query[i].toLowerCase());
|
||||
if (i < query.length - 1)
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(" )\n");
|
||||
}
|
||||
} else if (!mm.isEquals() && mm.isCaseSensitve()) {
|
||||
if (query.length == 1) {
|
||||
sb.append("name like ?\n");
|
||||
values.add("%" + query[0] + "%");
|
||||
} else {
|
||||
sb.append("(\n");
|
||||
for (int i = 0; i < query.length; i++) {
|
||||
sb.append(indent);
|
||||
sb.append(" ");
|
||||
sb.append("name like ?");
|
||||
values.add("%" + query[i] + "%");
|
||||
if (i < query.length - 1)
|
||||
sb.append(" or");
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append(")\n");
|
||||
}
|
||||
} else {
|
||||
if (query.length == 1) {
|
||||
sb.append("lower(name) like ?\n");
|
||||
values.add("%" + query[0].toLowerCase() + "%");
|
||||
} else {
|
||||
sb.append("(\n");
|
||||
for (int i = 0; i < query.length; i++) {
|
||||
sb.append(indent);
|
||||
sb.append(" ");
|
||||
sb.append("lower(name) like ?");
|
||||
values.add("%" + query[i].toLowerCase() + "%");
|
||||
if (i < query.length - 1)
|
||||
sb.append(" or");
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append(")\n");
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Object> values = new ArrayList<>();
|
||||
String sql = toSql(" ", StringMatchMode.CONTAINS_CASE_INSENSITIVE, values, "foo", "bar", "fub");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.CONTAINS_CASE_INSENSITIVE, values, "foo");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.CONTAINS_CASE_SENSITIVE, values, "foo", "bar", "fub");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.CONTAINS_CASE_SENSITIVE, values, "foo");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.EQUALS_CASE_INSENSITIVE, values, "foo", "bar", "fub");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.EQUALS_CASE_INSENSITIVE, values, "foo");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.EQUALS_CASE_SENSITIVE, values, "foo", "bar", "fub");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
|
||||
sql = toSql(" ", StringMatchMode.EQUALS_CASE_SENSITIVE, values, "foo");
|
||||
System.out.println(sql);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
|
@ -15,10 +15,13 @@
|
|||
*/
|
||||
package li.strolch.persistence.postgresql;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import li.strolch.model.Tags;
|
||||
import li.strolch.model.query.DateSelection;
|
||||
import li.strolch.model.query.OrderQueryVisitor;
|
||||
import li.strolch.model.query.StateSelection;
|
||||
import ch.eitchnet.utils.collections.DateRange;
|
||||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
|
@ -42,15 +45,34 @@ public class PostgreSqlOrderQueryVisitor extends PostgreSqlQueryVisitor implemen
|
|||
|
||||
@Override
|
||||
public void visit(DateSelection selection) {
|
||||
|
||||
// TODO handle inclusive and exclusive date ranges
|
||||
|
||||
DateRange dateRange = selection.getDateRange();
|
||||
if (dateRange.isDate()) {
|
||||
sb.append(indent);
|
||||
sb.append("date = ?\n");
|
||||
values.add(selection.getDate());
|
||||
values.add(new Date(dateRange.getFromDate().getTime()));
|
||||
} else if (dateRange.isBounded()) {
|
||||
sb.append(indent);
|
||||
sb.append("date between ? and ?\n");
|
||||
values.add(new Date(dateRange.getFromDate().getTime()));
|
||||
values.add(new Date(dateRange.getToDate().getTime()));
|
||||
} else if (dateRange.isToBounded()) {
|
||||
sb.append(indent);
|
||||
sb.append("date < ?\n");
|
||||
values.add(new Date(dateRange.getToDate().getTime()));
|
||||
} else if (dateRange.isFromBounded()) {
|
||||
sb.append(indent);
|
||||
sb.append("date > ?\n");
|
||||
values.add(new Date(dateRange.getFromDate().getTime()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(StateSelection selection) {
|
||||
sb.append(indent);
|
||||
sb.append("stae = ?\n");
|
||||
sb.append("state = ?::order_state\n");
|
||||
values.add(selection.getState().name());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package li.strolch.persistence.postgresql;
|
||||
|
||||
import static li.strolch.persistence.postgresql.PostgreSqlHelper.toSql;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -96,13 +98,6 @@ public abstract class PostgreSqlQueryVisitor implements StrolchElementSelectionV
|
|||
return this.any;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the values
|
||||
*/
|
||||
public List<Object> getValues() {
|
||||
return this.values;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
@ -148,27 +143,8 @@ public abstract class PostgreSqlQueryVisitor implements StrolchElementSelectionV
|
|||
public void visit(NameSelection selection) {
|
||||
this.sb.append(this.indent);
|
||||
String name = selection.getName();
|
||||
|
||||
// CS EQ
|
||||
// 1. x x
|
||||
// 2. x o
|
||||
// 3. o x
|
||||
// 4. o o
|
||||
|
||||
StringMatchMode mm = selection.getMatchMode();
|
||||
if (mm.isCaseSensitve() && mm.isEquals()) {
|
||||
this.sb.append("name = ?\n");
|
||||
this.values.add(name);
|
||||
} else if (!mm.isCaseSensitve() && mm.isEquals()) {
|
||||
this.sb.append("lower(name) = lower(?)\n");
|
||||
this.values.add(name);
|
||||
} else if (!mm.isEquals() && mm.isCaseSensitve()) {
|
||||
this.sb.append("name like ?");
|
||||
this.values.add("%" + name + "%");
|
||||
} else {
|
||||
this.sb.append("lower(name) like ?");
|
||||
this.values.add(name.toLowerCase());
|
||||
}
|
||||
this.sb.append(toSql(this.indent, mm, this.values, name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -240,8 +216,8 @@ public abstract class PostgreSqlQueryVisitor implements StrolchElementSelectionV
|
|||
}
|
||||
|
||||
private void xpath(String bagKey, String paramKey, String paramValue) {
|
||||
this.sb.append(this.indent);
|
||||
String xpath = "cast(xpath('//Resource/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\" and @Value=\"${paramValue}\"]', asxml) as text[]) != '{}'\n";
|
||||
this.sb.append(this.indent);
|
||||
xpath = xpath.replace("${bagKey}", bagKey);
|
||||
xpath = xpath.replace("${paramKey}", paramKey);
|
||||
xpath = xpath.replace("${paramValue}", paramValue);
|
||||
|
@ -250,7 +226,50 @@ public abstract class PostgreSqlQueryVisitor implements StrolchElementSelectionV
|
|||
|
||||
@Override
|
||||
public void visit(StringParameterSelection selection) {
|
||||
xpath(selection.getBagKey(), selection.getParamKey(), selection.getValue());
|
||||
String value = selection.getValue();
|
||||
|
||||
String xpath = "xpath('//Resource/ParameterBag[@Id=\"${bagKey}\"]/Parameter[@Id=\"${paramKey}\"]/@Value', asxml))::TEXT AS content";
|
||||
xpath = xpath.replace("${bagKey}", selection.getBagKey());
|
||||
xpath = xpath.replace("${paramKey}", selection.getParamKey());
|
||||
|
||||
sb.append(this.indent);
|
||||
sb.append("id in (\n");
|
||||
sb.append(this.indent);
|
||||
sb.append(" SELECT id\n");
|
||||
sb.append(this.indent);
|
||||
sb.append(" FROM (\n");
|
||||
sb.append(this.indent);
|
||||
sb.append(" SELECT id, UNNEST(");
|
||||
sb.append(xpath);
|
||||
sb.append("\n");
|
||||
sb.append(this.indent);
|
||||
sb.append("from ");
|
||||
sb.append(getTableName());
|
||||
sb.append("\n");
|
||||
sb.append(this.indent);
|
||||
sb.append(") AS alias\n");
|
||||
sb.append(this.indent);
|
||||
sb.append("WHERE ");
|
||||
|
||||
if (selection.getMatchMode().isEquals()) {
|
||||
if (selection.getMatchMode().isCaseSensitve()) {
|
||||
sb.append("content = ?\n");
|
||||
} else {
|
||||
sb.append("content ILIKE ?\n");
|
||||
}
|
||||
} else {
|
||||
value = "%" + value + "%";
|
||||
if (selection.getMatchMode().isCaseSensitve()) {
|
||||
sb.append("content LIKE ?\n");
|
||||
} else {
|
||||
sb.append("content ILIKE ?\n");
|
||||
}
|
||||
}
|
||||
|
||||
sb.append(this.indent);
|
||||
sb.append(")\n");
|
||||
|
||||
this.values.add(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -15,8 +15,16 @@
|
|||
*/
|
||||
package li.strolch.persistence.postgresql.dao.test;
|
||||
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.CONFIG_SRC;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_PASSWORD;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_STORE_PATH_DIR;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_URL;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_USERNAME;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.RUNTIME_PATH;
|
||||
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.dropSchema;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
|
@ -24,24 +32,39 @@ import java.sql.ResultSet;
|
|||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import li.strolch.agent.api.OrderMap;
|
||||
import li.strolch.agent.api.ResourceMap;
|
||||
import li.strolch.agent.api.StrolchRealm;
|
||||
import li.strolch.model.ModelGenerator;
|
||||
import li.strolch.model.State;
|
||||
import li.strolch.model.query.DateSelection;
|
||||
import li.strolch.model.query.IdSelection;
|
||||
import li.strolch.model.query.NameSelection;
|
||||
import li.strolch.model.query.OrSelection;
|
||||
import li.strolch.model.query.OrderQuery;
|
||||
import li.strolch.model.query.ParameterSelection;
|
||||
import li.strolch.model.query.ResourceQuery;
|
||||
import li.strolch.model.query.StateSelection;
|
||||
import li.strolch.model.query.StrolchTypeNavigation;
|
||||
import li.strolch.persistence.api.StrolchTransaction;
|
||||
import li.strolch.persistence.postgresql.PostgreSqlOrderQueryVisitor;
|
||||
import li.strolch.persistence.postgresql.PostgreSqlQueryVisitor;
|
||||
import li.strolch.persistence.postgresql.PostgreSqlResourceQueryVisitor;
|
||||
import li.strolch.runtime.StrolchConstants;
|
||||
import li.strolch.testbase.runtime.RuntimeMock;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ch.eitchnet.privilege.model.Certificate;
|
||||
import ch.eitchnet.utils.StringMatchMode;
|
||||
|
||||
/**
|
||||
|
@ -50,6 +73,65 @@ import ch.eitchnet.utils.StringMatchMode;
|
|||
public class QueryTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(QueryTest.class);
|
||||
private static RuntimeMock runtimeMock;
|
||||
|
||||
private static Date past;
|
||||
private static Date earlier;
|
||||
private static Date current;
|
||||
private static Date later;
|
||||
private static Date future;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws SQLException {
|
||||
|
||||
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
|
||||
|
||||
File rootPath = new File(RUNTIME_PATH);
|
||||
File configSrc = new File(CONFIG_SRC);
|
||||
runtimeMock = new RuntimeMock();
|
||||
runtimeMock.mockRuntime(rootPath, configSrc);
|
||||
new File(rootPath, DB_STORE_PATH_DIR).mkdir();
|
||||
runtimeMock.startContainer();
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.clear();
|
||||
cal.set(2000, 1, 1);
|
||||
past = cal.getTime();
|
||||
cal.set(2000, 4, 1);
|
||||
earlier = cal.getTime();
|
||||
cal.set(2000, 6, 1);
|
||||
current = cal.getTime();
|
||||
cal.set(2000, 8, 1);
|
||||
later = cal.getTime();
|
||||
cal.set(2000, 11, 1);
|
||||
future = cal.getTime();
|
||||
|
||||
Certificate cert = runtimeMock.getPrivilegeHandler().authenticate("test", "test".getBytes());
|
||||
StrolchRealm realm = runtimeMock.getRealm(StrolchConstants.DEFAULT_REALM);
|
||||
try (StrolchTransaction tx = realm.openTx(cert, "test")) {
|
||||
OrderMap orderMap = tx.getOrderMap();
|
||||
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@1", "Order 1", "MyType1", earlier, State.CREATED));
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@2", "Order 2", "MyType1", current, State.OPEN));
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@3", "Order 3", "MyType1", later, State.CLOSED));
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@4", "Order 4", "MyType2", earlier, State.CREATED));
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@5", "Order 5", "MyType2", current, State.OPEN));
|
||||
orderMap.add(tx, ModelGenerator.createOrder("@6", "Order 6", "MyType2", later, State.CLOSED));
|
||||
|
||||
ResourceMap resourceMap = tx.getResourceMap();
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@1", "Resource 1", "MyType1"));
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@2", "Resource 2", "MyType1"));
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@3", "Resource 3", "MyType1"));
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@4", "Resource 4", "MyType2"));
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@5", "Resource 5", "MyType2"));
|
||||
resourceMap.add(tx, ModelGenerator.createResource("@6", "Resource 6", "MyType2"));
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
runtimeMock.destroyRuntime();
|
||||
}
|
||||
|
||||
public Connection openConn() throws SQLException {
|
||||
String url = "jdbc:postgresql://localhost/testdb";
|
||||
|
@ -63,54 +145,110 @@ public class QueryTest {
|
|||
@Test
|
||||
public void shouldQueryOrderAll() throws SQLException {
|
||||
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("QTestType1"));
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.withAny();
|
||||
performOrderQuery(query, Arrays.asList("myTestOrder1"));
|
||||
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceAll() throws SQLException {
|
||||
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
|
||||
query.withAny();
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@4", "@5", "@6"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryOrderByDate() throws SQLException {
|
||||
|
||||
// range
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(earlier, false).to(later, false));
|
||||
performOrderQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
|
||||
// equals current
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(current, false).to(current, false));
|
||||
performOrderQuery(query, Arrays.asList("@2"));
|
||||
|
||||
// equals later
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(later, false).to(later, false));
|
||||
performOrderQuery(query, Arrays.<String> asList("@3"));
|
||||
|
||||
// equals earlier
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(earlier, false).to(earlier, false));
|
||||
performOrderQuery(query, Arrays.<String> asList("@1"));
|
||||
|
||||
// past
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().to(past, false));
|
||||
performOrderQuery(query, Arrays.<String> asList());
|
||||
|
||||
// future
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(future, false));
|
||||
performOrderQuery(query, Arrays.<String> asList());
|
||||
|
||||
// earlier
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(past, false).to(earlier, true));
|
||||
performOrderQuery(query, Arrays.<String> asList("@1"));
|
||||
|
||||
// later
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new DateSelection().from(later, false).to(future, true));
|
||||
performOrderQuery(query, Arrays.<String> asList("@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryOrderByState() throws SQLException {
|
||||
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new StateSelection(State.CREATED));
|
||||
performOrderQuery(query, Arrays.asList("@1"));
|
||||
|
||||
query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new StateSelection(State.OPEN));
|
||||
performOrderQuery(query, Arrays.<String> asList("@2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryOrder1() throws SQLException {
|
||||
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("QTestType1"));
|
||||
query.and().with(new IdSelection("myTestOrder1", "@2"),
|
||||
new NameSelection("Test Name", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performOrderQuery(query, Arrays.asList("myTestOrder1"));
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(new IdSelection("@1", "@2"),
|
||||
new NameSelection("Order 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performOrderQuery(query, Arrays.asList("@1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryOrder2() throws SQLException {
|
||||
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("QTestType1"));
|
||||
query.or().with(new IdSelection("myTestOrder1", "@2"),
|
||||
new NameSelection("Test Name", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performOrderQuery(query, Arrays.asList("myTestOrder1"));
|
||||
OrderQuery query = new OrderQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.or().with(new IdSelection("@1", "@2"),
|
||||
new NameSelection("order 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performOrderQuery(query, Arrays.asList("@1", "@2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResource1() throws SQLException {
|
||||
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.or().with(new IdSelection("@_00000000", "@_00000001"),
|
||||
new NameSelection("Test Name", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001"));
|
||||
query.or().with(new IdSelection("@1", "@2"),
|
||||
new NameSelection("Resource 1", StringMatchMode.EQUALS_CASE_SENSITIVE));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResource2() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(
|
||||
new OrSelection(new IdSelection("@_00000000"), new IdSelection("@_00000001")),
|
||||
new OrSelection(new NameSelection("My Resource 0", StringMatchMode.EQUALS_CASE_SENSITIVE),
|
||||
new NameSelection("My Resource 1", StringMatchMode.EQUALS_CASE_SENSITIVE)));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001"));
|
||||
new OrSelection(new IdSelection("@1"), new IdSelection("@2")),
|
||||
new OrSelection(new NameSelection("Resource 1", StringMatchMode.EQUALS_CASE_SENSITIVE),
|
||||
new NameSelection("Resource 2", StringMatchMode.EQUALS_CASE_SENSITIVE)));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -127,34 +265,34 @@ public class QueryTest {
|
|||
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(ParameterSelection.booleanSelection("@bag01", "@param1", true));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByFloagParam() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(ParameterSelection.floatSelection("@bag01", "@param2", 44.3));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByIntegerParam() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(ParameterSelection.integerSelection("@bag01", "@param3", 77));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByLongParam() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType2"));
|
||||
query.and().with(ParameterSelection.longSelection("@bag01", "@param4", 4453234566L));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@4", "@5", "@6"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldQueryResourceByStringParam() throws SQLException {
|
||||
|
||||
List<String> expected = Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004");
|
||||
List<String> expected = Arrays.asList("@1", "@2", "@3");
|
||||
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(
|
||||
|
@ -185,7 +323,7 @@ public class QueryTest {
|
|||
public void shouldQueryResourceByDateParam() throws SQLException {
|
||||
ResourceQuery query = new ResourceQuery(new StrolchTypeNavigation("MyType1"));
|
||||
query.and().with(ParameterSelection.dateSelection("@bag01", "@param6", new Date(1354295525628L)));
|
||||
performResourceQuery(query, Arrays.asList("@_00000000", "@_00000001", "@_00000002", "@_00000003", "@_00000004"));
|
||||
performResourceQuery(query, Arrays.asList("@1", "@2", "@3"));
|
||||
|
||||
}
|
||||
|
||||
|
@ -200,7 +338,7 @@ public class QueryTest {
|
|||
PostgreSqlResourceQueryVisitor visitor = new PostgreSqlResourceQueryVisitor("id");
|
||||
query.accept(visitor);
|
||||
List<String> ids = queryIds(visitor);
|
||||
assertEquals(expected, ids);
|
||||
assertEquals(new HashSet<>(expected), new HashSet<>(ids));
|
||||
}
|
||||
|
||||
private List<String> queryIds(PostgreSqlQueryVisitor visitor) throws SQLException {
|
||||
|
|
Loading…
Reference in New Issue