25 #include "sqlite/sqlite3.h" 36 #pragma warning( disable: 4521 4522 ) // multiple copy constructors and assignment operators specified 46 #define SQLITE_BLOBX 20 48 HC_ASSERT(
sizeof( sqlite3_int64 ) <=
sizeof(
long long ) );
65 sqlite3_int64 m_integer;
71 long long m_largest_field;
79 assert( m_isConst || !m_largest_field );
112 other.m_isConst =
true;
121 assert( m_isConst || !m_largest_field );
123 m_largest_field = other.m_largest_field;
135 assert( m_isConst || !m_largest_field );
137 m_largest_field = other.m_largest_field;
151 assert( m_isConst || !m_largest_field );
152 m_isConst = other.m_isConst;
153 m_largest_field = other.m_largest_field;
155 other.m_isConst =
true;
195 LOGICAL_CLASS = mxLOGICAL_CLASS,
196 INT8_CLASS = mxINT8_CLASS,
197 UINT8_CLASS = mxUINT8_CLASS,
198 INT16_CLASS = mxINT16_CLASS,
199 INT32_CLASS = mxINT32_CLASS,
200 UINT16_CLASS = mxUINT16_CLASS,
201 UINT32_CLASS = mxUINT32_CLASS,
202 INT64_CLASS = mxINT64_CLASS,
203 DOUBLE_CLASS = mxDOUBLE_CLASS,
204 SINGLE_CLASS = mxSINGLE_CLASS,
205 CHAR_CLASS = mxCHAR_CLASS,
259 m_pcItem =
const_cast<mxArray*
>(pcItem);
270 ValueMex( mwIndex m, mwIndex n,
int clsid = mxDOUBLE_CLASS )
272 m_pcItem = mxCreateNumericMatrix( m, n, (mxClassID)clsid, mxREAL );
284 m_isConst = !doAdopt;
337 if( !m_isConst && m_pcItem )
340 mxArray* tmp = m_pcItem;
342 mxDestroyArray( m_pcItem );
353 return const_cast<const mxArray*
>( m_pcItem );
373 return ValueMex( m_pcItem ? mxDuplicateArray( m_pcItem ) : NULL ).
Adopt();
383 assert( !m_isConst );
384 mxArray* pcItem = m_pcItem;
396 return mxGetM(m_pcItem);
405 return mxGetN(m_pcItem);
414 return !m_pcItem || mxIsEmpty( m_pcItem );
423 return m_pcItem ? mxIsCell( m_pcItem ) :
false;
432 return m_pcItem ? mxIsComplex( m_pcItem ) :
false;
441 return NumElements() == 1;
450 return mxIsStruct( m_pcItem );
459 return NumDims() == 2 && min( GetM(), GetN() ) == 1;
468 return mxDOUBLE_CLASS == ClassID();
477 return mxFUNCTION_CLASS == ClassID();
486 return m_pcItem ? mxGetNumberOfElements( m_pcItem ) : 0;
495 return m_pcItem ? mxGetElementSize( m_pcItem ) : 0;
504 return m_pcItem ? mxGetNumberOfDimensions( m_pcItem ) : 0;
513 return NumElements() * ByElement();
522 return m_pcItem ? mxGetClassID( m_pcItem ) : mxUNKNOWN_CLASS;
533 if( IsEmpty() )
return TC_EMPTY;
539 if( mxIsComplex( m_pcItem ) )
544 case mxLOGICAL_CLASS:
553 if( IsScalar() )
return TC_SIMPLE;
554 return IsVector() ? TC_SIMPLE_VECTOR : TC_SIMPLE_ARRAY;
556 return ( IsScalar() || IsVector() ) ? TC_SIMPLE : TC_SIMPLE_ARRAY;
557 case mxUNKNOWN_CLASS:
559 return bCanSerialize ? TC_COMPLEX : TC_UNSUPP;
574 return !IsEmpty() ? mxGetData( m_pcItem ) : NULL;
584 char *
GetString(
bool flagUTF =
false,
const char* format = NULL )
const 588 mxArray* new_string = NULL;
589 const mxArray* org_string = m_pcItem;
594 mxArray* args[2] = { mxCreateString( format ),
const_cast<mxArray*
>(org_string) };
596 mexCallMATLAB( 1, &new_string, 2, args,
"sprintf" );
597 mxDestroyArray( args[0] );
599 org_string = new_string;
605 char* temp = mxArrayToString( org_string );
610 count = strlen( temp ) + 1;
611 result = (
char*)
MEM_ALLOC( count,
sizeof(
char) );
615 memcpy( result, temp, count );
639 buflen =
utils_latin2utf( (
unsigned char*)result, (
unsigned char*)buffer );
640 buffer = (
char*)
MEM_ALLOC( buflen,
sizeof(
char) );
683 case mxINT8_CLASS :
return (
int) *( (int8_t*) Data() );
684 case mxUINT8_CLASS :
return (
int) *( (uint8_t*) Data() );
685 case mxINT16_CLASS :
return (
int) *( (int16_t*) Data() );
686 case mxUINT16_CLASS:
return (
int) *( (uint16_t*) Data() );
687 case mxINT32_CLASS :
return (
int) *( (int32_t*) Data() );
688 case mxUINT32_CLASS:
return (
int) *( (uint32_t*) Data() );
689 case mxSINGLE_CLASS:
return (
int) *( (
float*) Data() );
690 case mxDOUBLE_CLASS:
return (
int) *( (
double*) Data() );
691 case mxLOGICAL_CLASS:
return (
int) mxIsLogicalScalarTrue( m_pcItem );
714 case mxINT64_CLASS :
return *( (sqlite3_int64*) Data() );
718 return (sqlite3_int64) errval;
728 return IsScalar() ? mxGetScalar( m_pcItem ) : DBL_NAN;
738 const mxArray*
GetField(
int n,
const char* name )
const 740 mxArray* result = NULL;
744 result = mxGetField( m_pcItem, n, name );
752 if( mxGetFieldNumber( m_pcItem, name ) >= 0 )
756 result = mxCreateNumericMatrix( 0, 1, mxDOUBLE_CLASS, mxREAL );
774 mxSetCell( m_pcItem, i, const_cast<mxArray*>(cell) );
786 mexMakeArrayPersistent( m_pcItem );
797 if( !IsEmpty() && mxIsClass( m_pcItem,
"MException" ) )
799 mxArray* exception = Detach();
800 mexCallMATLAB( 0, NULL, 1, &exception,
"throw" );
814 assert( IsCell() && !IsEmpty() );
816 mxArray* _lhs = NULL;
817 mxArray** prhs = (mxArray**)Data();
818 mxArray* _exception = mexCallMATLABWithTrap( 1, &_lhs, (
int)NumElements(), prhs,
"feval" );
838 mxDestroyArray( _lhs );
843 mxDestroyArray( _exception );
878 m_typeID = SQLITE_NULL;
898 m_typeID = other.m_typeID;
899 m_blobsize = other.m_blobsize;
924 m_typeID = other.m_typeID;
925 m_blobsize = other.m_blobsize;
934 m_typeID = SQLITE_FLOAT;
942 m_typeID = SQLITE_INTEGER;
949 m_text =
const_cast<char*
>(txtValue);
950 m_typeID = SQLITE_TEXT;
958 m_typeID = SQLITE_TEXT;
975 m_blob =
const_cast<mxArray*
>(blobValue);
976 m_typeID = SQLITE_BLOB;
985 m_typeID = SQLITE_BLOB;
992 assert( m_text && ( m_typeID == SQLITE_TEXT || m_typeID == SQLITE_BLOB || m_typeID ==
SQLITE_BLOBX ) );
994 return (
void*)m_text;
1008 if( m_typeID == SQLITE_TEXT && m_text )
1019 if( m_typeID == SQLITE_BLOB && m_blob )
1021 mxDestroyArray( m_blob );
1026 m_typeID = SQLITE_NULL;
1059 : m_col_name(name.first), m_name(name.second), m_isAnyType(false)
1070 for(
int i = 0; i < (int)m_any.size(); i++ )
1083 if( m_isAnyType && row < (
int)m_any.size() )
1085 m_any[row].Destroy();
1092 return m_isAnyType ? m_any.size() : m_float.size();
1103 return m_isAnyType ?
const_cast<const ValueSQL&
>(m_any[index]) :
ValueSQL( m_float[index] );
1116 assert( !m_any.size() );
1119 for(
int i = 0; i < (int)m_float.size(); i++ )
1121 m_any.push_back(
ValueSQL(m_float[i]) );
1135 m_any.push_back(
ValueSQL(value) );
1139 m_float.push_back( value );
1147 double dVal = (double)(value);
1148 long long llVal = (sqlite3_int64) dVal;
1150 if( llVal == value )
1158 m_any.push_back(
ValueSQL(value) );
1166 m_any.push_back(
ValueSQL(value) );
1173 m_any.push_back(
ValueSQL(value) );
1180 m_any.push_back(
ValueSQL(value) );
1187 m_any.push_back(
ValueSQL(value) );
1196 append( item.m_float );
1199 case SQLITE_INTEGER:
1200 append( item.m_integer );
1218 m_any.push_back( item );
1233 case SQLITE_INTEGER:
1281 size_t nElements = 0;
1285 nElements = dims[0];
1286 for(
int i = 1; i < (int)nDims; i++ )
1288 nElements *= dims[i];
1293 sizeof(dims[0]) * nDims +
1294 nElements * elBytes;
1301 pThis->
m_dims[0] = nDims;
1303 for(
int i = 0; i < (int)nDims; i++ )
1305 pThis->
m_dims[i+1] = dims[i];
1318 size_t dims[] = {m, n};
1319 return CreateArray( 2, dims, typeID );
void append(char *value)
Appends a new row element (non-const text)
Encapsulating a MATLAB mxArray.
size_t m_blobsize
Size of BLOB in bytes (only type SQLITE_BLOBX)
const mxArray * GetField(int n, const char *name) const
Get field from a struct array.
ValueMex & operator=(const ValueMex &other)
Assignment operator for const objects.
void append(double value)
Appends a new row element (floating point)
ValueBase & operator=(ValueBase &other)
Move assignment operator for lvalues.
ValueSQL & operator=(const ValueSQL &other)
Assignment operator for constant objects.
mxArray * Item()
Returns hosted MATLAB array.
ValueBase & operator=(const ValueBase &other)
Assignment operator.
mxClassID ClassID() const
Returns item class ID or mxUNKNOWN_CLASS if item is NULL.
type_complexity_e Complexity(bool bCanSerialize=false) const
Get complexity information. Which storage level is necessary (scalar, vector, matrix, text, blob)
single non-complex value, char or simple string (SQLite simple types)
static ValueMex CreateCellMatrix(int m, int n)
Create a cell array.
ValueMex Duplicate() const
Returns a duplictae of the hosted MATLAB array.
ValueMex(ValueMex &&other)
Move ctor for rvalues (temporary objects)
vector< double > m_float
row elements as pure double type
char * GetString(bool flagUTF=false, const char *format=NULL) const
Convert a string to char, due flagUTF converted to utf8.
void swapToAnyType()
Transform storage type.
int GetInt(int errval=0) const
Get integer value from item.
int m_typeID
Type of SQL value as integer ID.
string m_name
Table column name (MATLAB)
void append(ValueSQL &item)
Appends a new row element (non-const SQL value)
const char * getLocaleMsg(int iMsgNr)
Returns the translation for a defined message.
void MakePersistent()
Make the MATLAB array persistent.
void Destroy()
Freeing memory space if having ownership.
size_t utils_elbytes(mxClassID classID)
Get the size of one element in bytes.
size_t ByElement() const
Returns size in bytes of one element.
mxArray * Detach()
Detach hosted MATLAB array.
ValueBase()
Standard ctor.
ValueBase(ValueBase &&other)
Move ctor for rvalues (temporary objects)
Base class for ValueMex and ValueSQL.
mxArray replacement for speed improvement
bool m_isConst
if flagged as non-const, class may swap memory ownership (custody)
ValueSQL & operator=(ValueSQL &&other)
Move assignment operator for rvalues (temporary objects)
size_t ByData() const
Returns data size in bytes.
void append(const mxArray *value)
Appends a new row element (const MATLAB array)
sqlite3_int64 GetInt64(int errval=0) const
Get 64 bit integer value from item.
size_t m_dims[1]
count of dimensions
Class encapsulating a SQL field value.
ValueSQL(const mxArray *blobValue)
Ctor for MATLAB const mxArray* type initializer.
ValueMex & operator=(ValueMex &&other)
Move assignment operator for rvalues (temporary objects)
ValueSQL(mxArray *blobValue)
Ctor for MATLAB mxArray* type initializer.
ValueSQL(char *txtValue)
Ctor for char* type initializer.
bool IsVector() const
Returns true if m_pcItem is of size 1xN or Mx1.
static tagNativeArray * CreateArray(size_t nDims, size_t dims[], int typeID)
Array allocator.
void blob_free(void **pBlob)
Free memory allocated for a BLOB.
ValueBase(ValueBase &other)
Move ctor for lvalues.
const ValueSQL operator[](int index)
Indexing operator.
void append(sqlite3_int64 value)
Appends a new row element (integer)
static ValueMex CreateDoubleScalar(double value)
Create a double scalar.
structs, cells, complex data (SQLite typed ByteStream BLOB)
void Destroy(int row)
Deleting a single row element.
#define MEM_FREE(ptr)
standard memory free function
void utils_destroy_array(mxArray *&pmxarr)
Freeing memory allocated by mxCreateNumericMatrix() or mxCreateNumericArray().
int NumDims() const
Returns number of dimensions.
ValueSQL(const ValueSQL &other)
Copy ctor for constant objects.
string m_col_name
Table column name (SQL)
size_t NumElements() const
Returns number of elements.
void append(const ValueSQL &item)
Appends a new row element (const SQL value)
bool IsComplex() const
Returns true if item is not NULL and complex.
size_t GetM() const
Returns row count (1st dimension)
ValueSQL & operator=(ValueSQL &other)
Move assignment operator for lvalues.
ValueMex & operator=(ValueMex &other)
Move assignment operator for lvalues.
size_t m_elBytes
size of one single element in bytes
bool IsEmpty() const
Returns true if item is NULL or empty ([])
int g_convertUTF8
Flag: String representation (utf8 or ansi)
ValueSQL(const char *txtValue)
Ctor for const char* type initializer.
ValueMex(const ValueMex &other)
Copy ctor for const objects.
void utils_free_ptr(T *&pmxarr)
Freeing memory allocated by mxAlloc() or mxRealloc()
void Call(ValueMex *lhs, ValueMex *exception)
Calling a MATLAB function (handle) with arguemnts.
void * Data() const
Returns pointer to raw data.
pair< string, string > StringPair
Holds one table column name (first=SQL name, second=MATLAB name)
bool IsFunctionHandle() const
Returns true if m_pcItem is of type mxFUNCTION_CLASS.
size_t GetN() const
Returns col count (2nd dimension)
bool IsStruct() const
Returns true if item is a struct array.
ValueSQL(ValueSQL &&other)
Move ctor for rvalues (temporary objects)
ValueBase & operator=(ValueBase &&other)
Move assignment operator for rvalues (temporary objects)
static void FreeArray(tagNativeArray *pNativeArray)
Array deallocator.
ValueBase(const ValueBase &other)
Copy ctor for constant objects.
char * GetEncString() const
Returns allocated memory with items test, due to global flag converted to UTF.
void append(const char *value)
Appends a new row element (const text)
#define SQLITE_BLOBX
Identifier to flag another allocator as used for SQLITE_BLOB.
static ValueMex CreateString(const char *str)
Create a string.
int g_NULLasNaN
Flag: return NULL as NaN.
int utils_latin2utf(const unsigned char *s, unsigned char *buffer)
Convert char string to UTF-8 string.
ValueMex & Adopt(bool doAdopt=true)
Take ownership (custody) of a MEX array.
bool m_isAnyType
true, if it's pure double (integer) type
ValueSQLCol(StringPair name)
Ctor with column name-pair.
HC_ASSERT(sizeof(sqlite3_int64)<=sizeof(long long))
Asserting size of SQLite 64-bit integer type at least size of long long type.
ValueMex(ValueMex &other)
Move ctor for lvalues.
Class encapsulating a complete SQL table column with type and name.
void Throw()
Throws an exception if any occured.
void append(mxArray *value)
Appends a new row element (non-const MATLAB array)
const mxArray * Item() const
Returns hosted MATLAB array.
ValueSQL(char *blobValue, size_t size)
Ctor for char* type initializer.
bool IsDoubleClass() const
Returns true if m_pcItem is of type mxDOUBLE_CLASS.
size_t size()
Returns the row count.
void SetCell(int i, const mxArray *cell)
Sets a cell of a MATLAB cell array.
multidimensional non-complex numeric or char arrays (SQLite typed BLOB)
non-complex numeric vectors (SQLite BLOB)
vector< ValueSQL > m_any
row elements with type information
static tagNativeArray * CreateMatrix(size_t m, size_t n, int typeID)
Matrix allocator.
ValueSQL(ValueSQL &other)
Move ctor for lvalues.
ValueMex(const mxArray *pcItem)
Copy ctor for mxArrays.
bool IsScalar() const
Returns true if item consists of exact 1 element.
ValueMex(mwIndex m, mwIndex n, int clsid=mxDOUBLE_CLASS)
Ctor allocating new MATLAB matrix object.
ValueSQL(sqlite3_int64 iValue)
Ctor for llong type initializer.
void * Detach()
Release custody and return pointer type.
vector< StringPair > StringPairList
list of string pairs
ValueSQL(double dValue)
Ctor for double type initializer.
#define MEM_ALLOC(count, bytes)
standard memory allocator
bool IsCell() const
Returns true if item is a cell array.