Retrieving the previous and the next record of a selected record (SQL)

Let’s assume we have selected a record (record_id = 7) and we want to select its previous and next record.

A very naive approach would be to add “1” (and to subtract “1”) to the record_id:
SELECT * FROM table WHERE record_id = (7-1) # selects previous record
SELECT * FROM table WHERE record_id = (7+1) # selects next record

However, this does not work in many scenarios, e.g., if record_id is not a number or if numbers are “missing” (…,4,5,7,8,…). Therefore, a more sophisticated query is needed.

One possibility is to explicitly sort the records according to the record_id, add a condition which selects only records that have a record_id that is greater (or less) than the record_id of the selected record, and finally select only one record:
SELECT * FROM table WHERE record_id < 7 ORDER BY record_id DESC LIMIT 1 # selects previous record
SELECT * FROM table WHERE record_id > 7 ORDER BY record_id ASC LIMIT 1 # selects next record

Fixing the NULL Pointer of the OculusWorldDemo [Oculus Rift SDK 0.4.3, Windows] when running in OpenGL mode

The OculusWorldDemo is little demo application which is shipped with the Oculus Rift SDK and demonstrates some features of the Rift. Recently, I (and some more people) came across a nasty exception while trying out the OculusWorldDemo in OpenGL mode.

When starting the demo, the demo breaks when trying to retrieve the OpenGL version of my system (Line 1901 in Render_GL_Device.cpp):
const char* version = (const char*)glGetString(GL_VERSION);

The function returns NULL and a few lines later an assert checks for the version and throws an exception. For retrieving a valid OpenGL version, one must create a valid OpenGL context before calling the glGetString function. In OculusWorldDemo this happens in Render_GL_Win32_Device.cpp at line 145 (HGLRC context = wglCreateContextAttribsARB(dc, 0, attribs);).
On my system (Windows 7, GeForce GTX 560 Ti), wglCreateContextAttribsARB fails to create a context and returns a NULL pointer. There are more than one reason for getting a NULL pointer here. Long story short, you can replace the function call by calling wglCreateContext instead. The function has only one argument (the handle to the device context) which is why we do not have to add some additional code.
Changing line 145 to HGLRC context = wglCreateContext(dc); does the trick. However, a proper solution would be to call wglCreateContext only if wglCreateContextAttribsARB fails to create a context. 😉

How to map String to TEXT instead of String to VARCHAR(255) (Teneo/Hibernate)

Teneo/Hibernate maps String to VARCHAR(255) per default. If values with sizes of more than 255 characters are expected, this mapping strategy might result in a lot of exceptions. A simple solution  for this is to change the mapping from VARCHAR to TEXT of the EAttribute. Teneo supports such a mapping by looking for a specific annotation value (“@Lob” in “teneo.jpa” annotation).

1. Add an EAnnotation to the EAttribute
2. Set source of the EAnnotation to “teneo.jpa”
3. Add a Details Entry to the EAnnotation
4. Set the key of the Details Entry to “value”
5. Set the value of the Details Entry to “@Lob”
6. Regenerate your model
7. Recreate the database (Teneo will not update the table per default)