00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <iostream>
00013 #include <errno.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016
00017 #ifdef _WIN32
00018 extern "C" {
00019 extern int getopt(int, char * const *, const char *);
00020 extern int optind;
00021 }
00022 #else
00023 #include <unistd.h>
00024 #endif
00025
00026 #include <db_cxx.h>
00027
00028 #define DATABASE "sequence.db"
00029 #define SEQUENCE "my_sequence"
00030
00031 using std::cout;
00032 using std::cerr;
00033
00034 class SequenceExample
00035 {
00036 public:
00037 SequenceExample();
00038 void run(bool removeExistingDatabase, const char *fileName);
00039
00040 private:
00041
00042 SequenceExample(const SequenceExample &);
00043 void operator = (const SequenceExample &);
00044 };
00045
00046 int
00047 usage()
00048 {
00049 (void)fprintf(stderr, "usage: SequenceExample [-r] [database]\n");
00050 return (EXIT_FAILURE);
00051 }
00052
00053 int
00054 main(int argc, char *argv[])
00055 {
00056 int ch, rflag;
00057 const char *database;
00058
00059 rflag = 0;
00060 while ((ch = getopt(argc, argv, "r")) != EOF)
00061 switch (ch) {
00062 case 'r':
00063 rflag = 1;
00064 break;
00065 case '?':
00066 default:
00067 return (usage());
00068 }
00069 argc -= optind;
00070 argv += optind;
00071
00072
00073 database = *argv == NULL ? DATABASE : argv[0];
00074
00075
00076
00077
00078
00079
00080 try {
00081 SequenceExample app;
00082 app.run((bool)(rflag == 1 ? true : false), database);
00083 return (EXIT_SUCCESS);
00084 }
00085 catch (DbException &dbe) {
00086 cerr << "SequenceExample: " << dbe.what() << "\n";
00087 return (EXIT_FAILURE);
00088 }
00089 }
00090
00091 SequenceExample::SequenceExample()
00092 {
00093 }
00094
00095 void SequenceExample::run(bool removeExistingDatabase, const char *fileName)
00096 {
00097
00098 if (removeExistingDatabase)
00099 (void)remove(fileName);
00100
00101
00102
00103 Db db(0, 0);
00104
00105 db.set_error_stream(&cerr);
00106 db.set_errpfx("SequenceExample");
00107 db.open(NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0664);
00108
00109
00110
00111
00112
00113 try {
00114 Dbt key((void *)SEQUENCE, (u_int32_t)strlen(SEQUENCE));
00115 DbSequence seq(&db, 0);
00116 seq.open(0, &key, DB_CREATE);
00117
00118 for (int i = 0; i < 10; i++) {
00119 db_seq_t seqnum;
00120 seq.get(0, 1, &seqnum, 0);
00121
00122
00123 cout << "Got sequence number (" <<
00124 (int)(seqnum >> 32) << ", " << (unsigned)seqnum <<
00125 ")\n";
00126 }
00127
00128 seq.close(0);
00129 } catch (DbException &dbe) {
00130 cerr << "SequenceExample: " << dbe.what() << "\n";
00131 }
00132
00133 db.close(0);
00134 }