2011-02-13 18:51:43 +01:00
|
|
|
/**
|
|
|
|
* libtransport -- C++ library for easy XMPP Transports development
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 02111-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2011-09-07 14:19:10 +02:00
|
|
|
#ifdef WITH_SQLITE
|
|
|
|
|
2011-02-13 18:51:43 +01:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-11-18 14:05:57 +01:00
|
|
|
#include "transport/StorageBackend.h"
|
2011-09-07 14:03:27 +02:00
|
|
|
#include "sqlite3.h"
|
2011-02-13 18:51:43 +01:00
|
|
|
|
|
|
|
namespace Transport {
|
|
|
|
|
2015-11-18 14:05:57 +01:00
|
|
|
class Config;
|
|
|
|
|
2011-02-15 15:48:52 +01:00
|
|
|
/// Used to store transport data into SQLite3 database.
|
2011-02-13 18:51:43 +01:00
|
|
|
class SQLite3Backend : public StorageBackend
|
|
|
|
{
|
|
|
|
public:
|
2011-02-15 15:48:52 +01:00
|
|
|
/// Creates new SQLite3Backend instance.
|
|
|
|
/// \param config cofiguration, this class uses following Config values:
|
|
|
|
/// - database.database - path to SQLite3 database file, database file is created automatically
|
|
|
|
/// - service.prefix - prefix for tables created by createDatabase method
|
2011-02-13 22:47:28 +01:00
|
|
|
SQLite3Backend(Config *config);
|
2011-02-15 15:48:52 +01:00
|
|
|
|
|
|
|
/// Destructor.
|
2011-02-13 18:51:43 +01:00
|
|
|
~SQLite3Backend();
|
|
|
|
|
2011-02-15 15:48:52 +01:00
|
|
|
/// Connects to the database and creates it if it's needed. This method call createDatabase() function
|
|
|
|
/// automatically.
|
|
|
|
/// \return true if database is opened successfully.
|
2011-02-13 18:51:43 +01:00
|
|
|
bool connect();
|
2011-02-15 15:48:52 +01:00
|
|
|
|
|
|
|
/// Creates database structure.
|
|
|
|
/// \see connect()
|
|
|
|
/// \return true if database structure has been created successfully. Note that it returns True also if database structure
|
|
|
|
/// already exists.
|
2011-02-13 18:51:43 +01:00
|
|
|
bool createDatabase();
|
|
|
|
|
2011-02-15 15:48:52 +01:00
|
|
|
/// Stores user into database.
|
|
|
|
/// \param user user struct containing all information about user which have to be stored
|
2011-02-13 18:51:43 +01:00
|
|
|
void setUser(const UserInfo &user);
|
2011-02-15 15:48:52 +01:00
|
|
|
|
|
|
|
/// Gets user data from database and stores them into user reference.
|
|
|
|
/// \param barejid barejid of user
|
|
|
|
/// \param user UserInfo object where user data will be stored
|
|
|
|
/// \return true if user has been found in database
|
2011-02-13 18:51:43 +01:00
|
|
|
bool getUser(const std::string &barejid, UserInfo &user);
|
2011-02-15 15:48:52 +01:00
|
|
|
|
|
|
|
/// Changes users online state variable in database.
|
|
|
|
/// \param id id of user - UserInfo.id
|
|
|
|
/// \param online online state
|
2011-02-13 18:51:43 +01:00
|
|
|
void setUserOnline(long id, bool online);
|
|
|
|
|
2011-12-07 10:30:39 +01:00
|
|
|
bool getOnlineUsers(std::vector<std::string> &users);
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
bool getUsers(std::vector<std::string> &users);
|
|
|
|
|
2011-02-15 15:48:52 +01:00
|
|
|
/// Removes user and all connected data from database.
|
|
|
|
/// \param id id of user - UserInfo.id
|
|
|
|
/// \return true if user has been found in database and removed
|
|
|
|
bool removeUser(long id);
|
|
|
|
|
|
|
|
/// Returns JIDs of all buddies in user's roster.
|
|
|
|
/// \param id id of user - UserInfo.id
|
|
|
|
/// \param roster string list used to store user's roster
|
|
|
|
/// \return true if user has been found in database and roster has been fetched
|
2011-04-10 20:22:21 +02:00
|
|
|
bool getBuddies(long id, std::list<BuddyInfo> &roster);
|
2011-02-13 18:51:43 +01:00
|
|
|
|
2011-04-07 14:21:42 +02:00
|
|
|
long addBuddy(long userId, const BuddyInfo &buddyInfo);
|
|
|
|
|
2011-04-07 14:51:34 +02:00
|
|
|
void updateBuddy(long userId, const BuddyInfo &buddyInfo);
|
2012-07-20 13:46:28 +02:00
|
|
|
void removeBuddy(long id);
|
|
|
|
|
|
|
|
void getBuddySetting(long userId, long buddyId, const std::string &variable, int &type, std::string &value);
|
|
|
|
void updateBuddySetting(long userId, long buddyId, const std::string &variable, int type, const std::string &value);
|
2011-04-07 09:53:33 +02:00
|
|
|
|
2011-05-19 17:53:31 +02:00
|
|
|
void getUserSetting(long userId, const std::string &variable, int &type, std::string &value);
|
2011-05-19 18:58:25 +02:00
|
|
|
void updateUserSetting(long userId, const std::string &variable, const std::string &value);
|
2011-05-19 17:53:31 +02:00
|
|
|
|
2011-04-07 14:51:34 +02:00
|
|
|
void beginTransaction();
|
|
|
|
void commitTransaction();
|
2011-04-07 09:53:33 +02:00
|
|
|
|
2011-02-13 18:51:43 +01:00
|
|
|
private:
|
|
|
|
bool exec(const std::string &query);
|
|
|
|
|
|
|
|
sqlite3 *m_db;
|
2011-02-13 22:47:28 +01:00
|
|
|
Config *m_config;
|
2011-02-13 18:51:43 +01:00
|
|
|
std::string m_prefix;
|
2011-03-12 00:55:40 +01:00
|
|
|
|
|
|
|
// statements
|
|
|
|
sqlite3_stmt *m_setUser;
|
|
|
|
sqlite3_stmt *m_getUser;
|
2011-05-19 17:53:31 +02:00
|
|
|
sqlite3_stmt *m_getUserSetting;
|
|
|
|
sqlite3_stmt *m_setUserSetting;
|
2011-05-19 18:58:25 +02:00
|
|
|
sqlite3_stmt *m_updateUserSetting;
|
2011-03-15 22:22:58 +01:00
|
|
|
sqlite3_stmt *m_removeUser;
|
|
|
|
sqlite3_stmt *m_removeUserBuddies;
|
|
|
|
sqlite3_stmt *m_removeUserSettings;
|
|
|
|
sqlite3_stmt *m_removeUserBuddiesSettings;
|
2012-07-20 13:46:28 +02:00
|
|
|
sqlite3_stmt *m_removeBuddy;
|
|
|
|
sqlite3_stmt *m_removeBuddySettings;
|
2011-04-07 14:21:42 +02:00
|
|
|
sqlite3_stmt *m_addBuddy;
|
2011-04-07 14:51:34 +02:00
|
|
|
sqlite3_stmt *m_updateBuddy;
|
2011-06-05 11:35:37 +02:00
|
|
|
sqlite3_stmt *m_updateBuddySetting;
|
2012-07-20 13:46:28 +02:00
|
|
|
sqlite3_stmt *m_getBuddySetting;
|
2011-04-10 20:22:21 +02:00
|
|
|
sqlite3_stmt *m_getBuddies;
|
2011-04-11 13:41:19 +02:00
|
|
|
sqlite3_stmt *m_getBuddiesSettings;
|
2011-09-11 12:09:07 +02:00
|
|
|
sqlite3_stmt *m_setUserOnline;
|
2011-12-07 10:30:39 +01:00
|
|
|
sqlite3_stmt *m_getOnlineUsers;
|
2015-11-24 12:26:26 +01:00
|
|
|
sqlite3_stmt *m_getUsers;
|
2011-02-13 18:51:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2011-09-07 14:19:10 +02:00
|
|
|
|
|
|
|
#endif
|