1 /// Exceptions defined by mysql-native.
2 module mysql.exceptions;
3 
4 import std.algorithm;
5 import mysql.protocol.packets;
6 
7 /++
8 An exception type to distinguish exceptions thrown by this package.
9 +/
10 class MYX: Exception
11 {
12 @safe pure:
13 	this(string msg, string file = __FILE__, size_t line = __LINE__)
14 	{
15 		super(msg, file, line);
16 	}
17 }
18 
19 /++
20 The server sent back a MySQL error code and message. If the server is 4.1+,
21 there should also be an ANSI/ODBC-standard SQLSTATE error code.
22 
23 See_Also: $(LINK https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html)
24 +/
25 class MYXReceived: MYX
26 {
27 	ushort errorCode;
28 	char[5] sqlState;
29 
30 @safe pure:
31 
32 	this(OKErrorPacket okp, string file, size_t line)
33 	{
34 		this(okp.message, okp.serverStatus, okp.sqlState, file, line);
35 	}
36 
37 	this(string msg, ushort errorCode, char[5] sqlState, string file, size_t line)
38 	{
39 		this.errorCode = errorCode;
40 		this.sqlState = sqlState;
41 		super("MySQL error: " ~ msg, file, line);
42 	}
43 }
44 
45 /++
46 Received invalid data from the server which violates the MySQL network protocol.
47 (Quite possibly mysql-native's fault. Please
48 $(LINK2 https://github.com/mysql-d/mysql-native/issues, file an issue)
49 if you receive this.)
50 +/
51 class MYXProtocol: MYX
52 {
53 @safe pure:
54 	this(string msg, string file, size_t line)
55 	{
56 		super(msg, file, line);
57 	}
58 }
59 
60 /++
61 Deprecated - No longer thrown by mysql-native.
62 
63 In previous versions, this had been thrown when attempting to use a
64 prepared statement which had already been released.
65 
66 But as of v2.0.0, prepared statements are connection-independent and
67 automatically registered on connections as needed, so this exception
68 is no longer used.
69 +/
70 deprecated("No longer thrown by mysql-native. You can safely remove all handling of this exception from your code.")
71 class MYXNotPrepared: MYX
72 {
73 @safe pure:
74 	this(string file = __FILE__, size_t line = __LINE__)
75 	{
76 		super("The prepared statement has already been released.", file, line);
77 	}
78 }
79 
80 /++
81 Common base class of `MYXResultRecieved` and `MYXNoResultRecieved`.
82 
83 Thrown when making the wrong choice between `mysql.commands.exec` versus `mysql.commands.query`.
84 
85 The query functions (`mysql.commands.query`, `mysql.commands.queryRow`, etc.)
86 are for SQL statements such as SELECT that
87 return results (even if the result set has zero elements.)
88 
89 The `mysql.commands.exec` functions
90 are for SQL statements, such as INSERT, that never return result sets,
91 but may return `rowsAffected`.
92 
93 Using one of those functions, when the other should have been used instead,
94 results in an exception derived from this.
95 +/
96 class MYXWrongFunction: MYX
97 {
98 @safe pure:
99 	this(string msg, string file = __FILE__, size_t line = __LINE__)
100 	{
101 		super(msg, file, line);
102 	}
103 }
104 
105 /++
106 Thrown when a result set was returned unexpectedly.
107 
108 Use the query functions (`mysql.commands.query`, `mysql.commands.queryRow`, etc.),
109 not `mysql.commands.exec` for commands
110 that return result sets (such as SELECT), even if the result set has zero elements.
111 +/
112 class MYXResultRecieved: MYXWrongFunction
113 {
114 @safe pure:
115 	this(string file = __FILE__, size_t line = __LINE__)
116 	{
117 		super(
118 			"A result set was returned. Use the query functions, not exec, "~
119 			"for commands that return result sets.",
120 			file, line
121 		);
122 	}
123 }
124 
125 /++
126 Thrown when the executed query, unexpectedly, did not produce a result set.
127 
128 Use the `mysql.commands.exec` functions,
129 not `mysql.commands.query`/`mysql.commands.queryRow`/etc.
130 for commands that don't produce result sets (such as INSERT).
131 +/
132 class MYXNoResultRecieved: MYXWrongFunction
133 {
134 @safe pure:
135 	this(string msg, string file = __FILE__, size_t line = __LINE__)
136 	{
137 		super(
138 			"The executed query did not produce a result set. Use the exec "~
139 			"functions, not query, for commands that don't produce result sets.",
140 			file, line
141 		);
142 	}
143 }
144 
145 /++
146 Thrown when attempting to use a range that's been invalidated.
147 
148 This can occur when using a `mysql.result.ResultRange` after a new command
149 has been issued on the same connection.
150 +/
151 class MYXInvalidatedRange: MYX
152 {
153 @safe pure:
154 	this(string msg, string file = __FILE__, size_t line = __LINE__)
155 	{
156 		super(msg, file, line);
157 	}
158 }