From 66a01c2f277a0118ee4307fcfaf07df81218497e Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Tue, 15 Nov 2011 11:59:11 +0200 Subject: Support mapping of QByteArray and QString to Oracle LOB types --- odb/qt/basic/oracle/qbyte-array-traits.hxx | 88 +++++++++++++++++++- odb/qt/basic/oracle/qstring-traits.hxx | 124 +++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+), 1 deletion(-) diff --git a/odb/qt/basic/oracle/qbyte-array-traits.hxx b/odb/qt/basic/oracle/qbyte-array-traits.hxx index 3d99cd7..063d52d 100644 --- a/odb/qt/basic/oracle/qbyte-array-traits.hxx +++ b/odb/qt/basic/oracle/qbyte-array-traits.hxx @@ -68,9 +68,95 @@ namespace odb }; template <> + struct default_value_traits + { + typedef QByteArray value_type; + typedef QByteArray query_type; + typedef lob_callback image_type; + + static void + set_value (QByteArray& v, + result_callback_type& cb, + void*& context, + bool is_null) + { + if (is_null) + v = QByteArray (); + else + { + cb = &result_callback; + context = &v; + } + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const QByteArray& v) + { + if (v.isNull ()) + is_null = true; + else + { + is_null = false; + cb = ¶m_callback; + context = &v; + } + } + + static bool + result_callback (void* context, + ub4*, + void* b, + ub4 s, + chunk_position p) + { + QByteArray& v (*static_cast (context)); + + switch (p) + { + case one_chunk: + case first_chunk: + { + v.clear (); + + // Falling through. + } + case next_chunk: + case last_chunk: + { + v.append (static_cast (b), static_cast (s)); + break; + } + } + + return true; + } + + static bool + param_callback (const void* context, + ub4*, + const void** b, + ub4* s, + chunk_position* p, + void*, + ub4) + { + const QByteArray& v (*static_cast (context)); + + *p = one_chunk; + *s = static_cast (v.size ()); + *b = v.constData (); + + return true; + } + }; + + template <> struct default_type_traits { - static const database_type_id db_type_id = id_raw; + static const database_type_id db_type_id = id_blob; }; } } diff --git a/odb/qt/basic/oracle/qstring-traits.hxx b/odb/qt/basic/oracle/qstring-traits.hxx index c0b6775..bca7bab 100644 --- a/odb/qt/basic/oracle/qstring-traits.hxx +++ b/odb/qt/basic/oracle/qstring-traits.hxx @@ -64,6 +64,130 @@ namespace odb } }; + class qstring_lob_value_traits + { + public: + typedef QString value_type; + typedef QString query_type; + typedef lob_callback image_type; + + static void + set_value (QString& v, + result_callback_type& cb, + void*& context, + bool is_null) + { + if (is_null) + v = QString (); + else + { + cb = &result_callback; + context = &v; + } + } + + static void + set_image (param_callback_type& cb, + const void*& context, + bool& is_null, + const QString& v) + { + if (v.isNull ()) + is_null = true; + else + { + is_null = false; + cb = ¶m_callback; + context = &v; + } + } + + static bool + result_callback (void* context, + ub4*, + void* b, + ub4 s, + chunk_position p) + { + QString& v (*static_cast (context)); + + switch (p) + { + case one_chunk: + case first_chunk: + { + v.clear (); + + // Falling through. + } + case next_chunk: + case last_chunk: + { + v += QString::fromUtf8(static_cast (b), + static_cast (s)); + break; + } + } + + return true; + } + + static bool + param_callback (const void* context, + ub4* position_context, + const void** b, + ub4* s, + chunk_position* p, + void* temp_buffer, + ub4 capacity) + { + const QByteArray& v (static_cast (context)->toUtf8 ()); + + *s = static_cast (v.size ()); + + if (*position_context == 0) + { + if (*s <= capacity) + *p = one_chunk; + else + { + *s = capacity; + *p = first_chunk; + } + } + else + { + *s -= *position_context; + + if (*s <= capacity) + *p = last_chunk; + else + { + *s = capacity; + *p = next_chunk; + } + } + + std::memcpy (temp_buffer, v.constData () + *position_context, *s); + *b = temp_buffer; + *position_context += *s; + + return true; + } + }; + + template <> + struct LIBODB_ORACLE_EXPORT default_value_traits: + qstring_lob_value_traits + { + }; + + template <> + struct LIBODB_ORACLE_EXPORT default_value_traits: + qstring_lob_value_traits + { + }; + template <> struct default_type_traits { -- cgit v1.1