Otclient  14/8/2020
database.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "database.h"
24 
25 boost::recursive_mutex DBQuery::databaseLock;
26 
28 {
29  if(result->next())
30  return result;
31 
32  result->free();
33  return nullptr;
34 }
35 
36 void DBInsert::setQuery(const std::string& query)
37 {
38  m_query = query;
39  m_buf = "";
40  m_rows = 0;
41 }
42 
43 bool DBInsert::addRow(const std::string& row)
44 {
45  ++m_rows;
46  if(m_buf.empty()) {
47  m_buf = "(" + row + ")";
48  }
49  else if(m_buf.length() > 8192)
50  {
51  if(!execute())
52  return false;
53 
54  m_buf = "(" + row + ")";
55  }
56  else
57  m_buf += ",(" + row + ")";
58 
59  return true;
60 }
61 
62 bool DBInsert::addRow(std::stringstream& row)
63 {
64  bool ret = addRow(row.str());
65  row.str("");
66  return ret;
67 }
68 
70 {
71  if(m_buf.empty() || !m_rows)
72  return true;
73 
74  m_rows = 0;
75  bool ret = m_db->executeQuery(m_query + m_buf);
76  m_buf = "";
77  return ret;
78 }
DBInsert::m_buf
std::string m_buf
Definition: database.h:264
DBInsert::m_db
DatabasePtr m_db
Definition: database.h:260
DBInsert::addRow
bool addRow(const std::string &row)
Definition: database.cpp:43
DBInsert::setQuery
void setQuery(const std::string &query)
Definition: database.cpp:36
DBInsert::execute
bool execute()
Definition: database.cpp:69
Database::executeQuery
virtual bool executeQuery(const std::string &query)
Definition: database.h:69
Database::verifyResult
DBResultPtr verifyResult(DBResultPtr result)
Definition: database.cpp:27
database.h
stdext::shared_object_ptr
Definition: shared_object.h:39
DBQuery::databaseLock
static boost::recursive_mutex databaseLock
Definition: database.h:214
DBInsert::m_query
std::string m_query
Definition: database.h:263
DBInsert::m_rows
uint32 m_rows
Definition: database.h:262