1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: cxx_txn.cpp,v 12.9 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#include "dbinc/txn.h"
17
18// Helper macro for simple methods that pass through to the
19// underlying C method. It may return an error or raise an exception.
20// Note this macro expects that input _argspec is an argument
21// list element (e.g., "char *arg") and that _arglist is the arguments
22// that should be passed through to the C method (e.g., "(db, arg)")
23//
24#define	DBTXN_METHOD(_name, _delete, _argspec, _arglist)		   \
25int DbTxn::_name _argspec						   \
26{									   \
27	int ret;							   \
28	DB_TXN *txn = unwrap(this);					   \
29	DbEnv *dbenv = DbEnv::get_DbEnv(txn->mgrp->env->dbenv);		   \
30									   \
31	ret = txn->_name _arglist;					   \
32	/* Weird, but safe if we don't access this again. */		   \
33	if (_delete)							   \
34		delete this;						   \
35	if (!DB_RETOK_STD(ret))						   \
36		DB_ERROR(dbenv, "DbTxn::" # _name, ret, ON_ERROR_UNKNOWN); \
37	return (ret);							   \
38}
39
40// private constructor, never called but needed by some C++ linkers
41DbTxn::DbTxn()
42:	imp_(0)
43{
44}
45
46DbTxn::DbTxn(DB_TXN *txn)
47:	imp_(txn)
48{
49	txn->api_internal = this;
50}
51
52DbTxn::~DbTxn()
53{
54}
55
56DBTXN_METHOD(abort, 1, (), (txn))
57DBTXN_METHOD(commit, 1, (u_int32_t flags), (txn, flags))
58DBTXN_METHOD(discard, 1, (u_int32_t flags), (txn, flags))
59
60u_int32_t DbTxn::id()
61{
62	DB_TXN *txn;
63
64	txn = unwrap(this);
65	return (txn->id(txn));		// no error
66}
67
68DBTXN_METHOD(get_name, 0, (const char **namep), (txn, namep))
69DBTXN_METHOD(prepare, 0, (u_int8_t *gid), (txn, gid))
70DBTXN_METHOD(set_name, 0, (const char *name), (txn, name))
71DBTXN_METHOD(set_timeout, 0, (db_timeout_t timeout, u_int32_t flags),
72    (txn, timeout, flags))
73
74// static method
75DbTxn *DbTxn::wrap_DB_TXN(DB_TXN *txn)
76{
77	DbTxn *wrapped_txn = get_DbTxn(txn);
78	return (wrapped_txn != NULL) ?  wrapped_txn : new DbTxn(txn);
79}
80