2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
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.
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.
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
19 #include "DatabaseEnv.h"
20 #include "Config/ConfigEnv.h"
31 bool Database::Initialize(const char *)
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())
39 if((m_logsDir
.at(m_logsDir
.length()-1)!='/') && (m_logsDir
.at(m_logsDir
.length()-1)!='\\'))
40 m_logsDir
.append("/");
46 void Database::ThreadStart()
50 void Database::ThreadEnd()
54 void Database::escape_string(std::string
& str
)
59 char* buf
= new char[str
.size()*2+1];
60 escape_string(buf
,str
.c_str(),str
.size());
65 bool Database::PExecuteLog(const char * format
,...)
71 char szQuery
[MAX_QUERY_LEN
];
73 int res
= vsnprintf( szQuery
, MAX_QUERY_LEN
, format
, ap
);
78 sLog
.outError("SQL Query truncated (and not execute) for format: %s",format
);
86 time(&curr
); // get current time_t value
87 local
=*(localtime(&curr
)); // dereference and assign
89 sprintf( fName
, "%04d-%02d-%02d_logSQL.sql", local
.tm_year
+1900, local
.tm_mon
+1, local
.tm_mday
);
92 std::string logsDir_fname
= m_logsDir
+fName
;
93 log_file
= fopen(logsDir_fname
.c_str(), "a");
96 fprintf(log_file
, "%s;\n", szQuery
);
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
);
106 return Execute(szQuery
);
109 void Database::SetResultQueue(SqlResultQueue
* queue
)
111 m_queryQueues
[ZThread::ThreadImpl::current()] = queue
;
114 QueryResult
* Database::PQuery(const char *format
,...)
116 if(!format
) return NULL
;
119 char szQuery
[MAX_QUERY_LEN
];
120 va_start(ap
, format
);
121 int res
= vsnprintf( szQuery
, MAX_QUERY_LEN
, format
, ap
);
126 sLog
.outError("SQL Query truncated (and not execute) for format: %s",format
);
130 return Query(szQuery
);
133 bool Database::PExecute(const char * format
,...)
139 char szQuery
[MAX_QUERY_LEN
];
140 va_start(ap
, format
);
141 int res
= vsnprintf( szQuery
, MAX_QUERY_LEN
, format
, ap
);
146 sLog
.outError("SQL Query truncated (and not execute) for format: %s",format
);
150 return Execute(szQuery
);
153 bool Database::DirectPExecute(const char * format
,...)
159 char szQuery
[MAX_QUERY_LEN
];
160 va_start(ap
, format
);
161 int res
= vsnprintf( szQuery
, MAX_QUERY_LEN
, format
, ap
);
166 sLog
.outError("SQL Query truncated (and not execute) for format: %s",format
);
170 return DirectExecute(szQuery
);