How does Empire-DB handle '' (empty string)?

Empire-DB internally treats all empty strings as null and this behavior is by design.
Some databases accept empty strings as a valid value for a text column, others don’t.
However allowing empty strings is a major reason for many database problems and changing the empire-db code here would have a major impact on existing projects.
We have thought about this a long time ago and we came to the conclusion that empty strings are evil and should be avoided completely.
In fact NULL has a logical meaning of a value not being supplied for a field but what different logical meaning would an empty string have instead?

In some rare cases however you might need to explicitly use an empty string rather than null.
One of it is with existing databases when you want to search for field that contain an empty string. In this case (and others) you must explicitly say that you want to use an empty string. This is possible through the DBDatabase.EMPTY_STRING constant.

The following example shows how to replace all empty strings of a column by null:

DBCommand cmd = db.createCommand();           
cmd.set  ( null ));
cmd.where( DBDatabase.EMPTY_STRING ));
db.executeSQL(cmd.getUpdate(), conn);

This will generate:


Is DBDatabase (and related DBTable, DBColumn) thread-safe?

We use empire-db a lot in Web applications which of course use mutlithreaded requests. Normally the data model definition is static i.e. once it is set up, it is not changing and thus accessing it from multiple threads is not a problem. All stateful objects used to access and manipulate data such as DBRecord and DBReader are allocated on a per thread basis. However, if you plan to make dynamic changes to your data model at runtime you will have to make sure that these changes are thread-safe. This must be done at application level.

Should DBDatabase be reopened on every connection or it is enough to open it only once?

The conneciton used when opening the database is not stored with the database. It's purpose is soley to allow the driver to perform intialization functions and may not be used at all. Hence the conneciton may even be null, when opening the database. The open function on the database must only be called once in order to attach the driver. You may then use as many connections as you want - everytime a connection is needed you must supply it with the API call.
In our (web-)projects we usually obtain a connection from a connection pool managed by the application server on a per request basis.

See these projects for more info about connection pools:

Do you have a Scala integration module?

None of us has experience with Scala, but feel free to contribute. This blog post might also help as it points to a project that integrates Scala and empire-db:

How do I set the maximum number of rows returned

cmd.skipRows(…) //to set the offset.
  • No labels