1 /// Internal - Protocol-related data types.
2 module mysql.protocol.extra_types;
3 
4 import std.exception;
5 import std.variant;
6 
7 import mysql.commands;
8 import mysql.exceptions;
9 import mysql.protocol.sockets;
10 import mysql.result;
11 
12 struct SQLValue
13 {
14 	bool isNull;
15 	bool isIncomplete;
16 	Variant _value;
17 
18 	// empty template as a template and non-template won't be added to the same overload set
19 	@property inout(Variant) value()() inout
20 	{
21 		enforceEx!MYX(!isNull, "SQL value is null");
22 		enforceEx!MYX(!isIncomplete, "SQL value not complete");
23 		return _value;
24 	}
25 
26 	@property void value(T)(T value)
27 	{
28 		enforceEx!MYX(!isNull, "SQL value is null");
29 		enforceEx!MYX(!isIncomplete, "SQL value not complete");
30 		_value = value;
31 	}
32 
33 	pure const nothrow invariant()
34 	{
35 		isNull && assert(!isIncomplete);
36 		isIncomplete && assert(!isNull);
37 	}
38 }
39 
40 
41 /// Length Coded Binary Value
42 struct LCB
43 {
44 	/// True if the `LCB` contains a null value
45 	bool isNull;
46 
47 	/// True if the packet that created this `LCB` didn't have enough bytes
48 	/// to store a value of the size specified. More bytes have to be fetched from the server.
49 	bool isIncomplete;
50 
51 	/// Number of bytes needed to store the value (Extracted from the LCB header. The header byte is not included)
52 	ubyte numBytes;
53 
54 	/// Number of bytes total used for this `LCB`
55 	@property ubyte totalBytes() pure const nothrow
56 	{
57 		return cast(ubyte)(numBytes <= 1 ? 1 : numBytes+1);
58 	}
59 
60 	/// The decoded value. This is always 0 if `isNull` or `isIncomplete` is set.
61 	ulong value;
62 
63 	pure const nothrow invariant()
64 	{
65 		if(isIncomplete)
66 		{
67 			assert(!isNull);
68 			assert(value == 0);
69 			assert(numBytes > 0);
70 		}
71 		else if(isNull)
72 		{
73 			assert(!isIncomplete);
74 			assert(value == 0);
75 			assert(numBytes == 0);
76 		}
77 		else
78 		{
79 			assert(!isNull);
80 			assert(!isIncomplete);
81 			assert(numBytes > 0);
82 		}
83 	}
84 }
85 
86 /// Length Coded String
87 ///
88 /// Dummy struct just to tell what value we are using.
89 /// We don't need to store anything here as the result is always a string.
90 struct LCS
91 {
92 	// Nothing
93 }