summaryrefslogtreecommitdiff
path: root/cpp/demo/Database/Oracle/occi/DeptI.cpp
blob: 485ee8ba1f18cba878e4413e4acc43d4cbc660b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <DeptI.h>
#include <Util.h>
#include <EmpI.h>

#ifdef _MSC_VER
//
// ott generates placement new without the corresponding delete
//
#   pragma warning( 4 : 4291 )
#endif

using namespace std;
using namespace oracle::occi;

DeptI::DeptI(Environment* env, StatelessConnectionPool* pool, const string& empCategory) :
    _env(env),
    _pool(pool),
    _empCategory(empCategory)
{
}

void 
DeptI::ice_ping(const Ice::Current& current) const
{
    ConnectionHolder conh(_pool);
    getRef(conh.connection(), decodeName(current.id.name));
    conh.commit();
}

HR::EmpPrx 
DeptI::createEmp(int empno, const HR::EmpDesc& desc, const Ice::Current& current)
{
    ConnectionHolder conh(_pool);
    {
	Ref<DEPT_T> deptRef = getRef(conh.connection(), decodeName(current.id.name));

	//
	// Inserted into the OCCI cache
	//
	EMP_T* emp = new(conh.connection(), "EMP_VIEW")EMP_T;
	
	emp->setEmpno(empno);
	emp->setEname(desc.ename);
	emp->setJob(desc.job);
	if(desc.mgr != 0)
	{
	    Ref<EMP_T> mgrRef = 
		EmpI::getRef(conh.connection(), decodeName(desc.mgr->ice_getIdentity().name));
	    
	    emp->setMgrref(mgrRef);
	}
	
	if(desc.hiredate != "")
	{
	    Date hiredate(_env);
	    hiredate.fromText(desc.hiredate);
	    emp->setHiredate(hiredate);
	}
	
	if(desc.sal != "")
	{
	    Number sal(0);
	    sal.fromText(_env, desc.sal, "99999.99");
	    emp->setSal(sal);
	}
	
	if(desc.comm != "")
	{
	    Number comm(0);
	    comm.fromText(_env, desc.comm, "0.999");
	    emp->setComm(comm);
	}
	
	emp->setDeptref(deptRef);
    }
    conh.commit();

    Ice::Identity empId;
    empId.name = encodeName(empno);
    empId.category = _empCategory;
    return HR::EmpPrx::uncheckedCast(current.adapter->createProxy(empId));
}

HR::DeptDesc 
DeptI::getDesc(const Ice::Current& current)
{
    assert(this != 0);
    HR::DeptDesc result;
    
    ConnectionHolder conh(_pool);
    {
	Ref<DEPT_T> deptRef = getRef(conh.connection(), decodeName(current.id.name));	
	result.dname = deptRef->getDname();
	result.loc = deptRef->getLoc();
    }
    conh.commit();
    return result;
}
    
void DeptI::updateDesc(const HR::DeptDesc& newDesc, const Ice::Current& current)
{
    ConnectionHolder conh(_pool);
    {
	Ref<DEPT_T> deptRef = getRef(conh.connection(), decodeName(current.id.name));
    
	deptRef->setDname(newDesc.dname);
	deptRef->setLoc(newDesc.loc);
	deptRef->markModified();
    }
    conh.commit();
}

void DeptI::remove(const Ice::Current& current)
{
    ConnectionHolder conh(_pool);
    {
	Ref<DEPT_T> deptRef = getRef(conh.connection(), decodeName(current.id.name));
	deptRef->markDelete();
    }
    conh.commit();
}

HR::EmpPrxSeq 
DeptI::findAll(const Ice::Current& current)
{
    HR::EmpPrxSeq result;

    ConnectionHolder conh(_pool);
    {
	StatementHolder stmth(conh);
    
	auto_ptr<ResultSet> rs(
	    stmth.statement()->executeQuery("SELECT EMPNO FROM EMP_VIEW"));
	
	while(rs->next() != ResultSet::END_OF_FETCH)
	{
	    Ice::Identity empId;
	    empId.category = _empCategory;
	    empId.name = rs->getString(1); // first column as string
	    
	    result.push_back(
		HR::EmpPrx::uncheckedCast(
		    current.adapter->createProxy(empId)));
	}
    }
    conh.commit();
    return result;
}


HR::EmpPrxSeq 
DeptI::findByName(const string& name, const Ice::Current& current)
{
    HR::EmpPrxSeq result;

    ConnectionHolder conh(_pool);
    {
	StatementHolder stmth(conh);
	stmth.statement()->setSQL("SELECT EMPNO FROM EMP_VIEW WHERE ENAME = :1");
	stmth.statement()->setString(1, name);
	stmth.statement()->execute();
	
	auto_ptr<ResultSet> rs(
	    stmth.statement()->getResultSet());

	while(rs->next() != ResultSet::END_OF_FETCH)
	{
	    Ice::Identity empId;
	    empId.category = _empCategory;
	    empId.name = rs->getString(1); // first column as string
	    
	    result.push_back(
		HR::EmpPrx::uncheckedCast(
		    current.adapter->createProxy(empId)));
	}
    }
    conh.commit();
    return result;
}


/*static*/
Ref<DEPT_T> 
DeptI::getRef(Connection* con, int deptno)
{
    StatementHolder stmth(con);
    
    stmth.statement()->setSQL("SELECT REF(d) FROM DEPT_VIEW d WHERE DEPTNO = :1");
    stmth.statement()->setInt(1, deptno);
    auto_ptr<ResultSet> rs(stmth.statement()->executeQuery());
    
    if(rs->next() == ResultSet::END_OF_FETCH)
    {
	throw Ice::ObjectNotExistException(__FILE__, __LINE__);
    }
   
    return rs->getRef(1);
}