Files
@ 271a7134db1a
Branch filter:
Location: libtransport.git/src/sqlite3backend.cpp
271a7134db1a
3.9 KiB
text/x-c++hdr
Docs
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 | /**
* 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
*/
#include "transport/sqlite3backend.h"
#include <boost/bind.hpp>
#define SQLITE_DB_VERSION 3
using namespace boost;
namespace Transport {
SQLite3Backend::SQLite3Backend(Config *config) {
m_config = config;
m_db = NULL;
m_prefix = CONFIG_STRING(m_config, "database.prefix");
}
SQLite3Backend::~SQLite3Backend(){
if (m_db) {
sqlite3_close(m_db);
}
}
bool SQLite3Backend::connect() {
if (sqlite3_open(CONFIG_STRING(m_config, "database.database").c_str(), &m_db)) {
sqlite3_close(m_db);
return false;
}
return createDatabase();
}
bool SQLite3Backend::createDatabase() {
int not_exist = exec("CREATE TABLE " + m_prefix + "buddies ("
" id INTEGER PRIMARY KEY NOT NULL,"
" user_id int(10) NOT NULL,"
" uin varchar(255) NOT NULL,"
" subscription varchar(20) NOT NULL,"
" nickname varchar(255) NOT NULL,"
" groups varchar(255) NOT NULL,"
" flags int(4) NOT NULL DEFAULT '0'"
");");
if (not_exist) {
exec("CREATE UNIQUE INDEX IF NOT EXISTS user_id ON " + m_prefix + "buddies (user_id, uin);");
exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "buddies_settings ("
" user_id int(10) NOT NULL,"
" buddy_id int(10) NOT NULL,"
" var varchar(50) NOT NULL,"
" type int(4) NOT NULL,"
" value varchar(255) NOT NULL,"
" PRIMARY KEY (buddy_id, var)"
");");
exec("CREATE INDEX IF NOT EXISTS user_id02 ON " + m_prefix + "buddies_settings (user_id);");
exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "users ("
" id INTEGER PRIMARY KEY NOT NULL,"
" jid varchar(255) NOT NULL,"
" uin varchar(4095) NOT NULL,"
" password varchar(255) NOT NULL,"
" language varchar(25) NOT NULL,"
" encoding varchar(50) NOT NULL DEFAULT 'utf8',"
" last_login datetime,"
" vip int(1) NOT NULL DEFAULT '0',"
" online int(1) NOT NULL DEFAULT '0'"
");");
exec("CREATE UNIQUE INDEX IF NOT EXISTS jid ON " + m_prefix + "users (jid);");
exec("CREATE TABLE " + m_prefix + "users_settings ("
" user_id int(10) NOT NULL,"
" var varchar(50) NOT NULL,"
" type int(4) NOT NULL,"
" value varchar(255) NOT NULL,"
" PRIMARY KEY (user_id, var)"
");");
exec("CREATE INDEX IF NOT EXISTS user_id03 ON " + m_prefix + "users_settings (user_id);");
exec("CREATE TABLE IF NOT EXISTS " + m_prefix + "db_version ("
" ver INTEGER NOT NULL DEFAULT '3'"
");");
exec("REPLACE INTO " + m_prefix + "db_version (ver) values(3)");
}
return true;
}
bool SQLite3Backend::exec(const std::string &query) {
char *errMsg = 0;
int rc = sqlite3_exec(m_db, query.c_str(), NULL, 0, &errMsg);
if (rc != SQLITE_OK) {
onStorageError(query, errMsg);
sqlite3_free(errMsg);
return false;
}
return true;
}
void SQLite3Backend::setUser(const UserInfo &user) {
}
bool SQLite3Backend::getUser(const std::string &barejid, UserInfo &user) {
return true;
}
void SQLite3Backend::setUserOnline(long id, bool online) {
}
bool SQLite3Backend::getBuddies(long id, std::list<std::string> &roster) {
return true;
}
void SQLite3Backend::removeUser(long id) {
}
}
|