OCILIB (C and C++ Driver for Oracle)  4.7.3
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
BindObjectAdaptor.hpp
1 /*
2  * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI)
3  *
4  * Website: http://www.ocilib.net
5  *
6  * Copyright (c) 2007-2021 Vincent ROGIER <vince.rogier@ocilib.net>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 
23 #include "ocilibcpp/support.hpp"
24 
25 namespace ocilib
26 {
27  namespace support
28  {
29  template<class T>
30  void BindObjectAdaptor<T>::SetInData()
31  {
32  if (GetMode() & OCI_BDM_IN)
33  {
34  size_t size = _object.size();
35 
36  if (size > _size)
37  {
38  size = _size;
39  }
40 
41  if (size > 0)
42  {
43  memcpy(_data, &_object[0], size * sizeof(NativeType));
44  }
45 
46  _data[size] = 0;
47  }
48  }
49 
50  template<class T>
51  void BindObjectAdaptor<T>::SetOutData()
52  {
53  if (GetMode() & OCI_BDM_OUT)
54  {
55  OCI_Bind* bnd = core::Check(OCI_GetBind2(_statement, _name.c_str()));
56  size_t size = core::Check(OCI_BindGetDataSize(bnd));
57 
58  _object.assign(_data, _data + size);
59  }
60  }
61 
62  template<class T>
63  BindObjectAdaptor<T>::BindObjectAdaptor(const ocilib::Statement& statement, const ostring& name, unsigned int mode, ObjectType& object, unsigned int size) :
64  BindObject(statement, name, mode),
65  _object(object),
66  _data(core::OnAllocate(new NativeType[size + 1], size + 1)),
67  _size(size)
68  {
69  memset(_data, 0, (_size + 1) * sizeof(NativeType));
70  }
71 
72  template<class T>
73  BindObjectAdaptor<T>::~BindObjectAdaptor() noexcept
74  {
75  delete core::OnDeallocate(_data);
76  }
77 
78  template<class T>
79  BindObjectAdaptor<T>::operator NativeType* () const
80  {
81  return _data;
82  }
83  }
84 }
OCI_SYM_PUBLIC unsigned int OCI_API OCI_BindGetDataSize(OCI_Bind *bnd)
Return the actual size of the element held by the given bind handle.
OCILIB ++ Namespace.
Object used for executing SQL or PL/SQL statement and returning the produced results.
Definition: types.hpp:5536
OCI_SYM_PUBLIC OCI_Bind *OCI_API OCI_GetBind2(OCI_Statement *stmt, const otext *name)
Return a bind handle from its name.
static T Check(T result)
Internal usage. Checks if the last OCILIB function call has raised an error. If so, it raises a C++ exception using the retrieved error handle.
Definition: Utils.hpp:53
struct OCI_Bind OCI_Bind
Internal bind representation.
Definition: types.h:136
std::basic_string< otext, std::char_traits< otext >, std::allocator< otext > > ostring
string class wrapping the OCILIB otext * type and OTEXT() macros ( see Character sets ) ...
Definition: config.hpp:120