1 /* $Id: cstmt_impl.cpp 399230 2013-05-13 13:59:10Z ucko $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name:  $Id: cstmt_impl.cpp 399230 2013-05-13 13:59:10Z ucko $
27 *
28 * Author:  Michael Kholodov
29 *
30 * File Description:  Callable statement implementation
31 *
32 */
33 
34 #include <ncbi_pch.hpp>
35 #include "conn_impl.hpp"
36 #include "cstmt_impl.hpp"
37 #include "rs_impl.hpp"
38 #include <dbapi/driver/public.hpp>
39 #include <dbapi/error_codes.hpp>
40 
41 
42 #define NCBI_USE_ERRCODE_X   Dbapi_ObjImpls
43 
44 BEGIN_NCBI_SCOPE
45 
46 // implementation
CCallableStatement(const string & proc,CConnection * conn)47 CCallableStatement::CCallableStatement(const string& proc,
48                        CConnection* conn)
49 : CStatement(conn)
50 , m_status(0)
51 , m_StatusIsAvailable(false)
52 {
53     SetBaseCmd(conn->GetCDB_Connection()->RPC(proc));
54     SetIdent("CCallableStatement");
55 }
56 
~CCallableStatement()57 CCallableStatement::~CCallableStatement()
58 {
59     try {
60         Notify(CDbapiClosedEvent(this));
61     }
62     NCBI_CATCH_ALL_X( 2, kEmptyStr )
63 }
64 
GetRpcCmd()65 CDB_RPCCmd* CCallableStatement::GetRpcCmd()
66 {
67     return (CDB_RPCCmd*)GetBaseCmd();
68 }
69 
HasMoreResults()70 bool CCallableStatement::HasMoreResults()
71 {
72     _TRACE("CCallableStatement::HasMoreResults(): Calling parent method");
73     bool more = CStatement::HasMoreResults();
74 
75     if (more
76         && GetCDB_Result() != 0
77         && GetCDB_Result()->ResultType() == eDB_StatusResult ) {
78 
79         _TRACE("CCallableStatement::HasMoreResults(): Status result received");
80         CDB_Int *res = 0;
81         while( GetCDB_Result()->Fetch() ) {
82             res = dynamic_cast<CDB_Int*>(GetCDB_Result()->GetItem());
83         }
84 
85         if( res != 0 ) {
86             m_status = res->Value();
87 			m_StatusIsAvailable = true;
88             _TRACE("CCallableStatement::HasMoreResults(): Return status "
89                    << m_status );
90             delete res;
91         }
92 
93         more = CStatement::HasMoreResults();
94     }
95 
96     return more;
97 }
98 
SetParam(const CVariant & v,const CDBParamVariant & param)99 void CCallableStatement::SetParam(const CVariant& v,
100                   const CDBParamVariant& param)
101 {
102     if (param.IsPositional()) {
103         // Decrement position by ONE.
104         GetRpcCmd()->GetBindParams().Set(param.GetPosition() - 1, v.GetData());
105     } else {
106         GetRpcCmd()->GetBindParams().Set(param, v.GetData());
107     }
108 }
109 
SetOutputParam(const CVariant & v,const CDBParamVariant & param)110 void CCallableStatement::SetOutputParam(const CVariant& v,
111                     const CDBParamVariant& param)
112 {
113     if (param.IsPositional()) {
114         // Decrement position by ONE.
115         GetRpcCmd()->GetBindParams().Set(param.GetPosition() - 1, v.GetData(), true);
116     } else {
117         GetRpcCmd()->GetBindParams().Set(param, v.GetData(), true);
118     }
119 }
120 
121 
Execute()122 void CCallableStatement::Execute()
123 {
124     SetFailed(false);
125 
126     // Reset status value ...
127     m_status = 0;
128     m_StatusIsAvailable = false;
129 
130     _TRACE("Executing stored procedure: " + GetRpcCmd()->GetProcName());
131     GetRpcCmd()->Send();
132 
133     if ( IsAutoClearInParams() ) {
134         // Implicitely clear all parameters.
135         ClearParamList();
136     }
137 }
138 
ExecuteUpdate()139 void CCallableStatement::ExecuteUpdate()
140 {
141     Execute();
142 
143     PurgeResults();
144 }
145 
GetReturnStatus()146 int CCallableStatement::GetReturnStatus()
147 {
148     CHECK_NCBI_DBAPI(!m_StatusIsAvailable, "Return status is not available yet.");
149 
150     /*
151     if (!m_StatusIsAvailable) {
152         ERR_POST_X(10, Warning << "Return status is not available yet.");
153     }
154     */
155 
156     return m_status;
157 }
158 
159 
Close()160 void CCallableStatement::Close()
161 {
162     Notify(CDbapiClosedEvent(this));
163     FreeResources();
164 }
165 
166 
167 END_NCBI_SCOPE
168