metadata_table.c File Reference

#include <glib.h>
#include <string.h>
#include <liberutils/er_error.h>
#include "ermetadb_log.h"
#include "metadata_table.h"
#include "metadata_table_private.h"
Include dependency graph for metadata_table.c:

Go to the source code of this file.

Defines

#define LOGGING_ON   0

Functions

metadata_tablemetadata_table_new (void)
 Create a new metadata_table.
void metadata_table_free_impl (metadata_table *thiz)
void metadata_table_dump_impl (const metadata_table *thiz)
int metadata_table_add_column (metadata_table *thiz, const char *name)
 Add a column at the end of the table, only allowed on empty table or table with one row.
int metadata_table_find_column (const metadata_table *thiz, const char *name)
 Find column by its name.
static void metadata_table_add_rows (metadata_table *thiz, const guint idx)
static metadata_celladd_row_if_needed (metadata_table *thiz, guint idx)
int metadata_table_set_value (metadata_table *thiz, const guint idx, const sqlite3_value *value)
 Assign a value to a specific cell in a metadata_table This may move data already in the table to another memory location.
int metadata_table_set_int64 (metadata_table *thiz, const guint idx, const gint64 value)
 Assign an integer value to a specific cell in a sql table.
int metadata_table_set_double (metadata_table *thiz, const guint idx, const double value)
 Assign a floating point value to a specific cell in a sql table.
int metadata_table_set_text (metadata_table *thiz, const guint idx, const char *value)
 Assign a string value to a specific cell in a sql table This may move data already in the table to another memory location.
int metadata_table_set_blob (metadata_table *thiz, const guint idx, gchar *value, const guint len)
 Assign a blob to a specific cell in a sql table BLOB = Binary Large OBject.
int metadata_table_set_blob_static (metadata_table *thiz, const guint idx, const gchar *value, const guint len)
 Assign a blob to a specific cell in a sql table, assuming blob is static data BLOB = Binary Large OBject.
int metadata_table_set_cell (metadata_table *thiz, const guint idx, const metadata_cell *value)
 Assign a metadata_cell value to a specific cell in a metadata_table This may move data already in the table to another memory location.
const metadata_cellmetadata_table_get_cell (const metadata_table *thiz, const guint idx)
 Report the value of a specific cell in a metadata_table.
GString * metadata_table_get_string (const metadata_table *thiz, const guint idx)
 Report the value of a specific cell in a metadata_table, converted to a UTF-8 text string Not possible for SQLCELL_BLOB types.

Define Documentation

#define LOGGING_ON   0

Copyright (C) 2008 iRex Technologies B.V. All rights reserved.

Definition at line 39 of file metadata_table.c.


Function Documentation

static metadata_cell* add_row_if_needed ( metadata_table thiz,
guint  idx 
) [static]

Definition at line 212 of file metadata_table.c.

References metadata_table::cell_data, metadata_cell_clear_value(), and metadata_table_add_rows().

Referenced by metadata_table_set_blob(), metadata_table_set_blob_static(), metadata_table_set_cell(), metadata_table_set_double(), metadata_table_set_int64(), metadata_table_set_text(), and metadata_table_set_value().

00213 {
00214     // add row(s) to table, when needed
00215     metadata_table_add_rows(thiz, idx);
00216     metadata_cell *cell = &g_array_index(thiz->cell_data, metadata_cell, idx);
00217     metadata_cell_clear_value(cell);
00218     return cell;
00219 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_add_column ( metadata_table thiz,
const char *  name 
)

Add a column at the end of the table, only allowed on empty table or table with one row.

---------------------------------------------------------------------------

Name : metadata_table_add_column

Parameters:
thiz - the metadata_table
[in] name - name of the column to be added
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 127 of file metadata_table.c.

References metadata_table::cell_data, ER_INVALID_PARAMETER, ER_OK, LOGPRINTF, metadata_cell_set_name(), metadata_table::n_columns, and metadata_table::n_rows.

Referenced by create_result_table(), db_query_create(), db_query_update_lastread(), load_application_data(), load_file_metadata(), meta_file_close(), meta_file_open(), rotate_keyval_table(), save_application_data(), save_file_metadata(), and set_annotation_impl().

00128 {
00129     LOGPRINTF("entry: thiz [%p] name [%s]", thiz, name);
00130 
00131     g_return_val_if_fail(thiz,                ER_INVALID_PARAMETER);
00132     g_return_val_if_fail(name,                ER_INVALID_PARAMETER);
00133     g_return_val_if_fail((thiz->n_rows <= 1), ER_INVALID_PARAMETER);
00134 
00135     // add column
00136     thiz->n_columns++;
00137     guint n_col = thiz->n_columns;
00138     g_array_set_size(thiz->cell_data, n_col);
00139 
00140     // set name
00141     metadata_cell *cell = &g_array_index(thiz->cell_data, metadata_cell, n_col - 1);
00142     metadata_cell_set_name(cell, name);
00143 
00144     return ER_OK;
00145 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void metadata_table_add_rows ( metadata_table thiz,
const guint  idx 
) [static]

Definition at line 169 of file metadata_table.c.

References metadata_table::cell_data, LOGPRINTF, metadata_table::n_columns, and metadata_table::n_rows.

Referenced by add_row_if_needed().

00170 {
00171     guint n_rows = thiz->n_rows;
00172     const guint n_columns = thiz->n_columns;
00173 
00174     LOGPRINTF("entry: thiz [%p] idx [%d]", thiz, idx);
00175 
00176     // add cells to table, when needed
00177     guint n_cells_old = thiz->cell_data->len;
00178     if (idx >= n_cells_old)
00179     {
00180         if (n_columns == 0)
00181         {
00182             // no columns: allocate the requested number of cells
00183             g_array_set_size(thiz->cell_data, idx + 1);
00184         }
00185         else
00186         {
00187             // table has columns: allocate complete rows
00188             {
00189                 n_rows      = 1 + (idx / n_columns);
00190                 guint n_cells_new = n_rows * n_columns;
00191 
00192                 g_array_set_size(thiz->cell_data, n_cells_new);
00193             }
00194         }
00195     }
00196 
00197     // set current number of rows
00198     // note: n_rows is zero for a table that has columns defined but no data yet,
00199     //       therefore we must set n_rows even when no new cells are added
00200     if (n_columns)
00201     {
00202         n_rows = thiz->cell_data->len / n_columns;
00203     }
00204     else
00205     {
00206         n_rows = thiz->cell_data->len;
00207     }
00208     thiz->n_rows = n_rows;
00209 }

Here is the caller graph for this function:

void metadata_table_dump_impl ( const metadata_table thiz  ) 

Definition at line 83 of file metadata_table.c.

References LOGPRINTF, metadata_table_cell_index, metadata_table_get_cell(), metadata_table_get_string(), metadata_table_n_columns, metadata_table_n_rows, metadata_cell::name, and QUERYPRINTF.

00084 {
00085     LOGPRINTF("entry: thiz [%p]", thiz);
00086     if (thiz == NULL) return;
00087 
00088     int n_rows = metadata_table_n_rows(thiz);
00089     int n_cols = metadata_table_n_columns(thiz);
00090     GString *dump = g_string_new("");
00091 
00092     // print column names
00093     int col;
00094     for (col = 0; col < n_cols ; col++)
00095     {
00096         const metadata_cell *cell = metadata_table_get_cell(thiz, col);
00097         if (cell->name  &&  cell->name->str)
00098         {
00099             g_string_append(dump, cell->name->str);
00100         }
00101         g_string_append_c(dump, '|');
00102     }
00103     QUERYPRINTF("%s", dump->str);
00104 
00105     // print row values
00106     int row;
00107     for (row = 0 ; row < n_rows ; row++)
00108     {
00109         g_string_set_size(dump, 0);
00110         for (col = 0; col < n_cols ; col++)
00111         {
00112             GString *value = metadata_table_get_string(thiz, metadata_table_cell_index(thiz, row, col));
00113             if (value  &&  value->str)
00114             {
00115                 g_string_append(dump, value->str);
00116                 g_string_free(value, TRUE);
00117             }
00118             g_string_append_c(dump, '|');
00119         }
00120         QUERYPRINTF("%s", dump->str);
00121     }
00122 
00123     g_string_free(dump, TRUE);
00124 }

Here is the call graph for this function:

int metadata_table_find_column ( const metadata_table thiz,
const char *  name 
)

Find column by its name.

---------------------------------------------------------------------------

Name : metadata_table_find_column

Parameters:
thiz - the metadata_table
[in] name - name of the column to be found
Returns:
index of the column (0 ...), or -1 on error

--------------------------------------------------------------------------

Definition at line 148 of file metadata_table.c.

References metadata_table::cell_data, LOGPRINTF, metadata_table::n_columns, and metadata_cell::name.

Referenced by check_global_database_columns(), check_global_database_tables(), get_column_data(), load_application_data(), load_file_metadata(), load_page_data(), load_pages_basic_data(), rotate_keyval_table(), save_application_data(), set_boolean(), and set_double().

00149 {
00150     LOGPRINTF("entry: thiz [%p] name [%s]", thiz, name);
00151     g_assert(thiz);
00152     g_assert(name);
00153 
00154     metadata_cell *cell = &g_array_index(thiz->cell_data, metadata_cell, 0);
00155     unsigned int i;
00156     for (i = 0 ; i < thiz->n_columns; i++, cell++)
00157     {
00158         if (strcmp(name, cell->name->str) == 0)
00159         {
00160             return i;
00161         }
00162     }
00163 
00164     return -1;
00165 }

Here is the caller graph for this function:

void metadata_table_free_impl ( metadata_table thiz  ) 

Definition at line 65 of file metadata_table.c.

References metadata_table::cell_data, and metadata_cell_clear().

00066 {
00067     if (thiz == NULL) return;
00068     g_return_if_fail(thiz->cell_data);
00069 
00070     // clear all cells
00071     metadata_cell *cell = &g_array_index(thiz->cell_data, metadata_cell, 0);
00072     unsigned int i;
00073     for (i = 0 ; i < thiz->cell_data->len ; i++, cell++)
00074     {
00075         metadata_cell_clear(cell);
00076     }
00077 
00078     g_array_free(thiz->cell_data, TRUE);
00079     g_free(thiz);
00080 }

Here is the call graph for this function:

const metadata_cell* metadata_table_get_cell ( const metadata_table thiz,
const guint  idx 
)

Report the value of a specific cell in a metadata_table.

---------------------------------------------------------------------------

Name : metadata_table_get_cell

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
Returns:
cell value as a metadata_cell -- DO NOT MODIFY or NULL when error

--------------------------------------------------------------------------

Definition at line 310 of file metadata_table.c.

References metadata_table::cell_data, and LOGPRINTF.

Referenced by add_file_ids(), bind_single_statement(), check_database_version(), ermetadb_local_set_application_data(), get_column_data(), get_column_names(), get_global_id(), get_keys(), get_local_id(), load_application_data(), load_file_metadata(), load_page_data(), load_pages_basic_data(), metadata_table_dump_impl(), metadata_table_get_string(), rotate_keyval_table(), and set_file_metadata_impl().

00311 {
00312     LOGPRINTF("entry: thiz [%p] idx [%d]", thiz, idx);
00313     g_return_val_if_fail(thiz,                         NULL);
00314     g_return_val_if_fail(thiz->cell_data,              NULL);
00315 
00316     if (idx < thiz->cell_data->len)
00317     {
00318         // report cell
00319         const metadata_cell *cell = &g_array_index(thiz->cell_data, metadata_cell, idx);
00320         return cell;
00321     }
00322 
00323     return NULL;
00324 }

Here is the caller graph for this function:

GString* metadata_table_get_string ( const metadata_table thiz,
const guint  idx 
)

Report the value of a specific cell in a metadata_table, converted to a UTF-8 text string Not possible for SQLCELL_BLOB types.

---------------------------------------------------------------------------

Name : metadata_table_get_string

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
Returns:
cell value as a zero-terminated GString, or NULL when error caller must release this string using g_string_free(string, TRUE)

--------------------------------------------------------------------------

Definition at line 327 of file metadata_table.c.

References ERRORPRINTF, LOGPRINTF, metadata_cell_get_string(), and metadata_table_get_cell().

Referenced by check_global_database_columns(), check_global_database_tables(), metadata_table_dump_impl(), and rotate_keyval_table().

00328 {
00329     LOGPRINTF("entry: thiz [%p] idx [%d]", thiz, idx);
00330 
00331     const metadata_cell *cell = metadata_table_get_cell(thiz, idx);
00332     if (cell == NULL)
00333     {
00334         ERRORPRINTF("cannot find cell: thiz [%p] idx [%u]", thiz, idx);
00335         return NULL;
00336     }
00337     else
00338     {
00339         return metadata_cell_get_string(cell);
00340     }
00341 }

Here is the call graph for this function:

Here is the caller graph for this function:

metadata_table* metadata_table_new ( void   ) 

Create a new metadata_table.

---------------------------------------------------------------------------

Name : metadata_table_new

Parameters:
-- 
Returns:
Pointer to the newly created table, or NULL

--------------------------------------------------------------------------

Definition at line 49 of file metadata_table.c.

References metadata_table::cell_data, metadata_table::n_columns, and metadata_table::n_rows.

Referenced by create_result_table(), db_query_create(), db_query_update_lastread(), load_application_data(), load_file_metadata(), meta_file_close(), meta_file_open(), rotate_keyval_table(), save_application_data(), save_file_metadata(), and set_annotation_impl().

00050 {
00051     metadata_table *thiz = g_new0(metadata_table, 1);
00052     g_assert(thiz);
00053     thiz->n_rows    = 0;
00054     thiz->n_columns = 0;
00055     thiz->cell_data = g_array_new(TRUE, TRUE, sizeof(metadata_cell));
00056 #if METADATA_NULL != 0x00
00057 # error  METADATA_NULL must have value 0x00
00058 # error    new entries in the metadata_table->cell_data array are initialised to 0x00,
00059 # error    which is assumed to set the metadata_cell.type to METADATA_NULL
00060 #endif
00061     return thiz;
00062 }

Here is the caller graph for this function:

int metadata_table_set_blob ( metadata_table thiz,
const guint  idx,
gchar *  value,
const guint  len 
)

Assign a blob to a specific cell in a sql table BLOB = Binary Large OBject.

---------------------------------------------------------------------------

Name : metadata_table_set_blob

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[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
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 271 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_blob(), and metadata_table::n_columns.

Referenced by save_file_metadata().

00272 {
00273     LOGPRINTF("entry: thiz [%p] idx [%d] value [%p]", thiz, idx, value);
00274 
00275     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00276     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00277     g_return_val_if_fail( value,                ER_INVALID_PARAMETER);
00278 
00279     metadata_cell *cell = add_row_if_needed(thiz, idx);
00280     return metadata_cell_set_blob(cell, value, len);
00281 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_blob_static ( metadata_table thiz,
const guint  idx,
const gchar *  value,
const guint  len 
)

Assign a blob to a specific cell in a sql table, assuming blob is static data BLOB = Binary Large OBject.

---------------------------------------------------------------------------

Name : metadata_table_set_blob_static

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - binary value Note: The metadata_table will NOT release this memory block so caller must ensure the data stays available for as long as 'thiz' metadata_table exists or until a new value is assigned to the same cell.
[in] len - length of value in bytes
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 284 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_blob_static(), and metadata_table::n_columns.

Referenced by set_annotation_impl().

00285 {
00286     LOGPRINTF("entry: thiz [%p] idx [%d] value [%p]", thiz, idx, value);
00287 
00288     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00289     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00290     g_return_val_if_fail( value,                ER_INVALID_PARAMETER);
00291 
00292     metadata_cell *cell = add_row_if_needed(thiz, idx);
00293     return metadata_cell_set_blob_static(cell, value, len);
00294 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_cell ( metadata_table thiz,
const guint  idx,
const metadata_cell value 
)

Assign a metadata_cell value to a specific cell in a metadata_table This may move data already in the table to another memory location.

---------------------------------------------------------------------------

Name : metadata_table_set_cell

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - metadata_cell whose value must be copied into the metadata_table
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 297 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_copy_value(), and metadata_table::n_columns.

Referenced by rotate_keyval_table().

00298 {
00299     LOGPRINTF("entry: thiz [%p] idx [%d]", thiz, idx);
00300 
00301     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00302     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00303     g_return_val_if_fail( value,                ER_INVALID_PARAMETER);
00304 
00305     metadata_cell *cell = add_row_if_needed(thiz, idx);
00306     return metadata_cell_copy_value(cell, value);
00307 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_double ( metadata_table thiz,
const guint  idx,
const double  value 
)

Assign a floating point value to a specific cell in a sql table.

---------------------------------------------------------------------------

Name : metadata_table_set_double

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - floating point value (double)
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 246 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_double(), and metadata_table::n_columns.

Referenced by set_double().

00247 {
00248     LOGPRINTF("entry: thiz [%p] idx [%d] value [%f]", thiz, idx, value);
00249 
00250     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00251     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00252 
00253     metadata_cell *cell = add_row_if_needed(thiz, idx);
00254     return metadata_cell_set_double(cell, value);
00255 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_int64 ( metadata_table thiz,
const guint  idx,
const gint64  value 
)

Assign an integer value to a specific cell in a sql table.

---------------------------------------------------------------------------

Name : metadata_table_set_int64

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - integer value (gint64)
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 234 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_int64(), and metadata_table::n_columns.

Referenced by db_query_update_lastread(), and set_boolean().

00235 {
00236     LOGPRINTF("entry: thiz [%p] idx [%d] value [%lld]", thiz, idx, value);
00237 
00238     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00239     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00240 
00241     metadata_cell *cell = add_row_if_needed(thiz, idx);
00242     return metadata_cell_set_int64(cell, value);
00243 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_text ( metadata_table thiz,
const guint  idx,
const char *  value 
)

Assign a string value to a specific cell in a sql table This may move data already in the table to another memory location.

---------------------------------------------------------------------------

Name : metadata_table_set_text

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - string value Note: The value is copied into metadata_cell, so caller keeps ownership and must eventually free it.
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 258 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_text(), and metadata_table::n_columns.

Referenced by save_application_data(), and save_file_metadata().

00259 {
00260     LOGPRINTF("entry: thiz [%p] idx [%d] value [%s]", thiz, idx, value);
00261 
00262     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00263     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00264     g_return_val_if_fail( value,                ER_INVALID_PARAMETER);
00265 
00266     metadata_cell *cell = add_row_if_needed(thiz, idx);
00267     return metadata_cell_set_text(cell, value);
00268 }

Here is the call graph for this function:

Here is the caller graph for this function:

int metadata_table_set_value ( metadata_table thiz,
const guint  idx,
const sqlite3_value *  value 
)

Assign a value to a specific cell in a metadata_table This may move data already in the table to another memory location.

File Name : metadata_table_private.h

Description: Extension of metadata_table.h for libermetadb internal use only. Copyright (C) 2008 iRex Technologies B.V. All rights reserved.---------------------------------------------------------------------------

Name : metadata_table_set_value

Parameters:
thiz - the metadata_table
[in] idx - cell index (0 ...)
[in] value - sqlite3_value to be assigned to this cell
Returns:
ER_OK or error code

--------------------------------------------------------------------------

Definition at line 222 of file metadata_table.c.

References add_row_if_needed(), ER_INVALID_PARAMETER, LOGPRINTF, metadata_cell_set_value(), and metadata_table::n_columns.

Referenced by execute_single_statement().

00223 {
00224     LOGPRINTF("entry: thiz [%p] idx [%d]", thiz, idx);
00225 
00226     g_return_val_if_fail( thiz,                 ER_INVALID_PARAMETER);
00227     g_return_val_if_fail((thiz->n_columns > 0), ER_INVALID_PARAMETER);
00228     
00229     metadata_cell *cell = add_row_if_needed(thiz, idx);
00230     return metadata_cell_set_value(cell, value);
00231 }

Here is the call graph for this function:

Here is the caller graph for this function:

Generated by  doxygen 1.6.2-20100208