NCBI C++ ToolKit
bytestreambuf.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: bytestreambuf.cpp 63657 2014-07-17 15:12:39Z 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 * Author: Michael Kholodov
27 *
28 * File Description: streambuf implementation for BLOBs
29 */
30 
31 #include <ncbi_pch.hpp>
32 #include <exception>
33 #include <algorithm>
34 #include "bytestreambuf.hpp"
35 #include <dbapi/driver/public.hpp>
36 #include <dbapi/error_codes.hpp>
37 #include "rs_impl.hpp"
38 
39 
40 #define NCBI_USE_ERRCODE_X Dbapi_BlobStream
41 
43 
44 enum { DEF_BUFSIZE = 2048 };
45 
48  : m_buf(0),
49  m_size(bufsize > 0 ? size_t(bufsize) : DEF_BUFSIZE),
50  /* m_len(0),*/ m_rs(0), m_cmd(0) //, m_column(-1)
51 {
52  m_buf = new CT_CHAR_TYPE[m_size * 2]; // read and write buffer in one
53  setg(0, 0, 0); // call underflow on the first read
54 
55  setp(getPBuf(), getPBuf() + m_size);
56  _TRACE("I/O buffer size: " << m_size);
57  if ((flags & fBOS_UseTransaction) != 0) {
58  _ASSERT(conn != NULL);
59  m_AutoTrans.reset(new CAutoTrans(*conn));
60  }
61 }
62 
64 {
65  try {
66 #if 0 // misbehaves
67  if( m_rs != 0 && m_len > 0 )
68  m_rs->SkipItem();
69 #endif
70 
71  delete[] m_buf;
72  delete m_cmd;
73  }
75 }
76 
78 {
79  return m_buf;
80 }
81 
83 {
84  return m_buf + m_size;
85 }
86 
88  delete m_cmd;
89  m_cmd = cmd;
90 }
91 
93  //delete m_rs;
94  m_rs = rs;
95  //m_column = m_rs->CurrentItemNo();
96 }
97 
99 {
100  if( m_rs == 0 )
101  throw runtime_error("CByteStreamBuf::underflow(): CResultSet* is null");
102 
103 #if 0
104  static size_t total = 0;
105 
106  if( m_column < 0 || m_column != m_rs->CurrentItemNo() ) {
107  if( m_column < 0 ) {
108  _TRACE("Column for ReadItem not set, current column: "
109  << m_rs->CurrentItemNo());
110 #ifdef _DEBUG
111  _ASSERT(0);
112 #endif
113  }
114  else
115  _TRACE("Total read from ReadItem: " << total);
116  total = 0;
117  m_column = m_rs->CurrentItemNo();
118  return CT_EOF;
119  }
120  else {
121 #endif
122  size_t len = m_rs->Read(getGBuf(), m_size);
123  _TRACE("Column: " << m_rs->GetColumnNo() << ", Bytes read to buffer: " << len);
124  if( len == 0 )
125  return CT_EOF;
126  //total += len;
127  setg(getGBuf(), getGBuf(), getGBuf() + len);
128  return CT_TO_INT_TYPE(*getGBuf());
129 #if 0
130  }
131 #endif
132 
133 }
134 
136 {
137  if( m_cmd == 0 ) {
138  throw runtime_error
139  ("CByteStreamBuf::overflow(): CDB_SendDataCmd* is null");
140  }
141 
142  static size_t total = 0;
143  size_t put = m_cmd->SendChunk(pbase(), pptr() - pbase());
144  total += put;
145  if (m_AutoTrans.get() && m_cmd->HasMoreResults()) {
146  m_AutoTrans->Finish();
147  m_AutoTrans.reset();
148  }
149  if( put > 0 ) {
150  memmove(pbase(), pbase() + put, pptr() - pbase() - put);
151  pbump(-int(put));
152 
153  if( ! CT_EQ_INT_TYPE(c, CT_EOF) )
154  sputc(CT_TO_CHAR_TYPE(c));
155 
156  return c;
157  }
158  else {
159  _TRACE("Total sent: " << total);
160  total = 0;
161  m_AutoTrans.reset();
162  return CT_EOF;
163  }
164 
165 }
166 
168 {
169  overflow(CT_EOF);
170  return 0;
171 }
172 
173 streambuf*
174 CByteStreamBuf::setbuf(CT_CHAR_TYPE* /*p*/, streamsize /*n*/)
175 {
176  throw runtime_error("CByteStreamBuf::setbuf(): not allowed");
177 }
178 
179 
181 {
182  streamsize left = egptr() - gptr();
183  return min(left, (streamsize)1);
184 }
185 //======================================================
@ DEF_BUFSIZE
CResultSet * m_rs
virtual CT_INT_TYPE underflow()
virtual ~CByteStreamBuf()
virtual CT_INT_TYPE overflow(CT_INT_TYPE c)
void SetCmd(CDB_SendDataCmd *cmd)
void SetRs(CResultSet *rs)
CT_CHAR_TYPE * getPBuf()
CT_CHAR_TYPE * m_buf
CDB_SendDataCmd * m_cmd
CT_CHAR_TYPE * getGBuf()
CByteStreamBuf(streamsize bufsize, TBlobOStreamFlags flags=0, CDB_Connection *conn=NULL)
virtual streamsize showmanyc()
virtual int sync()
unique_ptr< CAutoTrans > m_AutoTrans
virtual streambuf * setbuf(CT_CHAR_TYPE *p, streamsize n)
virtual int GetColumnNo()
Get column number, currently available for Read()
Definition: rs_impl.cpp:303
virtual size_t Read(void *buf, size_t size)
Read unformatted data.
Definition: rs_impl.cpp:270
static uch flags
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
#define NULL
Definition: ncbistd.hpp:225
int TBlobOStreamFlags
Definition: dbapi.hpp:97
@ fBOS_UseTransaction
Use a (sub)transaction (committed once all data's been sent successfully, rolled back for unrecoverab...
Definition: dbapi.hpp:93
virtual bool HasMoreResults() const
Definition: public.cpp:1144
virtual size_t SendChunk(const void *data, size_t size)
Send chunk of data to the server.
Definition: public.cpp:1123
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define NCBI_CATCH_ALL_X(err_subcode, message)
Definition: ncbiexpt.hpp:619
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define CT_TO_CHAR_TYPE
Definition: ncbistre.hpp:735
#define CT_EOF
Definition: ncbistre.hpp:732
#define CT_TO_INT_TYPE
Definition: ncbistre.hpp:734
#define CT_INT_TYPE
Definition: ncbistre.hpp:728
#define CT_EQ_INT_TYPE
Definition: ncbistre.hpp:736
#define CT_CHAR_TYPE
Definition: ncbistre.hpp:729
#define kEmptyStr
Definition: ncbistr.hpp:123
Definition of all error codes used in dbapi libraries (dbapi_driver.lib and others).
int len
T min(T x_, T y_)
#define memmove(a, b, c)
static int bufsize
Definition: pcregrep.c:162
#define _ASSERT
Modified on Mon Apr 22 04:05:52 2024 by modify_doxy.py rev. 669887