Added a EditorSocial.
[UnsignedByte.git] / src / Core / Socket / SQLSocket.cpp
blob03fe77b17a0d593a2335bcf4bcfe0da34ce2ae8f
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <stdarg.h>
22 #include <string>
23 #include <vector>
25 #include "DatabaseMgr.h"
26 #include "Global.h"
27 #include "Query.h"
28 #include "SQLSocket.h"
30 SQLSocket::SQLSocket(ISocketHandler& h) :
31 TcpSocket(h)
33 SetLineProtocol();
36 SQLSocket::~SQLSocket(void)
41 void SQLSocket::OnAccept()
43 std::string addr = GetRemoteAddress();
44 if(addr.compare("127.0.0.1") && addr.compare("localhost"))
46 std::string msg = "Unauthorized SQL request from '";
47 msg.append(addr);
48 msg.append("'!\n");
50 Global::Get()->printlog(msg);
51 SetCloseAndDelete();
52 return;
56 void SQLSocket::OnLine(const std::string &line)
58 std::string msg = "SQL request: '";
59 msg.append(line);
60 msg.append("'\n");
62 Query q(DatabaseMgr::Get()->DBref());
63 q.get_result(line);
65 std::vector< std::vector<std::string> > result;
66 while(q.fetch_row())
68 std::vector<std::string> row;
69 std::string column = q.getstr();
70 while(column.size())
72 row.push_back(column);
73 column = q.getstr();
75 result.push_back(row);
77 q.free_result();
78 if(result.size() == 0)
80 Send("No result.\n");
81 return;
84 std::string res;
85 for(std::vector< std::vector<std::string> >::iterator its = result.begin(); its != result.end(); its++)
87 std::vector<std::string> row = *its;
88 if(row.size() == 0)
89 continue;
91 if(row.size() == 1)
93 res.append(row[0]);
94 res.append("\n");
95 continue;
98 std::string rowres;
99 for(std::vector<std::string>::iterator it = row.begin(); it != row.end(); it++)
101 if(it != row.begin())
102 rowres.append(" | ");
103 rowres.append(*it);
106 res.append(rowres);
107 res.append("\n");
110 Send(res);
111 return;
114 void SQLSocket::Send(const std::string& msg)
116 SendBuf(msg.c_str(), msg.size());
117 return;