Skip navigation.
Home SourceForge.net Logo

Upcoming Release

This last month has been a very busy one for me, with that ODK development slowed down, however a new release is already on works, this new release will sport a very interesting feature, fetching columns to a structure, this can be really useful to create ORM like features.

This is a sample code of this feature:

typedef struct{
char *name;
char *address;
char *phone;
char *email;
} Contact;

odkFieldDef ContactDef[4] = {{100, ODKDA_TEXT, ODK_STRING_PTR},
{100, ODKDA_TEXT, ODK_STRING_PTR},
{100, ODKDA_TEXT, ODK_STRING_PTR},
{100, ODKDA_TEXT, ODK_STRING_PTR}};

Contact MyContact;

cmd = odk_sql_new("SELECT name,address,phone,email FROM contacts");
odk_command_define_struct(cmd, 4, ContactDef, &MyContact);

odk_connection_execute(conn, cmd);

while(odk_command_fetchex(cmd)){
printf("Contact:\n");
printf("\tName: %s\n", MyContact.name);
printf("\tAddress: %s\n", MyContact.address);
printf("\tPhone: %s\n", MyContact.phone);
printf("\tE-Mail: %s\n", MyContact.email);
printf("\n");
}

As you can see first you define your structure, then create an array of odkFieldDef to define the characteristics of the field on the structure, as you can see there is lenght (only used for strings), variable type and flags. Now you create a provider, create a connection, open the connection, create a command (SQL command) and define the structure with it (odk_command_define_struct), you pass the command, the number of elements in the definition array, the definition array (odkFieldDef) and a pointer to the structure.

ODK can work with strings in two way, one is to assume that we are pre allocating the string (char name[100]), ODK will fill the string on the structure, this mode dosen't requiere any flags on the odkFieldDef structure. This is the default behaviour.

The other way to work with string is to assume that we are working with string pointers (char *name), ODK will allocate the apropiate memory for the string and set the pointer to it, this mode will ignore the field lenght on the odkFieldDef struct, to work on this mode you need to add the flag ODK_STRING_PTR to the odkFieldDef struct.

Amusing isn't it?, currently all plugins support this feature.

Happy Hacking!