Skip to content

Commit

Permalink
Merge pull request #123 from Herder-IT-Solutions/database
Browse files Browse the repository at this point in the history
#116 Which DateTime is nil?
  • Loading branch information
cnmicha authored Mar 8, 2017
2 parents 3bc3ffc + 24d7817 commit e64e913
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 44 deletions.
40 changes: 31 additions & 9 deletions lazarus/DBConnection.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface

uses
Classes, SysUtils, sqlite3conn, sqldb, DB, FileUtil, Forms, Controls,
Graphics, Dialogs, DBGrids, book, booktype, rental, student;
Graphics, Dialogs, DBGrids, book, booktype, rental, student, DBConstants;

type
ArrayOfStudents = array of TStudent;
Expand Down 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 = SQLNull 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 = SQLNull 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 Expand Up @@ -669,8 +685,11 @@ procedure TDBConnection.setBookFields(var resultVar: ArrayOfBooks; returnOne: bo
resultVar[length(resultVar) - 1] := TBook.Create; //create new book object
resultVar[length(resultVar) - 1].setId(FieldByName('id').AsLargeInt);
resultVar[length(resultVar) - 1].setIsbn(FieldByName('isbn').AsString);
resultVar[length(resultVar) - 1].setCondition(
FieldByName('condition').AsInteger);

if not (FieldByName('condition').IsNull) then
resultVar[length(resultVar) - 1].setCondition(
FieldByName('condition').AsInteger);

if returnOne then
exit;
Next;
Expand Down Expand Up @@ -824,7 +843,10 @@ procedure TDBConnection.setBooktypeFields(var resultVar: ArrayOfBookTypes;
resultVar[length(resultVar) - 1].setIsbn(FieldByName('isbn').AsString);
resultVar[length(resultVar) - 1].setTitle(FieldByName('title').AsString);
resultVar[length(resultVar) - 1].setSubject(FieldByName('subject').AsString);
resultVar[length(resultVar) - 1].setStorage(FieldByName('storage').AsInteger);

if not (FieldByName('storage').IsNull) then
resultVar[length(resultVar) - 1].setStorage(FieldByName('storage').AsInteger);

if returnOne then
exit;
Next;
Expand Down
10 changes: 5 additions & 5 deletions lazarus/book.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
Classes, SysUtils;
Classes, SysUtils, DBConstants;

type
TBook = class
Expand Down Expand Up @@ -38,7 +38,7 @@ TBook = class

// Sets a new book condition
// parameter: newCondition
// result: TRUE on success, so if newIsbn has 1 character, is <=5 and not NULL
// result: TRUE on success, so if newIsbn has 1 character, is <=5 or is =SQLNull
function setCondition(newCondition: cardinal): boolean;

constructor Create;
Expand Down Expand Up @@ -97,7 +97,7 @@ function TBook.getCondition(): cardinal;
function TBook.setCondition(newCondition: cardinal): boolean;
begin
Result := False;
if (newCondition <= 6) then
if (newCondition = SQLNull ) or (newCondition <= 6) then
begin
self.condition := newCondition;
Result := True;
Expand All @@ -106,9 +106,9 @@ function TBook.setCondition(newCondition: cardinal): boolean;

constructor TBook.Create;
begin
self.id := -1;
self.id := SQLNull;
self.isbn := '';
self.condition := 0;
self.condition := SQLNull;
end;

end.
8 changes: 4 additions & 4 deletions lazarus/booktype.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
Classes, SysUtils;
Classes, SysUtils, DBConstants;

type
TBooktype = class
Expand Down Expand Up @@ -48,7 +48,7 @@ TBooktype = class

// Sets a new book storage
// parameter: newStorage
// result: TRUE on success, so if newStorage <= (2^63)-1)
// result: TRUE on success, so if newStorage <= (2^63)-1) or =SQLNull
function setStorage(newStorage: integer): boolean;

constructor Create;
Expand Down Expand Up @@ -119,7 +119,7 @@ function TBooktype.getStorage(): integer;
function TBooktype.setStorage(newStorage: integer): boolean;
begin
Result := False;
if (newStorage <> NULL) then
if (newStorage = SQLNull) or (newStorage <> NULL) then
begin
self.storage := newStorage;
Result := True;
Expand All @@ -131,7 +131,7 @@ constructor TBooktype.Create;
self.isbn := '';
self.title := '';
self.subject := '';
self.storage := 0;
self.storage := SQLNull;
end;

end.
12 changes: 12 additions & 0 deletions lazarus/dbconstants.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
unit DBConstants;

{$mode objfpc}{$H+}

interface

const
SQLNull = -1;

implementation

end.
46 changes: 23 additions & 23 deletions lazarus/rental.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@
interface

uses
Classes, SysUtils;
Classes, SysUtils, DBConstants;

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
function getReturnDate(): TDate;

// Sets a new book return datetime in seconds
// parameter: newReturnDate
// result: TRUE on success, so if newReturnDate >= 0 and not NULL
// result: TRUE on success, so if newReturnDate >= 0 or =SQLNull
function setReturnDate(newReturnDate: TDate): boolean;

// Returns the book rental datetime in seconds
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 All @@ -119,7 +119,7 @@ function TRental.getReturnDate(): TDate;
function TRental.setReturnDate(newReturnDate: TDate): boolean;
begin
Result := False;
if (newReturnDate <> NuLL) and (newReturnDate >= 0) then
if (newReturnDate = SQLNull) or (newReturnDate <> NuLL) and (newReturnDate >= 0) then
begin
self.return_date := newReturnDate;
Result := True;
Expand All @@ -143,11 +143,11 @@ function TRental.setRentalDate(newRentalDate: TDate): boolean;

constructor TRental.Create;
begin
self.id := -1;
self.book_id := -1;
self.student_id := -1;
self.return_date := -1;
self.rental_date := -1;
self.id := SQLNull;
self.book_id := SQLNull;
self.student_id := SQLNull;
self.return_date := SQLNull;
self.rental_date := SQLNull;
end;

end.
Expand Down
6 changes: 3 additions & 3 deletions lazarus/student.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
Classes, SysUtils;
Classes, SysUtils, DBConstants;

type
TStudent = class
Expand Down Expand Up @@ -168,11 +168,11 @@ function TStudent.setLDAPUser(newLDAPUser: string): boolean;

constructor TStudent.Create;
begin
self.id := -1;
self.id := SQLNull;
self.last_name := '';
self.first_name := '';
self.class_name := '';
self.birth := -1;
self.birth := SQLNull;
self.ldap_user := '';
end;

Expand Down

0 comments on commit e64e913

Please sign in to comment.