Skip to content

Commit

Permalink
WIP: player data to Database
Browse files Browse the repository at this point in the history
Add player data into databases (SQLite3 & PG only)

This is a WIP

ATM data is saved in Sqlite3 db and in file together, no reading is done
No pg write atm
  • Loading branch information
nerzhul committed Mar 28, 2017
1 parent 4b05fea commit 1f402ab
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/client.cpp
Expand Up @@ -913,7 +913,7 @@ void Client::initLocalMapSaving(const Address &address,

fs::CreateAllDirs(world_path);

m_localdb = new Database_SQLite3(world_path);
m_localdb = new Database_SQLite3(world_path, DATABASE_TYPE_MAP);
m_localdb->beginSave();
actionstream << "Local map saving started, map will be saved at '" << world_path << "'" << std::endl;
}
Expand Down
3 changes: 2 additions & 1 deletion src/database-dummy.h
Expand Up @@ -25,14 +25,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "database.h"
#include "irrlichttypes.h"

class Database_Dummy : public Database
class Database_Dummy : public Database, public PlayerDatabase
{
public:
bool saveBlock(const v3s16 &pos, const std::string &data);
void loadBlock(const v3s16 &pos, std::string *block);
bool deleteBlock(const v3s16 &pos);
void listAllLoadableBlocks(std::vector<v3s16> &dst);

bool savePlayer(RemotePlayer *player) { return true; }
private:
std::map<s64, std::string> m_database;
};
Expand Down
107 changes: 61 additions & 46 deletions src/database-postgresql.cpp
Expand Up @@ -40,12 +40,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "exceptions.h"
#include "settings.h"

Database_PostgreSQL::Database_PostgreSQL(const Settings &conf) :
m_connect_string(""),
Database_PostgreSQL::Database_PostgreSQL(const std::string &connect_string, DatabaseType type) :
m_connect_string(connect_string),
m_db_type(type),
m_conn(NULL),
m_pgversion(0)
{
if (!conf.getNoEx("pgsql_connection", m_connect_string)) {
if (m_connect_string.empty()) {
throw SettingNotFoundException(
"Set pgsql_connection string in world.mt to "
"use the postgresql backend\n"
Expand Down Expand Up @@ -120,36 +121,41 @@ bool Database_PostgreSQL::initialized() const

void Database_PostgreSQL::initStatements()
{
prepareStatement("read_block",
if (m_db_type == DATABASE_TYPE_MAP) {
prepareStatement("read_block",
"SELECT data FROM blocks "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4");

if (m_pgversion < 90500) {
prepareStatement("write_block_insert",
"INSERT INTO blocks (posX, posY, posZ, data) SELECT "
"$1::int4, $2::int4, $3::int4, $4::bytea "
"WHERE NOT EXISTS (SELECT true FROM blocks "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4)");

prepareStatement("write_block_update",
"UPDATE blocks SET data = $4::bytea "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4");
} else {
prepareStatement("write_block",
"INSERT INTO blocks (posX, posY, posZ, data) VALUES "
"($1::int4, $2::int4, $3::int4, $4::bytea) "
"ON CONFLICT ON CONSTRAINT blocks_pkey DO "
"UPDATE SET data = $4::bytea");
}

prepareStatement("delete_block", "DELETE FROM blocks WHERE "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4");

if (m_pgversion < 90500) {
prepareStatement("write_block_insert",
"INSERT INTO blocks (posX, posY, posZ, data) SELECT "
"$1::int4, $2::int4, $3::int4, $4::bytea "
"WHERE NOT EXISTS (SELECT true FROM blocks "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4)");

prepareStatement("write_block_update",
"UPDATE blocks SET data = $4::bytea "
"WHERE posX = $1::int4 AND posY = $2::int4 AND "
"posZ = $3::int4");
} else {
prepareStatement("write_block",
"INSERT INTO blocks (posX, posY, posZ, data) VALUES "
"($1::int4, $2::int4, $3::int4, $4::bytea) "
"ON CONFLICT ON CONSTRAINT blocks_pkey DO "
"UPDATE SET data = $4::bytea");
}

prepareStatement("delete_block", "DELETE FROM blocks WHERE "
"posX = $1::int4 AND posY = $2::int4 AND posZ = $3::int4");

prepareStatement("list_all_loadable_blocks",
prepareStatement("list_all_loadable_blocks",
"SELECT posX, posY, posZ FROM blocks");
}
else if (m_db_type == DATABASE_TYPE_PLAYERS) {
// @TODO
}
}

PGresult *Database_PostgreSQL::checkResults(PGresult *result, bool clear)
Expand All @@ -175,25 +181,30 @@ PGresult *Database_PostgreSQL::checkResults(PGresult *result, bool clear)

void Database_PostgreSQL::createDatabase()
{
PGresult *result = checkResults(PQexec(m_conn,
"SELECT relname FROM pg_class WHERE relname='blocks';"),
false);

// If table doesn't exist, create it
if (!PQntuples(result)) {
static const char* dbcreate_sql = "CREATE TABLE blocks ("
"posX INT NOT NULL,"
"posY INT NOT NULL,"
"posZ INT NOT NULL,"
"data BYTEA,"
"PRIMARY KEY (posX,posY,posZ)"
");";
checkResults(PQexec(m_conn, dbcreate_sql));
}
if (m_db_type == DATABASE_TYPE_MAP) {
PGresult *result = checkResults(PQexec(m_conn,
"SELECT relname FROM pg_class WHERE relname='blocks';"),
false);

// If table doesn't exist, create it
if (!PQntuples(result)) {
static const char *dbcreate_sql = "CREATE TABLE blocks ("
"posX INT NOT NULL,"
"posY INT NOT NULL,"
"posZ INT NOT NULL,"
"data BYTEA,"
"PRIMARY KEY (posX,posY,posZ)"
");";
checkResults(PQexec(m_conn, dbcreate_sql));
}

PQclear(result);
PQclear(result);

infostream << "PostgreSQL: Game Database was inited." << std::endl;
infostream << "PostgreSQL: Game Database was inited." << std::endl;
}
else if (m_db_type == DATABASE_TYPE_PLAYERS) {
// @TODO
}
}


Expand Down Expand Up @@ -302,4 +313,8 @@ void Database_PostgreSQL::listAllLoadableBlocks(std::vector<v3s16> &dst)
PQclear(results);
}

bool Database_PostgreSQL::savePlayer(RemotePlayer *player)
{
return false;
}
#endif // USE_POSTGRESQL
6 changes: 4 additions & 2 deletions src/database-postgresql.h
Expand Up @@ -27,10 +27,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,

class Settings;

class Database_PostgreSQL : public Database
class Database_PostgreSQL: public Database, public PlayerDatabase
{
public:
Database_PostgreSQL(const Settings &conf);
Database_PostgreSQL(const std::string &connect_string, DatabaseType type);
~Database_PostgreSQL();

void beginSave();
Expand All @@ -42,6 +42,7 @@ class Database_PostgreSQL : public Database
void listAllLoadableBlocks(std::vector<v3s16> &dst);
bool initialized() const;

bool savePlayer(RemotePlayer *player);
private:
// Database initialization
void connectToDatabase();
Expand Down Expand Up @@ -87,6 +88,7 @@ class Database_PostgreSQL : public Database

// Attributes
std::string m_connect_string;
DatabaseType m_db_type;
PGconn *m_conn;
int m_pgversion;
};
Expand Down

0 comments on commit 1f402ab

Please sign in to comment.