summaryrefslogtreecommitdiff
path: root/cpp/demo/Database/Oracle/occi/DeptI.cpp
blob: 0703cfb9facb1ee247087b14352daea036e0838a (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
// **********************************************************************
//
// 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 <DeptI.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(const RefAny& ref, const ConnectionHolderPtr& conh, const DeptFactoryIPtr& factory) :
    _ref(ref), _conh(conh), _factory(factory)
{
}


HR::EmpPrx 
DeptI::createEmp(int empno, const HR::EmpDesc& desc, const Ice::Current& current)
{
    Environment* env = _factory->getEnv();

    //
    // 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 = decodeRef(desc.mgr->ice_getIdentity().name, env, _conh->connection());
	
	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(_ref);
    
    Ice::Identity empId;
    empId.category = _factory->getCategory();
    empId.name = encodeRef(emp->getRef(), env);
    _conh->commit();
    return HR::EmpPrx::uncheckedCast(current.adapter->createProxy(empId));
}

HR::DeptDesc 
DeptI::getDesc(const Ice::Current& current)
{
    HR::DeptDesc result;
   
    Ref<DEPT_T> deptRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    result.deptno = deptRef->getDeptno();
    result.dname = deptRef->getDname();
    result.loc = deptRef->getLoc();
   
    _conh->commit();
    return result;
}
    
void 
DeptI::updateField(const string& field, const string& newValue, const Ice::Current& current)
{
    Ref<DEPT_T> deptRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
	    
    if(field == "dname")
    {
	deptRef->setDname(newValue);
    }
    else if(field == "loc")
    {
	deptRef->setLoc(newValue);
    }
    else
    {
	throw HR::SqlException("There is no field " + field + " in object DEPT_T");
    }
    deptRef->markModified();
    _conh->commit();
}

void 
DeptI::remove(const Ice::Current& current)
{
    Ref<DEPT_T> deptRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    deptRef->markDelete();
    _conh->commit();
}

HR::EmpPrxSeq 
DeptI::findAll(const Ice::Current& current)
{
    HR::EmpPrxSeq result;
    {
	Ref<DEPT_T> deptRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());

	StatementHolder stmth(_conh);
	stmth.statement()->setSQL("SELECT REF(e) FROM EMP_VIEW e WHERE DEPTREF = :1");
	stmth.statement()->setRef(1, deptRef);
	auto_ptr<ResultSet> rs(stmth.statement()->executeQuery());
	
	while(rs->next() != ResultSet::END_OF_FETCH)
	{
	    Ice::Identity empId;
	    empId.category = _factory->getCategory();
	    empId.name = encodeRef(rs->getRef(1), _factory->getEnv());
	    
	    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;
    {
	Ref<DEPT_T> deptRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());	

	StatementHolder stmth(_conh);
	stmth.statement()->setSQL("SELECT REF(e) FROM EMP_VIEW e WHERE DEPTREF = :1 AND ENAME = :2");
	stmth.statement()->setRef(1, deptRef);
	stmth.statement()->setString(2, name);
	auto_ptr<ResultSet> rs(stmth.statement()->executeQuery());
	
	while(rs->next() != ResultSet::END_OF_FETCH)
	{
	    Ice::Identity empId;
	    empId.category = _factory->getCategory();
	    empId.name = encodeRef(rs->getRef(1), _factory->getEnv());
	    
	    result.push_back(HR::EmpPrx::uncheckedCast(current.adapter->createProxy(empId)));
	}
    }
    _conh->commit();
    return result;
}