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_connection_h
31 #define __db_connection_h
32 
33 struct db_connection;
34 typedef struct db_connection db_connection_t;
35 
36 #include "db_configuration.h"
37 #include "db_backend.h"
38 #include "db_result.h"
39 #include "db_object.h"
40 #include "db_join.h"
41 #include "db_clause.h"
42 
43 /**
44  * A database connection.
45  */
46 struct db_connection {
47     const db_configuration_list_t* configuration_list;
48     db_backend_t* backend;
49 };
50 
51 /**
52  * Create a new database connection.
53  * \return a db_connection_t pointer or NULL on error.
54  */
55 extern db_connection_t* db_connection_new(void);
56 
57 /**
58  * Delete a database connection and the database backend within.
59  * \param[in] connection a db_connection_t pointer.
60  */
61 extern void db_connection_free(db_connection_t* connection);
62 
63 /**
64  * Set the database configuration list for a database connection.
65  * \param[in] connection a db_connection_t pointer.
66  * \param[in] configuration_list a db_configuration_list_t pointer.
67  * \return DB_ERROR_* on failure, otherwise DB_OK.
68  */
69 extern int db_connection_set_configuration_list(db_connection_t* connection, const db_configuration_list_t* configuration_list);
70 
71 /**
72  * Setup the database connection, this verifies the information in the database
73  * configuration list and allocated a database backend.
74  * \param[in] connection a db_connection_t pointer.
75  * \return DB_ERROR_* on failure, otherwise DB_OK.
76  */
77 extern int db_connection_setup(db_connection_t* connection);
78 
79 /**
80  * Connect to the database.
81  * \param[in] connection a db_connection_t pointer.
82  * \return DB_ERROR_* on failure, otherwise DB_OK.
83  */
84 extern int db_connection_connect(const db_connection_t* connection);
85 
86 /**
87  * Create an object in the database. The `object` refer to the database object
88  * begin created, the `object_field_list` describes the fields that should be
89  * set in the object and the `value_set` has the values for each field.
90  * \param[in] connection a db_connection_t pointer.
91  * \param[in] object a db_object_t pointer.
92  * \param[in] object_field_list a db_object_field_list_t pointer.
93  * \param[in] value_set a db_value_set_t pointer.
94  * \return DB_ERROR_* on failure, otherwise DB_OK.
95  */
96 extern 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);
97 
98 /**
99  * Read an object or objects from the database.
100  * \param[in] connection a db_connection_t pointer.
101  * \param[in] object a db_object_t pointer.
102  * \param[in] join_list a db_join_list_t pointer.
103  * \param[in] clause_list a db_clause_list_t pointer.
104  * \return a db_result_list_t pointer or NULL on error or if no objects where
105  * read.
106  */
107 extern 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);
108 
109 /**
110  * Update an object or objects in the database.
111  * \param[in] connection a db_connection_t pointer.
112  * \param[in] object a db_object_t pointer.
113  * \param[in] object_field_list a db_object_field_list_t pointer.
114  * \param[in] value_set a db_value_set_t pointer.
115  * \param[in] clause_list a db_clause_list_t pointer.
116  * \return DB_ERROR_* on failure, otherwise DB_OK.
117  */
118 extern 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);
119 
120 /**
121  * Delete an object or objects from the database.
122  * \param[in] connection a db_connection_t pointer.
123  * \param[in] object a db_object_t pointer.
124  * \param[in] clause_list a db_clause_list_t pointer.
125  * \return DB_ERROR_* on failure, otherwise DB_OK.
126  */
127 extern int db_connection_delete(const db_connection_t* connection, const db_object_t* object, const db_clause_list_t* clause_list);
128 
129 /**
130  * Count objects from the database. Return the count in `count`.
131  * \param[in] connection a db_connection_t pointer.
132  * \param[in] object a db_object_t pointer.
133  * \param[in] join_list a db_join_list_t pointer.
134  * \param[in] clause_list a db_clause_list_t pointer.
135  * \param[out] count a size_t pointer.
136  * \return DB_ERROR_* on failure, otherwise DB_OK.
137  */
138 extern 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);
139 
140 #endif
141