1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SequenceExample.cpp,v 12.7 2008/01/08 20:58:26 bostic Exp $
7 */
8
9#include <sys/types.h>
10
11#include <iostream>
12#include <errno.h>
13#include <stdlib.h>
14#include <string.h>
15
16#ifdef _WIN32
17extern "C" {
18  extern int getopt(int, char * const *, const char *);
19  extern int optind;
20}
21#else
22#include <unistd.h>
23#endif
24
25#include <db_cxx.h>
26
27#define	DATABASE	"sequence.db"
28#define	SEQUENCE	"my_sequence"
29
30using std::cout;
31using std::cerr;
32
33class SequenceExample
34{
35public:
36	SequenceExample();
37	void run(bool removeExistingDatabase, const char *fileName);
38
39private:
40	// no need for copy and assignment
41	SequenceExample(const SequenceExample &);
42	void operator = (const SequenceExample &);
43};
44
45int
46usage()
47{
48	(void)fprintf(stderr, "usage: SequenceExample [-r] [database]\n");
49	return (EXIT_FAILURE);
50}
51
52int
53main(int argc, char *argv[])
54{
55	int ch, rflag;
56	const char *database;
57
58	rflag = 0;
59	while ((ch = getopt(argc, argv, "r")) != EOF)
60		switch (ch) {
61		case 'r':
62			rflag = 1;
63			break;
64		case '?':
65		default:
66			return (usage());
67		}
68	argc -= optind;
69	argv += optind;
70
71	/* Accept optional database name. */
72	database = *argv == NULL ? DATABASE : argv[0];
73
74	// Use a try block just to report any errors.
75	// An alternate approach to using exceptions is to
76	// use error models (see DbEnv::set_error_model()) so
77	// that error codes are returned for all Berkeley DB methods.
78	//
79	try {
80		SequenceExample app;
81		app.run((bool)(rflag == 1 ? true : false), database);
82		return (EXIT_SUCCESS);
83	}
84	catch (DbException &dbe) {
85		cerr << "SequenceExample: " << dbe.what() << "\n";
86		return (EXIT_FAILURE);
87	}
88}
89
90SequenceExample::SequenceExample()
91{
92}
93
94void SequenceExample::run(bool removeExistingDatabase, const char *fileName)
95{
96	// Remove the previous database.
97	if (removeExistingDatabase)
98		(void)remove(fileName);
99
100	// Create the database object.
101	// There is no environment for this simple example.
102	Db db(0, 0);
103
104	db.set_error_stream(&cerr);
105	db.set_errpfx("SequenceExample");
106	db.open(NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0664);
107
108	// We put a try block around this section of code
109	// to ensure that our database is properly closed
110	// in the event of an error.
111	//
112	try {
113		Dbt key((void *)SEQUENCE, (u_int32_t)strlen(SEQUENCE));
114		DbSequence seq(&db, 0);
115		seq.open(0, &key, DB_CREATE);
116
117		for (int i = 0; i < 10; i++) {
118			db_seq_t seqnum;
119			seq.get(0, 1, &seqnum, 0);
120
121			// We don't have a portable way to print 64-bit numbers.
122			cout << "Got sequence number (" <<
123			    (int)(seqnum >> 32) << ", " << (unsigned)seqnum <<
124			    ")\n";
125		}
126
127		seq.close(0);
128	} catch (DbException &dbe) {
129		cerr << "SequenceExample: " << dbe.what() << "\n";
130	}
131
132	db.close(0);
133}
134