1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: cxx_seq.cpp,v 12.10 2008/01/08 20:58:09 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13#include "db_cxx.h"
14#include "dbinc/cxx_int.h"
15
16// Helper macro for simple methods that pass through to the
17// underlying C method. It may return an error or raise an exception.
18// Note this macro expects that input _argspec is an argument
19// list element (e.g., "char *arg") and that _arglist is the arguments
20// that should be passed through to the C method (e.g., "(db, arg)")
21//
22#define	DBSEQ_METHOD(_name, _argspec, _arglist, _destructor)		\
23int DbSequence::_name _argspec						\
24{									\
25	int ret;							\
26	DB_SEQUENCE *seq = unwrap(this);				\
27	DbEnv *dbenv = DbEnv::get_DbEnv(seq->seq_dbp->dbenv);		\
28									\
29	ret = seq->_name _arglist;					\
30	if (_destructor)						\
31		imp_ = 0;						\
32	if (!DB_RETOK_STD(ret))						\
33		DB_ERROR(dbenv,						\
34		    "DbSequence::" # _name, ret, ON_ERROR_UNKNOWN);	\
35	return (ret);							\
36}
37
38DbSequence::DbSequence(Db *db, u_int32_t flags)
39:	imp_(0)
40{
41	DB_SEQUENCE *seq;
42	int ret;
43
44	if ((ret = db_sequence_create(&seq, unwrap(db), flags)) != 0)
45		DB_ERROR(db->get_env(), "DbSequence::DbSequence", ret,
46		    ON_ERROR_UNKNOWN);
47	else {
48		imp_ = seq;
49		seq->api_internal = this;
50	}
51}
52
53DbSequence::DbSequence(DB_SEQUENCE *seq)
54:	imp_(seq)
55{
56	seq->api_internal = this;
57}
58
59DbSequence::~DbSequence()
60{
61	DB_SEQUENCE *seq;
62
63	seq = unwrap(this);
64	if (seq != NULL)
65		(void)seq->close(seq, 0);
66}
67
68DBSEQ_METHOD(open, (DbTxn *txnid, Dbt *key, u_int32_t flags),
69    (seq, unwrap(txnid), key, flags), 0)
70DBSEQ_METHOD(initial_value, (db_seq_t value), (seq, value), 0)
71DBSEQ_METHOD(close, (u_int32_t flags), (seq, flags), 1)
72DBSEQ_METHOD(remove, (DbTxn *txnid, u_int32_t flags),
73    (seq, unwrap(txnid), flags), 1)
74DBSEQ_METHOD(stat, (DB_SEQUENCE_STAT **sp, u_int32_t flags),
75    (seq, sp, flags), 0)
76DBSEQ_METHOD(stat_print, (u_int32_t flags), (seq, flags), 0)
77
78DBSEQ_METHOD(get,
79    (DbTxn  *txnid, int32_t delta, db_seq_t *retp, u_int32_t flags),
80    (seq, unwrap(txnid), delta, retp, flags), 0)
81DBSEQ_METHOD(get_cachesize, (int32_t *sizep), (seq, sizep), 0)
82DBSEQ_METHOD(set_cachesize, (int32_t size), (seq, size), 0)
83DBSEQ_METHOD(get_flags, (u_int32_t *flagsp), (seq, flagsp), 0)
84DBSEQ_METHOD(set_flags, (u_int32_t flags), (seq, flags), 0)
85DBSEQ_METHOD(get_range, (db_seq_t *minp, db_seq_t *maxp), (seq, minp, maxp), 0)
86DBSEQ_METHOD(set_range, (db_seq_t min, db_seq_t max), (seq, min, max), 0)
87
88Db *DbSequence::get_db()
89{
90	DB_SEQUENCE *seq = unwrap(this);
91	DB *db;
92	(void)seq->get_db(seq, &db);
93	return Db::get_Db(db);
94}
95
96Dbt *DbSequence::get_key()
97{
98	DB_SEQUENCE *seq = unwrap(this);
99	memset(&key_, 0, sizeof(DBT));
100	(void)seq->get_key(seq, &key_);
101	return Dbt::get_Dbt(&key_);
102}
103
104// static method
105DbSequence *DbSequence::wrap_DB_SEQUENCE(DB_SEQUENCE *seq)
106{
107	DbSequence *wrapped_seq = get_DbSequence(seq);
108	return (wrapped_seq != NULL) ? wrapped_seq : new DbSequence(seq);
109}
110