Skip to content

Commit 188d6ee

Browse files
committed
Throw standard exceptions from Prepare.
1 parent e0ea838 commit 188d6ee

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public override object ExecuteScalar()
7272

7373
public override void Prepare()
7474
{
75+
if (Connection == null)
76+
throw new InvalidOperationException("Connection property must be non-null.");
77+
if (Connection.State != ConnectionState.Open)
78+
throw new InvalidOperationException("Connection must be Open; current state is {0}".FormatInvariant(Connection.State));
79+
7580
// NOTE: Prepared statements in MySQL are not currently supported.
7681
// 1) Only a subset of statements are actually preparable by the server: http://dev.mysql.com/worklog/task/?id=2871
7782
// 2) Although CLIENT_MULTI_STATEMENTS is supposed to mean that the Server "Can handle multiple statements per COM_QUERY and COM_STMT_PREPARE" (https://dev.mysql.com/doc/internals/en/capability-flags.html#flag-CLIENT_MULTI_STATEMENTS),

tests/SideBySide/CommandTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ public void ExecuteReaderRequiresOpenConnection()
3939
}
4040
}
4141

42+
[Fact]
43+
public void PrepareRequiresConnection()
44+
{
45+
using (var command = new MySqlCommand())
46+
{
47+
Assert.Throws<InvalidOperationException>(() => command.Prepare());
48+
}
49+
}
50+
51+
[Fact]
52+
public void PrepareRequiresOpenConnection()
53+
{
54+
using (var connection = new MySqlConnection())
55+
using (var command = connection.CreateCommand())
56+
{
57+
Assert.Throws<InvalidOperationException>(() => command.Prepare());
58+
}
59+
}
60+
4261
readonly DatabaseFixture m_database;
4362
}
4463
}

0 commit comments

Comments
 (0)