/* * OCILIB - C Driver for Oracle (C Wrapper for Oracle OCI) * * Website: http://www.ocilib.net * * Copyright (c) 2007-2023 Vincent ROGIER * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include "ocilibcpp/types.hpp" // ReSharper disable CppClangTidyMiscMisplacedConst namespace ocilib { template Collection::Collection() { } template Collection::Collection(const TypeInfo &typeInfo) { AcquireAllocated ( core::Check(OCI_CollCreate(typeInfo)), typeInfo.GetConnection().GetHandle() ); } template Collection::Collection(OCI_Coll *pColl, core::Handle *parent) { AcquireTransient(pColl,parent); } template Collection Collection::Clone() const { Collection result(GetTypeInfo()); core::Check(OCI_CollAssign(result, *this)); return result; } template TypeInfo Collection::GetTypeInfo() const { return TypeInfo(core::Check(OCI_CollGetTypeInfo(*this))); } template typename Collection::CollectionType Collection::GetType() const { return CollectionType(core::Check(OCI_CollGetType(*this))); } template unsigned int Collection::GetMax() const { return core::Check(OCI_CollGetMax(*this)); } template unsigned int Collection::GetSize() const { return core::Check(OCI_CollGetSize(*this)); } template unsigned int Collection::GetCount() const { return core::Check(OCI_CollGetCount(*this)); } template void Collection::Truncate(unsigned int size) { core::Check(OCI_CollTrim(*this, size)); } template void Collection::Clear() { core::Check(OCI_CollClear(*this)); } template bool Collection::IsElementNull(unsigned int index) const { return (core::Check(OCI_ElemIsNull(core::Check(OCI_CollGetElem(*this, index)))) == TRUE); } template void Collection::SetElementNull(unsigned int index) { core::Check(OCI_ElemSetNull(core::Check(OCI_CollGetElem(*this, index)))); } template bool Collection::Delete(unsigned int index) const { return (core::Check(OCI_CollDeleteElem(*this, index)) == TRUE); } template typename Collection::iterator Collection::begin() { return iterator(this, 1); } template typename Collection::const_iterator Collection::begin() const { return const_iterator(const_cast(this), 1); } template typename Collection::iterator Collection::end() { return iterator(const_cast(this), GetCount() + 1); } template typename Collection::const_iterator Collection::end() const { return const_iterator(const_cast(this), GetCount() + 1); } template T Collection::Get(unsigned int index) const { return GetElem(core::Check(OCI_CollGetElem(*this, index)), GetHandle()); } template void Collection::Set(unsigned int index, const T & value) { OCI_Elem * elem = core::Check(OCI_CollGetElem(*this, index)); SetElem(elem, value); core::Check(OCI_CollSetElem(*this, index, elem)); } template void Collection::Append(const T &value) { OCI_Elem * elem = core::Check(OCI_ElemCreate(OCI_CollGetTypeInfo(*this))); SetElem(elem, value); core::Check(OCI_CollAppend(*this, elem)); core::Check(OCI_ElemFree(elem)); } template<> inline bool Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return (core::Check(OCI_ElemGetBoolean(elem)) == TRUE); } template<> inline short Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetShort(elem)); } template<> inline unsigned short Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetUnsignedShort(elem)); } template<> inline int Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetInt(elem)); } template<> inline unsigned int Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetUnsignedInt(elem)); } template<> inline big_int Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetBigInt(elem)); } template<> inline big_uint Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetUnsignedBigInt(elem)); } template<> inline float Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetFloat(elem)); } template<> inline double Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::Check(OCI_ElemGetDouble(elem)); } template<> inline Number Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Number(core::Check(OCI_ElemGetNumber(elem)), parent); } template<> inline ostring Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); return core::MakeString(core::Check(OCI_ElemGetString(elem))); } template<> inline Raw Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { ARG_NOT_USED(parent); unsigned int size = core::Check(OCI_ElemGetRawSize(elem)); core::ManagedBuffer buffer(static_cast(size + 1)); size = core::Check(OCI_ElemGetRaw(elem, static_cast(buffer), size)); return core::MakeRaw(buffer, size); } template<> inline Date Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Date(core::Check(OCI_ElemGetDate(elem)), parent); } template<> inline Timestamp Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Timestamp(core::Check(OCI_ElemGetTimestamp(elem)), parent); } template<> inline Interval Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Interval(core::Check(OCI_ElemGetInterval(elem)), parent); } template<> inline Object Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Object(core::Check(OCI_ElemGetObject(elem)), parent); } template<> inline Reference Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Reference(core::Check(OCI_ElemGetRef(elem)), parent); } template<> inline Clob Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Clob(core::Check(OCI_ElemGetLob(elem)), parent); } template<> inline NClob Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return NClob(core::Check(OCI_ElemGetLob(elem)), parent); } template<> inline Blob Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return Blob(core::Check(OCI_ElemGetLob(elem)), parent); } template<> inline File Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return File(core::Check(OCI_ElemGetFile(elem)), parent); } template T Collection::GetElem(OCI_Elem *elem, core::Handle *parent) { return T(core::Check(OCI_ElemGetColl(elem)), parent); } template<> inline void Collection::SetElem(OCI_Elem *elem, const bool &value) { core::Check(OCI_ElemSetBoolean(elem, static_cast(value))); } template<> inline void Collection::SetElem(OCI_Elem *elem, const short &value) { core::Check(OCI_ElemSetShort(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const unsigned short &value) { core::Check(OCI_ElemSetUnsignedShort(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const int &value) { core::Check(OCI_ElemSetInt(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const unsigned int &value) { core::Check(OCI_ElemSetUnsignedInt(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const big_int &value) { core::Check(OCI_ElemSetBigInt(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const big_uint &value) { core::Check(OCI_ElemSetUnsignedBigInt(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const float &value) { core::Check(OCI_ElemSetFloat(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const double &value) { core::Check(OCI_ElemSetDouble(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Number &value) { core::Check(OCI_ElemSetNumber(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const ostring& value) { core::Check(OCI_ElemSetString(elem, value.c_str())); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Raw &value) { const AnyPointer data = value.empty() ? nullptr : static_cast(const_cast(&value[0])) ; core::Check(OCI_ElemSetRaw(elem, data, static_cast(value.size()))); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Date &value) { core::Check(OCI_ElemSetDate(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Timestamp &value) { core::Check(OCI_ElemSetTimestamp(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Interval &value) { core::Check(OCI_ElemSetInterval(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Object &value) { core::Check(OCI_ElemSetObject(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Reference &value) { core::Check(OCI_ElemSetRef(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Clob &value) { core::Check(OCI_ElemSetLob(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const NClob &value) { core::Check(OCI_ElemSetLob(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const Blob &value) { core::Check(OCI_ElemSetLob(elem, value)); } template<> inline void Collection::SetElem(OCI_Elem *elem, const File &value) { core::Check(OCI_ElemSetFile(elem, value)); } template void Collection::SetElem(OCI_Elem *elem, const T &value) { core::Check(OCI_ElemSetColl(elem, value)); } template ostring Collection::ToString() const { if (!IsNull()) { unsigned int len = 0; core::Check(OCI_CollToText(*this, &len, nullptr)); core::ManagedBuffer buffer(static_cast(len + 1)); core::Check(OCI_CollToText(*this, &len, buffer)); return core::MakeString(static_cast(buffer), static_cast(len)); } return OCI_STRING_NULL; } template CollectionElement Collection::operator [] (unsigned int index) { return CollectionElement(this, index); } template CollectionElement Collection::operator [](unsigned int index) const { return CollectionElement(const_cast*>(this), index); } }