ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Smersh/Person.cpp
Revision: 179
Committed: 2004-07-02T21:16:12-07:00 (21 years ago) by Douglas Thrift
File size: 2591 byte(s)
Log Message:
Works now, uses menes-dbi for PostgreSQL.

File Contents

# User Rev Content
1 Douglas Thrift 164 // Smersh
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Person.hpp"
8    
9 Douglas Thrift 167 Person::Person(const string& sn) : name("Unknown Person")
10 Douglas Thrift 164 {
11 Douglas Thrift 179 #ifdef __FreeBSD__
12     if (db.empty()) configure();
13     #else
14 Douglas Thrift 170 if (file.empty()) configure();
15 Douglas Thrift 179 #endif
16 Douglas Thrift 170 if (!sn.empty()) query(sn);
17     }
18    
19 Douglas Thrift 179 #ifdef __FreeBSD__
20     string Person::user, Person::db;
21     #else
22 Douglas Thrift 170 string Person::file;
23     vector<string> Person::args(1, "smersh");
24     string Person::separator, Person::begin, Person::end;
25 Douglas Thrift 179 #endif
26 Douglas Thrift 170
27     void Person::configure()
28     {
29     ext::Handle<xml::Document> document(xml::Parse("smersh.xml"));
30     ext::Handle<xml::Node> smersh(*document/"smersh");
31    
32 Douglas Thrift 179 #ifdef __FreeBSD__
33     user = *smersh/"user";
34     db = *smersh/"db";
35    
36     if (debug) cerr << "user = " << user << "\ndb = " << db << '\n';
37     #else
38 Douglas Thrift 170 file = *smersh/"command"/"file";
39    
40     xml::NodeSet nodes(*smersh/"command"/"arg");
41    
42     for (xml::NodeSet::Iterator node(nodes.Begin()); node != nodes.End();
43     ++node) args.push_back(**node);
44    
45     separator = *smersh/"separator";
46     begin = *smersh/"begin";
47     end = *smersh/"end";
48    
49     if (debug)
50 Douglas Thrift 167 {
51 Douglas Thrift 170 cerr << "file = " << file << '\n'
52     << "args = {\n";
53    
54     for (vector<string>::size_type index(0); index < args.size(); ++index)
55     {
56     cerr << " [" << index << "] = " << args[index] << '\n';
57     }
58    
59     cerr << "}\n"
60     << "separator = " << separator << '\n'
61     << "begin = " << begin << '\n'
62     << "end = " << end << '\n';
63 Douglas Thrift 167 }
64 Douglas Thrift 179 #endif
65 Douglas Thrift 164 }
66 Douglas Thrift 170
67     void Person::query(const string& sn)
68     {
69 Douglas Thrift 179 #ifdef __FreeBSD__
70     ext::Handle<dbi::Connection> db(dbi::Connect(user, this->db));
71     ext::Handle<dbi::ResultSet> people(db->Execute(string("SELECT name FROM pe")
72     + "ople, peopleaimmap, aim WHERE people.id=pid AND aid=aim.id AND sn=LO"
73     + "WER(\'" + sn + "\')"));
74    
75     if (people->MoveNext()) name = people->GetString("name");
76    
77     while (people->MoveNext())
78     {
79     Person person;
80    
81     person.name = people->GetString("name");
82    
83     multiple.push_back(person);
84     }
85     #else
86 Douglas Thrift 170 vector<string> args(Person::args);
87    
88     args.push_back(string("SELECT name FROM people, peopleaimmap, aim WHERE p")
89 Douglas Thrift 179 + "eople.id=pid AND aid=aim.id AND sn=LOWER(\'" + sn + "\');");
90 Douglas Thrift 170
91     ipstream sql(file, args);
92     bool first(true);
93     Matcher separator(this->separator), heading(begin + "\\s*name\\s*" + end),
94     matcher(begin + "(.*)" + end);
95    
96     do
97     {
98     string line;
99    
100     getline(sql, line);
101    
102     if (debug) cerr << "line = " << line << '\n';
103    
104     if (line == separator) continue;
105     else if (line == heading) continue;
106     else if (line == matcher)
107     {
108     if (first)
109     {
110     name = matcher[1];
111     first = false;
112     }
113     else
114     {
115     Person person;
116    
117     person.name = matcher[1];
118    
119     multiple.push_back(person);
120     }
121     }
122     else break;
123     }
124     while (sql.good());
125 Douglas Thrift 179 #endif
126 Douglas Thrift 170 }

Properties

Name Value
svn:eol-style native
svn:keywords Id