Skip to content

Commit

Permalink
#116 Which DateTime is nil?
Browse files Browse the repository at this point in the history
  • Loading branch information
cnmicha committed Mar 8, 2017
1 parent 75c94c1 commit 9207b8e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
26 changes: 21 additions & 5 deletions lazarus/DBConnection.pas
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ procedure TDBConnection.setStudentFields(var resultVar: ArrayOfStudents;
FieldByName('first_name').AsString);
resultVar[length(resultVar) - 1].setClassName(
FieldByName('class_name').AsString);
resultVar[length(resultVar) - 1].setBirth(FieldByName('birth').AsDateTime);

if not (FieldByName('birth').IsNull) then
resultVar[length(resultVar) - 1].setBirth(FieldByName('birth').AsDateTime);

resultVar[length(resultVar) - 1].setLDAPUser(FieldByName('ldap_user').AsString);
if returnOne then
exit;
Expand Down Expand Up @@ -419,7 +422,12 @@ function TDBConnection.updateInsertStudent(student: TStudent): boolean;
FieldByName('last_name').AsString := student.getLastName;
FieldByName('first_name').AsString := student.getFirstName;
FieldByName('class_name').AsString := student.getClassName;
FieldByName('birth').AsDateTime := student.getBirth;

if student.getBirth <= -1 then //if set to null
FieldByName('birth').Clear
else
FieldByName('birth').AsDateTime := student.getBirth;

FieldByName('ldap_user').AsString := student.getLDAPUser;
Post; //add to change buffer
ApplyUpdates; //commit change buffer to db
Expand Down Expand Up @@ -481,8 +489,11 @@ procedure TDBConnection.setRentalFields(var resultVar: ArrayOfRentals;
resultVar[length(resultVar) - 1].setBookId(FieldByName('book_id').AsLargeInt);
resultVar[length(resultVar) - 1].setStudentId(
FieldByName('student_id').AsLargeInt);
resultVar[length(resultVar) - 1].setReturnDate(
FieldByName('return_date').AsDateTime);

if not (FieldByName('return_date').IsNull) then
resultVar[length(resultVar) - 1].setReturnDate(
FieldByName('return_date').AsDateTime);

resultVar[length(resultVar) - 1].setRentalDate(
FieldByName('rental_date').AsDateTime);
if returnOne then
Expand Down Expand Up @@ -578,7 +589,12 @@ function TDBConnection.updateInsertRental(rental: TRental): boolean;
FieldByName('student_id').AsLargeInt := rental.getStudentId;
FieldByName('book_id').AsLargeInt := rental.getBookId;
FieldByName('rental_date').AsDateTime := rental.getRentalDate;
FieldByName('return_date').AsDateTime := rental.getReturnDate;

if rental.getReturnDate <= -1 then //if set to null
FieldByName('return_date').Clear
else
FieldByName('return_date').AsDateTime := rental.getReturnDate;

Post; //add to change buffer
ApplyUpdates; //commit change buffer to db
SQLTransaction.commit;
Expand Down
32 changes: 16 additions & 16 deletions lazarus/rental.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ interface
type
TRental = class
private
id: LongInt;
book_id: LongInt;
student_id: LongInt;
id: longint;
book_id: longint;
student_id: longint;
return_date: TDate;
rental_date: TDate;
public
// Returns the id
// result: id
function getId(): LongInt;
function getId(): longint;

// Sets a new id
// parameter: newId
// result: TRUE on success, so if newID is not NULL
function setId(newId: LongInt): boolean;
function setId(newId: longint): boolean;

// Returns the book id
// result: book_id
function getBookId(): LongInt;
function getBookId(): longint;

// Sets a new book id
// parameter: newBookId
// result: TRUE on success, so if newBookID is not NULL
function setBookId(newBookId: LongInt): boolean;
function setBookId(newBookId: longint): boolean;

// Returns the student id
// result: student_id
function getStudentId(): LongInt;
function getStudentId(): longint;

// Sets a new sudent id
// parameter: newStudentId
// result: TRUE on success, so if new StudentId is not NULL
function setStudentId(newStudentId: LongInt): boolean;
function setStudentId(newStudentId: longint): boolean;

// Returns the book return date
// result: return_date
Expand All @@ -66,12 +66,12 @@ TRental = class

implementation

function TRental.getId(): LongInt;
function TRental.getId(): longint;
begin
Result := self.id;
end;

function TRental.setId(newId: LongInt): boolean;
function TRental.setId(newId: longint): boolean;
begin
Result := False;
if (newId <> NULL) then
Expand All @@ -81,12 +81,12 @@ function TRental.setId(newId: LongInt): boolean;
end;
end;

function TRental.getBookId(): LongInt;
function TRental.getBookId(): longint;
begin
Result := self.book_id;
end;

function TRental.setBookId(newBookId: LongInt): boolean;
function TRental.setBookId(newBookId: longint): boolean;
begin
Result := False;
if (newBookId <> NULL) then
Expand All @@ -96,12 +96,12 @@ function TRental.setBookId(newBookId: LongInt): boolean;
end;
end;

function TRental.getStudentId(): LongInt;
function TRental.getStudentId(): longint;
begin
Result := self.student_id;
end;

function TRental.setStudentId(newStudentId: LongInt): boolean;
function TRental.setStudentId(newStudentId: longint): boolean;
begin
Result := False;
if (newStudentId <> NULL) then
Expand Down Expand Up @@ -146,7 +146,7 @@ constructor TRental.Create;
self.id := -1;
self.book_id := -1;
self.student_id := -1;
self.return_date := -1;
self.return_date := -1; // -1 or smaller equals NULL
self.rental_date := -1;
end;

Expand Down
2 changes: 1 addition & 1 deletion lazarus/student.pas
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ constructor TStudent.Create;
self.last_name := '';
self.first_name := '';
self.class_name := '';
self.birth := -1;
self.birth := -1; // -1 or smaller equals NULL
self.ldap_user := '';
end;

Expand Down

0 comments on commit 9207b8e

Please sign in to comment.