1 /* $Id: rsmeta_impl.cpp 487444 2015-12-17 18:38:53Z 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: rsmeta_impl.cpp 487444 2015-12-17 18:38:53Z ucko $
27 *
28 * Author:  Michael Kholodov
29 *
30 * File Description:  Resultset metadata implementation
31 *
32 *
33 */
34 
35 #include <ncbi_pch.hpp>
36 #include <dbapi/driver/exception.hpp>
37 #include <dbapi/driver/public.hpp>
38 #include <dbapi/error_codes.hpp>
39 
40 #include "rsmeta_impl.hpp"
41 #include "rs_impl.hpp"
42 
43 
44 #define NCBI_USE_ERRCODE_X   Dbapi_ObjImpls
45 
46 
47 BEGIN_NCBI_SCOPE
48 
49 
CResultSetMetaData(CDB_Result * rs)50 CResultSetMetaData::CResultSetMetaData(CDB_Result *rs)
51 {
52     SetIdent("CResultSetMetaData");
53 
54     // Fill out column metadata
55     const CDBParams& params = rs->GetDefineParams();
56     const unsigned int param_num = params.GetNum();
57 
58     for (unsigned int i = 0; i < param_num; ++i) {
59 
60         SColMetaData md(
61                 params.GetName(i),
62                 params.GetDataType(i),
63                 params.GetMaxSize(i)
64                 );
65 
66         m_colInfo.push_back(md);
67 
68     }
69 }
70 
~CResultSetMetaData()71 CResultSetMetaData::~CResultSetMetaData()
72 {
73     try {
74         Notify(CDbapiDeletedEvent(this));
75         _TRACE(GetIdent() << " " << (void*)this << " deleted.");
76     }
77     NCBI_CATCH_ALL_X( 7, kEmptyStr )
78 }
79 
FindParamPosInternal(const string & name) const80 unsigned int CResultSetMetaData::FindParamPosInternal(const string& name) const
81 {
82     const size_t param_num = m_colInfo.size();
83 
84     for (unsigned int i = 0; i < param_num; ++i) {
85         if (m_colInfo[i].m_name == name) {
86             return i;
87         }
88     }
89 
90     DATABASE_DRIVER_ERROR("Invalid parameter name " + name, 20001);
91     return 0;
92 }
93 
GetTotalColumns() const94 unsigned int CResultSetMetaData::GetTotalColumns() const
95 {
96     return (unsigned int)m_colInfo.size();
97 }
98 
GetType(const CDBParamVariant & param) const99 EDB_Type CResultSetMetaData::GetType(const CDBParamVariant& param) const
100 {
101     if (param.IsPositional()) {
102         return m_colInfo[param.GetPosition() - 1].m_type;
103     }
104 
105     return m_colInfo[FindParamPosInternal(param.GetName())].m_type;
106 }
107 
GetMaxSize(const CDBParamVariant & param) const108 int CResultSetMetaData::GetMaxSize(const CDBParamVariant& param) const
109 {
110     if (param.IsPositional()) {
111         return m_colInfo[param.GetPosition() - 1].m_maxSize;
112     }
113 
114     return m_colInfo[FindParamPosInternal(param.GetName())].m_maxSize;
115 }
116 
GetName(const CDBParamVariant & param) const117 string CResultSetMetaData::GetName(const CDBParamVariant& param) const
118 {
119     if (param.IsPositional()) {
120         return m_colInfo[param.GetPosition() - 1].m_name;
121     }
122 
123     return m_colInfo[FindParamPosInternal(param.GetName())].m_name;
124 }
125 
GetDirection(const CDBParamVariant &) const126 CDBParams::EDirection CResultSetMetaData::GetDirection(const CDBParamVariant&) const
127 {
128     return CDBParams::eOut;
129 }
130 
Action(const CDbapiEvent & e)131 void CResultSetMetaData::Action(const CDbapiEvent& e)
132 {
133     _TRACE(GetIdent() << " " << (void*)this << ": '" << e.GetName()
134            << "' from " << e.GetSource()->GetIdent());
135 
136   if(dynamic_cast<const CDbapiDeletedEvent*>(&e) != 0 ) {
137     RemoveListener(e.GetSource());
138     if(dynamic_cast<CResultSet*>(e.GetSource()) != 0 ) {
139         _TRACE("Deleting " << GetIdent() << " " << (void*)this);
140       delete this;
141     }
142   }
143 }
144 
145 END_NCBI_SCOPE
146