Public feed #602889

Untitled

public text v1 · immutable
Raw Edit
·published 2007-07-04 11:23 UTC
rendered paste body
/* 
 * Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __SQLSTATEMENTS_H
#define __SQLSTATEMENTS_H

#include <queue>
using namespace std;

// ---- BASE ---

class Database;

class SqlOperation
{
    public:
        SqlOperation() {}
        virtual void Execute(Database *db) = 0;
};

// ---- ASYNC STATEMENTS / TRANSACTIONS ----

class SqlStatement : public SqlOperation
{
    private:
        const char *m_sql;
    public:
        SqlStatement(const char *sql) : m_sql(strdup(sql)), SqlOperation() {}
        ~SqlStatement() { delete[] m_sql; }
        void Execute(Database *db);
};

class SqlTransaction : public SqlOperation
{
    private:
        queue<const char *> m_queue;
    public:
        SqlTransaction() : SqlOperation() {}
        void DelayExecute(const char *sql) { m_queue.push(strdup(sql)); }
        void Execute(Database *db);
};

// ---- ASYNC QUERIES ----

class SqlQueryQueue;
class SqlQuery;
class QueryResult;

typedef void(* pQueryCallback)(SqlQuery *);

class SqlQueryQueue
{
    private:
        ZThread::LockedQueue<SqlQuery*, ZThread::FastMutex> m_queue;
    public:
        SqlQueryQueue() {}
        void push(SqlQuery * query) { m_queue.add(query); }
        SqlQuery* pop() { return m_queue.next(); }
        void Update();
};

class SqlQuery : public SqlOperation
{
    private:
        QueryResult *m_result;
        pQueryCallback m_callback;
        SqlQueryQueue *m_queue;
        const char *m_sql;
    public:
        SqlQuery(const char *sql, pQueryCallback callback, SqlQueryQueue *queue) 
            : m_sql(strdup(sql)), m_callback(callback), m_queue(queue), m_result(NULL), SqlOperation() {}
        void Execute(Database *db);
        void QueueCallback() { if(m_queue) m_queue->push(this); else Callback(); }
        void Callback() { m_callback(this); }
};

#endif