1 /* 2 * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com> 3 * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation). 4 * Copyright (c) 2014 OpenDNSSEC AB (svb) 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 24 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #ifndef __db_object_h 31 #define __db_object_h 32 33 struct db_object; 34 struct db_object_field; 35 struct db_object_field_list; 36 typedef struct db_object db_object_t; 37 typedef struct db_object_field db_object_field_t; 38 typedef struct db_object_field_list db_object_field_list_t; 39 40 #include "db_connection.h" 41 #include "db_result.h" 42 #include "db_join.h" 43 #include "db_clause.h" 44 #include "db_type.h" 45 #include "db_value.h" 46 #include "db_enum.h" 47 #include "db_backend.h" 48 49 /** 50 * A representation of an field/value for a database object. 51 */ 52 struct db_object_field { 53 db_object_field_t* next; 54 const char* name; 55 db_type_t type; 56 const db_enum_t* enum_set; 57 }; 58 59 /** 60 * Create a database object field. 61 * \return a db_object_field_t pointer or NULL on error. 62 */ 63 extern db_object_field_t* db_object_field_new(void); 64 65 /** 66 * Create a database object field that is a copy of another. 67 * \param[in] from_object_field a db_object_field_t pointer. 68 * \return a db_object_field_t pointer or NULL on error. 69 */ 70 extern db_object_field_t* db_object_field_new_copy(const db_object_field_t* from_object_field); 71 72 /** 73 * Delete a database object field. 74 * \param[in] object_field a db_object_field_t pointer. 75 */ 76 extern void db_object_field_free(db_object_field_t* object_field); 77 78 /** 79 * Copy the content of a database object field. 80 * \param[in] object_field a db_object_field_t pointer. 81 * \param[in] from_object_field a db_object_field_t pointer. 82 * \return DB_ERROR_* on failure, otherwise DB_OK. 83 */ 84 extern int db_object_field_copy(db_object_field_t* object_field, const db_object_field_t* from_object_field); 85 86 /** 87 * Get the name of a database object field. 88 * \param[in] object_field a db_object_field_t pointer. 89 * \return a character pointer or NULL on error or if no field name has been set. 90 */ 91 extern const char* db_object_field_name(const db_object_field_t* object_field); 92 93 /** 94 * Get the type of a database object field. 95 * \param[in] object_field a db_object_field_t pointer. 96 * \return a db_type_t. 97 */ 98 extern db_type_t db_object_field_type(const db_object_field_t* object_field); 99 100 /** 101 * Set the name of a database object field. 102 * \param[in] object_field a db_object_field_t pointer. 103 * \param[in] name a character pointer. 104 * \return DB_ERROR_* on failure, otherwise DB_OK. 105 */ 106 extern int db_object_field_set_name(db_object_field_t* object_field, const char* name); 107 108 /** 109 * Set the type of a database object field. 110 * \param[in] object_field a db_object_field_t pointer. 111 * \param[in] type a db_type_t. 112 * \return DB_ERROR_* on failure, otherwise DB_OK. 113 */ 114 extern int db_object_field_set_type(db_object_field_t* object_field, db_type_t type); 115 116 /** 117 * Set the enumerate set of a database object field. 118 * \param[in] object_field a db_object_field_t pointer. 119 * \param[in] enum_set a NULL terminated db_enum_t list. 120 * \return DB_ERROR_* on failure, otherwise DB_OK. 121 */ 122 extern int db_object_field_set_enum_set(db_object_field_t* object_field, const db_enum_t* enum_set); 123 124 /** 125 * Check if the object field is not empty. 126 * \param[in] object_field a db_object_field_t pointer. 127 * \return DB_ERROR_* if empty, otherwise DB_OK. 128 */ 129 extern int db_object_field_not_empty(const db_object_field_t* object_field); 130 131 /** 132 * Get the next object field connected in a database object field list. 133 * \param[in] object_field a db_object_field_t pointer. 134 * \return a db_object_field_t pointer or NULL on error or if there are no more 135 * object fields in the list. 136 */ 137 extern const db_object_field_t* db_object_field_next(const db_object_field_t* object_field); 138 139 /** 140 * A list of object fields. 141 */ 142 struct db_object_field_list { 143 db_object_field_t* begin; 144 db_object_field_t* end; 145 size_t size; 146 }; 147 148 /** 149 * Create a new object field list. 150 * \return a db_object_field_list_t pointer or NULL on error. 151 */ 152 extern db_object_field_list_t* db_object_field_list_new(void); 153 154 /** 155 * Create a new object field list that is a copy of another. 156 * \param[in] from_object_field_list a db_object_field_list_t pointer. 157 * \return a db_object_field_list_t pointer or NULL on error. 158 */ 159 extern db_object_field_list_t* db_object_field_list_new_copy(const db_object_field_list_t* from_object_field_list); 160 161 /** 162 * Delete a object field list and all object fields within the list. 163 * \param[in] object_field_list a db_object_field_list_t pointer. 164 */ 165 extern void db_object_field_list_free(db_object_field_list_t* object_field_list); 166 167 /** 168 * Copy the content of a database object field list. 169 * \param[in] object_field_list a db_object_field_list_t pointer. 170 * \param[in] from_object_field_list a db_object_field_list_t pointer. 171 * \return DB_ERROR_* on failure, otherwise DB_OK. 172 */ 173 extern int db_object_field_list_copy(db_object_field_list_t* object_field_list, const db_object_field_list_t* from_object_field_list); 174 175 /** 176 * Add a database object field to a database object field list, this will takes 177 * over the ownership of the object field. 178 * \param[in] object_field_list a db_object_field_list_t pointer. 179 * \param[in] object_field a db_object_field_t pointer. 180 * \return DB_ERROR_* on failure, otherwise DB_OK. 181 */ 182 extern int db_object_field_list_add(db_object_field_list_t* object_field_list, db_object_field_t* object_field); 183 184 /** 185 * Return the first database object field in a database object field list. 186 * \param[in] object_field_list a db_object_field_list_t pointer. 187 * \return a db_object_field_t pointer or NULL on error or if the list is empty. 188 */ 189 extern const db_object_field_t* db_object_field_list_begin(const db_object_field_list_t* object_field_list); 190 191 /** 192 * Return the size of a object field list. 193 * \param[in] object_field_list a db_object_field_list_t pointer. 194 * \return a size_t, may be zero on error. 195 */ 196 extern size_t db_object_field_list_size(const db_object_field_list_t* object_field_list); 197 198 /** 199 * A database object. 200 */ 201 struct db_object { 202 const db_connection_t* connection; 203 const char* table; 204 const char* primary_key_name; 205 db_object_field_list_t* object_field_list; 206 }; 207 208 /** 209 * Create a new database object. 210 * \return a db_object_t pointer or NULL on error. 211 */ 212 extern db_object_t* db_object_new(void); 213 214 /** 215 * Delete a database object and the object field list and backend meta data list 216 * if set. 217 * \param[in] object a db_object_t pointer. 218 */ 219 extern void db_object_free(db_object_t* object); 220 221 /** 222 * Get the database connection of a database object. 223 * \param[in] object a db_object_t pointer. 224 * \return a db_connection_t pointer or NULL on error or if no connection has 225 * been set. 226 */ 227 extern const db_connection_t* db_object_connection(const db_object_t* object); 228 229 /** 230 * Get the table name of a database object. 231 * \param[in] object a db_object_t pointer. 232 * \return a character pointer or NULL on error or if no table name has been 233 * set. 234 */ 235 extern const char* db_object_table(const db_object_t* object); 236 237 /** 238 * Get the object field list of a database object. 239 * \param[in] object a db_object_t pointer. 240 * \return a db_object_field_list_t pointer or NULL on error or if no object 241 * field list has been set. 242 */ 243 extern const db_object_field_list_t* db_object_object_field_list(const db_object_t* object); 244 245 /** 246 * Set the database connection of a database object. 247 * \param[in] object a db_object_t pointer. 248 * \param[in] connection a db_connection_t pointer. 249 * \return DB_ERROR_* on failure, otherwise DB_OK. 250 */ 251 extern int db_object_set_connection(db_object_t* object, const db_connection_t* connection); 252 253 /** 254 * Set the table name of a database object. 255 * \param[in] object a db_object_t pointer. 256 * \param[in] table a character pointer. 257 * \return DB_ERROR_* on failure, otherwise DB_OK. 258 */ 259 extern int db_object_set_table(db_object_t* object, const char* table); 260 261 /** 262 * Set the primary key name of a database object. 263 * \param[in] object a db_object_t pointer. 264 * \param[in] primary_key_name a character pointer. 265 * \return DB_ERROR_* on failure, otherwise DB_OK. 266 */ 267 extern int db_object_set_primary_key_name(db_object_t* object, const char* primary_key_name); 268 269 /** 270 * Set the object field list of a database object, this takes over the ownership 271 * of the object field list. 272 * \param[in] object a db_object_t pointer. 273 * \param[in] object_field_list a db_object_field_list_t pointer. 274 * \return DB_ERROR_* on failure, otherwise DB_OK. 275 */ 276 extern int db_object_set_object_field_list(db_object_t* object, db_object_field_list_t* object_field_list); 277 278 /** 279 * Create an object in the database. The `object_field_list` describes the 280 * fields that should be set in the object and the `value_set` has the values 281 * for each field. 282 * \param[in] object a db_object_t pointer. 283 * \param[in] object_field_list a db_object_field_list_t pointer. 284 * \param[in] value_set a db_value_set_t pointer. 285 * \return DB_ERROR_* on failure, otherwise DB_OK. 286 */ 287 int db_object_create(const db_object_t* object, const db_object_field_list_t* object_field_list, const db_value_set_t* value_set); 288 289 /** 290 * Read an object or objects from the database. 291 * \param[in] object a db_object_t pointer. 292 * \param[in] join_list a db_join_list_t pointer. 293 * \param[in] clause_list a db_clause_list_t pointer. 294 * \return a db_result_list_t pointer or NULL on error or if no objects where 295 * read. 296 */ 297 extern db_result_list_t* db_object_read(const db_object_t* object, const db_join_list_t* join_list, const db_clause_list_t* clause_list); 298 299 /** 300 * Update an object or objects in the database. 301 * \param[in] object a db_object_t pointer. 302 * \param[in] object_field_list a db_object_field_list_t pointer. 303 * \param[in] value_set a db_value_set_t pointer. 304 * \param[in] clause_list a db_clause_list_t pointer. 305 * \return DB_ERROR_* on failure, otherwise DB_OK. 306 */ 307 extern int db_object_update(const db_object_t* object, const db_object_field_list_t* object_field_list, const db_value_set_t* value_set, const db_clause_list_t* clause_list); 308 309 /** 310 * Delete an object or objects from the database. 311 * \param[in] object a db_object_t pointer. 312 * \param[in] clause_list a db_clause_list_t pointer. 313 * \return DB_ERROR_* on failure, otherwise DB_OK. 314 */ 315 extern int db_object_delete(const db_object_t* object, const db_clause_list_t* clause_list); 316 317 /** 318 * Count objects from the database. Return the count in `count`. 319 * \param[in] object a db_object_t pointer. 320 * \param[in] join_list a db_join_list_t pointer. 321 * \param[in] clause_list a db_clause_list_t pointer. 322 * \param[out] count a size_t pointer. 323 * \return DB_ERROR_* on failure, otherwise DB_OK. 324 */ 325 extern int db_object_count(const db_object_t* object, const db_join_list_t* join_list, const db_clause_list_t* clause_list, size_t* count); 326 327 #endif 328