Question

I am writing a systems application that has to integrate into an existing older architecture. In order to do this, I have to access a bitmask field in a table; something like so:

SELECT * FROM directory WHERE (status & 64) | (status & 256);

Our existing system runs on MySQL -- and we have a statement similar to the above that works just fine.

However, in my new application which I have to integrate with the existing system, I am using embedded-HSQL in my unit-tests. And for the life of me I cannot figure out how to do bitwise operations in HSQL. Furthermore, even if I am able to figure it out, I am starting to worry that there is not a single statement compatible between both SQL engines.

Any tips on how to go about this? At the moment I'm thinking I'll have to just select everything where status != 0 (limiting the result-set, of course) and then use java to pick-out the specific ones I want that match the status's I'm targeting. Yikes.

Was it helpful?

Solution 2

I never found a better solution to this than just making the effort to replace my embedded database from HSQL to MYSQL.

I also took the time to write better unit tests; ie: exercise my database logic against an actual database, but all other layers use mock database logic.

So that's my take away... run your DDL tests against the same database you are using in production and run all your other logic against DDL mocks.

For posterity, here's what I used to build my embedded MYSQL: http://zhentao-li.blogspot.com/2013/06/using-embedded-mysql-database-for-unit.html?m=1

OTHER TIPS

These operations are done using functions in HSQLDB.

http://hsqldb.org/doc/2.0/guide/builtinfunctions-chapt.html#bfc_numeric_functions

See BITOR, BITXOR, BITAND, BITNOT, BITANDNOT functions.

Bitwise operators are not very common in SQL dialects. MySQL is an exception rather than the norm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top