diff options
Diffstat (limited to 'src/script/lua_api/l_mainmenu.cpp')
0 files changed, 0 insertions, 0 deletions
![]() |
index : minetest.git | |
modified minetest for gpcfs purposes | gpcf |
aboutsummaryrefslogtreecommitdiff |
/*
Copyright (C) 2016 Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#if USE_POSTGRESQL
#include "database-postgresql.h"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
// Without this some of the network functions are not found on mingw
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <windows.h>
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif
#include "log.h"
#include "exceptions.h"
#include "settings.h"
#include "content_sao.h"
#include "remoteplayer.h"
Database_PostgreSQL::Database_PostgreSQL(const std::string &connect_string) :
m_connect_string(connect_string),
m_conn(NULL),
m_pgversion(0)
{
if (m_connect_string.empty()) {
throw SettingNotFoundException(
"Set pgsql_connection string in world.mt to "
"use the postgresql backend\n"
"Notes:\n"
"pgsql_connection has the following form: \n"
"\tpgsql_connection = host=127.0.0.1 port=5432 user=mt_user "
"password=mt_password dbname=minetest_world\n"
"mt_user should have CREATE TABLE, INSERT, SELECT, UPDATE and "
"DELETE rights on the database.\n"
"Don't create mt_user as a SUPERUSER!");
}
}
Database_PostgreSQL::~Database_PostgreSQL()
{
PQfinish(m_conn);
}
void Database_PostgreSQL::connectToDatabase()
{
m_conn = PQconnectdb(m_connect_string.c_str());
if (PQstatus(m_conn) != CONNECTION_OK) {
throw DatabaseException(std::string(
"PostgreSQL database error: ") +
PQerrorMessage(m_conn));
}
m_pgversion = PQserverVersion(m_conn);
/*
* We are using UPSERT feature from PostgreSQL 9.5
* to have the better performance where possible.
*/
if (m_pgversion < 90500) {
warningstream << "Your PostgreSQL server lacks UPSERT "
<< "support. Use version 9.5 or better if possible."
<< std::endl;
}
infostream << "PostgreSQL Database: Version " << m_pgversion
<< " Connection made." << std::endl;
createDatabase();
initStatements();
}
void Database_PostgreSQL::verifyDatabase()
{
if (PQstatus(m_conn) == CONNECTION_OK)
return;
PQreset(m_conn);
ping();
}
void Database_PostgreSQL::ping()
{
if (PQping(m_connect_string.c_str()) != PQPING_OK) {
throw DatabaseException(std::string(
"PostgreSQL database error: ") +
PQerrorMessage(m_conn));
}
}
bool Database_PostgreSQL::initialized() const
{
return (PQstatus(m_conn) == CONNECTION_OK);
}
PGresult *Database_PostgreSQL::checkResults(PGresult *result, bool clear)
{
ExecStatusType statusType = PQresultStatus(result);
switch (statusType) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
break;
case PGRES_FATAL_ERROR:
default:
throw DatabaseException(
std::string("PostgreSQL database error: ") +
PQresultErrorMessage(result));
}
if (clear)
PQclear(result);
return result;
}
void Database_PostgreSQL::createTableIfNotExists(const std::string &table_name,
const std::string &definition)
{
std::string sql_check_table = "SELECT relname FROM pg_class WHERE relname='" +
table_name + "';";
PGresult *result = checkResults(PQexec(m_conn, sql_check_table.c_str()), false);
// If table doesn't exist, create it
if (!PQntuples(result)) {
checkResults(PQexec(m_conn, definition.c_str()));
}
PQclear(result);