1 | // |
2 | // MySQLException.cpp |
3 | // |
4 | // Library: SQL/MySQL |
5 | // Package: MySQL |
6 | // Module: MySQLException |
7 | // |
8 | // Copyright (c) 2008, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/SQL/MySQL/MySQLException.h" |
16 | #include <mysql.h> |
17 | #include <stdio.h> |
18 | |
19 | namespace Poco { |
20 | namespace SQL { |
21 | namespace MySQL { |
22 | |
23 | |
24 | MySQLException::MySQLException(const std::string& msg, int errCode) : |
25 | Poco::SQL::SQLException(std::string("[MySQL]: " ) + msg, errCode) |
26 | { |
27 | } |
28 | |
29 | |
30 | MySQLException::MySQLException(const MySQLException& exc) : |
31 | Poco::SQL::SQLException(exc) |
32 | { |
33 | } |
34 | |
35 | |
36 | MySQLException::~MySQLException() throw() |
37 | { |
38 | } |
39 | |
40 | |
41 | ///// |
42 | // |
43 | // ConnectionException |
44 | // |
45 | ///// |
46 | |
47 | |
48 | ConnectionException::ConnectionException(const std::string& msg) : MySQLException(msg) |
49 | { |
50 | } |
51 | |
52 | |
53 | ConnectionException::ConnectionException(const std::string& text, MYSQL* h) : |
54 | MySQLException(compose(text, h), mysql_errno(h)) |
55 | { |
56 | } |
57 | |
58 | |
59 | std::string ConnectionException::compose(const std::string& text, MYSQL* h) |
60 | { |
61 | std::string str; |
62 | str += "[Comment]: " ; |
63 | str += text; |
64 | str += "\t[mysql_error]: " ; |
65 | str += mysql_error(h); |
66 | |
67 | str += "\t[mysql_errno]: " ; |
68 | char buff[30]; |
69 | sprintf(buff, "%d" , mysql_errno(h)); |
70 | str += buff; |
71 | |
72 | str += "\t[mysql_sqlstate]: " ; |
73 | str += mysql_sqlstate(h); |
74 | return str; |
75 | } |
76 | |
77 | |
78 | ///// |
79 | // |
80 | // TransactionException |
81 | // |
82 | ///// |
83 | |
84 | |
85 | TransactionException::TransactionException(const std::string& msg) : ConnectionException(msg) |
86 | { |
87 | } |
88 | |
89 | |
90 | TransactionException::TransactionException(const std::string& text, MYSQL* h) : ConnectionException(text, h) |
91 | { |
92 | } |
93 | |
94 | |
95 | ///// |
96 | // |
97 | // StatementException |
98 | // |
99 | ///// |
100 | |
101 | |
102 | StatementException::StatementException(const std::string& msg) : MySQLException(msg) |
103 | { |
104 | } |
105 | |
106 | |
107 | StatementException::StatementException(const std::string& text, MYSQL_STMT* h, const std::string& stmt) : |
108 | MySQLException(compose(text, h, stmt), mysql_stmt_errno(h)) |
109 | { |
110 | } |
111 | |
112 | |
113 | std::string StatementException::compose(const std::string& text, MYSQL_STMT* h, const std::string& stmt) |
114 | { |
115 | std::string str; |
116 | str += "[Comment]: " ; |
117 | str += text; |
118 | |
119 | if (h != 0) |
120 | { |
121 | str += "\t[mysql_stmt_error]: " ; |
122 | str += mysql_stmt_error(h); |
123 | |
124 | str += "\t[mysql_stmt_errno]: " ; |
125 | char buff[30]; |
126 | sprintf(buff, "%d" , mysql_stmt_errno(h)); |
127 | str += buff; |
128 | |
129 | str += "\t[mysql_stmt_sqlstate]: " ; |
130 | str += mysql_stmt_sqlstate(h); |
131 | } |
132 | |
133 | if (stmt.length() > 0) |
134 | { |
135 | str += "\t[statement]: " ; |
136 | str += stmt; |
137 | } |
138 | |
139 | return str; |
140 | } |
141 | |
142 | } } } // namespace Poco::SQL::MySQL |
143 | |