SAS/CourseDAOImpl.cpp
2022-07-03 18:22:02 +08:00

45 lines
1.5 KiB
C++

#include"CourseDAO.h"
void addCourse(_ConnectionPtr connection, Course course) {
string sql = "insert into course values('" + course.getId() + "'," +
"'" + course.getName() + "')";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteCourse(_ConnectionPtr connection, string id) {
string sql = "delete from course where id = '" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void updateCourseName(_ConnectionPtr connection, string id, string name) {
string sql = "update course set name = '" + name + "\' where id = \'" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
Course& getCourseById(_ConnectionPtr connection, string id) {
string sql = "select * from course where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
Course* course = new Course();
course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
return *course;
}
std::vector<Course>& getCourseByName(_ConnectionPtr connection, string name) {
string sql = "select * from course where name = '" + name + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
std::vector<Course>* v = new std::vector<Course>();
Course* course;
for (; !record->EndOfFile; record->MoveNext()) {
course = new Course();
course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
v->push_back(*course);
}
return *v;
}