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 <tqdir.h> //stack allocated
#include <tqimage.h>
#include <tqobject.h> //baseclass
#include <tqptrqueue.h> //baseclass
#include <tqsemaphore.h> //stack allocated
#include <tqstringlist.h> //stack allocated
class DbConnection;
class DbConnectionPool;
class DbConfig
{};
class SqliteConfig : public DbConfig
{
public:
SqliteConfig( const TQString& /* dbfile */ );
const TQString dbFile() const { return m_dbfile; }
private:
TQString m_dbfile;
};
class DbConnection
{
public:
enum DbConnectionType { sqlite = 0, mysql = 1, postgresql = 2 };
DbConnection( DbConfig* /* config */ );
virtual ~DbConnection() = 0;
virtual TQStringList query( const TQString& /* statement */ ) = 0;
virtual int insert( const TQString& /* statement */, const TQString& /* table */ ) = 0;
const bool isInitialized() const { return m_initialized; }
virtual bool isConnected() const = 0;
virtual const TQString 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();
TQStringList query( const TQString& /* statement */ );
int insert( const TQString& /* statement */, const TQString& /* 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 : TQPtrQueue<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 */ );
TQString escapeString( TQString string )
{
return
string.replace( '\'', "''" );
}
private:
static const int POOL_SIZE = 5;
bool m_isTemporary;
TQSemaphore m_semaphore;
DbConnection::DbConnectionType m_dbConnType;
DbConfig *m_dbConfig;
};
class CollectionDB : public TQObject
{
TQ_OBJECT
signals:
public:
CollectionDB( bool temporary = false );
~CollectionDB();
static CollectionDB *instance();
const TQString escapeString( const TQString &string ) { return m_dbConnPool->escapeString(string); }
const TQString boolT() { if (m_dbConnPool->getDbConnectionType() == DbConnection::postgresql) return "'t'"; else return "1"; }
const TQString boolF() { if (m_dbConnPool->getDbConnectionType() == DbConnection::postgresql) return "'f'"; else return "0"; }
const TQString textColumnType() { if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return "VARCHAR(255)"; }
const TQString textColumnType(int length){ if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return TQString("VARCHAR(%1)").arg(length); }
// We might consider using LONGTEXT type, as some lyrics could be VERY long..???
const TQString longTextColumnType() { if ( m_dbConnPool->getDbConnectionType() == DbConnection::postgresql ) return "TEXT"; else return "TEXT"; }
const TQString 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
TQStringList query( const TQString& statement, DbConnection *conn = NULL );
int insert( const TQString& statement, const TQString& table, DbConnection *conn = NULL );
//table management methods
bool isEmpty();
bool isValid(const TQString &column, const TQString &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 );
TQString typeCount( const TQString &type_id );
TQStringList messagesForType( const TQString &type_id, const bool isValue );
TQStringList allActivity( );
TQStringList allActivityOfType(const TQString &type, const TQString &days );
void insertEvent(const TQString &type, const TQString &event,const TQString &file = NULL, DbConnection *conn = NULL );
void expireActivity(const TQString &days );
void insertMetaDBEntry(const TQString &date, const TQString &submission, const TQString &creator,const TQString &virus,const TQString &alias, const TQString &sender,DbConnection *conn = NULL);
TQString latestMetaDBDate( );
protected:
TQCString md5sum( const TQString& artist, const TQString& album, const TQString& file = TQString::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( TQString name, TQString value, bool autocreate = true, const bool temporary = false,
const bool updateSpelling = false, DbConnection *conn = NULL );
TQString valueFromID( TQString table, uint id );
//member variables
DbConnectionPool *m_dbConnPool;
bool m_isTemporary;
bool m_monitor;
};
#endif /* KLAMAV_COLLECTIONDB_H */
|