metadata_cell.c

Go to the documentation of this file.
00001 /*
00002  * File Name: metadata_cell.c
00003  */
00004 
00005 /*
00006  * This file is part of libermetadb.
00007  *
00008  * libermetadb is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * libermetadb is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <glib.h>
00033 #include <unistd.h>
00034 #include <sys/types.h>
00035 
00036 // ereader include files, between < >
00037 #include <liberutils/er_error.h>
00038 
00039 // local include files, between " "
00040 #define LOGGING_ON 0
00041 #include "ermetadb_log.h"
00042 #include "metadata_cell.h"
00043 #include "metadata_cell_private.h"
00044 
00045 //============================================================================
00046 // Functions Implementation
00047 //============================================================================
00048 
00049 void metadata_cell_clear (metadata_cell *thiz)
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 }
00061 
00062 
00063 void metadata_cell_clear_value (metadata_cell *thiz)
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 }
00088 
00089 
00090 int metadata_cell_copy_value (metadata_cell *thiz, const metadata_cell *src)
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 }
00131 
00132 
00133 int metadata_cell_set_name (metadata_cell *thiz, const char *name)
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 }
00151 
00152 
00153 int metadata_cell_set_value (metadata_cell *thiz, const sqlite3_value *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 }
00207 
00208 
00209 int metadata_cell_set_int64 (metadata_cell *thiz, const gint64 value)
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 }
00219 
00220 
00221 int metadata_cell_set_double (metadata_cell *thiz, const double value)
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 }
00232 
00233 
00234 int metadata_cell_set_text (metadata_cell *thiz, const char *value)
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 }
00258 
00259 
00260 int metadata_cell_set_blob (metadata_cell *thiz, gchar *value, const guint len)
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 }
00278 
00279 
00280 int metadata_cell_set_blob_static (metadata_cell *thiz, const gchar *value, const guint len)
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 }
00297 
00298 
00299 GString* metadata_cell_get_string (const metadata_cell *thiz)
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 }
00328 
Generated by  doxygen 1.6.2-20100208