File: dbase.h

package info (click to toggle)
dvbstreamer 2.1.0-5.8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,676 kB
  • sloc: ansic: 42,193; sh: 10,230; python: 519; makefile: 363
file content (402 lines) | stat: -rw-r--r-- 10,748 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Copyright (C) 2006  Adam Charrett

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

dbase.h

Opens/Closes and setups the sqlite database for use by the rest of the application.

*/

#ifndef _DBASE_H
#define _DBASE_H
#include <sqlite3.h>

/** @defgroup DatabaseConstants Database Constants
 * @{
 */

/**
 * Constant for Services Table name
 */
#define SERVICES_TABLE          "Services"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_MULTIPLEXUID    "mplexuid"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_ID              "id"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_SOURCE          "source"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_CA              "ca"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_NAME            "name"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_TYPE            "type"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_PMTPID          "pmtpid"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_PMTVERSION      "pmtversion"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_PCRPID          "pcrpid"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_PROVIDER        "provider"
/**
 * Constant for Service Table Column name
 */
#define SERVICE_DEFAUTHORITY    "defauthority"

/**
 * Constant for the Multiplexes table name.
 */
#define MULTIPLEXES_TABLE       "Multiplexes"
/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_UID           "uid"

/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_TSID          "tsid"

/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_NETID         "netid"

/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_TYPE          "type"

/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_PATVERSION    "patversion"

/**
 * Constant for Multiplex column name
 */
#define MULTIPLEX_TUNINGPARAMS "tuningparams"

/**
 * Constant for the PIDs table name,
 */
#define PIDS_TABLE              "PIDs"
/**
 * Constant for PID column name.
 */
#define PID_MULTIPLEXUID        "mplexuid"
/**
 * Constant for PID column name.
 */
#define PID_SERVICEID           "serviceid"
/**
 * Constant for PID column name.
 */
#define PID_PID                 "pid"
/**
 * Constant for PID column name.
 */
#define PID_TYPE                "type"
/**
 * Constant for PID column name.
 */
#define PID_SUBTYPE             "subtype"
/**
 * Constant for PID column name.
 */
#define PID_PMTVERSION          "pmtversion"
/**
 * Constant for PID column name.
 */
#define PID_DESCRIPTORS         "descriptors"

/**
 * Constant for Metadata table name.
 */
#define METADATA_TABLE           "Metadata"
/**
 * Constant for Metadata column name.
 */
#define METADATA_NAME            "name"
/**
 * Constant for Metadata column name.
 */
#define METADATA_VALUE           "value"

/**
 * Constant for Metadata property for LNB settings.
 * String defining the name of the LNB being used.
 */
#define METADATA_NAME_LNB             "lnb"
/**
 * Constant for Metadata property for LNB settings.
 * unsigned long for low frequency setting.
 */
#define METADATA_NAME_LNB_LOW_FREQ     "lnb.lowfreq"
/**
 * Constant for Metadata property for LNB settings.
 * unsigned long for high frequency setting.
 */
#define METADATA_NAME_LNB_HIGH_FREQ    "lnb.highfreq"
/**
 * Constant for Metadata property for LNB settings.
 * unsigned long for switch frequency setting.
 */
#define METADATA_NAME_LNB_SWITCH_FREQ  "lnb.switchfreq"

/**
 * Constant for Metadata property to scan all multiplexes on startup.
 */
#define METADATA_NAME_SCAN_ALL "scan.all"

/* This is the version of the database not the application!*/
#define METADATA_DBASE_VERSION "dbase_version"

#define DBASE_VERSION 2.0

/** @} */

/**
 * @defgroup Database Database Management
 * @{
 */

/**
 * Macro to define the varaiable required for executing an sql statement,
 * Only 1 call to STATEMENT_INIT per function is supported!
 */
#define STATEMENT_INIT int rc; sqlite3_stmt *stmt=NULL

#ifndef DBASE_CONNECTION_GET
/**
 * Macro for the function to call to get a database connection.
 */
#define DBASE_CONNECTION_GET DBaseConnectionGet
#endif

/**
 * Macro to prepare an sql statement.
 * @param _statement The sql statement to prepare.
 */
#define STATEMENT_PREPARE(_statement) rc = sqlite3_prepare( DBASE_CONNECTION_GET(),  _statement, -1, &stmt, NULL)

/**
 * Macro to prepare an sql statement with arguments.
 * @param _statement The sql statement to prepare.
 * @param _args The arguments to be inserted into the statement.
 */
#define STATEMENT_PREPAREVA(_statement, _args...) \
    do{\
        char *sqlstring;\
        sqlstring = sqlite3_mprintf(_statement, _args);\
        if (sqlstring)\
        {\
            STATEMENT_PREPARE(sqlstring);\
            sqlite3_free(sqlstring);\
        }\
        else\
        {\
            rc = SQLITE_NOMEM;\
        }\
    }while(0)

/**
 * Macro to perform a 'step' on an sql statement.
 */
#define STATEMENT_STEP()                rc = sqlite3_step(stmt)

/**
 * Macro to retrieve an int value from the result of a 'step'.
 * @param _index Column index of the result to retrieve.
 */
#define STATEMENT_COLUMN_INT(_index)    sqlite3_column_int(stmt, _index)
/**
 * Macro to retrieve a double value from the result of a 'step'.
 * @param _index Column index of the result to retrieve.
 */
#define STATEMENT_COLUMN_DOUBLE(_index) sqlite3_column_double(stmt, _index)

/**
 * Macro to retrieve a string from the result of a 'step'.
 * The string should be free'd using the sqlite_free function when it is no
 * longer required.
 * @param _index Column index of the result to retrieve.
 */
#define STATEMENT_COLUMN_TEXT(_index)   (char*)sqlite3_column_text( stmt, _index)

/**
 * Macro to finalise an sql statement.
 */
#define STATEMENT_FINALIZE()            rc = sqlite3_finalize(stmt)

/**
 * Macro to log the last SQLite error.
 * This macro logs at the LOG_ERROR level.
 */
#define PRINTLOG_SQLITE3ERROR() \
    do{\
        LogModule(LOG_DEBUG, "dbase", "%s(%d): Failed with error code 0x%x=%s\n",\
            __FUNCTION__,__LINE__, rc, sqlite3_errmsg(DBASE_CONNECTION_GET()));\
    }while(0)

/**
 * Macro to tidy up an sql statement and return the specified value if an error occured.
 *
 * This macro logs the sqlite error, finalises the sql statement and returns if
 * the error was not SQLITE_OK, SQLITE_ROW or SQLITE_DONE.
 * @param _result The value to return from the current function.
 */
#define RETURN_ON_ERROR(_result) \
    do{\
        if ((rc != SQLITE_OK) && (rc != SQLITE_ROW) && (rc != SQLITE_DONE))\
        {\
            PRINTLOG_SQLITE3ERROR();\
            if (stmt)\
            {\
                STATEMENT_FINALIZE();\
            }\
            return _result;\
        }\
    }while(0)

/**
 * Macro to tidy up an sql statemente and return the last result from an sqlite call.
 */
#define RETURN_RC_ON_ERROR RETURN_ON_ERROR(rc)

/**
 * @internal
 * Initialise the database for the given adapter.
 * This function will create the database if one doesn't exist,
 * and upgrade the database if it is a different version to the one being used
 * by the application.
 * @param adapter The DVB adapter number to open the database of.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseInit(int adapter);

/**
 * @internal
 * De-initialise the database.
 */
void DBaseDeInit();

/**
 * Get the sqlite3 connection object for this thread.
 * @return An sqlite3 connection object or NULL if the database could not be opened.
 */
sqlite3* DBaseConnectionGet(void);
    
/**
 * Start a transaction on the database.
 * Can be used to increase the speed when reading from multiple tables.
 * @return 0 on success, otherwise an SQLite error code. 
 */
int DBaseTransactionBegin(void);

/**
 * Commit a transaction on the database.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseTransactionCommit(void);

/**
 * Return the number of entries in the specified table.
 * @param table The table to count the entries in.
 * @param where Where clause, which may be null for all entries.
 * @return Number of entries in the table.
 */
int DBaseCount(char *table, char *where);

/**
 * Retrieve the specified metadata property.
 * @param name The name of the property.
 * @param value The location to store the value in. This should must be free'd 
 *              once finished with.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataGet(char *name, char **value);

/**
 * Set the specified metadata property to the string specified.
 * @param name The name of the property.
 * @param value The value to set it to.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataSet(char *name, char *value);

/**
 * Retrieve the specified metadata property.
 * @param name The name of the property.
 * @param value The location to store the value in.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataGetInt(char *name, int *value);
/**
 * Set the specified metadata property to the int specified.
 * @param name The name of the property.
 * @param value The value to set it to.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataSetInt(char *name, int value);

/**
 * Retrieve the specified metadata property.
 * @param name The name of the property.
 * @param value The location to store the value in.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataGetDouble(char *name, double *value);

/**
 * Set the specified metadata property to the double specified.
 * @param name The name of the property.
 * @param value The value to set it to.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataSetDouble(char *name, double value);

/**
 * Delete the specified metadata property.
 * @param name The name of the property.
 * @return 0 on success, otherwise an SQLite error code.
 */
int DBaseMetadataDelete(char *name);

/** @} */
#endif