1 /// Structures for MySQL types not built-in to D/Phobos. 2 module mysql.types; 3 4 import std.datetime; 5 import mysql.exceptions; 6 7 /++ 8 A simple struct to represent time difference. 9 10 D's std.datetime does not have a type that is closely compatible with the MySQL 11 interpretation of a time difference, so we define a struct here to hold such 12 values. 13 +/ 14 struct TimeDiff 15 { 16 bool negative; 17 int days; 18 ubyte hours, minutes, seconds; 19 } 20 21 /++ 22 A D struct to stand for a TIMESTAMP 23 24 It is assumed that insertion of TIMESTAMP values will not be common, since in general, 25 such columns are used for recording the time of a row insertion, and are filled in 26 automatically by the server. If you want to force a timestamp value in a prepared insert, 27 set it into a timestamp struct as an unsigned long in the format YYYYMMDDHHMMSS 28 and use that for the appropriate parameter. When TIMESTAMPs are retrieved as part of 29 a result set it will be as DateTime structs. 30 +/ 31 struct Timestamp 32 { 33 ulong rep; 34 } 35 36 /++ 37 In certain circumstances, MySQL permits storing invalid dates (such as the 38 "zero date": 0000-00-00). Phobos's Date/DateTime disallows invalid dates, so 39 the MySQLDate and MySQLDateTime type allow reading such dates when they occur. 40 +/ 41 struct MySQLDate 42 { 43 int year; 44 int month; 45 int day; 46 47 private void throwInvalidDate() pure 48 { 49 throw new MYXInvalidatedDate(this); 50 } 51 52 @property Date getDate() pure 53 { 54 if(year < 1 || month < 1 || day < 1 || month > 12 || day > 31) 55 throwInvalidDate(); 56 57 try 58 return Date(year, month, day); 59 catch(DateTimeException e) 60 throwInvalidDate(); 61 62 assert(0); 63 } 64 65 string toString() pure 66 { 67 import std.format; 68 return format("%04s-%02s-%02s", year, month, day); 69 } 70 71 //alias getDate this; 72 } 73 74 ///ditto 75 struct MySQLDateTime 76 { 77 int year; 78 int month; 79 int day; 80 81 int hour; 82 int minute; 83 int second; 84 85 @property DateTime getDateTime() pure 86 { 87 // Ensure date is valid 88 MySQLDate(year, month, day).getDate(); 89 90 return DateTime(year, month, day, hour, minute, second); 91 } 92 93 //alias getDateTime this; 94 }