Re: basic question about runtime query parsing

George Neuner <gneuner2@comcast.net>
12 Jul 2005 05:16:53 -0400

          From comp.compilers

Related articles
basic question about runtime query parsing shahbazc@gmail.com (falcon) (2005-07-11)
Re: basic question about runtime query parsing antounk@comcast.net (Antoun Kanawati) (2005-07-12)
Re: basic question about runtime query parsing skandgoe@gwdg.de (Skandinavisches Seminar) (2005-07-12)
Re: basic question about runtime query parsing wyrmwif@tsoft.org (SM Ryan) (2005-07-12)
Re: basic question about runtime query parsing gneuner2@comcast.net (George Neuner) (2005-07-12)
Re: basic question about runtime query parsing kers@hpl.hp.com (Chris Dollin) (2005-07-17)
Re: basic question about runtime query parsing lfinsto1@gwdg.de (Laurence Finston) (2005-07-22)
Re: basic question about runtime query parsing shahbazc@gmail.com (falcon) (2005-07-22)
Re: basic question about runtime query parsing kers@hpl.hp.com (Chris Dollin) (2005-07-26)
Re: basic question about runtime query parsing lfinsto1@gwdg.de (Laurence Finston) (2005-07-28)
Re: basic question about runtime query parsing kers@hpl.hp.com (Chris Dollin) (2005-07-31)
[3 later articles]
| List of all articles for this month |

From: George Neuner <gneuner2@comcast.net>
Newsgroups: comp.compilers
Date: 12 Jul 2005 05:16:53 -0400
Organization: Compilers Central
References: 05-07-045
Keywords: parse, practice

On 11 Jul 2005 10:49:28 -0400, "falcon" <shahbazc@gmail.com> wrote:


>Say I have a c library which implements various database functions
>and contains relevant data structures. For effeciency, I keep
>'records' of a 'table' in a native c struct (so 4 'columns' in a
>table will show up as 4 attributes of a struct).


Generally it's a bad idea to code table and field names into your
program unless you are certain they will never change. A better
solution is to read the table layout from the database and adapt your
program. Obviously if you are creating the table, you know the
layout.


>if someone issues 'create table(x int, y int, z int),' I parse it and
>figure out I need a struct{int x, int y, int z} (this struct will
>then be passed all over the code). How do I create this struct
>during runtime?


C doesn't allow you to define a new type of structure at run time.
But you can define a generic data structure that can be used to access
any database table.


You don't have to bind data fields to typed variables ... that's just
a convenience. All you need do is provide a buffer with enough bytes
to hold the field data. The entire row can be held in a single large
byte array.


So, instead of defining a new kind of structure for each table, you
define a single expandable structure which contains a byte array to
hold the row data and a map of the fields to their locations in the
array.




Here is a C example of a variable size structure created using the
single element array trick. Be warned that I made this up this code
on the spot and have not tested it - but it should give you basic
idea:




struct _map
      {
      char *name; // pointer to field name string
      char *data; // pointer to field data
      enum { ... } type; // field data type for casting
      };
struct _table
      {
      int num_fields;
      struct _map field[1];
      };


      :
      int n = /* # of fields */;
      int bufsiz = /* # of bytes to hold row data */;
      int size = sizeof( struct table )
                        + ((n-1) * sizeof( struct map ))
                        + bufsiz;


      struct _table *t = malloc( size );
char *buf = &(t->field[n]);
      t->num_fields = n;
      for ( i = 0; i < n; ++i )
            {
            t->field[i].name = ...
            t->field[i].data = buf + ...
            t->field[i].type = ...
            }
      :




This creates a 'table' structure containing an array of 'n' map
structures and an anonymous byte array which starts immediately
following the nth map structure. By varying the number of map
structures and the size of the anonymous byte array you can use this
structure to accomodate any field layout.




If you were working in C++ you could just use expandable arrays for
the buffer and field map instead of manually working the arrangement
for each different kind of table.




George


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.