/[H6]/DatabaseLayer.cpp
ViewVC logotype

Annotation of /DatabaseLayer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (hide annotations) (download)
Wed Aug 30 21:57:57 2006 UTC (17 years, 7 months ago) by torben
File size: 4595 byte(s)
Finished (???) administration part - plus made a dummy offline version of the DatabaseLayer class.
1 torben 8 #include "StdAfx.h"
2     #include ".\databaselayer.h"
3    
4 torben 9 DatabaseLayer::DatabaseLayer(void)
5     {
6     Person p1;
7     p1.id="0";
8     p1.inits="MaPe";
9     p1.isadmin = 0;
10     p1.name="Mads Pedersen";
11     persons.push_back(p1);
12 torben 8
13 torben 9 Person p2;
14     p2.id="1";
15     p2.inits = "thn";
16     p2.isadmin = 1;
17     p2.name = "Torben Nielsen";
18     p2.pass = "thn";
19     persons.push_back(p2);
20    
21     Person p3;
22     p3.id = "2";
23     p3.inits = "FiKa";
24     p3.isadmin = 0;
25     p3.name = "Finn Karlsen";
26     persons.push_back(p3);
27    
28     Equipment e1;
29     e1.barcode = "1234567890123";
30     e1.name = "1 ks. øl";
31     e1.description = "30 Stk. Hancock Høker";
32     e1.placement = "i kælderen";
33     e1.status = "Drukket";
34     equipments.push_back(e1);
35    
36     }
37    
38     vector<Person> DatabaseLayer::GetPersonAll(void)
39     {
40     return persons;
41     }
42    
43     Person DatabaseLayer::GetPerson(CString wantInits)
44     {
45     for (int i=0; i<persons.size(); i++) {
46     if (persons[i].inits == wantInits)
47     return persons[i];
48     }
49     Person p;
50     return p;
51     }
52    
53     Equipment DatabaseLayer::GetEquipment(CString wantBarcode)
54     {
55     for (int i=0; i<equipments.size(); i++) {
56     if (equipments[i].barcode == wantBarcode) {
57     return equipments[i];
58     }
59     }
60     }
61    
62     bool DatabaseLayer::UpdatePerson(Person ChangePerson)
63     {
64     for (int i=0; i<persons.size(); i++) {
65     if (persons[i].inits == ChangePerson.inits) {
66     persons[i] = ChangePerson;
67     break;
68     }
69     }
70    
71     return true;
72     }
73    
74    
75     bool DatabaseLayer::UpdateEquipment(Equipment ChangeEquipment)
76     {
77     for (int i=0; i<equipments.size(); i++) {
78     if (equipments[i].barcode == ChangeEquipment.barcode) {
79     equipments[i] = ChangeEquipment;
80     break;
81     }
82     }
83    
84     return true;
85    
86     }
87    
88     bool DatabaseLayer::AddPerson(Person NewPerson)
89     {
90     persons.push_back(NewPerson);
91     return true;
92     }
93    
94    
95     vector<Equipment> DatabaseLayer::GetEquipmentAll()
96     {
97     return equipments;
98     }
99    
100     bool DatabaseLayer::AddEquipment(Equipment e)
101     {
102     equipments.push_back(e);
103     return true;
104     }
105    
106     bool DatabaseLayer::DeletePerson(Person RemovePerson)
107     {
108     //vector kan ikke slette fra midten, så de der ikke slettes kopieres til ny vector
109     //og gl.vector overskrivets
110    
111     vector<Person> newvector;
112     for (int i = 0; i != persons.size(); i++) {
113     if ( persons[i].inits != RemovePerson.inits) {
114     newvector.push_back( persons[i] );
115     }
116     }
117     persons = newvector;
118     return true;
119     }
120    
121     /*
122 torben 8 DatabaseLayer::DatabaseLayer(void)
123     {
124     db.OpenEx("ODBC; Description=; DRIVER=SQL Server; SERVER=TK-Teacher; UID=GroupTwo; PWD=toog30");
125     db.ExecuteSQL("USE GroupTwo");
126     }
127    
128     DatabaseLayer::~DatabaseLayer(void)
129     {
130     }
131    
132     vector<Person> DatabaseLayer::GetPersonAll(void)
133     {
134     vector<Person> buffer;
135     CString id, inits, name, isadmin, pass;
136    
137     CRecordset rs(&db);
138     rs.Open(AFX_DB_USE_DEFAULT_TYPE, "SELECT PersonID, Initialer, Navn, Admin, Pass FROM Person");
139     while(!rs.IsEOF())
140     {
141     rs.GetFieldValue((short)0,id);
142     rs.GetFieldValue(1,inits);
143     rs.GetFieldValue(2,name);
144     rs.GetFieldValue(3,isadmin);
145     rs.GetFieldValue(4,pass);
146    
147     Person p;
148     p.id = id;
149     p.inits = inits;
150     p.name = name;
151     p.isadmin = (isadmin == 1);
152     p.pass = pass;
153    
154     buffer.push_back(p);
155    
156     rs.MoveNext();
157     }
158     rs.Close();
159     return buffer;
160     }
161    
162     Person DatabaseLayer::GetPerson(CString wantInits)
163     {
164    
165     CString id, inits, name, isadmin, pass;
166     CString SQL;
167    
168     Person p;
169     //FIX SQL INJECTION
170     SQL = "SELECT PersonID, Initialer, Navn, Admin, Pass FROM Person WHERE (Initialer = '" + wantInits + "')";
171     CRecordset rs(&db);
172     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
173    
174     if(!rs.IsEOF() )
175     {
176     rs.GetFieldValue((short)0,id);
177     rs.GetFieldValue(1,inits);
178     rs.GetFieldValue(2,name);
179     rs.GetFieldValue(3,isadmin);
180     rs.GetFieldValue(4,pass);
181    
182     p.id = id;
183     p.inits = inits;
184     p.name = name;
185 torben 9 p.isadmin = (isadmin == "1");
186 torben 8 p.pass = pass;
187     }
188     return p;
189     }
190    
191     bool DatabaseLayer::AddPerson(Person NewPerson)
192     {
193     CString SQL;
194     SQL.Format("INSERT into Person(Initialer, Navn, Admin, Pass) VALUES('%s', '%s', '%d', '%s')",NewPerson.inits, NewPerson.name, NewPerson.isadmin, NewPerson.pass);
195     db.ExecuteSQL(SQL);
196     return true;
197     }
198    
199     bool DatabaseLayer::UpdatePerson(Person ChangePerson)
200     {
201     CString SQL;
202     SQL.Format("UPDATE Person SET Initialer = '%s', Navn = '%s', Pass = '%s', Admin = '%d' WHERE PersonID = '%s'", ChangePerson.inits, ChangePerson.name, ChangePerson.pass, ChangePerson.isadmin, ChangePerson.id);
203     db.ExecuteSQL(SQL);
204     return true;
205     }
206    
207     bool DatabaseLayer::DeletePerson(Person RemovePerson)
208     {
209     CString SQL;
210     SQL.Format("DELETE FROM Person WHERE PersonID = '%s'", RemovePerson.id);
211     db.ExecuteSQL(SQL);
212     return true;
213     }
214 torben 9
215     */

  ViewVC Help
Powered by ViewVC 1.1.20