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
|
// (c) 2004 Mark Kretschmann <[email protected]>
// (c) 2004 Christian Muehlhaeuser <[email protected]>
// (c) 2004 Sami Nieminen <[email protected]>
// (c) 2005 Ian Monroe <[email protected]>
// See COPYING file for licensing information.
#ifndef KLAMAV_COLLECTIONDB_H
#define KLAMAV_COLLECTIONDB_H
#include <kurl.h>
#include <qdir.h> //stack allocated
#include <qimage.h>
#include <qobject.h> //baseclass
#include <qptrqueue.h> //baseclass
#include <qsemaphore.h> //stack allocated
#include <qstringlist.h> //stack allocated
class DbConnection;
class DbConnectionPool;
class DbConfig
{};
class SqliteConfig : public DbConfig
{
public:
SqliteConfig( const QString& /* dbfile */ );
const QString dbFile() const { return m_dbfile; }
private:
QString m_dbfile;
};
class DbConnection
{
public:
enum DbConnectionType { sqlite = 0, mysql = 1, postgresql = 2 };
DbConnection( DbConfig* /* config */ );
virtual ~DbConnection() = 0;
virtual QStringList query( const QString& /* statement */ ) = 0;
virtual int insert( const QString& /* statement */, const QString& /* table */ ) = 0;
const bool isInitialized() const { return m_initialized; }
virtual bool isConnected() const = 0;
virtual const QString lastError() const { return "None"; }
protected:
bool m_initialized;
DbConfig *m_config;
};
typedef struct sqlite3 sqlite3;
typedef struct sqlite3_context sqlite3_context;
typedef struct Mem sqlite3_value;
class SqliteConnection : public DbConnection
{
public:
SqliteConnection( SqliteConfig* /* config */ );
~SqliteConnection();
QStringList query( const QString& /* statement */ );
int insert( const QString& /* statement */, const QString& /* table */ );
bool isConnected()const { return true; }
private:
static void sqlite_rand(sqlite3_context *context, int /*argc*/, sqlite3_value ** /*argv*/);
static void sqlite_power(sqlite3_context *context, int argc, sqlite3_value **argv);
sqlite3* m_db;
};
class DbConnectionPool : QPtrQueue<DbConnection>
{
public:
DbConnectionPool( bool temporary );
~DbConnectionPool();
const DbConnection::DbConnectionType getDbConnectionType() const { return m_dbConnType; }
const DbConfig *getDbConfig() const { return m_dbConfig; }
void createDbConnections();
DbConnection *getDbConnection();
void putDbConnection( const DbConnection* /* conn */ );
QString escapeString( QString string )
{
return
string.replace( '\'', "''" );
}
private:
static const int POOL_SIZE = 5;
bool m_isTemporary;
QSemaphore m_semaphore;
DbConnection::DbConnectionType m_dbConnType;
DbConfig *m_dbConfig;
};
class CollectionDB : public QObject
{
Q_OBJECT
signals:
public:
CollectionDB( bool temporary = false );
~CollectionDB();
static CollectionDB *instance();
const QString escapeString( const QString &string ) { return m_dbConnPool->escapeString(string); }
const QString boolT() { if (m_dbConnPool->getDbConnectionType() == DbConnection::postgresql) return "'t'"; else return "1"; }
const QString boolF() { if (m_dbConnPool->getDbConnectionType() == DbConnection::postgresql) return "'f'"; else return "0"; }
const QString textColumnType() { if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return "VARCHAR(255)"; }
const QString textColumnType(int length){ if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return QString("VARCHAR(%1)").arg(length); }
// We might consider using LONGTEXT type, as some lyrics could be VERY long..???
const QString longTextColumnType() { if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return "TEXT"; }
const QString randomFunc() { if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "random()"; else return "RAND()"; }
int getType() { return m_dbConnPool->getDbConnectionType(); }
/**
* This method returns a static DbConnection for components that want to use
* the same connection for the whole time. Should not be used anywhere else
* but in CollectionReader.
*
* @return static DbConnection
*/
DbConnection *getStaticDbConnection();
/**
* Returns the DbConnection back to connection pool.
*
* @param conn DbConnection to be returned
*/
void returnStaticDbConnection( DbConnection *conn );
//sql helper methods
QStringList query( const QString& statement, DbConnection *conn = NULL );
int insert( const QString& statement, const QString& table, DbConnection *conn = NULL );
//table management methods
bool isEmpty();
bool isValid(const QString &column, const QString &table);
void createTables( DbConnection *conn = NULL );
void createActivityTable( DbConnection *conn = NULL );
void createMetaDBTable( DbConnection *conn = NULL );
void loadMetaDBTable( DbConnection *conn = NULL );
void dropTables( DbConnection *conn = NULL );
void clearTables( DbConnection *conn = NULL );
void moveTempTables( DbConnection *conn );
QString typeCount( const QString &type_id );
QStringList messagesForType( const QString &type_id, const bool isValue );
QStringList allActivity( );
QStringList allActivityOfType(const QString &type, const QString &days );
void insertEvent(const QString &type, const QString &event,const QString &file = NULL, DbConnection *conn = NULL );
void expireActivity(const QString &days );
void insertMetaDBEntry(const QString &date, const QString &submission, const QString &creator,const QString &virus,const QString &alias, const QString &sender,DbConnection *conn = NULL);
QString latestMetaDBDate( );
protected:
QCString md5sum( const QString& artist, const QString& album, const QString& file = QString::null );
/** Manages regular folder monitoring scan */
public slots:
private slots:
private:
//bump DATABASE_VERSION whenever changes to the table structure are made. will remove old db file.
static const int DATABASE_VERSION = 20;
static const int DATABASE_STATS_VERSION = 3;
static const int MONITOR_INTERVAL = 60; //sec
static const bool DEBUGSQL = false;
void initialize();
void destroy();
//general management methods
uint IDFromValue( QString name, QString value, bool autocreate = true, const bool temporary = false,
const bool updateSpelling = false, DbConnection *conn = NULL );
QString valueFromID( QString table, uint id );
//member variables
DbConnectionPool *m_dbConnPool;
bool m_isTemporary;
bool m_monitor;
};
#endif /* KLAMAV_COLLECTIONDB_H */
|