Commit e7adf910 authored by jan.koester's avatar jan.koester
Browse files

db abstraction layer now extern

parent d8b18c82
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -9,24 +9,22 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
find_package(libnetplus REQUIRED)
find_package(libhtmlpp REQUIRED)
find_package(libhttppp REQUIRED)
find_package(PostgreSQL REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(OpenLDAP)
find_package(Hiredis REQUIRED)
find_package(libsecureid REQUIRED)
find_package(Brotli REQUIRED)
find_package(libcmdplus REQUIRED)
find_package(libconfplus REQUIRED)
find_package(libdbpp REQUIRED)

include_directories(
    ${NETPLUS_INCLUDE_DIRS}
    ${HTTPPP_INCLUDE_DIRS}
    ${CRYPTPLUS_INCLUDE_DIRS}
    ${PostgreSQL_INCLUDE_DIRS}
    ${SQLite3_INCLUDE_DIRS}
    ${OPENLDAP_INCLUDE_DIRS}
    ${CMAKE_SOURCE_DIR}/src
    ${SECUREID_INCLUDE_DIRS}
    ${DBPP_INCLUDE_DIR}
)

if(${OPENLDAP_FOUND})
+2 −1
Original line number Diff line number Diff line
@@ -36,7 +36,8 @@
#include <httppp/http.h>
#include <httppp/exception.h>

#include <database.h>
#include <dbpp/database.h>

#include <auth.h>
#include <conf.h>
#include <plugin.h>
+60 −59
Original line number Diff line number Diff line
@@ -34,7 +34,8 @@
#include <httppp/http.h>
#include <httppp/exception.h>

#include <database.h>
#include <dbpp/database.h>

#include <auth.h>
#include <conf.h>
#include <plugin.h>
@@ -58,15 +59,15 @@ namespace blogi {
            condat << "<div><div id=\"contentidx\">";

            char url[512];
            blogi::SQL sql;
            blogi::DBResult res;
            dbpp::SQL sql;
            dbpp::DBResult res;
            std::vector<char> sbuf;

            int ncount=0;

            if (tag) {
                sql << "SELECT id FROM tags where name='" << SQL::escaped(sbuf,tag) << "' LIMIT 1";
                if(Args->database[tid]->exec(&sql,res)<1){
                sql << "SELECT id FROM tags where name='" << dbpp::SQL::escaped(sbuf,tag) << "' LIMIT 1";
                if(Args->database[tid]->exec(sql,res)<1){
                    excep[libhttppp::HTTPException::Critical] << "no tag data found for this name!";
                    throw excep;
                }else {
@@ -74,12 +75,12 @@ namespace blogi {
                    sql <<"LEFT JOIN users ON content.author=users.id LEFT JOIN tags_content ON tags_content.content_id=content.id where tags_content.tag_id='"
                    << res[0][0]
                    <<"' ORDER BY content.id DESC LIMIT '" << end << "' OFFSET " << start;
                    ncount=Args->database[tid]->exec(&sql,res);
                    ncount=Args->database[tid]->exec(sql,res);
                }
            } else {
                sql="SELECT content.id,content.title,content.descrition,users.displayname,content.created FROM content LEFT JOIN users ON content.author=users.id ORDER BY content.id DESC";
                sql << " LIMIT '" << end << "' OFFSET " << start;
                ncount=Args->database[tid]->exec(&sql,res);
                ncount=Args->database[tid]->exec(sql,res);
            }

            std::string meta;
@@ -133,7 +134,7 @@ namespace blogi {

            sql="SELECT name,id FROM tags";

            int tcount=Args->database[tid]->exec(&sql,res);
            int tcount=Args->database[tid]->exec(sql,res);


            if (tcount>0) {
@@ -142,9 +143,9 @@ namespace blogi {
                for (int i = 0; i < tcount; i++) {
                    sql="select tag_id FROM tags_content where tag_id = '";
                    sql << res[i][1] << "'";
                    blogi::DBResult rescnt;
                    dbpp::DBResult rescnt;
                    condat << "<li><a href=\""
                    << Args->config->buildurl("content/tag/",url,512) << res[i][0] << "\">" << res[i][0] << "(" << Args->database[tid]->exec(&sql,rescnt) << ")" << "</a></li>";
                    << Args->config->buildurl("content/tag/",url,512) << res[i][0] << "\">" << res[i][0] << "(" << Args->database[tid]->exec(sql,rescnt) << ")" << "</a></li>";

                }

@@ -174,14 +175,14 @@ namespace blogi {

            sscanf(curreq->getRequestURL(), Args->config->buildurl("content/read/%d",url,512),&cid);

            blogi::SQL sql;
            dbpp::SQL sql;

            sql << "select content.id,content.title,content.text,users.displayname,content.created from content LEFT JOIN users ON content.author=users.id WHERE content.id='" << cid <<"' LIMIT 1";

            blogi::DBResult res;
            dbpp::DBResult res;


            if(Args->database[tid]->exec(&sql,res)<1){
            if(Args->database[tid]->exec(sql,res)<1){
                excep[libhttppp::HTTPException::Critical] << "No entry found for content id: " << cid;
                throw excep;
            }
@@ -261,8 +262,8 @@ namespace blogi {

                if(!text.empty() && !title.empty() && !descrition.empty()){

                    blogi::SQL asql,sql;
                    blogi::DBResult ares,res;
                    dbpp::SQL asql,sql;
                    dbpp::DBResult ares,res;

                    std::string uid;
                    Session sess(Args->database[tid]);
@@ -270,7 +271,7 @@ namespace blogi {

                    asql << "SELECT id from users WHERE sid='"<< uid.c_str() <<"' LIMIT 1;";

                    if (Args->database[tid]->exec(&asql,ares) < 1) {
                    if (Args->database[tid]->exec(asql,ares) < 1) {
                        excep[libhttppp::HTTPException::Error] << "User with SID not found!";
                        throw excep;
                    }
@@ -285,12 +286,12 @@ namespace blogi {
                    asctime_r(&time,ttmp);

                    sql << "INSERT INTO content (title,descrition,text,author,created) VALUES ('" <<
                    SQL::escaped(sbuf,title.c_str()) << "','" <<
                    SQL::escaped(sbuf,descrition.c_str()) <<"','" <<
                    SQL::escaped(sbuf,text.c_str()) <<"','"<< author <<"','" << ttmp << "') RETURNING id;";
                    dbpp::SQL::escaped(sbuf,title.c_str()) << "','" <<
                    dbpp::SQL::escaped(sbuf,descrition.c_str()) <<"','" <<
                    dbpp::SQL::escaped(sbuf,text.c_str()) <<"','"<< author <<"','" << ttmp << "') RETURNING id;";

                    try {
                        Args->database[tid]->exec(&sql,res);
                        Args->database[tid]->exec(sql,res);
                        content_id=atoi(res[0][0]);
                    }catch(libhttppp::HTTPException &e){
                        throw e;
@@ -306,26 +307,26 @@ namespace blogi {
                                tgsstart = i+1;
                                int tries = 0;
                                TAGNAMECHECK:
                                blogi::SQL sql;
                                blogi::DBResult res;
                                dbpp::SQL sql;
                                dbpp::DBResult res;
                                if (tries>5) {
                                    excep[libhttppp::HTTPException::Critical] << "tag doesn't exists and could not created!";
                                    throw excep;
                                }

                                sql = "select id,name from tags where name='";
                                sql << SQL::escaped(sbuf,tag.c_str()) << "' LIMIT 1;";
                                sql << dbpp::SQL::escaped(sbuf,tag.c_str()) << "' LIMIT 1;";

                                if (Args->database[tid]->exec(&sql,res) != 1) {
                                if (Args->database[tid]->exec(sql,res) != 1) {
                                    sql = "insert into tags (name) VALUES ('";
                                    sql << SQL::escaped(sbuf,tag.c_str()) <<"');";
                                    Args->database[tid]->exec(&sql,res);
                                    sql << dbpp::SQL::escaped(sbuf,tag.c_str()) <<"');";
                                    Args->database[tid]->exec(sql,res);
                                    ++tries;
                                    goto TAGNAMECHECK;
                                }

                                sql =  "insert into tags_content (content_id,tag_id) VALUES ('"; sql << content_id <<"','" << res[0][0] <<"');";
                                Args->database[tid]->exec(&sql,res);
                                Args->database[tid]->exec(sql,res);
                            }
                        }
                    }
@@ -379,8 +380,8 @@ namespace blogi {

            libhttppp::HTTPException excep;

            blogi::DBResult res;
            blogi::SQL      sql;
            dbpp::DBResult res;
            dbpp::SQL      sql;

            int cid = -1;

@@ -389,7 +390,7 @@ namespace blogi {
            sql ="DELETE FROM tags_content WHERE content_id='";
            sql << cid << "';" <<" DELETE FROM content WHERE id='"; sql << cid << "';";

            if(Args->database[tid]->exec(&sql,res)<0){
            if(Args->database[tid]->exec(sql,res)<0){
                libhttppp::HTTPException exp;
                exp[libhttppp::HTTPException::Error] << "Can'T content sql data!";
                throw exp;
@@ -422,12 +423,12 @@ namespace blogi {

        void cleartags(const int tid) {

            blogi::SQL sql;
            blogi::DBResult res;
            dbpp::SQL sql;
            dbpp::DBResult res;

            sql="SELECT name,id FROM tags;";

            int n=Args->database[tid]->exec(&sql,res);
            int n=Args->database[tid]->exec(sql,res);

            libhttppp::HTTPException excep;

@@ -436,9 +437,9 @@ namespace blogi {

                sql="SELECT COUNT(*) FROM tags_content WHERE tag_id='"; sql << res[i][1] << "';";

                blogi::DBResult res2;
                dbpp::DBResult res2;

                if(Args->database[tid]->exec(&sql,res2)<0){
                if(Args->database[tid]->exec(sql,res2)<0){
                    libhttppp::HTTPException excep;
                    excep[libhttppp::HTTPException::Critical] << "clear tags count failed this shouldn't happend!";
                    throw excep;
@@ -449,7 +450,7 @@ namespace blogi {

                sql = "DELETE FROM tags WHERE id='"; sql << res[i][1] << "';";

                if(Args->database[tid]->exec(&sql,res2)<0){
                if(Args->database[tid]->exec(sql,res2)<0){
                    libhttppp::HTTPException excep;
                    excep[libhttppp::HTTPException::Critical] << "clear tags deletd failed this shouldn't happend!";
                    throw excep;
@@ -473,8 +474,8 @@ namespace blogi {

            sscanf(curreq->getRequestURL(), Args->config->buildurl("content/edit/%d",url,512), &cid);

            blogi::SQL sql;
            blogi::DBResult res;
            dbpp::SQL sql;
            dbpp::DBResult res;
            std::vector<char> sbuf;

            libhttppp::HttpForm curform;
@@ -506,15 +507,15 @@ namespace blogi {
                }

                if (!text.empty() && !title.empty() && !descrition.empty()) {
                    blogi::SQL sqltext;
                    dbpp::SQL sqltext;
                    sqltext << "UPDATE content SET title='" <<
                    SQL::escaped(sbuf,title.c_str()) << "',descrition='" <<
                    SQL::escaped(sbuf,descrition.c_str()) << "',text='" <<
                    SQL::escaped(sbuf,text.c_str())  << "' where id='" << cid << "';";
                    dbpp::SQL::escaped(sbuf,title.c_str()) << "',descrition='" <<
                    dbpp::SQL::escaped(sbuf,descrition.c_str()) << "',text='" <<
                    dbpp::SQL::escaped(sbuf,text.c_str())  << "' where id='" << cid << "';";

                    blogi::DBResult textres;
                    dbpp::DBResult textres;

                    if (Args->database[tid]->exec(&sqltext, textres)<0) {
                    if (Args->database[tid]->exec(sqltext, textres)<0) {
                        excep[libhttppp::HTTPException::Critical] << "can't update updare content with id: " << cid << " !";
                        throw excep;
                    }
@@ -522,7 +523,7 @@ namespace blogi {

                sql <<  "DELETE FROM tags_content WHERE content_id='" << cid <<"';";

                if (Args->database[tid]->exec(&sql,res) < 0) {
                if (Args->database[tid]->exec(sql,res) < 0) {
                    excep[libhttppp::HTTPException::Critical] << "can't delete old tags id from tags_id!";
                    throw excep;
                }
@@ -544,26 +545,26 @@ namespace blogi {
                                    throw excep;
                                }
                                sql="select id,name from tags where name='";
                                sql << SQL::escaped(sbuf,tag.c_str()) << "' LIMIT 1;";
                                sql << dbpp::SQL::escaped(sbuf,tag.c_str()) << "' LIMIT 1;";

                                int tamount;

                                if ((tamount=Args->database[tid]->exec(&sql,res)) < 0) {
                                if ((tamount=Args->database[tid]->exec(sql,res)) < 0) {
                                    excep[libhttppp::HTTPException::Critical] << "can't find existing tags !";
                                    throw excep;
                                }

                                if (tamount != 1) {
                                    sql="insert into tags (name) VALUES ('";
                                    sql << SQL::escaped(sbuf,tag.c_str()) << "');";
                                    Args->database[tid]->exec(&sql,res);
                                    sql << dbpp::SQL::escaped(sbuf,tag.c_str()) << "');";
                                    Args->database[tid]->exec(sql,res);
                                    ++tries;
                                    goto TAGNAMECHECK;
                                }

                                sql= "insert into tags_content (content_id,tag_id) VALUES ('"; sql << cid << "','" << res[0][0] << "');";

                                if (Args->database[tid]->exec(&sql,res) < 0) {
                                if (Args->database[tid]->exec(sql,res) < 0) {
                                    excep[libhttppp::HTTPException::Critical] << "can't create link between tag and content !";
                                    throw excep;
                                }
@@ -582,7 +583,7 @@ namespace blogi {

            int ccamount;

            if ((ccamount=Args->database[tid]->exec(&sql,res)) < 0) {
            if ((ccamount=Args->database[tid]->exec(sql,res)) < 0) {
                excep[libhttppp::HTTPException::Error] << "can't find tages for this content id !";
                throw excep;
            }
@@ -638,10 +639,10 @@ namespace blogi {
        }

        void initPlugin(){
            blogi::SQL sql;
            blogi::DBResult res;
            dbpp::SQL sql;
            dbpp::DBResult res;
            sql << "CREATE TABLE IF NOT EXISTS content ("
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement(sql) << ","
                <<     "title character varying(255) NOT NULL,"
                <<     "text text NOT NULL,"
                <<     "descrition character varying(255) NOT NULL,"
@@ -650,7 +651,7 @@ namespace blogi {
                <<     "FOREIGN KEY (author) REFERENCES users(id)"
                <<   "); "
                << "CREATE TABLE IF NOT EXISTS tags ("
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement(sql) << ","
                <<      "name character varying(255) NOT NULL UNIQUE"
                << "); "
                << "CREATE TABLE IF NOT EXISTS tags_content ("
@@ -659,7 +660,7 @@ namespace blogi {
                <<   "FOREIGN KEY (content_id) REFERENCES content (id),"
                <<   "FOREIGN KEY (tag_id) REFERENCES tags (id)"
                << ");";
            Args->database[0]->exec(&sql,res);
            Args->database[0]->exec(sql,res);
            return;
        }

@@ -713,12 +714,12 @@ namespace blogi {

        void Search(const int tid, const char* word, blogi::SearchRet& result, const char* sessionid){
            std::vector<char> buf;
            blogi::SQL sql;
            blogi::DBResult res;
            dbpp::SQL sql;
            dbpp::DBResult res;

            sql << "SELECT title,descrition,id FROM content WHERE title @@ to_tsquery('" << sql.escaped(buf,word) <<"') OR descrition @@ to_tsquery('" << sql.escaped(buf,word) <<"')  OR text @@ to_tsquery('" << sql.escaped(buf,word) <<"');";

            int count=Args->database[tid]->exec(&sql,res);
            int count=Args->database[tid]->exec(sql,res);

            for (int i=0; i < count; ++i) {
                SearchRet::Data data;
+8 −7
Original line number Diff line number Diff line
@@ -34,8 +34,9 @@
#include <gameinfoplus/hldsview.h>
#include <gameinfoplus/exception.h>

#include <dbpp/database.h>

#include <plugin.h>
#include <database.h>
#include <theme.h>
#include <conf.h>
#include <mutex>
@@ -62,27 +63,27 @@ namespace blogi {
        }

        void initPlugin(){
            blogi::SQL        sql;
            blogi::DBResult   res;
            dbpp::SQL        sql;
            dbpp::DBResult   res;


            sql << "CREATE TABLE IF NOT EXISTS gameserver_protocols("
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement(sql) << ","
                <<   "pname character varying(255) NOT NULL"
                << "); "
                << "CREATE TABLE IF NOT EXISTS gameserver("
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement() << ","
                <<   "id integer PRIMARY KEY " << Args->database[0]->autoincrement(sql) << ","
                <<   "protocol integer,"
                <<   "addr character varying(255) NOT NULL,"
                <<   "port integer,"
                <<   "FOREIGN KEY (protocol) REFERENCES gameserver_protocols (id)"
                << ");";

            Args->database[0]->exec(&sql,res);
            Args->database[0]->exec(sql,res);

            sql="select protocol,addr,port from gameserver";

            int count = Args->database[0]->exec(&sql,res);
            int count = Args->database[0]->exec(sql,res);

            for (int i = 0; i < count; i++) {
                if(atoi(res[i][0])!=0)
+44 −44

File changed.

Preview size limit exceeded, changes collapsed.

Loading