Skip to content

Commit

Permalink
Fix parsing of collections of dates / functions
Browse files Browse the repository at this point in the history
Resolves: #7574
  • Loading branch information
luigidellaquila committed Jul 21, 2017
1 parent 56a498b commit cbf45af
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,14 @@ public static int getCollection(final String iText, final int iStartPosition, fi
if (buffer.length() == 0 && c == ' ')
continue;

if (c == iCollectionBegin) {
if (insideQuote == ' ' && (c == iCollectionBegin || c == '(')) {
// BEGIN
buffer.append(c);
deep++;
} else if (c == iCollectionEnd) {
} else if (insideQuote == ' ' && c == ')') {
buffer.append(c);
deep--;
} else if (insideQuote == ' ' && c == iCollectionEnd) {
// END
if (deep > 1)
buffer.append(c);
Expand Down Expand Up @@ -743,7 +746,9 @@ public static Map<String, String> getMap(final String iText) {
* Transforms, only if needed, the source string escaping the characters \ and ".
*
* @param iText Input String
*
* @return Modified string if needed, otherwise the same input object
*
* @see OStringSerializerHelper#decode(String)
*/
public static String encode(final String iText) {
Expand Down Expand Up @@ -782,7 +787,9 @@ public static String encode(final String iText) {
* Transforms, only if needed, the source string un-escaping the characters \ and ".
*
* @param iText Input String
*
* @return Modified string if needed, otherwise the same input object
*
* @see OStringSerializerHelper#encode(String)
*/
public static String decode(final String iText) {
Expand Down Expand Up @@ -854,9 +861,11 @@ public static OClass getRecordClassName(final String iValue, OClass iLinkedClass
* Use OIOUtils.getStringContent(iValue) instead.
*
* @param iValue
*
* @return
*/
@Deprecated public static String getStringContent(final Object iValue) {
@Deprecated
public static String getStringContent(final Object iValue) {
// MOVED
return OIOUtils.getStringContent(iValue);
}
Expand Down Expand Up @@ -891,6 +900,7 @@ else if (iValue instanceof String) {
* Checks if a string contains alphanumeric only characters.
*
* @param iContent String to check
*
* @return true is all the content is alphanumeric, otherwise false
*/
public static boolean isAlphanumeric(final String iContent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.orientechnologies.orient.core.sql;

/**
* Created by luigidellaquila on 20/07/17.
*/

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.junit.Assert.assertTrue;

public class TestListOfDates {

private final String className = "TestListOfDates";
private final String dateField = "date";
private final String dateTimeField = "dateTime";
private final String dateFormat = "yyyy-MM-dd";
private final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private final String dbUrl = "memory:TestListOfDates";
private final String dateValue = "2017-07-17";
private final String dateTimeValue = "2017-07-17 23:09:00";

private ODatabaseDocument db;

@Before
public void initDatabase() {
db = new ODatabaseDocumentTx(dbUrl);
if (!db.exists()) {
db.create();
}
openDatabase();
createClass();

}

@After
public void dropDatabase() {
openDatabase();
db.drop();
}

private void createClass() {
OClass testClass = db.getMetadata().getSchema().createClass(className);
testClass.createProperty(dateField, OType.DATE);
testClass.createProperty(dateTimeField, OType.DATETIME);
ODocument document = new ODocument(testClass.getName());

try {
document.field(dateField, new SimpleDateFormat(dateFormat).parse(dateValue));
document.field(dateTimeField, new SimpleDateFormat(dateTimeFormat).parse(dateTimeValue));
document.save();
} catch (ParseException e) {
e.printStackTrace();
}
db.commit();
}

@Test
public void testDateTimeCollectionPreparedStatement() throws ParseException {
openDatabase();
OSQLSynchQuery<List<ODocument>> query = new OSQLSynchQuery(
String.format("SELECT FROM %s WHERE %s IN :%s", className, dateTimeField, dateTimeField));
Map<String, Object> params = new HashMap();
Date date = new SimpleDateFormat(dateTimeFormat).parse(dateTimeValue);
params.put(dateTimeField, Arrays.asList(date));
assertTrue(db.query(query, params).size() == 1);
}

@Test
public void testDateCollectionPreparedStatement() throws ParseException {
openDatabase();
OSQLSynchQuery<List<ODocument>> query = new OSQLSynchQuery(
String.format("SELECT FROM %s WHERE %s IN :%s", className, dateField, dateField));

Map<String, Object> params = new HashMap();
Date date = new SimpleDateFormat(dateFormat).parse(dateValue);
params.put(dateField, Arrays.asList(date));
assertTrue(db.query(query, params).size() == 1);
}

private void openDatabase() {
if (db.isClosed()) {
db.open("admin", "admin");
}
}
}

0 comments on commit cbf45af

Please sign in to comment.