Conditional Selection

#1

I have an Employee class that holds...

private String firstName = "-";
private String middleName = "-";
private String lastName = "-";

I do not require that an Employee's middle name be provided so when I use...

SELECT firstName + " " + middleName + " " + lastName FROM Employee
    ORDER BY lastname, firstname, middleName

I may end up with the result list of...

Jane K Doe
John - Doe
Jacob B Smith

I'd prefer if I could have something similar to this in Query form...

ArrayList<String> list = new ArrayList<String>();

for(Employee emp : Employee.allEmployees())
    if(emp.getMiddleName.equals("-"))
        list.add(emp.getFirstName() + " " + emp.getLastName());
    else
        list.add(emp.getFullName());

alphabetizeList(list);  //lastName, firstName, middleName

The results in list will be...

Jane K Doe
John - Doe
Jacob B Smith

Can this same result list be constructed purely using an ObjectDB Query?

Thank you,
Dragon

#2

Possible solutions:

  1. Use a result class that will receive firstName, middleName and lastName and will have a toString() implementation that composes the correct name.
  2. Use a report query that returns firstName, middleName and lastName as separate fields and then process the results for presentation in your code, after getting the query results.
  3. Maintain an additional field. e.g. displayName.
  4. Use a method in the SELECT clause. It could be a static method that receives firstName, middleName and lastName and returns a string for display or an Employee method (e.g. toString). If client-server mode is used then the method must be available also on the server side (by running the server with the correct classpath).
ObjectDB Support
#3

Interesting solutions. I've chosen to use the additional field option as it should only contribute an extra 20 - 24 bytes on average per entry to the database given what I have to work with. However, I did note your first solution of the result class as a way to allow java garbage collection to remove these objects when the program terminates so as to not persist them unless necessary.

Thank you for the suggestions,
Dragon

Reply