root/trunk/src/shared/Database/Database.cpp @ 13

Revision 2, 4.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include "DatabaseEnv.h"
20#include "Config/ConfigEnv.h"
21
22#include <ctime>
23#include <iostream>
24#include <fstream>
25
26Database::~Database()
27{
28    /*Delete objects*/
29}
30
31bool Database::Initialize(const char *)
32{
33    // Enable logging of SQL commands (usally only GM commands)
34    // (See method: PExecuteLog)
35    m_logSQL = sConfig.GetBoolDefault("LogSQL", false);
36    m_logsDir = sConfig.GetStringDefault("LogsDir","");
37    if(!m_logsDir.empty())
38    {
39        if((m_logsDir.at(m_logsDir.length()-1)!='/') && (m_logsDir.at(m_logsDir.length()-1)!='\\'))
40            m_logsDir.append("/");
41    }
42
43    return true;
44}
45
46void Database::ThreadStart()
47{
48}
49
50void Database::ThreadEnd()
51{
52}
53
54void Database::escape_string(std::string& str)
55{
56    if(str.empty())
57        return;
58
59    char* buf = new char[str.size()*2+1];
60    escape_string(buf,str.c_str(),str.size());
61    str = buf;
62    delete[] buf;
63}
64
65bool Database::PExecuteLog(const char * format,...)
66{
67    if (!format)
68        return false;
69
70    va_list ap;
71    char szQuery [MAX_QUERY_LEN];
72    va_start(ap, format);
73    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
74    va_end(ap);
75
76    if(res==-1)
77    {
78        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
79        return false;
80    }
81
82    if( m_logSQL )
83    {
84        time_t curr;
85        tm local;
86        time(&curr);                                        // get current time_t value
87        local=*(localtime(&curr));                          // dereference and assign
88        char fName[128];
89        sprintf( fName, "%04d-%02d-%02d_logSQL.sql", local.tm_year+1900, local.tm_mon+1, local.tm_mday );
90
91        FILE* log_file;
92        std::string logsDir_fname = m_logsDir+fName;
93        log_file = fopen(logsDir_fname.c_str(), "a");
94        if (log_file)
95        {
96            fprintf(log_file, "%s;\n", szQuery);
97            fclose(log_file);
98        }
99        else
100        {
101            // The file could not be opened
102            sLog.outError("SQL-Logging is disabled - Log file for the SQL commands could not be openend: %s",fName);
103        }
104    }
105
106    return Execute(szQuery);
107}
108
109void Database::SetResultQueue(SqlResultQueue * queue)
110{
111    m_queryQueues[ZThread::ThreadImpl::current()] = queue;
112}
113
114QueryResult* Database::PQuery(const char *format,...)
115{
116    if(!format) return NULL;
117
118    va_list ap;
119    char szQuery [MAX_QUERY_LEN];
120    va_start(ap, format);
121    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
122    va_end(ap);
123
124    if(res==-1)
125    {
126        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
127        return false;
128    }
129
130    return Query(szQuery);
131}
132
133bool Database::PExecute(const char * format,...)
134{
135    if (!format)
136        return false;
137
138    va_list ap;
139    char szQuery [MAX_QUERY_LEN];
140    va_start(ap, format);
141    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
142    va_end(ap);
143
144    if(res==-1)
145    {
146        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
147        return false;
148    }
149
150    return Execute(szQuery);
151}
152
153bool Database::DirectPExecute(const char * format,...)
154{
155    if (!format)
156        return false;
157
158    va_list ap;
159    char szQuery [MAX_QUERY_LEN];
160    va_start(ap, format);
161    int res = vsnprintf( szQuery, MAX_QUERY_LEN, format, ap );
162    va_end(ap);
163
164    if(res==-1)
165    {
166        sLog.outError("SQL Query truncated (and not execute) for format: %s",format);
167        return false;
168    }
169
170    return DirectExecute(szQuery);
171}
Note: See TracBrowser for help on using the browser.