Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Socket / SQLSocket.cpp
blob06ed456972dcbcffcf5a559478580690d202f1c9
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 <string>
22 #include <vector>
23 #include <stdarg.h>
25 #include "SQLSocket.h"
26 #include "Global.h"
27 #include "DatabaseMgr.h"
29 #include "Query.h"
31 SQLSocket::SQLSocket(ISocketHandler& h) :
32 TcpSocket(h)
34 SetLineProtocol();
37 SQLSocket::~SQLSocket(void)
42 void SQLSocket::OnAccept()
44 std::string addr = GetRemoteAddress();
45 if(addr.compare("127.0.0.1") && addr.compare("localhost"))
47 Global::Get()->logf("Unauthorized SQL request from '%s'!\n", addr.c_str());
48 SetCloseAndDelete();
49 return;
53 void SQLSocket::OnLine(const std::string &line)
55 Global::Get()->logf("SQL request: '%s'\n", line.c_str());
56 Query q(DatabaseMgr::Get()->DBref());
57 q.get_result(line);
59 std::vector< std::vector<std::string> > result;
60 while(q.fetch_row())
62 std::vector<std::string> row;
63 std::string column = q.getstr();
64 while(column.size())
66 row.push_back(column);
67 column = q.getstr();
69 result.push_back(row);
71 q.free_result();
72 if(result.size() == 0)
74 Send("No result.\n");
75 return;
78 std::string res;
79 for(std::vector< std::vector<std::string> >::iterator its = result.begin(); its != result.end(); its++)
81 std::vector<std::string> row = *its;
82 if(row.size() == 0)
83 continue;
85 if(row.size() == 1)
87 res.append(row[0]);
88 res.append("\n");
89 continue;
92 std::string rowres;
93 for(std::vector<std::string>::iterator it = row.begin(); it != row.end(); it++)
95 if(it != row.begin())
96 rowres.append(" | ");
97 rowres.append(*it);
100 res.append(rowres);
101 res.append("\n");
104 Send(res);
105 return;
108 void SQLSocket::Send(const std::string& msg)
110 SendBuf(msg.c_str(), msg.size());
111 return;