Skip to content

Commit

Permalink
fix: correct order by with detabase locale
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Dec 11, 2023
1 parent 732c59f commit 0b7b69b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.collate.OCollate;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.sql.OSQLEngine;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultInternal;
import java.text.Collator;
import java.util.Locale;
import java.util.Map;

Expand All @@ -23,6 +26,7 @@ public class OOrderByItem {

// calculated at run time
private OCollate collateStrategy;
private Collator stringCollator;

public String getAlias() {
return alias;
Expand Down Expand Up @@ -135,7 +139,27 @@ public int compare(OResult a, OResult b, OCommandContext ctx) {
}
} else if (bVal == null) {
result = 1;
} else if (aVal instanceof String && bVal instanceof String) {

ODatabaseDocumentInternal internal = ((ODatabaseDocumentInternal) ctx.getDatabase());
if (stringCollator == null) {
String language = (String) internal.get(ATTRIBUTES.LOCALELANGUAGE);
String country = (String) internal.get(ATTRIBUTES.LOCALECOUNTRY);
Locale locale;
if (language != null) {
if (country != null) {
locale = new Locale(language, country);
} else {
locale = new Locale(language);
}
} else {
locale = Locale.getDefault();
}
stringCollator = Collator.getInstance(locale);
}
result = stringCollator.compare(aVal, bVal);
} else if (aVal instanceof Comparable && bVal instanceof Comparable) {

try {
result = ((Comparable) aVal).compareTo(bVal);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,62 @@

import static org.junit.Assert.assertEquals;

import com.orientechnologies.BaseMemoryDatabase;
import com.orientechnologies.orient.core.db.ODatabase.ATTRIBUTES;
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.OClass.INDEX_TYPE;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.junit.Ignore;
import org.junit.Test;

public class TestOrderBy {
public class TestOrderBy extends BaseMemoryDatabase {

@Test
public void testGermanOrderBy() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:testGermanOrderBy");
db.set(ATTRIBUTES.LOCALECOUNTRY, Locale.GERMANY.getCountry());
db.set(ATTRIBUTES.LOCALELANGUAGE, Locale.GERMANY.getLanguage());
db.create();
try {
db.getMetadata().getSchema().createClass("test");
ORecord res1 = db.save(new ODocument("test").field("name", "Ähhhh"));
ORecord res2 = db.save(new ODocument("test").field("name", "Ahhhh"));
ORecord res3 = db.save(new ODocument("test").field("name", "Zebra"));
List<?> queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name"));
assertEquals(queryRes.get(0), res2);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res3);
db.getMetadata().getSchema().createClass("test");
ORecord res1 = db.save(new ODocument("test").field("name", "Ähhhh"));
ORecord res2 = db.save(new ODocument("test").field("name", "Ahhhh"));
ORecord res3 = db.save(new ODocument("test").field("name", "Zebra"));
List<OResult> queryRes =
db.query("select from test order by name").stream().collect(Collectors.toList());
assertEquals(queryRes.get(0).getIdentity().get(), res2.getIdentity());
assertEquals(queryRes.get(1).getIdentity().get(), res1.getIdentity());
assertEquals(queryRes.get(2).getIdentity().get(), res3.getIdentity());

queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name desc "));
assertEquals(queryRes.get(0), res3);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res2);
} finally {
db.drop();
}
queryRes =
db.query("select from test order by name desc ").stream().collect(Collectors.toList());
assertEquals(queryRes.get(0).getIdentity().get(), res3.getIdentity());
assertEquals(queryRes.get(1).getIdentity().get(), res1.getIdentity());
assertEquals(queryRes.get(2).getIdentity().get(), res2.getIdentity());
}

@Test
@Ignore
public void testGermanOrderByIndex() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:testGermanOrderBy");
db.set(ATTRIBUTES.LOCALECOUNTRY, Locale.GERMANY.getCountry());
db.set(ATTRIBUTES.LOCALELANGUAGE, Locale.GERMANY.getLanguage());
db.create();
try {
OClass clazz = db.getMetadata().getSchema().createClass("test");
clazz.createProperty("name", OType.STRING).createIndex(INDEX_TYPE.NOTUNIQUE);
ORecord res1 = db.save(new ODocument("test").field("name", "Ähhhh"));
ORecord res2 = db.save(new ODocument("test").field("name", "Ahhhh"));
ORecord res3 = db.save(new ODocument("test").field("name", "Zebra"));
List<?> queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name"));
assertEquals(queryRes.get(0), res2);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res3);
OClass clazz = db.getMetadata().getSchema().createClass("test");
clazz.createProperty("name", OType.STRING).createIndex(INDEX_TYPE.NOTUNIQUE);
ORecord res1 = db.save(new ODocument("test").field("name", "Ähhhh"));
ORecord res2 = db.save(new ODocument("test").field("name", "Ahhhh"));
ORecord res3 = db.save(new ODocument("test").field("name", "Zebra"));
List<OResult> queryRes = db.query("select from test order by name").stream().collect(Collectors.toList());
assertEquals(queryRes.get(0).getIdentity().get(), res2.getIdentity());
assertEquals(queryRes.get(1).getIdentity().get(), res1.getIdentity());
assertEquals(queryRes.get(2).getIdentity().get(), res3.getIdentity());

queryRes = db.query(new OSQLSynchQuery<Object>("select from test order by name desc "));
assertEquals(queryRes.get(0), res3);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res2);
} finally {
db.drop();
}
queryRes = db.query("select from test order by name desc ").stream().collect(Collectors.toList());
assertEquals(queryRes.get(0), res3);
assertEquals(queryRes.get(1), res1);
assertEquals(queryRes.get(2), res2);
}
}

0 comments on commit 0b7b69b

Please sign in to comment.