|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using MySql.Data.MySqlClient; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace SideBySide |
| 7 | +{ |
| 8 | + public class PreparedCommandTests : IClassFixture<DatabaseFixture> |
| 9 | + { |
| 10 | + public PreparedCommandTests(DatabaseFixture database) |
| 11 | + { |
| 12 | + } |
| 13 | + |
| 14 | + [Theory] |
| 15 | + [MemberData(nameof(GetInsertAndQueryData))] |
| 16 | + public void InsertAndQuery(bool isPrepared, string dataType, object dataValue) |
| 17 | + { |
| 18 | + var csb = new MySqlConnectionStringBuilder(AppConfig.ConnectionString) |
| 19 | + { |
| 20 | + IgnorePrepare = !isPrepared, |
| 21 | + }; |
| 22 | + using (var connection = new MySqlConnection(csb.ConnectionString)) |
| 23 | + { |
| 24 | + connection.Open(); |
| 25 | + using (var command = new MySqlCommand($@"DROP TABLE IF EXISTS prepared_command_test; |
| 26 | +CREATE TABLE prepared_command_test(rowid INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, data {dataType});", connection)) |
| 27 | + { |
| 28 | + command.ExecuteNonQuery(); |
| 29 | + } |
| 30 | + |
| 31 | + using (var command = new MySqlCommand("INSERT INTO prepared_command_test(data) VALUES(@null), (@data);", connection)) |
| 32 | + { |
| 33 | + command.Parameters.AddWithValue("@null", null); |
| 34 | + command.Parameters.AddWithValue("@data", dataValue); |
| 35 | + if (isPrepared) |
| 36 | + command.Prepare(); |
| 37 | + Assert.Equal(isPrepared, command.IsPrepared); |
| 38 | + command.ExecuteNonQuery(); |
| 39 | + } |
| 40 | + |
| 41 | + using (var command = new MySqlCommand("SELECT data FROM prepared_command_test ORDER BY rowid;", connection)) |
| 42 | + { |
| 43 | + if (isPrepared) |
| 44 | + command.Prepare(); |
| 45 | + Assert.Equal(isPrepared, command.IsPrepared); |
| 46 | + |
| 47 | + using (var reader = command.ExecuteReader()) |
| 48 | + { |
| 49 | + Assert.True(reader.Read()); |
| 50 | + Assert.True(reader.IsDBNull(0)); |
| 51 | + |
| 52 | + Assert.True(reader.Read()); |
| 53 | + Assert.False(reader.IsDBNull(0)); |
| 54 | + Assert.Equal(dataValue, reader.GetValue(0)); |
| 55 | + |
| 56 | + Assert.False(reader.Read()); |
| 57 | + Assert.False(reader.NextResult()); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static IEnumerable<object[]> GetInsertAndQueryData() |
| 64 | + { |
| 65 | + foreach (var isPrepared in new[] { false, true }) |
| 66 | + { |
| 67 | + yield return new object[] { isPrepared, "TINYINT", (sbyte) -123 }; |
| 68 | + yield return new object[] { isPrepared, "TINYINT UNSIGNED", (byte) 123 }; |
| 69 | + yield return new object[] { isPrepared, "SMALLINT", (short) -12345 }; |
| 70 | + yield return new object[] { isPrepared, "SMALLINT UNSIGNED", (ushort) 12345 }; |
| 71 | + yield return new object[] { isPrepared, "MEDIUMINT", -1234567 }; |
| 72 | + yield return new object[] { isPrepared, "MEDIUMINT UNSIGNED", 1234567u }; |
| 73 | + yield return new object[] { isPrepared, "INT", -123456789 }; |
| 74 | + yield return new object[] { isPrepared, "INT UNSIGNED", 123456789u }; |
| 75 | + yield return new object[] { isPrepared, "BIGINT", -1234567890123456789L }; |
| 76 | + yield return new object[] { isPrepared, "BIGINT UNSIGNED", 1234567890123456789UL }; |
| 77 | + yield return new object[] { isPrepared, "BIT(10)", 1000UL }; |
| 78 | + yield return new object[] { isPrepared, "BINARY(5)", new byte[] { 5, 6, 7, 8, 9 } }; |
| 79 | + yield return new object[] { isPrepared, "VARBINARY(100)", new byte[] { 7, 8, 9, 10 } }; |
| 80 | + yield return new object[] { isPrepared, "BLOB", new byte[] { 5, 4, 3, 2, 1 } }; |
| 81 | + yield return new object[] { isPrepared, "CHAR(36)", new Guid("00112233-4455-6677-8899-AABBCCDDEEFF") }; |
| 82 | + yield return new object[] { isPrepared, "FLOAT", 12.375f }; |
| 83 | + yield return new object[] { isPrepared, "DOUBLE", 14.21875 }; |
| 84 | + yield return new object[] { isPrepared, "DECIMAL(9,3)", 123.45m }; |
| 85 | + yield return new object[] { isPrepared, "VARCHAR(100)", "test;@'; -- " }; |
| 86 | + yield return new object[] { isPrepared, "TEXT", "testing testing" }; |
| 87 | + yield return new object[] { isPrepared, "DATE", new DateTime(2018, 7, 23) }; |
| 88 | + yield return new object[] { isPrepared, "DATETIME(3)", new DateTime(2018, 7, 23, 20, 46, 52, 123) }; |
| 89 | + yield return new object[] { isPrepared, "ENUM('small', 'medium', 'large')", "medium" }; |
| 90 | + yield return new object[] { isPrepared, "SET('one','two','four','eight')", "two,eight" }; |
| 91 | + |
| 92 | +#if !BASELINE |
| 93 | + // https://bugs.mysql.com/bug.php?id=78917 |
| 94 | + yield return new object[] { isPrepared, "BOOL", true }; |
| 95 | + |
| 96 | + // https://bugs.mysql.com/bug.php?id=91770 |
| 97 | + yield return new object[] { isPrepared, "TIME(3)", TimeSpan.Zero.Subtract(new TimeSpan(15, 10, 34, 56, 789)) }; |
| 98 | + |
| 99 | + // https://bugs.mysql.com/bug.php?id=91751 |
| 100 | + yield return new object[] { isPrepared, "YEAR", 2134 }; |
| 101 | +#endif |
| 102 | + |
| 103 | + if (AppConfig.SupportsJson) |
| 104 | + yield return new object[] { isPrepared, "JSON", "{\"test\": true}" }; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments