db_object.c
Go to the documentation of this file.
/*
* Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
* Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
* Copyright (c) 2014 OpenDNSSEC AB (svb)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "db_object.h"
#include "db_error.h"
#include <stdlib.h>
/* DB OBJECT FIELD */
db_object_field_t* db_object_field_new(void) {
db_object_field_t* object_field =
(db_object_field_t*)calloc(1, sizeof(db_object_field_t));
if (object_field) {
object_field->type = DB_TYPE_EMPTY;
}
return object_field;
}
/* TODO: unit test */
db_object_field_t* db_object_field_new_copy(const db_object_field_t* from_object_field) {
db_object_field_t* object_field;
if (!from_object_field) {
return NULL;
}
if (!(object_field = db_object_field_new())
|| db_object_field_copy(object_field, from_object_field))
{
db_object_field_free(object_field);
return NULL;
}
return object_field;
}
void db_object_field_free(db_object_field_t* object_field) {
if (object_field) {
free(object_field);
}
}
/* TODO: unit test */
int db_object_field_copy(db_object_field_t* object_field, const db_object_field_t* from_object_field) {
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (!from_object_field) {
return DB_ERROR_UNKNOWN;
}
if (object_field->next) {
return DB_ERROR_UNKNOWN;
}
object_field->name = from_object_field->name;
object_field->type = from_object_field->type;
object_field->enum_set = from_object_field->enum_set;
return DB_OK;
}
const char* db_object_field_name(const db_object_field_t* object_field) {
if (!object_field) {
return NULL;
}
return object_field->name;
}
db_type_t db_object_field_type(const db_object_field_t* object_field) {
if (!object_field) {
return DB_TYPE_EMPTY;
}
return object_field->type;
}
int db_object_field_set_name(db_object_field_t* object_field, const char* name) {
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (!name) {
return DB_ERROR_UNKNOWN;
}
object_field->name = name;
return DB_OK;
}
int db_object_field_set_type(db_object_field_t* object_field, db_type_t type) {
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (type == DB_TYPE_EMPTY) {
return DB_ERROR_UNKNOWN;
}
object_field->type = type;
return DB_OK;
}
int db_object_field_set_enum_set(db_object_field_t* object_field, const db_enum_t* enum_set) {
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (object_field->type != DB_TYPE_ENUM) {
return DB_ERROR_UNKNOWN;
}
object_field->enum_set = enum_set;
return DB_OK;
}
int db_object_field_not_empty(const db_object_field_t* object_field) {
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (!object_field->name) {
return DB_ERROR_UNKNOWN;
}
if (object_field->type == DB_TYPE_EMPTY) {
return DB_ERROR_UNKNOWN;
}
if (object_field->type == DB_TYPE_ENUM && !object_field->enum_set) {
return DB_ERROR_UNKNOWN;
}
return DB_OK;
}
const db_object_field_t* db_object_field_next(const db_object_field_t* object_field) {
if (!object_field) {
return NULL;
}
return object_field->next;
}
/* DB OBJECT FIELD LIST */
db_object_field_list_t* db_object_field_list_new(void) {
db_object_field_list_t* object_field_list =
(db_object_field_list_t*)calloc(1, sizeof(db_object_field_list_t));
return object_field_list;
}
/* TODO: unit test */
db_object_field_list_t* db_object_field_list_new_copy(const db_object_field_list_t* from_object_field_list) {
db_object_field_list_t* object_field_list;
if (!from_object_field_list) {
return NULL;
}
if (!(object_field_list = db_object_field_list_new())
|| db_object_field_list_copy(object_field_list, from_object_field_list))
{
db_object_field_list_free(object_field_list);
return NULL;
}
return object_field_list;
}
void db_object_field_list_free(db_object_field_list_t* object_field_list) {
if (object_field_list) {
if (object_field_list->begin) {
db_object_field_t* this = object_field_list->begin;
db_object_field_t* next = NULL;
while (this) {
next = this->next;
db_object_field_free(this);
this = next;
}
}
free(object_field_list);
}
}
/* TODO: unit test */
int db_object_field_list_copy(db_object_field_list_t* object_field_list, const db_object_field_list_t* from_object_field_list) {
db_object_field_t* object_field;
db_object_field_t* object_field_copy;
if (!object_field_list) {
return DB_ERROR_UNKNOWN;
}
/*
* TODO: Should we be able to copy into a object field list that already
* contains data?
*/
if (object_field_list->begin) {
return DB_ERROR_UNKNOWN;
}
if (object_field_list->end) {
return DB_ERROR_UNKNOWN;
}
if (object_field_list->size) {
return DB_ERROR_UNKNOWN;
}
if (!from_object_field_list) {
return DB_ERROR_UNKNOWN;
}
object_field = from_object_field_list->begin;
while (object_field) {
if (!(object_field_copy = db_object_field_new_copy(object_field))
|| db_object_field_list_add(object_field_list, object_field_copy))
{
return DB_ERROR_UNKNOWN;
}
object_field = object_field->next;
}
return DB_OK;
}
int db_object_field_list_add(db_object_field_list_t* object_field_list, db_object_field_t* object_field) {
if (!object_field_list) {
return DB_ERROR_UNKNOWN;
}
if (!object_field) {
return DB_ERROR_UNKNOWN;
}
if (db_object_field_not_empty(object_field)) {
return DB_ERROR_UNKNOWN;
}
if (object_field->next) {
return DB_ERROR_UNKNOWN;
}
if (object_field_list->begin) {
if (!object_field_list->end) {
return DB_ERROR_UNKNOWN;
}
object_field_list->end->next = object_field;
object_field_list->end = object_field;
}
else {
object_field_list->begin = object_field;
object_field_list->end = object_field;
}
object_field_list->size++;
return DB_OK;
}
const db_object_field_t* db_object_field_list_begin(const db_object_field_list_t* object_field_list) {
if (!object_field_list) {
return NULL;
}
return object_field_list->begin;
}
size_t db_object_field_list_size(const db_object_field_list_t* object_field_list) {
if (!object_field_list) {
return 0;
}
return object_field_list->size;
}
/* DB OBJECT */
db_object_t* db_object_new(void) {
db_object_t* object =
(db_object_t*)calloc(1, sizeof(db_object_t));
return object;
}
void db_object_free(db_object_t* object) {
if (object) {
if (object->object_field_list) {
db_object_field_list_free(object->object_field_list);
}
free(object);
}
}
const db_connection_t* db_object_connection(const db_object_t* object) {
if (!object) {
return NULL;
}
return object->connection;
}
const char* db_object_table(const db_object_t* object) {
if (!object) {
return NULL;
}
return object->table;
}
const db_object_field_list_t* db_object_object_field_list(const db_object_t* object) {
if (!object) {
return NULL;
}
return object->object_field_list;
}
int db_object_set_connection(db_object_t* object, const db_connection_t* connection) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!connection) {
return DB_ERROR_UNKNOWN;
}
if (object->connection) {
return DB_ERROR_UNKNOWN;
}
object->connection = connection;
return DB_OK;
}
int db_object_set_table(db_object_t* object, const char* table) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!table) {
return DB_ERROR_UNKNOWN;
}
if (object->table) {
return DB_ERROR_UNKNOWN;
}
object->table = table;
return DB_OK;
}
int db_object_set_primary_key_name(db_object_t* object, const char* primary_key_name) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!primary_key_name) {
return DB_ERROR_UNKNOWN;
}
if (object->primary_key_name) {
return DB_ERROR_UNKNOWN;
}
object->primary_key_name = primary_key_name;
return DB_OK;
}
int db_object_set_object_field_list(db_object_t* object, db_object_field_list_t* object_field_list) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!object_field_list) {
return DB_ERROR_UNKNOWN;
}
if (object->object_field_list) {
return DB_ERROR_UNKNOWN;
}
object->object_field_list = object_field_list;
return DB_OK;
}
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) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!value_set) {
return DB_ERROR_UNKNOWN;
}
if (!object->connection) {
return DB_ERROR_UNKNOWN;
}
if (!object->table) {
return DB_ERROR_UNKNOWN;
}
if (!object->primary_key_name) {
return DB_ERROR_UNKNOWN;
}
if (object_field_list) {
return db_connection_create(object->connection, object, object_field_list, value_set);
}
return db_connection_create(object->connection, object, object->object_field_list, value_set);
}
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) {
if (!object) {
return NULL;
}
if (!object->connection) {
return NULL;
}
if (!object->table) {
return NULL;
}
if (!object->primary_key_name) {
return NULL;
}
return db_connection_read(object->connection, object, join_list, clause_list);
}
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) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!value_set) {
return DB_ERROR_UNKNOWN;
}
if (!object->connection) {
return DB_ERROR_UNKNOWN;
}
if (!object->table) {
return DB_ERROR_UNKNOWN;
}
if (!object->primary_key_name) {
return DB_ERROR_UNKNOWN;
}
if (object_field_list) {
return db_connection_update(object->connection, object, object_field_list, value_set, clause_list);
}
return db_connection_update(object->connection, object, object->object_field_list, value_set, clause_list);
}
int db_object_delete(const db_object_t* object, const db_clause_list_t* clause_list) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!object->connection) {
return DB_ERROR_UNKNOWN;
}
if (!object->table) {
return DB_ERROR_UNKNOWN;
}
if (!object->primary_key_name) {
return DB_ERROR_UNKNOWN;
}
return db_connection_delete(object->connection, object, clause_list);
}
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) {
if (!object) {
return DB_ERROR_UNKNOWN;
}
if (!count) {
return DB_ERROR_UNKNOWN;
}
if (!object->connection) {
return DB_ERROR_UNKNOWN;
}
if (!object->table) {
return DB_ERROR_UNKNOWN;
}
if (!object->primary_key_name) {
return DB_ERROR_UNKNOWN;
}
return db_connection_count(object->connection, object, join_list, clause_list, count);
}
db_clause_list_t
struct db_clause_list db_clause_list_t
Definition db_clause.h:110
db_connection_create
int db_connection_create(const db_connection_t *connection, const db_object_t *object, const db_object_field_list_t *object_field_list, const db_value_set_t *value_set)
Definition db_connection.c:102
db_connection_delete
int db_connection_delete(const db_connection_t *connection, const db_object_t *object, const db_clause_list_t *clause_list)
Definition db_connection.c:156
db_connection_read
db_result_list_t * db_connection_read(const db_connection_t *connection, const db_object_t *object, const db_join_list_t *join_list, const db_clause_list_t *clause_list)
Definition db_connection.c:122
db_connection_update
int db_connection_update(const db_connection_t *connection, 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)
Definition db_connection.c:136
db_connection_count
int db_connection_count(const db_connection_t *connection, const db_object_t *object, const db_join_list_t *join_list, const db_clause_list_t *clause_list, size_t *count)
Definition db_connection.c:170
db_connection_t
struct db_connection db_connection_t
Definition db_connection.h:34
db_enum_t
struct db_enum db_enum_t
db_error.h
DB_ERROR_UNKNOWN
#define DB_ERROR_UNKNOWN
Definition db_error.h:40
DB_OK
#define DB_OK
Definition db_error.h:36
db_join_list_t
struct db_join_list db_join_list_t
Definition db_join.h:36
db_object_field_copy
int db_object_field_copy(db_object_field_t *object_field, const db_object_field_t *from_object_field)
Definition db_object.c:76
db_object_field_list_new_copy
db_object_field_list_t * db_object_field_list_new_copy(const db_object_field_list_t *from_object_field_list)
Definition db_object.c:182
db_object_field_list_copy
int db_object_field_list_copy(db_object_field_list_t *object_field_list, const db_object_field_list_t *from_object_field_list)
Definition db_object.c:216
db_object_field_list_begin
const db_object_field_t * db_object_field_list_begin(const db_object_field_list_t *object_field_list)
Definition db_object.c:284
db_object_field_type
db_type_t db_object_field_type(const db_object_field_t *object_field)
Definition db_object.c:102
db_object_field_not_empty
int db_object_field_not_empty(const db_object_field_t *object_field)
Definition db_object.c:146
db_object_connection
const db_connection_t * db_object_connection(const db_object_t *object)
Definition db_object.c:320
db_object_field_new
db_object_field_t * db_object_field_new(void)
Definition db_object.c:40
db_object_update
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)
Definition db_object.c:441
db_object_field_list_add
int db_object_field_list_add(db_object_field_list_t *object_field_list, db_object_field_t *object_field)
Definition db_object.c:254
db_object_new
db_object_t * db_object_new(void)
Definition db_object.c:304
db_object_field_new_copy
db_object_field_t * db_object_field_new_copy(const db_object_field_t *from_object_field)
Definition db_object.c:52
db_object_set_table
int db_object_set_table(db_object_t *object, const char *table)
Definition db_object.c:356
db_object_field_set_name
int db_object_field_set_name(db_object_field_t *object_field, const char *name)
Definition db_object.c:110
db_object_set_connection
int db_object_set_connection(db_object_t *object, const db_connection_t *connection)
Definition db_object.c:341
db_object_read
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)
Definition db_object.c:424
db_object_field_set_type
int db_object_field_set_type(db_object_field_t *object_field, db_type_t type)
Definition db_object.c:122
db_object_object_field_list
const db_object_field_list_t * db_object_object_field_list(const db_object_t *object)
Definition db_object.c:334
db_object_field_list_size
size_t db_object_field_list_size(const db_object_field_list_t *object_field_list)
Definition db_object.c:292
db_object_table
const char * db_object_table(const db_object_t *object)
Definition db_object.c:327
db_object_field_set_enum_set
int db_object_field_set_enum_set(db_object_field_t *object_field, const db_enum_t *enum_set)
Definition db_object.c:134
db_object_free
void db_object_free(db_object_t *object)
Definition db_object.c:311
db_object_field_list_new
db_object_field_list_t * db_object_field_list_new(void)
Definition db_object.c:174
db_object_set_object_field_list
int db_object_set_object_field_list(db_object_t *object, db_object_field_list_t *object_field_list)
Definition db_object.c:386
db_object_field_name
const char * db_object_field_name(const db_object_field_t *object_field)
Definition db_object.c:94
db_object_field_free
void db_object_field_free(db_object_field_t *object_field)
Definition db_object.c:69
db_object_create
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)
Definition db_object.c:401
db_object_delete
int db_object_delete(const db_object_t *object, const db_clause_list_t *clause_list)
Definition db_object.c:464
db_object_field_list_free
void db_object_field_list_free(db_object_field_list_t *object_field_list)
Definition db_object.c:199
db_object_count
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)
Definition db_object.c:481
db_object_set_primary_key_name
int db_object_set_primary_key_name(db_object_t *object, const char *primary_key_name)
Definition db_object.c:371
db_object_field_next
const db_object_field_t * db_object_field_next(const db_object_field_t *object_field)
Definition db_object.c:162
db_object.h
db_object_field_list_t
struct db_object_field_list db_object_field_list_t
Definition db_object.h:38
db_object_field_t
struct db_object_field db_object_field_t
Definition db_object.h:37
db_object_t
struct db_object db_object_t
Definition db_object.h:36
db_result_list_t
struct db_result_list db_result_list_t
Definition db_result.h:36
db_type_t
db_type_t
Definition db_type.h:54
DB_TYPE_EMPTY
@ DB_TYPE_EMPTY
Definition db_type.h:58
DB_TYPE_ENUM
@ DB_TYPE_ENUM
Definition db_type.h:86
db_value_set_t
struct db_value_set db_value_set_t
Definition db_value.h:36
db_object_field_list::begin
db_object_field_t * begin
Definition db_object.h:143
db_object_field::type
db_type_t type
Definition db_object.h:55
db_object_field::enum_set
const db_enum_t * enum_set
Definition db_object.h:56
db_object_field::name
const char * name
Definition db_object.h:54
db_object_field::next
db_object_field_t * next
Definition db_object.h:53