Skip to content

Commit

Permalink
outsourced SQL Null constant into DBConstants class
Browse files Browse the repository at this point in the history
#116 Which DateTime is nil?
  • Loading branch information
cnmicha committed Mar 8, 2017
1 parent 9207b8e commit 381164f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 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 @@ -423,7 +423,7 @@ function TDBConnection.updateInsertStudent(student: TStudent): boolean;
FieldByName('first_name').AsString := student.getFirstName;
FieldByName('class_name').AsString := student.getClassName;

if student.getBirth <= -1 then //if set to null
if student.getBirth = SQLNull then //if set to null
FieldByName('birth').Clear
else
FieldByName('birth').AsDateTime := student.getBirth;
Expand Down Expand Up @@ -590,7 +590,7 @@ function TDBConnection.updateInsertRental(rental: TRental): boolean;
FieldByName('book_id').AsLargeInt := rental.getBookId;
FieldByName('rental_date').AsDateTime := rental.getRentalDate;

if rental.getReturnDate <= -1 then //if set to null
if rental.getReturnDate = SQLNull then //if set to null
FieldByName('return_date').Clear
else
FieldByName('return_date').AsDateTime := rental.getReturnDate;
Expand Down
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.
6 changes: 3 additions & 3 deletions lazarus/rental.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
TRental = class
Expand Down Expand Up @@ -146,8 +146,8 @@ constructor TRental.Create;
self.id := -1;
self.book_id := -1;
self.student_id := -1;
self.return_date := -1; // -1 or smaller equals NULL
self.rental_date := -1;
self.return_date := SQLNull;
self.rental_date := SQLNull;
end;

end.
Expand Down
4 changes: 2 additions & 2 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 @@ -172,7 +172,7 @@ constructor TStudent.Create;
self.last_name := '';
self.first_name := '';
self.class_name := '';
self.birth := -1; // -1 or smaller equals NULL
self.birth := SQLNull;
self.ldap_user := '';
end;

Expand Down

0 comments on commit 381164f

Please sign in to comment.