#include <glib.h>
#include <unistd.h>
#include <sys/types.h>
#include <liberutils/er_error.h>
#include "ermetadb_log.h"
#include "metadata_cell.h"
#include "metadata_cell_private.h"
Go to the source code of this file.
Defines | |
#define | LOGGING_ON 0 |
Functions | |
void | metadata_cell_clear (metadata_cell *thiz) |
Clear the content (name, value) of a metadata_cell. | |
void | metadata_cell_clear_value (metadata_cell *thiz) |
Clear the value of a metadata_cell, leave its name as is. | |
int | metadata_cell_copy_value (metadata_cell *thiz, const metadata_cell *src) |
Copy the value of a metadata_cell, leave target name as is. | |
int | metadata_cell_set_name (metadata_cell *thiz, const char *name) |
Set the name of a metadata_cell. | |
int | metadata_cell_set_value (metadata_cell *thiz, const sqlite3_value *value) |
Assign a sqlite3 value to a metadata_cell. | |
int | metadata_cell_set_int64 (metadata_cell *thiz, const gint64 value) |
Assign an integer value to a metadata_cell. | |
int | metadata_cell_set_double (metadata_cell *thiz, const double value) |
Assign a floating point value to a metadata_cell. | |
int | metadata_cell_set_text (metadata_cell *thiz, const char *value) |
Assign a string value to a metadata_cell. | |
int | metadata_cell_set_blob (metadata_cell *thiz, gchar *value, const guint len) |
Assign a blob to a metadata_cell BLOB = Binary Large OBject. | |
int | metadata_cell_set_blob_static (metadata_cell *thiz, const gchar *value, const guint len) |
Assign a blob to a metadata_cell, assuming blob is static data BLOB = Binary Large OBject. | |
GString * | metadata_cell_get_string (const metadata_cell *thiz) |
Report the value of a metadata_cell converted to a UTF-8 text string Not possible for SQLCELL_BLOB types. |
#define LOGGING_ON 0 |
Copyright (C) 2008 iRex Technologies B.V. All rights reserved.
Definition at line 40 of file metadata_cell.c.
void metadata_cell_clear | ( | metadata_cell * | thiz | ) |
Clear the content (name, value) of a metadata_cell.
---------------------------------------------------------------------------
Name : metadata_cell_clear
thiz | - the metadata_cell |
--------------------------------------------------------------------------
Definition at line 49 of file metadata_cell.c.
References metadata_cell_clear_value(), and metadata_cell::name.
Referenced by metadata_cell_set_value(), and metadata_table_free_impl().
00050 { 00051 g_return_if_fail(thiz); 00052 00053 if (thiz->name) 00054 { 00055 g_string_free(thiz->name, TRUE); 00056 thiz->name = NULL; 00057 } 00058 00059 metadata_cell_clear_value(thiz); 00060 }
void metadata_cell_clear_value | ( | metadata_cell * | thiz | ) |
Clear the value of a metadata_cell, leave its name as is.
---------------------------------------------------------------------------
Name : metadata_cell_clear_value
thiz | - the metadata_cell |
--------------------------------------------------------------------------
Definition at line 63 of file metadata_cell.c.
References LOGPRINTF, METADATA_BLOB, METADATA_NULL, METADATA_TEXT, metadata_cell::type, metadata_cell::v_blob, metadata_cell::v_text, and metadata_cell::value.
Referenced by add_row_if_needed(), metadata_cell_clear(), metadata_cell_set_blob(), metadata_cell_set_blob_static(), metadata_cell_set_double(), metadata_cell_set_int64(), and metadata_cell_set_text().
00064 { 00065 LOGPRINTF("entry: thiz [%p]", thiz); 00066 g_return_if_fail(thiz); 00067 00068 // release value 00069 switch (thiz->type) 00070 { 00071 case METADATA_TEXT: 00072 g_string_free(thiz->value.v_text, TRUE); 00073 break; 00074 case METADATA_BLOB: 00075 if ( thiz->value.v_blob.data_is_static == FALSE ) 00076 { 00077 g_free( (gchar*)(thiz->value.v_blob.data) ); 00078 } 00079 thiz->value.v_blob.len = 0; 00080 break; 00081 default: 00082 ; //ignore 00083 } 00084 00085 // set type to "empty" 00086 thiz->type = METADATA_NULL; 00087 }
int metadata_cell_copy_value | ( | metadata_cell * | thiz, | |
const metadata_cell * | src | |||
) |
Copy the value of a metadata_cell, leave target name as is.
---------------------------------------------------------------------------
Name : metadata_cell_copy_value
[out] | thiz | - the metadata_cell to copy into |
[in] | src | - the metadata_cell to copy from |
--------------------------------------------------------------------------
Definition at line 90 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, ERRORPRINTF, LOGPRINTF, METADATA_BLOB, metadata_cell_set_blob(), metadata_cell_set_double(), metadata_cell_set_int64(), metadata_cell_set_null, metadata_cell_set_text(), METADATA_DOUBLE, METADATA_INT64, METADATA_NULL, METADATA_TEXT, metadata_cell::type, metadata_cell::v_blob, metadata_cell::v_double, metadata_cell::v_int64, metadata_cell::v_text, and metadata_cell::value.
Referenced by metadata_table_set_cell().
00091 { 00092 int ret = ER_OK; // return value 00093 int len; 00094 gchar *blob; 00095 00096 LOGPRINTF("entry: thiz [%p] src [%p]", thiz, src); 00097 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00098 g_return_val_if_fail(src, ER_INVALID_PARAMETER); 00099 00100 switch (src->type) 00101 { 00102 case METADATA_NULL: 00103 metadata_cell_set_null(thiz); 00104 break; 00105 case METADATA_INT64: 00106 metadata_cell_set_int64(thiz, src->value.v_int64); 00107 break; 00108 case METADATA_DOUBLE: 00109 metadata_cell_set_double(thiz, src->value.v_double); 00110 break; 00111 case METADATA_TEXT: 00112 metadata_cell_set_text(thiz, src->value.v_text->str); 00113 break; 00114 case METADATA_BLOB: 00115 // note: metadata_cell_set_blob takes ownership of blob, 00116 // so we must pass it a copy of the blob data 00117 len = src->value.v_blob.len; 00118 blob = g_malloc(len); 00119 g_assert(blob); 00120 memcpy(blob, src->value.v_blob.data, len); 00121 metadata_cell_set_blob(thiz, blob, len); 00122 break; 00123 default: 00124 ERRORPRINTF("unknown cell type [%d]", src->type); 00125 metadata_cell_set_null(thiz); 00126 ret = ER_INVALID_PARAMETER; 00127 } 00128 00129 return ret; 00130 }
GString* metadata_cell_get_string | ( | const metadata_cell * | thiz | ) |
Report the value of a metadata_cell converted to a UTF-8 text string Not possible for SQLCELL_BLOB types.
---------------------------------------------------------------------------
Name : metadata_cell_get_string
thiz | - the metadata_cell |
--------------------------------------------------------------------------
Definition at line 299 of file metadata_cell.c.
References ERRORPRINTF, LOGPRINTF, METADATA_DOUBLE, METADATA_INT64, METADATA_NULL, METADATA_TEXT, metadata_cell::type, metadata_cell::v_double, metadata_cell::v_int64, metadata_cell::v_text, and metadata_cell::value.
Referenced by metadata_table_get_string().
00300 { 00301 LOGPRINTF("entry: thiz [%p]", thiz); 00302 g_return_val_if_fail(thiz, NULL); 00303 00304 // get cell value and convert 00305 GString *string = g_string_new(""); 00306 switch (thiz->type) 00307 { 00308 case METADATA_INT64: 00309 g_string_printf(string, "%lld", thiz->value.v_int64); 00310 break; 00311 case METADATA_DOUBLE: 00312 g_string_printf(string, "%f", thiz->value.v_double); 00313 break; 00314 case METADATA_TEXT: 00315 g_string_assign(string, thiz->value.v_text->str); 00316 break; 00317 default: 00318 if (thiz->type != METADATA_NULL) 00319 { 00320 ERRORPRINTF("pid [%lld] cannot convert type [%d] to string", (gint64)getpid(), thiz->type); 00321 } 00322 g_string_free(string, TRUE); 00323 string = NULL; 00324 } 00325 00326 return string; 00327 }
int metadata_cell_set_blob | ( | metadata_cell * | thiz, | |
gchar * | value, | |||
const guint | len | |||
) |
Assign a blob to a metadata_cell BLOB = Binary Large OBject.
---------------------------------------------------------------------------
Name : metadata_cell_set_blob
thiz | - the metadata_cell | |
[in] | value | - binary value Note: The value is "swallowed" by the metadata_cell so caller looses ownership and must drop all pointers to it. The metadata_cell will eventually release it with g_free() so caller must have allocated it with g_new() or g_malloc(). |
[in] | len | - length of value in bytes |
--------------------------------------------------------------------------
Definition at line 260 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, METADATA_BLOB, metadata_cell_clear_value(), metadata_cell::type, metadata_cell::v_blob, and metadata_cell::value.
Referenced by metadata_cell_copy_value(), metadata_cell_set_value(), and metadata_table_set_blob().
00261 { 00262 LOGPRINTF("entry: thiz [%p] value [%p]", thiz, value); 00263 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00264 g_return_val_if_fail(value, ER_INVALID_PARAMETER); 00265 00266 metadata_cell_clear_value(thiz); 00267 00268 // store pointer to blob value 00269 thiz->type = METADATA_BLOB; 00270 thiz->value.v_blob.data = value; 00271 thiz->value.v_blob.len = len; 00272 00273 // remember to release this data 00274 thiz->value.v_blob.data_is_static = FALSE;; 00275 00276 return ER_OK; 00277 }
int metadata_cell_set_blob_static | ( | metadata_cell * | thiz, | |
const gchar * | value, | |||
const guint | len | |||
) |
Assign a blob to a metadata_cell, assuming blob is static data BLOB = Binary Large OBject.
---------------------------------------------------------------------------
Name : metadata_cell_set_blob_static
thiz | - the metadata_cell | |
[in] | value | - binary value Note: The metadata_cell will NOT release this memory block so caller must ensure the data stays available for as long as 'thiz' metadata_cell exists or until a new value is assigned to 'thiz' cell. |
[in] | len | - length of value in bytes |
--------------------------------------------------------------------------
Definition at line 280 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, METADATA_BLOB, metadata_cell_clear_value(), metadata_cell::type, metadata_cell::v_blob, and metadata_cell::value.
Referenced by metadata_table_set_blob_static().
00281 { 00282 LOGPRINTF("entry: thiz [%p] value [%p]", thiz, value); 00283 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00284 g_return_val_if_fail(value, ER_INVALID_PARAMETER); 00285 00286 metadata_cell_clear_value(thiz); 00287 00288 // store pointer to blob value 00289 thiz->type = METADATA_BLOB; 00290 thiz->value.v_blob.data = value; 00291 thiz->value.v_blob.len = len; 00292 00293 // remember to leave this data as is, so NEVER release it 00294 thiz->value.v_blob.data_is_static = TRUE; 00295 return ER_OK; 00296 }
int metadata_cell_set_double | ( | metadata_cell * | thiz, | |
const double | value | |||
) |
Assign a floating point value to a metadata_cell.
---------------------------------------------------------------------------
Name : metadata_cell_set_double
thiz | - the metadata_cell | |
[in] | value | - floating point value (double) |
--------------------------------------------------------------------------
Definition at line 221 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, metadata_cell_clear_value(), METADATA_DOUBLE, metadata_cell::type, metadata_cell::v_double, and metadata_cell::value.
Referenced by metadata_cell_copy_value(), metadata_cell_set_value(), and metadata_table_set_double().
00222 { 00223 LOGPRINTF("entry: thiz [%p] value [%f]", thiz, value); 00224 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00225 00226 metadata_cell_clear_value(thiz); 00227 thiz->type = METADATA_DOUBLE; 00228 thiz->value.v_double = value; 00229 00230 return ER_OK; 00231 }
int metadata_cell_set_int64 | ( | metadata_cell * | thiz, | |
const gint64 | value | |||
) |
Assign an integer value to a metadata_cell.
---------------------------------------------------------------------------
Name : metadata_cell_set_int64
thiz | - the metadata_cell | |
[in] | value | - integer value (gint64) |
--------------------------------------------------------------------------
Definition at line 209 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, metadata_cell_clear_value(), METADATA_INT64, metadata_cell::type, metadata_cell::v_int64, and metadata_cell::value.
Referenced by metadata_cell_copy_value(), metadata_cell_set_value(), and metadata_table_set_int64().
00210 { 00211 LOGPRINTF("entry: thiz [%p] value [%lld]", thiz, value); 00212 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00213 00214 metadata_cell_clear_value(thiz); 00215 thiz->type = METADATA_INT64; 00216 thiz->value.v_int64 = value; 00217 return ER_OK; 00218 }
int metadata_cell_set_name | ( | metadata_cell * | thiz, | |
const char * | name | |||
) |
Set the name of a metadata_cell.
---------------------------------------------------------------------------
Name : metadata_cell_set_name
thiz | - the metadata_cell | |
[in] | name | - the new cell-name, or NULL to clear name |
--------------------------------------------------------------------------
Definition at line 133 of file metadata_cell.c.
References ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, and metadata_cell::name.
Referenced by metadata_table_add_column().
00134 { 00135 LOGPRINTF("entry: thiz [%p] name [%s]", thiz, name); 00136 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00137 00138 if (thiz->name) 00139 { 00140 g_string_free(thiz->name, TRUE); 00141 thiz->name = NULL; 00142 } 00143 00144 if (name) 00145 { 00146 thiz->name = g_string_new(name); 00147 } 00148 00149 return ER_OK; 00150 }
int metadata_cell_set_text | ( | metadata_cell * | thiz, | |
const char * | value | |||
) |
Assign a string value to a metadata_cell.
---------------------------------------------------------------------------
Name : metadata_cell_set_text
thiz | - the metadata_cell | |
[in] | value | - string value Note: The value is copied into metadata_cell so caller keeps ownership and must eventually free it. |
--------------------------------------------------------------------------
Definition at line 234 of file metadata_cell.c.
References ER_FAIL, ER_INVALID_PARAMETER, ER_OK, ERRORPRINTF, LOGPRINTF, metadata_cell_clear_value(), METADATA_TEXT, metadata_cell::type, metadata_cell::v_text, and metadata_cell::value.
Referenced by metadata_cell_copy_value(), metadata_cell_set_value(), and metadata_table_set_text().
00235 { 00236 int ret = ER_OK; 00237 00238 LOGPRINTF("entry: thiz [%p] value [%s]", thiz, value); 00239 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00240 g_return_val_if_fail(value, ER_INVALID_PARAMETER); 00241 00242 metadata_cell_clear_value(thiz); 00243 00244 thiz->value.v_text = g_string_new(value); 00245 if (thiz->value.v_text == NULL) 00246 { 00247 ERRORPRINTF("cannot create GString"); 00248 ret = ER_FAIL; 00249 } 00250 else 00251 { 00252 // string assignment ok 00253 thiz->type = METADATA_TEXT; 00254 } 00255 00256 return ret; 00257 }
int metadata_cell_set_value | ( | metadata_cell * | thiz, | |
const sqlite3_value * | value | |||
) |
Assign a sqlite3 value to a metadata_cell.
File Name : metadata_cell_private.h
Description: Extension of metadata_cell.h for libermetadb internal use only. Copyright (C) 2008 iRex Technologies B.V. All rights reserved.---------------------------------------------------------------------------
Name : metadata_cell_set_value
thiz | - the metadata_cell | |
[in] | value | - sqlite3_value to be assigned to this cell |
--------------------------------------------------------------------------
Definition at line 153 of file metadata_cell.c.
References cp, ER_INVALID_PARAMETER, ER_OK, ERRORPRINTF, LOGPRINTF, metadata_cell_clear(), metadata_cell_set_blob(), metadata_cell_set_double(), metadata_cell_set_int64(), metadata_cell_set_null, and metadata_cell_set_text().
Referenced by metadata_table_set_value().
00154 { 00155 int ret = ER_OK; // return value 00156 gint64 i64; 00157 double d; 00158 const char *cp; 00159 gchar *blob; 00160 int len; 00161 00162 LOGPRINTF("entry: thiz [%p] value [%p]", thiz, value); 00163 g_return_val_if_fail(thiz, ER_INVALID_PARAMETER); 00164 g_return_val_if_fail(value, ER_INVALID_PARAMETER); 00165 00166 sqlite3_value *val = (sqlite3_value*)value; // const_cast 00167 int type = sqlite3_value_type(val); 00168 switch (type) 00169 { 00170 case SQLITE_INTEGER: 00171 i64 = sqlite3_value_int64(val); 00172 ret = metadata_cell_set_int64(thiz, i64); 00173 break; 00174 00175 case SQLITE_FLOAT: 00176 d = sqlite3_value_double(val); 00177 ret = metadata_cell_set_double(thiz, d); 00178 break; 00179 00180 case SQLITE_TEXT: 00181 cp = (const char*) sqlite3_value_text(val); 00182 ret = metadata_cell_set_text(thiz, cp); 00183 break; 00184 00185 case SQLITE_BLOB: 00186 // note: metadata_cell_set_blob takes ownership of blob, 00187 // so we must pass it a copy of the blob data 00188 len = sqlite3_value_bytes(val); 00189 blob = g_malloc(len); 00190 g_assert(blob); 00191 memcpy(blob, sqlite3_value_blob(val), len); 00192 ret = metadata_cell_set_blob(thiz, blob, len); 00193 break; 00194 00195 case SQLITE_NULL: 00196 metadata_cell_set_null(thiz); 00197 break; 00198 00199 default: 00200 ERRORPRINTF("illegal return [%d] from sqlite3_value_type", type); 00201 metadata_cell_clear(thiz); 00202 ret= ER_INVALID_PARAMETER; 00203 } 00204 00205 return ret; 00206 }