forked from clark15b/xupnpd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_sqlite.h
148 lines (100 loc) · 3.47 KB
/
db_sqlite.h
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
/*
* Copyright (C) 2015-2018 Anton Burdinuk
* http://xupnpd.org
*/
#ifndef __DB_SQLITE_H
#define __DB_SQLITE_H
#include <string>
#include <map>
#include "sqlite3.h"
namespace db_sqlite
{
struct object_t
{
std::string objid;
std::string parentid;
int objtype;
std::string items;
std::string handler;
int mimecode;
std::string length;
std::string name;
std::string url;
std::string logo;
std::string uuid;
bool _is_empty;
object_t(void):objtype(-1),mimecode(0),_is_empty(true) {}
bool empty(void) { return _is_empty; }
void clear(void)
{
objid.clear(); parentid.clear(); objtype=-1; items.clear();
handler.clear(); mimecode=0; length.clear(); name.clear(); url.clear(); logo.clear(); uuid.clear();
_is_empty=true;
}
};
class stmt
{
public:
sqlite3_stmt* st;
object_t __row;
stmt(void):st(NULL) {}
~stmt(void)
{ if(st) sqlite3_finalize(st); }
bool prepare(const char* sql,...);
bool bind(int idx,const std::string& value);
bool bind_int64(int idx,u_int64_t value);
bool bind_null(int idx);
int column_count(void) { return sqlite3_column_count(st); }
bool step(void)
{ return sqlite3_step(st)==SQLITE_ROW?true:false; }
bool exec(void)
{ return sqlite3_step(st)==SQLITE_DONE?true:false; }
bool fetch(std::map<std::string,std::string>& row);
bool fetch(object_t& row);
const object_t* fetch(void)
{ return fetch(__row)?&__row:NULL; }
void text(int col,std::string& s)
{
const char* p=(const char*)sqlite3_column_text(st,col); int n=sqlite3_column_bytes(st,col);
if(p && n>0)
s.assign(p,n);
else
s.clear();
}
std::string text(int col)
{
std::string s;
text(col,s);
return s;
}
int integer(int col) { return sqlite3_column_int(st,col); }
double number(int col) { return sqlite3_column_double(st,col); }
};
class locker
{
public:
locker(void);
~locker(void);
};
void begin(void);
bool exec(const char* sql,...);
void end(void);
// public API
typedef stmt rowset_t;
bool init(void);
void done(void);
bool find_object_by_id(const std::string& objid,object_t& obj);
bool find_next_object_by_id(const std::string& objid,const std::string& parentid,object_t& obj);
int get_childs_count(const std::string& parentid);
bool find_objects_by_parent_id(const std::string& parentid,int limit,int offset,rowset_t& st);
int get_objects_count(const std::string& from,const std::string& to,int what);
bool search_objects(const std::string& from,const std::string& to,int what,int count,int offset,rowset_t& st);
bool add_container(int contid,int parentid,int childs_num,const std::string& name);
bool add_media(int objid,int parentid,int objtype,const std::string& handler,int mimecode,u_int64_t length,const std::string& name,
const std::string& url,const std::string& logo,const std::string& uuid);
void begin_scan(void);
void end_scan(void);
bool search_objects_without_meta(rowset_t& st);
}
#endif