Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

ex_sequence.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ex_sequence.c,v 12.3 2005/11/03 17:46:15 bostic Exp $
00008  */
00009 
00010 #include <sys/types.h>
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 #ifdef _WIN32
00017 extern int getopt(int, char * const *, const char *);
00018 #else
00019 #include <unistd.h>
00020 #endif
00021 
00022 #include <db.h>
00023 
00024 #define DATABASE        "sequence.db"
00025 #define SEQUENCE        "my_sequence"
00026 int main __P((int, char *[]));
00027 int usage __P((void));
00028 
00029 int
00030 main(argc, argv)
00031         int argc;
00032         char *argv[];
00033 {
00034         extern int optind;
00035         DB *dbp;
00036         DB_SEQUENCE *seq;
00037         DBT key;
00038         int i, ret, rflag;
00039         db_seq_t seqnum;
00040         char ch;
00041         const char *database, *progname = "ex_sequence";
00042 
00043         dbp = NULL;
00044         seq = NULL;
00045 
00046         rflag = 0;
00047         while ((ch = getopt(argc, argv, "r")) != EOF)
00048                 switch (ch) {
00049                 case 'r':
00050                         rflag = 1;
00051                         break;
00052                 case '?':
00053                 default:
00054                         return (usage());
00055                 }
00056         argc -= optind;
00057         argv += optind;
00058 
00059         /* Accept optional database name. */
00060         database = *argv == NULL ? DATABASE : argv[0];
00061 
00062         /* Optionally discard the database. */
00063         if (rflag)
00064                 (void)remove(database);
00065 
00066         /* Create and initialize database object, open the database. */
00067         if ((ret = db_create(&dbp, NULL, 0)) != 0) {
00068                 fprintf(stderr,
00069                     "%s: db_create: %s\n", progname, db_strerror(ret));
00070                 return (EXIT_FAILURE);
00071         }
00072         dbp->set_errfile(dbp, stderr);
00073         dbp->set_errpfx(dbp, progname);
00074         if ((ret = dbp->open(dbp,
00075             NULL, database, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
00076                 dbp->err(dbp, ret, "%s: open", database);
00077                 goto err;
00078         }
00079 
00080         if ((ret = db_sequence_create(&seq, dbp, 0)) != 0) {
00081                 dbp->err(dbp, ret, "db_sequence_create");
00082                 goto err;
00083         }
00084 
00085         memset(&key, 0, sizeof(DBT));
00086         key.data = SEQUENCE;
00087         key.size = (u_int32_t)strlen(SEQUENCE);
00088 
00089         if ((ret = seq->open(seq, NULL, &key, DB_CREATE)) != 0) {
00090                 dbp->err(dbp, ret, "%s: DB_SEQUENCE->open", SEQUENCE);
00091                 goto err;
00092         }
00093 
00094         for (i = 0; i < 10; i++) {
00095                 if ((ret = seq->get(seq, NULL, 1, &seqnum, 0)) != 0) {
00096                         dbp->err(dbp, ret, "DB_SEQUENCE->get");
00097                         goto err;
00098                 }
00099 
00100                 /* There's no portable way to print 64-bit numbers. */
00101 #ifdef _WIN32
00102                 printf("Got sequence number %l64d\n", (int64_t)seqnum);
00103 #else
00104                 printf(
00105                     "Got sequence number %llu\n", (unsigned long long)seqnum);
00106 #endif
00107         }
00108 
00109         /* Close everything down. */
00110         if ((ret = seq->close(seq, 0)) != 0) {
00111                 seq = NULL;
00112                 dbp->err(dbp, ret, "DB_SEQUENCE->close");
00113                 goto err;
00114         }
00115         if ((ret = dbp->close(dbp, 0)) != 0) {
00116                 fprintf(stderr,
00117                     "%s: DB->close: %s\n", progname, db_strerror(ret));
00118                 return (EXIT_FAILURE);
00119         }
00120         return (EXIT_SUCCESS);
00121 
00122 err:    if (seq != NULL)
00123                 (void)seq->close(seq, 0);
00124         if (dbp != NULL)
00125                 (void)dbp->close(dbp, 0);
00126         return (EXIT_FAILURE);
00127 }
00128 
00129 int
00130 usage()
00131 {
00132         (void)fprintf(stderr, "usage: ex_sequence [-r] [database]\n");
00133         return (EXIT_FAILURE);
00134 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2