Index: /trunk/glueforge/templates/psdb/alloc_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/alloc_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/alloc_c.tt	(revision 5950)
@@ -9,30 +9,34 @@
 int main ()
 {
-    [% indented(table.object_name, "*object") %];
+[% FOREACH table = tables -%]
+    {
+        [% indented(table.object_name, "*object") %];
 
-    object = [% table.object_name %]Alloc(
+        object = [% table.object_name %]Alloc(
 [%- SET i = 0 -%]
 [%- FOREACH item = table.columns -%]
-    [%- IF i == 0 -%][%- i = 1 -%][%- ELSE -%], [% END -%] [%- item.test -%]
+        [%- IF i == 0 -%][%- i = 1 -%][%- ELSE -%], [% END -%] [%- item.test -%]
 [%- END -%]
-);
+    );
 
-    if (!object) {
-        exit(EXIT_FAILURE);
-    }
+        if (!object) {
+            exit(EXIT_FAILURE);
+        }
 
 [% FOREACH item = table.columns -%]
 [% IF item.type == "S32" or item.type == "F32" or item.type == "F64" or item.type == "BOOL" -%]
-    if (!object->[% item.name %] == [% item.test %]) {
+        if (!object->[% item.name %] == [% item.test %]) {
 [% ELSIF item.type == "STR" -%]
-    if (strncmp(object->[% item.name %], [% item.test %], MAX_STRING_LENGTH)) {
+        if (strncmp(object->[% item.name %], [% item.test %], MAX_STRING_LENGTH)) {
 [% END -%]
-        psFree(object);
-        exit(EXIT_FAILURE);
-    }
+            psFree(object);
+            exit(EXIT_FAILURE);
+        }
 [% END -%]
 
-    psFree(object);
+        psFree(object);
+    }
 
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/cleanup_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/cleanup_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/cleanup_c.tt	(revision 5950)
@@ -12,5 +12,5 @@
     }
 
-    [% table.namespace %]Cleanup(dbh);
+    [% pkg_namespace %]Cleanup(dbh);
 
     exit(EXIT_SUCCESS);
Index: /trunk/glueforge/templates/psdb/createtable_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/createtable_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/createtable_c.tt	(revision 5950)
@@ -5,17 +5,21 @@
 int main ()
 {
-    psDB            *dbh;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        if(![% table.namespace %]CreateTable(dbh)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psDBCleanup(dbh);
     }
 
-    if(![% table.namespace %]CreateTable(dbh)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/dbcleanup_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/dbcleanup_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/dbcleanup_c.tt	(revision 5950)
@@ -11,5 +11,7 @@
     }
 
+[% FOREACH table = tables -%]
     p_psDBRunQuery(dbh, "DROP TABLE IF EXISTS [% table.name %]");
+[% END -%]
 
     psDBCleanup(dbh);
Index: /trunk/glueforge/templates/psdb/dbsetup_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/dbsetup_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/dbsetup_c.tt	(revision 5950)
@@ -13,7 +13,9 @@
 
     // remove the table if it already exists
+[% FOREACH table = tables -%]
     p_psDBRunQuery(dbh, "DROP TABLE IF EXISTS [% table.name %]");
     [% table.namespace %]CreateTable(dbh);
 
+[% END -%]
     psDBCleanup(dbh);
 
Index: /trunk/glueforge/templates/psdb/droptable_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/droptable_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/droptable_c.tt	(revision 5950)
@@ -5,17 +5,21 @@
 int main ()
 {
-    psDB            *dbh;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (![% table.namespace %]DropTable(dbh)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psDBCleanup(dbh);
     }
 
-    if (![% table.namespace %]DropTable(dbh)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/init_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/init_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/init_c.tt	(revision 5950)
@@ -7,5 +7,5 @@
     psDB            *dbh;
 
-    dbh = [% table.namespace %]Init("localhost", "test", NULL, "test");
+    dbh = [% pkg_namespace %]Init("localhost", "test", NULL, "test");
     if (!dbh) {
         exit(EXIT_FAILURE);
Index: /trunk/glueforge/templates/psdb/insert_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/insert_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/insert_c.tt	(revision 5950)
@@ -7,12 +7,14 @@
 int main ()
 {
-    psDB            *dbh;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
-    }
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
 
-    if (![% table.namespace %]Insert(dbh, 
+        if (![% table.namespace %]Insert(dbh, 
 [%- SET i = 0 -%]
 [%- FOREACH item = table.columns -%]
@@ -20,9 +22,11 @@
 [%- END -%]
 )) {
-        exit(EXIT_FAILURE);
+            exit(EXIT_FAILURE);
+        }
+
+        psDBCleanup(dbh);
     }
 
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/insertfits_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/insertfits_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/insertfits_c.tt	(revision 5950)
@@ -5,25 +5,29 @@
 int main ()
 {
-    psDB            *dbh;
-    psFits          *fits;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
+        psFits          *fits;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        // open a temp
+        fits = psFitsOpen("./blargh", "rw");
+        if (!fits) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (![% table.namespace %]InsertFits(dbh, fits)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psFree(fits);
+        psDBCleanup(dbh);
     }
 
-    // open a temp
-    fits = psFitsAlloc("./blargh");
-    if (!fits) {
-        exit(EXIT_FAILURE);
-    }
-
-    if (![% table.namespace %]InsertFits(dbh, fits)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(fits);
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/insertobject_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/insertobject_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/insertobject_c.tt	(revision 5950)
@@ -7,13 +7,15 @@
 int main ()
 {
-    psDB            *dbh;
-    [% indented(table.object_name, "*object") %];
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
+        [% indented(table.object_name, "*object") %];
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
-    }
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
 
-    object = [% table.object_name %]Alloc(
+        object = [% table.object_name %]Alloc(
 [%- SET i = 0 -%]
 [%- FOREACH item = table.columns -%]
@@ -21,15 +23,17 @@
 [%- END -%]
 );
-    if (!object) {
-        exit(EXIT_FAILURE);
+        if (!object) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (![% table.namespace %]InsertObject(dbh, object)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psFree(object);
+        psDBCleanup(dbh);
     }
 
-    if (![% table.namespace %]InsertObject(dbh, object)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(object);
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/metadatafromobject_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/metadatafromobject_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/metadatafromobject_c.tt	(revision 5950)
@@ -10,9 +10,11 @@
 int main ()
 {
-    psMetadata      *md;
-    [% indented(table.object_name, "*object") %];
-    bool            status;
+[% FOREACH table = tables -%]
+    {
+        psMetadata      *md;
+        [% indented(table.object_name, "*object") %];
+        bool            status;
 
-    object = [% table.object_name %]Alloc(
+        object = [% table.object_name %]Alloc(
 [%- SET i = 0 -%]
 [%- FOREACH item = table.columns -%]
@@ -20,30 +22,32 @@
 [%- END -%]
 );
-    if (!object) {
-        exit(EXIT_FAILURE);
-    }
+        if (!object) {
+            exit(EXIT_FAILURE);
+        }
 
-    md = [% table.namespace %]MetadataFromObject(object);
-    if (!md) {
-        exit(EXIT_FAILURE);
-    }
+        md = [% table.namespace %]MetadataFromObject(object);
+        if (!md) {
+            exit(EXIT_FAILURE);
+        }
 
-    psFree(object);
+        psFree(object);
 
 [% FOREACH item = table.columns -%]
 [% IF item.type == "S32" or item.type == "F32" or item.type == "F64" -%]
-    if (!psMetadataLookup[% item.type %](&status, md, "[% item.name %]") == [% item.test %]) {
+        if (!psMetadataLookup[% item.type %](&status, md, "[% item.name %]") == [% item.test %]) {
 [% ELSIF item.type == "STR" -%]
-    if (strncmp(psMetadataLookupPtr(&status, md, "[% item.name %]"), [% item.test %], MAX_STRING_LENGTH)) {
+        if (strncmp(psMetadataLookupPtr(&status, md, "[% item.name %]"), [% item.test %], MAX_STRING_LENGTH)) {
 [% ELSIF item.type == "BOOL" -%]
-    if (!psMetadataLookupBool(&status, md, "[% item.name %]") == [% item.test %]) {
+        if (!psMetadataLookupBool(&status, md, "[% item.name %]") == [% item.test %]) {
 [% END -%]
-        psFree(md);
-        exit(EXIT_FAILURE);
-    }
+            psFree(md);
+            exit(EXIT_FAILURE);
+        }
 [% END -%]
 
-    psFree(md);
+        psFree(md);
+    }
 
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/objectfrommetadata_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/objectfrommetadata_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/objectfrommetadata_c.tt	(revision 5950)
@@ -10,42 +10,46 @@
 int main ()
 {
-    psMetadata      *md;
-    [% indented(table.object_name, "*object") %];
+[% FOREACH table = tables -%]
+    {
+        psMetadata      *md;
+        [% indented(table.object_name, "*object") %];
 
-    md = psMetadataAlloc();
+        md = psMetadataAlloc();
 [% FOREACH item = table.columns -%]
 [% IF item.type == "S32" or item.type == "F32" or item.type == "F64" -%]
-    if (!psMetadataAdd[% item.type %](md, PS_LIST_TAIL, "[% item.name %]", NULL, [% item.test %])) {
+        if (!psMetadataAdd[% item.type %](md, PS_LIST_TAIL, "[% item.name %]", 0, NULL, [% item.test %])) {
 [% ELSIF item.type == "STR" -%]
-    if (!psMetadataAddStr(md, PS_LIST_TAIL, "[% item.name %]", NULL, [% item.test %])) {
+        if (!psMetadataAddStr(md, PS_LIST_TAIL, "[% item.name %]", 0, NULL, [% item.test %])) {
 [% ELSIF item.type == "BOOL" -%]
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "[% item.name %]", PS_DATA_BOOL, NULL, [% item.test %])) {
+        if (!psMetadataAdd(md, PS_LIST_TAIL, "[% item.name %]", PS_DATA_BOOL, NULL, [% item.test %])) {
 [% END -%]
-        psFree(md);
-        exit(EXIT_FAILURE);
-    }
+            psFree(md);
+            exit(EXIT_FAILURE);
+        }
 [% END -%]
 
-    object = [% table.namespace %]ObjectFromMetadata(md);
-    if (!object) {
+        object = [% table.namespace %]ObjectFromMetadata(md);
+        if (!object) {
+            psFree(md);
+            exit(EXIT_FAILURE);
+        }
+
         psFree(md);
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(md);
 
 [% FOREACH item = table.columns -%]
 [% IF item.type == "S32" or item.type == "F32" or item.type == "F64" or item.type == "BOOL" -%]
-    if (!object->[% item.name %] == [% item.test %]) {
+        if (!object->[% item.name %] == [% item.test %]) {
 [% ELSIF item.type == "STR" -%]
-    if (strncmp(object->[% item.name %], [% item.test %], MAX_STRING_LENGTH)) {
+        if (strncmp(object->[% item.name %], [% item.test %], MAX_STRING_LENGTH)) {
 [% END -%]
-        psFree(object);
-        exit(EXIT_FAILURE);
-    }
+            psFree(object);
+            exit(EXIT_FAILURE);
+        }
 [% END -%]
 
-    psFree(object);
+        psFree(object);
+    }
 
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/pop_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/pop_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/pop_c.tt	(revision 5950)
@@ -7,19 +7,21 @@
 int main ()
 {
-    psDB            *dbh;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
 [% FOREACH item = table.columns -%]
 [% IF item.type == "STR" -%]
-    [% indented("char", "$item.name[256]") %];
+        [% indented("char", "$item.name[256]") %];
 [% ELSE-%]
-    [% indented("$item.ctype", "$item.name") %];
+        [% indented("$item.ctype", "$item.name") %];
 [% END -%]
 [% END -%]
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
-    }
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
 
-    if (![% table.namespace %]Pop(dbh, 
+        if (![% table.namespace %]Pop(dbh, 
 [%- SET i = 0 -%]
 [% FOREACH item = table.columns -%]
@@ -31,9 +33,11 @@
 [% END -%]
 [% END %])) { 
-        exit(EXIT_FAILURE);
+            exit(EXIT_FAILURE);
+        }
+
+        psDBCleanup(dbh);
     }
 
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/popfits_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/popfits_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/popfits_c.tt	(revision 5950)
@@ -5,25 +5,29 @@
 int main ()
 {
-    psDB            *dbh;
-    psFits          *fits;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
+        psFits          *fits;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        // allocate a temp
+        fits = psFitsOpen("./blargh", "rw");
+        if (!fits) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (![% table.namespace %]PopFits(dbh, fits, 1)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psFree(fits);
+        psDBCleanup(dbh);
     }
 
-    // allocate a temp
-    fits = psFitsAlloc("./blargh");
-    if (!fits) {
-        exit(EXIT_FAILURE);
-    }
-
-    if (![% table.namespace %]PopFits(dbh, fits, 1)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(fits);
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/popobject_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/popobject_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/popobject_c.tt	(revision 5950)
@@ -7,20 +7,24 @@
 int main ()
 {
-    psDB            *dbh;
-    [% indented(table.object_name, "*object") %];
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
+        [% indented(table.object_name, "*object") %];
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        object = [% table.namespace %]PopObject(dbh);
+        if (!object) {
+            exit(EXIT_FAILURE);
+        }
+
+        psFree(object);
+        psDBCleanup(dbh);
     }
 
-    object = [% table.namespace %]PopObject(dbh);
-    if (!object) {
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(object);
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
Index: /trunk/glueforge/templates/psdb/selectrowsfits_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/selectrowsfits_c.tt	(revision 5949)
+++ /trunk/glueforge/templates/psdb/selectrowsfits_c.tt	(revision 5950)
@@ -5,25 +5,29 @@
 int main ()
 {
-    psDB            *dbh;
-    psFits          *fits;
+[% FOREACH table = tables -%]
+    {
+        psDB            *dbh;
+        psFits          *fits;
 
-    dbh = psDBInit("localhost", "test", NULL, "test");
-    if (!dbh) {
-        exit(EXIT_FAILURE);
+        dbh = psDBInit("localhost", "test", NULL, "test");
+        if (!dbh) {
+            exit(EXIT_FAILURE);
+        }
+
+        // FIXME: open a temp file and pass it as a FD
+        fits = psFitsOpen("./blargh", "rw");
+        if (!fits) {
+            exit(EXIT_FAILURE);
+        }
+
+        if (![% table.namespace %]SelectRowsFits(dbh, fits, NULL, 1)) {
+            exit(EXIT_FAILURE);
+        }
+
+        psFree(fits);
+        psDBCleanup(dbh);
     }
 
-    // FIXME: open a temp file and pass it as a FD
-    fits = psFitsAlloc("./blargh");
-    if (!fits) {
-        exit(EXIT_FAILURE);
-    }
-
-    if (![% table.namespace %]SelectRowsFits(dbh, fits, NULL, 1)) {
-        exit(EXIT_FAILURE);
-    }
-
-    psFree(fits);
-    psDBCleanup(dbh);
-
+[% END -%]
     exit(EXIT_SUCCESS);
 }
