Index: /trunk/glueforge/glueforge.in
===================================================================
--- /trunk/glueforge/glueforge.in	(revision 4162)
+++ /trunk/glueforge/glueforge.in	(revision 4163)
@@ -3,5 +3,5 @@
 # Copyright (C) 2005  Joshua Hoblitt
 #
-# $Id: glueforge.in,v 1.17 2005-06-08 04:04:34 jhoblitt Exp $
+# $Id: glueforge.in,v 1.18 2005-06-09 00:16:25 jhoblitt Exp $
 
 use strict;
@@ -99,4 +99,5 @@
     'droptable_c.tt'        => "$output/tests/droptable.c",
     'insert_c.tt'           => "$output/tests/insert.c",
+    'pop_c.tt'              => "$output/tests/pop.c",
     'insertobject_c.tt'     => "$output/tests/insertobject.c",
     'popobject_c.tt'        => "$output/tests/popobject.c",
Index: /trunk/glueforge/templates/psdb/code.tt
===================================================================
--- /trunk/glueforge/templates/psdb/code.tt	(revision 4162)
+++ /trunk/glueforge/templates/psdb/code.tt	(revision 4163)
@@ -9,4 +9,5 @@
 [% INCLUDE droptable.tt %]
 [% INCLUDE insert.tt %]
+[% INCLUDE pop.tt %]
 [% INCLUDE insertobject.tt %]
 [% INCLUDE popobject.tt %]
Index: /trunk/glueforge/templates/psdb/header.tt
===================================================================
--- /trunk/glueforge/templates/psdb/header.tt	(revision 4162)
+++ /trunk/glueforge/templates/psdb/header.tt	(revision 4163)
@@ -18,4 +18,5 @@
 [% INCLUDE droptable_h.tt %]
 [% INCLUDE insert_h.tt %]
+[% INCLUDE pop_h.tt %]
 [% INCLUDE insertobject_h.tt %]
 [% INCLUDE popobject_h.tt %]
Index: /trunk/glueforge/templates/psdb/pop.tt
===================================================================
--- /trunk/glueforge/templates/psdb/pop.tt	(revision 4163)
+++ /trunk/glueforge/templates/psdb/pop.tt	(revision 4163)
@@ -0,0 +1,54 @@
+[% USE format -%]
+[% indented = format('%-15s %s') -%]
+bool [% namespace %]Pop(psDB *dbh, 
+[%- SET i = 0 -%]
+[% FOREACH item = columns -%]
+[% IF i == 0 %][% i = 1 %][% ELSE %], [% END -%]
+[% item.ctype %] *[% item.name -%]
+[% END %])
+{
+    psArray         *rowSet;
+    psMetadata      *row;
+    psMetadata      *popped;
+    long            deleted;
+    bool            status;
+    int             rowID;
+
+    rowSet = psDBSelectRows(dbh, [% namespace FILTER upper %]_TABLE_NAME, NULL, 1);
+    row = psArrayGet(rowSet, 0);
+    if (!row) {
+        // psError(); error or table is empty
+        return NULL;
+    }
+
+    rowID = psMetadataLookupS32(&status, row, "position");
+    if (!status) {
+        // psError(); something bad happened
+        return NULL;
+    }
+
+    popped = psMetadataAlloc();
+    psMetadataAddS32(popped, PS_LIST_TAIL, "position", NULL, rowID);
+
+    deleted = psDBDeleteRows(dbh, [% namespace FILTER upper %]_TABLE_NAME, popped);
+    if (deleted != 1) {
+        // psError(); something bad happened
+        return NULL;
+    }
+
+[% FOREACH item = columns -%]
+[% IF item.type == "S32" or item.type == "F32" or item.type == "F64" -%]
+    *[% item.name %] = psMetadataLookup[% item.type %](&status, row, "[% item.name %]");
+[% ELSIF item.type == "STR" -%]
+    *[% item.name %] = psMetadataLookupPtr(&status, row, "[% item.name %]");
+[% ELSIF item.type == "BOOL" -%]
+    *[% item.name %] = psMetadataLookupBool(&status, row, "[% item.name %]");
+[% END -%]
+    if (!status) {
+        // psError(); something bad happened
+        return false;
+    }
+[% END -%]
+
+    return true;
+}
Index: /trunk/glueforge/templates/psdb/pop_at.tt
===================================================================
--- /trunk/glueforge/templates/psdb/pop_at.tt	(revision 4163)
+++ /trunk/glueforge/templates/psdb/pop_at.tt	(revision 4163)
@@ -0,0 +1,10 @@
+AT_SETUP([[% namespace %]Pop() - Removes the last row from the database and returns it])
+AT_KEYWORDS([[% namespace %]Pop])
+
+AT_CHECK([dbsetup])
+# run insert so there is a row in the table
+AT_CHECK([insert])
+AT_CHECK([pop])
+AT_CHECK([dbcleanup])
+
+AT_CLEANUP
Index: /trunk/glueforge/templates/psdb/pop_c.tt
===================================================================
--- /trunk/glueforge/templates/psdb/pop_c.tt	(revision 4163)
+++ /trunk/glueforge/templates/psdb/pop_c.tt	(revision 4163)
@@ -0,0 +1,37 @@
+[% USE format -%]
+[% indented = format('%-15s %s') -%]
+#include <pslib.h>
+#include <[% pkg_name %].h>
+#include <stdlib.h>
+
+int main ()
+{
+    psDB            *dbh;
+    bool            status;
+[% FOREACH item = columns -%]
+[% IF item.ctype == "char*" -%]
+    [% indented("char", "$item.name[256]") %];
+[% ELSE-%]
+    [% indented("$item.ctype", "$item.name") %];
+[% END -%]
+[% END -%]
+
+    dbh = psDBInit("localhost", "test", NULL, "test");
+    if (!dbh) {
+        exit(EXIT_FAILURE);
+    }
+
+    status = [% namespace %]Pop(dbh, 
+[%- SET i = 0 -%]
+[% FOREACH item = columns -%]
+[% IF i == 0 %][% i = 1 %][% ELSE %], [% END -%]
+&[% item.name -%]
+[% END %]);
+    if (!status) {
+        exit(EXIT_FAILURE);
+    }
+
+    psDBCleanup(dbh);
+
+    exit(EXIT_SUCCESS);
+}
Index: /trunk/glueforge/templates/psdb/pop_h.tt
===================================================================
--- /trunk/glueforge/templates/psdb/pop_h.tt	(revision 4163)
+++ /trunk/glueforge/templates/psdb/pop_h.tt	(revision 4163)
@@ -0,0 +1,16 @@
+[% USE format -%]
+[% indented = format('%-15s %s') -%]
+/** Removes the last row from the database and returns it
+ *
+ * @return true on success
+ */
+
+bool [% namespace %]Pop(
+    psDB            *dbh,               ///< Database handle
+[%- SET i = 0 -%]
+[% FOREACH item = columns -%]
+[% IF i == 0 %][% i = 1 %][% ELSE %],[% END %]
+    [% indented("$item.ctype", "*$item.name") -%]
+[% i = i + 1 -%]
+[% END %]
+);
Index: /trunk/glueforge/templates/psdb/tests_makefile_am.tt
===================================================================
--- /trunk/glueforge/templates/psdb/tests_makefile_am.tt	(revision 4162)
+++ /trunk/glueforge/templates/psdb/tests_makefile_am.tt	(revision 4163)
@@ -57,4 +57,5 @@
     droptable \
     insert \
+    pop \
     insertobject \
     popobject
Index: /trunk/glueforge/templates/psdb/testsuite_at.tt
===================================================================
--- /trunk/glueforge/templates/psdb/testsuite_at.tt	(revision 4162)
+++ /trunk/glueforge/templates/psdb/testsuite_at.tt	(revision 4163)
@@ -17,4 +17,6 @@
 [% INCLUDE insert_at.tt %]
 
+[% INCLUDE pop_at.tt %]
+
 [% INCLUDE insertobject_at.tt %]
 
