00001 #ifndef __METADATA_TABLE_H__ 00002 #define __METADATA_TABLE_H__ 00003 00004 /** 00005 * File Name : metadata_table.h 00006 * 00007 * Description: Table with metadata, usually results of a sql query. 00008 * This type is designed for easy compatibility with sqlite3. 00009 */ 00010 00011 /* 00012 * This file is part of libermetadb. 00013 * 00014 * libermetadb is free software: you can redistribute it and/or modify 00015 * it under the terms of the GNU General Public License as published by 00016 * the Free Software Foundation, either version 2 of the License, or 00017 * (at your option) any later version. 00018 * 00019 * libermetadb is distributed in the hope that it will be useful, 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 * GNU General Public License for more details. 00023 * 00024 * You should have received a copy of the GNU General Public License 00025 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00026 */ 00027 00028 /** 00029 * Copyright (C) 2008 iRex Technologies B.V. 00030 * All rights reserved. 00031 */ 00032 00033 00034 //---------------------------------------------------------------------------- 00035 // Include Files 00036 //---------------------------------------------------------------------------- 00037 00038 #include <glib.h> 00039 00040 #include "metadata_cell.h" 00041 00042 G_BEGIN_DECLS 00043 00044 00045 //---------------------------------------------------------------------------- 00046 // Macro Definitions 00047 //---------------------------------------------------------------------------- 00048 00049 #define metadata_table_n_rows(table) (table ? (table)->n_rows : 0 ) 00050 #define metadata_table_n_columns(table) (table ? (table)->n_columns : 0 ) 00051 #define metadata_table_data(table) (table ? (table)->cell_data : NULL) 00052 #define metadata_table_cell_index(table, row, column) (table ? (((row) * (table)->n_columns) + (column)) : 0) 00053 00054 00055 //---------------------------------------------------------------------------- 00056 // Type Declarations 00057 //---------------------------------------------------------------------------- 00058 00059 typedef struct 00060 { 00061 guint n_rows; // number of rows 00062 guint n_columns; // number of columns 00063 GArray *cell_data; // data as an array of metadata_cell 00064 } metadata_table; 00065 00066 00067 //============================================================================ 00068 // Public Functions 00069 //============================================================================ 00070 00071 /**--------------------------------------------------------------------------- 00072 * 00073 * Name : metadata_table_new 00074 * 00075 * @brief Create a new metadata_table 00076 * 00077 * @param -- 00078 * 00079 * @return Pointer to the newly created table, or NULL 00080 * 00081 *--------------------------------------------------------------------------*/ 00082 metadata_table *metadata_table_new (void); 00083 00084 00085 /**--------------------------------------------------------------------------- 00086 * 00087 * Name : metadata_table_free 00088 * 00089 * @brief Free the memory allocated to a metadata_table and the elements it holds 00090 * and clear the pointer passed as argument. 00091 * Function is void when a NULL pointer is passed. 00092 * 00093 * @param thiz - the metadata_table to be freed 00094 * 00095 * @return -- 00096 * 00097 *--------------------------------------------------------------------------*/ 00098 #define metadata_table_free(table) \ 00099 { \ 00100 metadata_table_free_impl(table); \ 00101 (table) = NULL; \ 00102 } 00103 void metadata_table_free_impl (metadata_table *thiz); 00104 00105 00106 /**--------------------------------------------------------------------------- 00107 * 00108 * Name : metadata_table_dump 00109 * 00110 * @brief Dump content of a metadata_table, 00111 * 00112 * @param thiz - the metadata_table 00113 * 00114 * @return -- 00115 * 00116 *--------------------------------------------------------------------------*/ 00117 #if QUERY_ON 00118 #define metadata_table_dump(thiz) metadata_table_dump_impl(thiz) 00119 #else 00120 #define metadata_table_dump(thiz) 00121 #endif 00122 void metadata_table_dump_impl (const metadata_table *thiz); 00123 00124 00125 /**--------------------------------------------------------------------------- 00126 * 00127 * Name : metadata_table_add_column 00128 * 00129 * @brief Add a column at the end of the table, 00130 * only allowed on empty table or table with one row 00131 * 00132 * @param thiz - the metadata_table 00133 * @param [in] name - name of the column to be added 00134 * 00135 * @return ER_OK or error code 00136 * 00137 *--------------------------------------------------------------------------*/ 00138 int metadata_table_add_column (metadata_table *thiz, const char *name); 00139 00140 00141 /**--------------------------------------------------------------------------- 00142 * 00143 * Name : metadata_table_find_column 00144 * 00145 * @brief Find column by its name 00146 * 00147 * @param thiz - the metadata_table 00148 * @param [in] name - name of the column to be found 00149 * 00150 * @return index of the column (0 ...), or -1 on error 00151 * 00152 *--------------------------------------------------------------------------*/ 00153 int metadata_table_find_column (const metadata_table *thiz, const char *name); 00154 00155 00156 /**--------------------------------------------------------------------------- 00157 * 00158 * Name : metadata_table_set_int64 00159 * 00160 * @brief Assign an integer value to a specific cell in a sql table 00161 * 00162 * @param thiz - the metadata_table 00163 * @param [in] idx - cell index (0 ...) 00164 * @param [in] value - integer value (gint64) 00165 * 00166 * @return ER_OK or error code 00167 * 00168 *--------------------------------------------------------------------------*/ 00169 int metadata_table_set_int64 (metadata_table *thiz, const guint idx, const gint64 value); 00170 00171 00172 /**--------------------------------------------------------------------------- 00173 * 00174 * Name : metadata_table_set_double 00175 * 00176 * @brief Assign a floating point value to a specific cell in a sql table 00177 * 00178 * @param thiz - the metadata_table 00179 * @param [in] idx - cell index (0 ...) 00180 * @param [in] value - floating point value (double) 00181 * 00182 * @return ER_OK or error code 00183 * 00184 *--------------------------------------------------------------------------*/ 00185 int metadata_table_set_double (metadata_table *thiz, const guint idx, const double value); 00186 00187 00188 /**--------------------------------------------------------------------------- 00189 * 00190 * Name : metadata_table_set_text 00191 * 00192 * @brief Assign a string value to a specific cell in a sql table 00193 * This may move data already in the table to another memory location 00194 * 00195 * @param thiz - the metadata_table 00196 * @param [in] idx - cell index (0 ...) 00197 * @param [in] value - string value 00198 * Note: The value is copied into metadata_cell, 00199 * so caller keeps ownership and must eventually free it. 00200 * 00201 * @return ER_OK or error code 00202 * 00203 *--------------------------------------------------------------------------*/ 00204 int metadata_table_set_text (metadata_table *thiz, const guint idx, const char *value); 00205 00206 00207 /**--------------------------------------------------------------------------- 00208 * 00209 * Name : metadata_table_set_blob 00210 * 00211 * @brief Assign a blob to a specific cell in a sql table 00212 * BLOB = Binary Large OBject 00213 * 00214 * @param thiz - the metadata_table 00215 * @param [in] idx - cell index (0 ...) 00216 * @param [in] value - binary value 00217 * Note: The value is "swallowed" by the metadata_cell 00218 * so caller looses ownership and must drop all pointers to it. 00219 * The metadata_cell will eventually release it with g_free() 00220 * so caller must have allocated it with g_new() or g_malloc(). 00221 * @param [in] len - length of value in bytes 00222 * 00223 * @return ER_OK or error code 00224 * 00225 *--------------------------------------------------------------------------*/ 00226 int metadata_table_set_blob (metadata_table *thiz, const guint idx, gchar *value, const guint len); 00227 00228 00229 /**--------------------------------------------------------------------------- 00230 * 00231 * Name : metadata_table_get_cell 00232 * 00233 * @brief Report the value of a specific cell in a metadata_table 00234 * 00235 * @param thiz - the metadata_table 00236 * @param [in] idx - cell index (0 ...) 00237 * 00238 * @return cell value as a metadata_cell -- DO NOT MODIFY 00239 * or NULL when error 00240 * 00241 *--------------------------------------------------------------------------*/ 00242 const metadata_cell* metadata_table_get_cell (const metadata_table *thiz, const guint idx); 00243 00244 00245 /**--------------------------------------------------------------------------- 00246 * 00247 * Name : metadata_table_get_string 00248 * 00249 * @brief Report the value of a specific cell in a metadata_table, 00250 * converted to a UTF-8 text string 00251 * Not possible for SQLCELL_BLOB types 00252 * 00253 * @param thiz - the metadata_table 00254 * @param [in] idx - cell index (0 ...) 00255 * 00256 * @return cell value as a zero-terminated GString, or NULL when error 00257 * caller must release this string using g_string_free(string, TRUE) 00258 * 00259 *--------------------------------------------------------------------------*/ 00260 GString *metadata_table_get_string (const metadata_table *thiz, const guint idx); 00261 00262 G_END_DECLS 00263 00264 #endif // __METADATA_TABLE_H__ 00265