/[H6]/MicrosoftDBLayer.cpp
ViewVC logotype

Annotation of /MicrosoftDBLayer.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (hide annotations) (download)
Wed Sep 6 20:27:24 2006 UTC (17 years, 7 months ago) by torben
File size: 12903 byte(s)
Added support for config file
1 torben 41 /*
2     * Primary developers: Hedin & Kevin
3     */
4    
5     #include "StdAfx.h"
6     #include ".\MicrosoftDBLayer.h"
7    
8    
9 torben 47 MicrosoftDBLayer::MicrosoftDBLayer(ConfigFile &config)
10 torben 41 {
11 torben 47 CString dsn;
12     dsn.Format("ODBC; Description=; DRIVER=SQL Server; SERVER=%s; UID=%s; PWD=%s", config.host, config.username, config.password);
13     db.OpenEx(dsn, CDatabase::noOdbcDialog);
14    
15     CString sql;
16     sql.Format("USE %s", config.database);
17     db.ExecuteSQL(sql);
18 torben 41 }
19    
20     MicrosoftDBLayer::~MicrosoftDBLayer(void)
21     {
22     db.Close();
23     }
24    
25     vector<Person> MicrosoftDBLayer::GetPersonAll(void)
26     {
27     vector<Person> buffer;
28     CString id, inits, name, isadmin, pass;
29    
30     CRecordset rs(&db);
31     rs.Open(AFX_DB_USE_DEFAULT_TYPE, "SELECT PersonID, Initialer, Navn, Admin, Pass FROM Person ORDER BY Initialer DESC");
32     while(!rs.IsEOF())
33     {
34     rs.GetFieldValue((short)0,id);
35     rs.GetFieldValue(1,inits);
36     rs.GetFieldValue(2,name);
37     rs.GetFieldValue(3,isadmin);
38     rs.GetFieldValue(4,pass);
39    
40     Person p;
41     p.id = id;
42     p.inits = inits;
43     p.name = name;
44     p.isadmin = (isadmin == 1);
45     p.pass = pass;
46    
47     buffer.push_back(p);
48    
49     rs.MoveNext();
50     }
51     rs.Close();
52     return buffer;
53     }
54    
55     Person MicrosoftDBLayer::GetPerson(CString wantInits)
56     {
57    
58     CString id, inits, name, isadmin, pass;
59     CString SQL;
60    
61     Person p;
62    
63     wantInits.Replace("'","\"");
64    
65     SQL = "SELECT PersonID, Initialer, Navn, Admin, Pass FROM Person WHERE (Initialer = '" + wantInits + "')";
66     CRecordset rs(&db);
67     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
68    
69     if(!rs.IsEOF() )
70     {
71     rs.GetFieldValue((short)0,id);
72     rs.GetFieldValue(1,inits);
73     rs.GetFieldValue(2,name);
74     rs.GetFieldValue(3,isadmin);
75     rs.GetFieldValue(4,pass);
76    
77     p.id = id;
78     p.inits = inits;
79     p.name = name;
80     p.isadmin = (isadmin == "1");
81     p.pass = pass;
82     }
83    
84     return p;
85     }
86    
87     bool MicrosoftDBLayer::AddPerson(Person NewPerson)
88     {
89     CString SQL;
90     NewPerson.inits.Replace("'","\"");
91     NewPerson.name.Replace("'","\"");
92     NewPerson.pass.Replace("'","\"");
93     SQL.Format("INSERT into Person(Initialer, Navn, Admin, Pass) VALUES('%s', '%s', '%d', '%s')",NewPerson.inits, NewPerson.name, NewPerson.isadmin, NewPerson.pass);
94     db.ExecuteSQL(SQL);
95     return true;
96     }
97    
98     bool MicrosoftDBLayer::UpdatePerson(Person ChangePerson)
99     {
100     CString SQL;
101     ChangePerson.inits.Replace("'","\"");
102     ChangePerson.name.Replace("'","\"");
103     ChangePerson.pass.Replace("'","\"");
104     ChangePerson.id.Replace("'","\"");
105     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);
106     db.ExecuteSQL(SQL);
107     return true;
108     }
109    
110     bool MicrosoftDBLayer::DeletePerson(Person RemovePerson)
111     {
112     CString SQL;
113     RemovePerson.id.Replace("'","\"");
114     SQL.Format("DELETE FROM Person WHERE PersonID = '%s'", RemovePerson.id);
115     db.ExecuteSQL(SQL);
116     return true;
117     }
118    
119     bool MicrosoftDBLayer::ReturnEquipment(CString barcode)
120     {
121     CString SQL;
122     barcode.Replace("'","\"");
123     SQL.Format("UPDATE Udstyr SET Status = '3' WHERE Stregkode= '%s'", barcode);
124     db.ExecuteSQL(SQL);
125    
126     SQL.Format("UPDATE Udlån SET Afleveringsdato = Getdate() WHERE Stregkode= '%s' AND Afleveringsdato IS NULL ", barcode);
127     db.ExecuteSQL(SQL);
128    
129     checkReservations(barcode);
130     return true;
131     }
132    
133     bool MicrosoftDBLayer::CheckoutEquipment(Person CheckPersID, Equipment CheckEquip, int Numdays)
134     {
135     CString SQL;
136     CheckEquip.barcode.Replace("'","\"");
137     CheckPersID.id.Replace("'","\"");
138     SQL.Format("UPDATE Udstyr SET Status = '1' WHERE Stregkode = '%s' ", CheckEquip.barcode);
139     db.ExecuteSQL(SQL);
140     SQL.Format("INSERT INTO Udlån (PersonID, Stregkode, Startdato, Antaldage) VALUES( '%s','%s', GetDate(),'%d')", CheckPersID.id, CheckEquip.barcode, Numdays);
141     db.ExecuteSQL(SQL);
142     return true;
143     }
144    
145     bool MicrosoftDBLayer::EquipmentReservation(CString barcode, Person CheckPerson)
146     {
147     CString SQL;
148     CheckPerson.id.Replace("'","\"");
149     barcode.Replace("'","\"");
150     SQL.Format("INSERT INTO Resevation (PersonID, Stregkode, Startdato) VALUES( '%s','%s', GetDate())", CheckPerson.id, barcode);
151     db.ExecuteSQL(SQL);
152     checkReservations(barcode);
153     return true;
154     }
155    
156     bool MicrosoftDBLayer::UpdateEquipment(Equipment CheckEquip)
157     {
158     CString SQL;
159     CheckEquip.name.Replace("'","\"");
160     CheckEquip.description.Replace("'","\"");
161     CheckEquip.placement.Replace("'","\"");
162     CheckEquip.status.Replace("'","\"");
163     CheckEquip.barcode.Replace("'","\"");
164     SQL.Format("UPDATE Udstyr SET Navn = '%s', Beskrivelse = '%s', Placering = '%s', Status = '%s' WHERE Stregkode = '%s'", CheckEquip.name, CheckEquip.description, CheckEquip.placement, CheckEquip.status, CheckEquip.barcode);
165     db.ExecuteSQL(SQL);
166     return true;
167     }
168    
169     bool MicrosoftDBLayer::AddEquipment(Equipment AddEquip)
170     {
171     CString SQL;
172     AddEquip.barcode.Replace("'","\"");
173     AddEquip.name.Replace("'","\"");
174     AddEquip.description.Replace("'","\"");
175     AddEquip.placement.Replace("'","\"");
176     SQL.Format("INSERT into Udstyr(Stregkode, Navn, Beskrivelse, Placering, Status) VALUES('%s', '%s', '%s', '%s', '%s')",AddEquip.barcode, AddEquip.name, AddEquip.description, AddEquip.placement, "3");
177     db.ExecuteSQL(SQL);
178     return true;
179     }
180    
181     vector<Equipment> MicrosoftDBLayer::GetEquipmentAll(void)
182     {
183     vector<Equipment> buffer;
184     CString barcode, name, description, placement, status;
185    
186     CRecordset rs(&db);
187     rs.Open(AFX_DB_USE_DEFAULT_TYPE, "SELECT Stregkode, Navn, Beskrivelse, Placering, Status FROM Udstyr");
188     while(!rs.IsEOF())
189     {
190     rs.GetFieldValue((short)0,barcode);
191     rs.GetFieldValue(1,name);
192     rs.GetFieldValue(2,description);
193     rs.GetFieldValue(3,placement);
194     rs.GetFieldValue(4,status);
195    
196     Equipment p;
197     p.barcode = barcode;
198     p.name = name;
199     p.description = description;
200     p.placement = placement;
201     p.status = status;
202     buffer.push_back(p);
203    
204     rs.MoveNext();
205     }
206     rs.Close();
207     return buffer;
208     }
209    
210     Equipment MicrosoftDBLayer::GetEquipment(CString wantBarcode)
211     {
212    
213     CString barcode,name,description,placement,status;
214    
215     CString SQL;
216    
217     wantBarcode.Replace("'","\"");
218    
219     Equipment e;
220    
221     SQL.Format("SELECT Stregkode,Navn,Udstyr.Beskrivelse,Placering,Status.Beskrivelse FROM Udstyr INNER JOIN Status ON Udstyr.Status = Status.StatusID Where (Stregkode = '%s')", wantBarcode);
222     CRecordset rs(&db);
223     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
224     if (!rs.IsEOF() )
225     {
226     rs.GetFieldValue((short)0,barcode);
227     rs.GetFieldValue(1,name);
228     rs.GetFieldValue(2,description);
229     rs.GetFieldValue(3,placement);
230     rs.GetFieldValue(4,status);
231    
232     e.barcode = barcode;
233     e.name = name;
234     e.description = description;
235     e.placement = placement;
236     e.status = status;
237     }
238     rs.Close();
239     return e;
240     }
241    
242     bool MicrosoftDBLayer::DeleteEquipment(Equipment DelEquip)
243     {
244     CString SQL;
245     DelEquip.barcode.Replace("'","\"");
246     SQL.Format("DELETE FROM Udstyr WHERE Stregkode = '%s'", DelEquip.barcode);
247     db.ExecuteSQL(SQL);
248     return true;
249     }
250    
251    
252     // Developed by Torben H. Nielsen
253     vector<Equipment> MicrosoftDBLayer::Search(CString barcode, CString name, CString inits, bool available, bool reserved, bool deposited)
254     {
255     vector<Equipment> buffer;
256    
257     barcode.Replace("'","\"");
258     name.Replace("'","\"");
259     inits.Replace("'","\"");
260    
261     //here are the wanted equipments found through 3 seperate queries - I could also
262     //find them all in one query where I joined the 3 selects via a UNION
263     CString SQL;
264     if (available == true) {
265     SQL = "SELECT DISTINCT Udstyr.stregkode,Udstyr.Navn,Udstyr.Beskrivelse,Udstyr.Placering,Status.Beskrivelse \
266     FROM Udstyr \
267     INNER JOIN Status ON Udstyr.status = Status.StatusID WHERE Udstyr.Status = 3 ";
268     if (barcode != "")
269     SQL += " AND Udstyr.stregkode = '" + barcode + "' ";
270     if (name != "")
271     SQL += " AND Udstyr.Navn LIKE '%" + name + "%' ";
272    
273     CRecordset rs(&db);
274     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
275     FillEquipmentVector(buffer,rs);
276     rs.Close();
277     }
278    
279     if (reserved == true) {
280     SQL = "SELECT DISTINCT Udstyr.stregkode,Udstyr.Navn,Udstyr.Beskrivelse,Udstyr.Placering,Status.Beskrivelse \r\n\
281     FROM Udstyr \r\n\
282     INNER JOIN Status ON Udstyr.status = Status.StatusID \r\n\
283     INNER JOIN Resevation ON Udstyr.Stregkode = Resevation.stregkode \r\n\
284     INNER JOIN Person ON Resevation.PersonID = Person.PersonID\r\n\
285     WHERE ( Udstyr.Status = 2 OR Person.Initialer = '" + inits + "') ";
286     if (barcode != "")
287     SQL += " AND Udstyr.stregkode = '" + barcode + "' ";
288     if (name != "")
289     SQL += " AND Udstyr.Navn LIKE '%" + name + "%' ";
290     //if (inits != "")
291     // SQL += " AND Person.Initialer = '" + inits + "' ";
292    
293     CRecordset rs(&db);
294     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
295     FillEquipmentVector(buffer,rs);
296     rs.Close();
297     }
298    
299    
300     if (deposited == true) {
301     SQL = "SELECT DISTINCT Udstyr.stregkode,Udstyr.Navn,Udstyr.Beskrivelse,Udstyr.Placering,Status.Beskrivelse \r\n\
302     FROM Udstyr \r\n\
303     INNER JOIN Status ON Udstyr.status = Status.StatusID \r\n\
304     INNER JOIN Udlån ON Udstyr.Stregkode = Udlån.stregkode \r\n\
305     INNER JOIN Person ON Udlån.PersonID = person.PersonID\r\n\
306     WHERE Udstyr.Status = 1 \r\n\
307     AND Udlån.Afleveringsdato IS NULL";
308     if (barcode != "")
309     SQL += " AND Udstyr.stregkode = '" + barcode + "' ";
310     if (name != "")
311     SQL += " AND Udstyr.Navn LIKE '%" + name + "%' ";
312     if (inits != "")
313     SQL += " AND Person.Initialer = '" + inits + "' ";
314    
315     CRecordset rs(&db);
316     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
317     FillEquipmentVector(buffer,rs);
318     rs.Close();
319     }
320    
321     // now I have the equipment rows the user asked for, now I need some more checkout and
322     // reservation data for each piece of equipment
323    
324     for (int i=0; i<buffer.size(); i++) {
325     //fill Equipment::checkouts
326    
327     SQL = "SELECT TOP 10 Udlån.StartDato, Udlån.Afleveringsdato,Udlån.Antaldage, Person.Initialer\
328     FROM Udlån\
329     INNER JOIN Person ON Udlån.PersonID = Person.PersonID ";
330     SQL += "WHERE Udlån.Stregkode = " + buffer[i].barcode + " ORDER BY STARTDATO DESC";
331     CRecordset rs(&db);
332     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
333     if (rs.GetRecordCount()>0) {
334     rs.MoveFirst();
335     while ( !rs.IsEOF() ) {
336     Checkout check;
337     rs.GetFieldValue((short)0, check.startdate);
338     rs.GetFieldValue(1, check.enddate);
339     rs.GetFieldValue(2, check.numdays);
340     rs.GetFieldValue(3, check.inits);
341    
342     buffer[i].checkouts.push_back(check);
343     rs.MoveNext();
344     }
345     }
346     rs.Close();
347    
348     // fill Equipment::reservations
349     SQL = "SELECT Resevation.ResevationID, Resevation.StartDato, Person.Initialer\
350     FROM Resevation\
351     INNER JOIN Person ON Resevation.PersonID = Person.PersonID ";
352     SQL += "WHERE Resevation.Stregkode = " + buffer[i].barcode + " ORDER BY STARTDATO ASC";
353    
354     rs.Open(AFX_DB_USE_DEFAULT_TYPE, SQL);
355     if (rs.GetRecordCount()>0) {
356     rs.MoveFirst();
357     while ( !rs.IsEOF() ) {
358     Reservation res;
359     rs.GetFieldValue((short)0, res.reservationID);
360     rs.GetFieldValue(1, res.startdate);
361     rs.GetFieldValue(2, res.inits);
362    
363     buffer[i].reservations.push_back(res);
364     rs.MoveNext();
365     }
366     }
367     rs.Close();
368     }
369    
370     return buffer;
371     }
372    
373     // Developed by Torben H. Nielsen
374     void MicrosoftDBLayer::FillEquipmentVector(vector<Equipment> &buffer, CRecordset &rs)
375     {
376     CString barcode,name,description,placement,status;
377     if (rs.GetRecordCount()>0) {
378     rs.MoveFirst();
379     while (!rs.IsEOF() ) {
380     Equipment eq;
381     rs.GetFieldValue((short)0, eq.barcode);
382     rs.GetFieldValue(1, eq.name);
383     rs.GetFieldValue(2, eq.description);
384     rs.GetFieldValue(3, eq.placement);
385     rs.GetFieldValue(4, eq.status);
386    
387     buffer.push_back(eq);
388    
389     rs.MoveNext();
390     }
391     }
392     }
393     //Developed by Torben H. Nielsen
394     void MicrosoftDBLayer::checkReservations(CString barcode)
395     {
396     CString scount,status;
397     barcode.Replace("'","\"");
398     CString SQL = "SELECT count(*) FROM Resevation WHERE Resevation.Stregkode = '" + barcode + "'";
399     CRecordset rs(&db);
400     rs.Open(AFX_DB_USE_DEFAULT_TYPE,SQL);
401     rs.MoveFirst();
402     rs.GetFieldValue((short)0,scount);
403     int reservationCount = atoi(scount);
404     rs.Close();
405    
406     SQL = "SELECT status FROM Udstyr WHERE Udstyr.Stregkode = '" + barcode + "'";
407     rs.Open(AFX_DB_USE_DEFAULT_TYPE,SQL);
408     if (rs.GetRecordCount() >0) {
409     rs.MoveFirst();
410     rs.GetFieldValue((short)0,status);
411     }
412     rs.Close();
413    
414     if (reservationCount > 0 && status == "3") {//equipment is available and has at lease one reservation
415     SQL = "UPDATE Udstyr SET Status = 2 WHERE Stregkode = '" + barcode + "'";
416     db.ExecuteSQL(SQL);
417     }
418     }
419    
420     //Developed by Torben H. Nielsen
421     bool MicrosoftDBLayer::DeleteReservation(CString barcode, CString resid)
422     {
423     barcode.Replace("'","\"");
424     resid.Replace("'","\"");
425     CString SQL = "DELETE FROM Resevation WHERE resevationid = " + resid;
426     db.ExecuteSQL(SQL);
427     checkReservations(barcode);
428     return true;
429     }
430    

  ViewVC Help
Powered by ViewVC 1.1.20