DVBCore  22.3.0
Open Source DVB Engine
database.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2013 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
26 #ifndef DBA_DATABASE_H
27 #define DBA_DATABASE_H
28 
29 #include <sqlite3.h>
30 
31 #include "stbllist.h"
32 
33 #define ARRAY_LENGTH(X) (sizeof(X)/sizeof(*X))
34 #define INITIAL_VERSION 0
35 
36 typedef enum
37 {
38  OPERATION_NONE,
39  OPERATION_UPDATE,
40  OPERATION_CREATE,
41  OPERATION_DELETE
42 } OPERATION_TYPE;
43 
44 typedef struct
45 {
46  U32BIT sqlite;
47  const char *sql;
48 } S_DATA_TYPE;
49 
50 typedef struct
51 {
52  U32BIT field_type;
53  const S_DATA_TYPE *data_type;
54 } S_COLUMN;
55 
56 typedef struct
57 {
58  U32BIT record_type;
59  const S_COLUMN *columns;
60  U32BIT length;
61 } S_TABLE;
62 
63 typedef struct
64 {
65  union
66  {
67  U32BIT uint;
68  char *text;
69  void *blob;
70  } value;
71  U32BIT size;
72 } S_FIELD;
73 
74 typedef struct s_record
75 {
76  LINK_LIST_PTR_BLK ptrs;
77  const S_TABLE *table;
78  sqlite3_int64 id;
79  struct s_record *parent;
80  struct s_record *sibling;
81  struct s_record *child;
82  OPERATION_TYPE operation;
83  S_FIELD fields[];
84 } S_RECORD;
85 
86 extern const S32BIT database_layout_version;
87 extern const S_DATA_TYPE database_type_uint;
88 extern const S_DATA_TYPE database_type_text;
89 extern const S_DATA_TYPE database_type_blob;
90 extern const S_TABLE database_layout[];
91 extern const U32BIT database_layout_length;
92 
97 BOOLEAN Database_Initialise(void);
98 
102 void Database_Uninitialise(void);
103 
107 void Database_Clear(void);
108 
114 const S_TABLE * Database_GetTable(U32BIT record_type);
115 
122 const S_COLUMN * Database_GetColumn(const S_TABLE *table, U32BIT field_type);
123 
131 S_RECORD * Database_GetRecord(const S_TABLE *table, const S_RECORD *prev, BOOLEAN exclude_deleted);
132 
139 S_RECORD * Database_FindRecord(const S_TABLE *table, sqlite3_int64 id);
140 
147 S_FIELD * Database_GetField(S_RECORD *record, const S_COLUMN *column);
148 
155 S_RECORD * Database_CreateRecord(const S_TABLE *table, S_RECORD *parent);
156 
163 BOOLEAN Database_DeleteRecord(S_RECORD *record);
164 
169 void Database_DestroyRecord(S_RECORD *record);
170 
176 void Database_AddRecordOperation(S_RECORD *record, OPERATION_TYPE operation);
177 
183 
190 
196 void Database_SetRecordParent(S_RECORD *record, S_RECORD *parent);
197 
203 U32BIT Database_GetRecordFieldUInt(S_FIELD *field);
204 
210 void Database_SetRecordFieldUInt(S_FIELD *field, U32BIT value);
211 
218 const char * Database_GetRecordFieldText(S_FIELD *field, U32BIT *size);
219 
226 void Database_SetRecordFieldText(S_FIELD *field, const unsigned char *value, U32BIT size);
227 
234 const void * Database_GetRecordFieldBlob(S_FIELD *field, U32BIT *size);
235 
242 void Database_SetRecordFieldBlob(S_FIELD *field, const void *value, U32BIT size);
243 
244 #endif
245 
void Database_SetRecordFieldBlob(S_FIELD *field, const void *value, U32BIT size)
Set blob value of &#39;field&#39;.
Definition: database.c:600
Definition: database.h:44
S_FIELD * Database_GetField(S_RECORD *record, const S_COLUMN *column)
Get field from &#39;column&#39; in given &#39;record&#39;.
Definition: database.c:249
void Database_ClearRecordOperation(S_RECORD *record)
Clear the record operation.
Definition: database.c:405
Definition: database.h:50
void Database_SetRecordFieldUInt(S_FIELD *field, U32BIT value)
Set uint value of &#39;field&#39;.
Definition: database.c:496
Definition: database.h:74
BOOLEAN Database_DeleteRecord(S_RECORD *record)
Free &#39;record&#39; buffers and mark as deleted. If not created in SQLite &#39;record&#39; is destroyed automatical...
Definition: database.c:307
void Database_SetRecordParent(S_RECORD *record, S_RECORD *parent)
Set the parent of &#39;record&#39;.
Definition: database.c:445
void Database_Clear(void)
Clear runtime database.
Definition: database.c:96
void Database_Uninitialise(void)
Uninitialise runtime database.
Definition: database.c:77
S_RECORD * Database_GetRecord(const S_TABLE *table, const S_RECORD *prev, BOOLEAN exclude_deleted)
Get record in &#39;table&#39; after &#39;prev&#39; or first record if &#39;prev&#39; is NULL.
Definition: database.c:185
Header file - Function prototypes for linked lists.
Definition: database.h:56
const S_COLUMN * Database_GetColumn(const S_TABLE *table, U32BIT field_type)
Get column in &#39;table&#39; with given &#39;field_type&#39;.
Definition: database.c:155
const char * Database_GetRecordFieldText(S_FIELD *field, U32BIT *size)
Get text value of &#39;field&#39;.
Definition: database.c:516
void Database_DestroyRecord(S_RECORD *record)
Destroy deleted &#39;record&#39;.
Definition: database.c:363
const S_TABLE * Database_GetTable(U32BIT record_type)
Get table with given &#39;record_type&#39;.
Definition: database.c:128
BOOLEAN Database_Initialise(void)
Initialise runtime database.
Definition: database.c:46
S_RECORD * Database_CreateRecord(const S_TABLE *table, S_RECORD *parent)
Create database record of type &#39;table&#39; with optional &#39;parent&#39;.
Definition: database.c:272
void Database_AddRecordOperation(S_RECORD *record, OPERATION_TYPE operation)
Add record operation (except delete). Operations are merged.
Definition: database.c:383
const void * Database_GetRecordFieldBlob(S_FIELD *field, U32BIT *size)
Get blob value of &#39;field&#39;.
Definition: database.c:575
void Database_SetRecordFieldText(S_FIELD *field, const unsigned char *value, U32BIT size)
Set text value of &#39;field&#39;.
Definition: database.c:541
S_RECORD * Database_GetRecordParent(S_RECORD *record)
Get parent of &#39;record&#39;.
Definition: database.c:424
Definition: database.h:63
U32BIT Database_GetRecordFieldUInt(S_FIELD *field)
Get uint value of &#39;field&#39;.
Definition: database.c:476
Definition: stbllist.h:38
S_RECORD * Database_FindRecord(const S_TABLE *table, sqlite3_int64 id)
Find record in &#39;table&#39; with given &#39;id&#39;.
Definition: database.c:222