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

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

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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