1 /* $Id: bulkinsert.cpp 180887 2010-01-13 20:05:34Z ivanovp $
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: bulkinsert.cpp 180887 2010-01-13 20:05:34Z ivanovp $
27 *
28 * Author:  Michael Kholodov
29 *
30 * File Description:  Base class for database access
31 */
32 
33 #include <ncbi_pch.hpp>
34 #include "conn_impl.hpp"
35 #include "bulkinsert.hpp"
36 #include <dbapi/driver/exception.hpp>
37 #include <dbapi/driver/public.hpp>
38 #include <dbapi/error_codes.hpp>
39 
40 
41 #define NCBI_USE_ERRCODE_X   Dbapi_BulkInsert
42 
43 BEGIN_NCBI_SCOPE
44 
45 // implementation
CDBAPIBulkInsert(const string & name,CConnection * conn)46 CDBAPIBulkInsert::CDBAPIBulkInsert(const string& name,
47                                    CConnection* conn)
48     : m_cmd(0), m_conn(conn)
49 {
50     m_cmd = m_conn->GetCDB_Connection()->BCPIn(name);
51     SetIdent("CDBAPIBulkInsert");
52 }
53 
~CDBAPIBulkInsert()54 CDBAPIBulkInsert::~CDBAPIBulkInsert()
55 {
56     try {
57         Notify(CDbapiClosedEvent(this));
58         FreeResources();
59         Notify(CDbapiDeletedEvent(this));
60         _TRACE(GetIdent() << " " << (void*)this << " deleted.");
61     }
62     NCBI_CATCH_ALL_X( 1, kEmptyStr )
63 }
64 
Close()65 void CDBAPIBulkInsert::Close()
66 {
67     Notify(CDbapiClosedEvent(this));
68     FreeResources();
69 }
70 
FreeResources()71 void CDBAPIBulkInsert::FreeResources()
72 {
73     delete m_cmd;
74     m_cmd = 0;
75     if( m_conn != 0 && m_conn->IsAux() ) {
76 	    delete m_conn;
77 	    m_conn = 0;
78 	    Notify(CDbapiAuxDeletedEvent(this));
79     }
80 }
81 
Bind(const CDBParamVariant & param,CVariant * v)82 void CDBAPIBulkInsert::Bind(const CDBParamVariant& param, CVariant* v)
83 {
84     // GetBCPInCmd()->GetBindParams().Bind(col - 1, v->GetData());
85 
86     if (param.IsPositional()) {
87         // Decrement position by ONE.
88         GetBCPInCmd()->GetBindParams().Bind(param.GetPosition() - 1, v->GetData());
89     } else {
90         GetBCPInCmd()->GetBindParams().Bind(param, v->GetData());
91     }
92 }
93 
94 
SetHints(CTempString hints)95 void CDBAPIBulkInsert::SetHints(CTempString hints)
96 {
97     GetBCPInCmd()->SetHints(hints);
98 }
99 
AddHint(EHints hint,unsigned int value)100 void CDBAPIBulkInsert::AddHint(EHints hint, unsigned int value /* = 0 */)
101 {
102     GetBCPInCmd()->AddHint((CDB_BCPInCmd::EBCP_Hints)hint, value);
103 }
104 
AddOrderHint(CTempString columns)105 void CDBAPIBulkInsert::AddOrderHint(CTempString columns)
106 {
107     GetBCPInCmd()->AddOrderHint(columns);
108 }
109 
AddRow()110 void CDBAPIBulkInsert::AddRow()
111 {
112     GetBCPInCmd()->SendRow();
113 }
114 
StoreBatch()115 void CDBAPIBulkInsert::StoreBatch()
116 {
117     GetBCPInCmd()->CompleteBatch();
118 }
119 
Cancel()120 void CDBAPIBulkInsert::Cancel()
121 {
122     GetBCPInCmd()->Cancel();
123 }
124 
Complete()125 void CDBAPIBulkInsert::Complete()
126 {
127     GetBCPInCmd()->CompleteBCP();
128 }
129 
Action(const CDbapiEvent & e)130 void CDBAPIBulkInsert::Action(const CDbapiEvent& e)
131 {
132     _TRACE(GetIdent() << " " << (void*)this << ": '" << e.GetName()
133            << "' from " << e.GetSource()->GetIdent());
134 
135     if(dynamic_cast<const CDbapiDeletedEvent*>(&e) != 0 ) {
136 	RemoveListener(e.GetSource());
137         if(dynamic_cast<CConnection*>(e.GetSource()) != 0 ) {
138             _TRACE("Deleting " << GetIdent() << " " << (void*)this);
139             delete this;
140         }
141     }
142 }
143 
144 END_NCBI_SCOPE
145