|
| 1 | +using System.Globalization; |
| 2 | + |
1 | 3 | namespace SideBySide;
|
2 | 4 |
|
3 | 5 | public class DataAdapterTests : IClassFixture<DatabaseFixture>, IDisposable
|
@@ -235,5 +237,109 @@ public void BatchInsert()
|
235 | 237 | Assert.Equal(new[] { null, "", "one", "two", "three", "four" }, m_connection.Query<string>("SELECT text_value FROM data_adapter ORDER BY id"));
|
236 | 238 | }
|
237 | 239 |
|
| 240 | +#if !BASELINE |
| 241 | + [Theory] |
| 242 | + [InlineData("INSERT INTO table(col1, col2) VALUES(@col1, @col2);", "@col1,@col2", "0,1")] |
| 243 | + [InlineData("INSERT INTO table(col1, col2) VALUES(@col1, @col2);", "@col2,@col1", "1,0")] |
| 244 | + [InlineData("INSERT INTO table(col1, col2) VALUES(@col1, @col2);", "@col1,@col2,@col3", "0,1")] |
| 245 | + [InlineData("INSERT INTO table(col1, col2) VALUES(@col2, @col3);", "@col1,@col2,@col3", "1,2")] |
| 246 | + [InlineData("INSERT INTO table(col1, col2) VALUES(?, ?);", "@col1,@col2", "0,1")] |
| 247 | + [InlineData("INSERT INTO table(col1, col2)\nVALUES(@col1, @col2);", "@col1,@col2", "0,1")] |
| 248 | + public void ExtractParameterIndexes(string sql, string parameterNames, string expectedIndexes) |
| 249 | + { |
| 250 | + var command = new MySqlCommand(sql, m_connection); |
| 251 | + foreach (var parameterName in parameterNames.Split(',')) |
| 252 | + command.Parameters.Add(new MySqlParameter(parameterName, MySqlDbType.Int32)); |
| 253 | + var parser = new MySqlDataAdapter.InsertSqlParser(command); |
| 254 | + parser.Parse(sql); |
| 255 | + Assert.Equal(expectedIndexes.Split(',').Select(x => int.Parse(x, CultureInfo.InvariantCulture)), parser.ParameterIndexes); |
| 256 | + } |
| 257 | + |
| 258 | + [Theory] |
| 259 | + [InlineData("SELECT * FROM table;", 2, 2, null)] |
| 260 | + [InlineData("SELECT * FROM table VALUES;", 2, 2, null)] |
| 261 | + [InlineData("INSERT INTO table VALUES(@param0, @param1)", 2, 2, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3);")] |
| 262 | + [InlineData("INSERT INTO table VALUES(@param0, @param1)", 3, 2, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3),(@p4,@p5);")] |
| 263 | + [InlineData("INSERT INTO table VALUES(@param0, @param1)", 2, 3, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3);")] |
| 264 | + [InlineData("INSERT INTO table VALUES(@param0, @param1, @param2)", 2, 3, "INSERT INTO table VALUES(@p0,@p1,@p2),(@p3,@p4,@p5);")] |
| 265 | + [InlineData("INSERT INTO table VALUES(@param0, @param1);", 2, 2, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3);")] |
| 266 | + [InlineData("INSERT INTO table VALUES(?, ?)", 2, 2, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3);")] |
| 267 | + [InlineData("INSERT INTO table VALUES ( @param0 , \n @param1 ) ; ", 2, 2, "INSERT INTO table VALUES(@p0,@p1),(@p2,@p3);")] |
| 268 | + [InlineData("INSERT INTO table VALUES(@param0, @param2);", 2, 2, null)] |
| 269 | + [InlineData("INSERT INTO table\nVALUES\n(@param0, @param1);", 2, 2, "INSERT INTO table\nVALUES(@p0,@p1),(@p2,@p3);")] |
| 270 | + [InlineData("INSERT INTO `table` VALUES (@\"param0\", @\"param1\");", 2, 2, "INSERT INTO `table` VALUES(@p0,@p1),(@p2,@p3);")] |
| 271 | + [InlineData("INSERT\nINTO\ntable\nVALUES\n(@param0, @param1);", 2, 2, null)] // ideally should work but \n not supported |
| 272 | + [InlineData("INSERT INTO table VALUES(1, @param0, @param1);", 2, 2, null)] |
| 273 | + public void ConvertBatchToCommand(string insertSql, int commandCount, int parameterCount, string expected) |
| 274 | + { |
| 275 | + var batch = new MySqlBatch(m_connection); |
| 276 | + for (var i = 0; i < commandCount; i++) |
| 277 | + { |
| 278 | + var batchCommand = new MySqlBatchCommand(insertSql); |
| 279 | + for (var j = 0; j < parameterCount; j++) |
| 280 | + { |
| 281 | + batchCommand.Parameters.Add(new MySqlParameter($"@param{j}", MySqlDbType.Int32)); |
| 282 | + } |
| 283 | + batch.BatchCommands.Add(batchCommand); |
| 284 | + } |
| 285 | + |
| 286 | + var command = MySqlDataAdapter.TryConvertToCommand(batch); |
| 287 | + if (expected is null) |
| 288 | + { |
| 289 | + Assert.Null(command); |
| 290 | + } |
| 291 | + else |
| 292 | + { |
| 293 | + Assert.NotNull(command); |
| 294 | + Assert.Equal(expected, command.CommandText); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + [Fact] |
| 299 | + public void ConvertBatchToCommandParameters() |
| 300 | + { |
| 301 | + var insertSql = "INSERT INTO table(col1, col2, col3) VALUES(@c1, @c2, @c1);"; |
| 302 | + var batch = new MySqlBatch(m_connection) |
| 303 | + { |
| 304 | + BatchCommands = |
| 305 | + { |
| 306 | + new MySqlBatchCommand(insertSql) |
| 307 | + { |
| 308 | + Parameters = |
| 309 | + { |
| 310 | + new MySqlParameter("@c1", 1), |
| 311 | + new MySqlParameter("@c2", 2), |
| 312 | + }, |
| 313 | + }, |
| 314 | + new MySqlBatchCommand(insertSql) |
| 315 | + { |
| 316 | + Parameters = |
| 317 | + { |
| 318 | + new MySqlParameter("@c1", 3), |
| 319 | + new MySqlParameter("@c2", 4), |
| 320 | + }, |
| 321 | + }, |
| 322 | + }, |
| 323 | + }; |
| 324 | + |
| 325 | + var command = MySqlDataAdapter.TryConvertToCommand(batch); |
| 326 | + Assert.NotNull(command); |
| 327 | + Assert.Equal("INSERT INTO table(col1, col2, col3) VALUES(@p0,@p1,@p2),(@p3,@p4,@p5);", command.CommandText); |
| 328 | + Assert.Equal(6, command.Parameters.Count); |
| 329 | + Assert.Equal("@p0", command.Parameters[0].ParameterName); |
| 330 | + Assert.Equal(1, command.Parameters[0].Value); |
| 331 | + Assert.Equal("@p1", command.Parameters[1].ParameterName); |
| 332 | + Assert.Equal(2, command.Parameters[1].Value); |
| 333 | + Assert.Equal("@p2", command.Parameters[2].ParameterName); |
| 334 | + Assert.Equal(1, command.Parameters[2].Value); |
| 335 | + Assert.Equal("@p3", command.Parameters[3].ParameterName); |
| 336 | + Assert.Equal(3, command.Parameters[3].Value); |
| 337 | + Assert.Equal("@p4", command.Parameters[4].ParameterName); |
| 338 | + Assert.Equal(4, command.Parameters[4].Value); |
| 339 | + Assert.Equal("@p5", command.Parameters[5].ParameterName); |
| 340 | + Assert.Equal(3, command.Parameters[5].Value); |
| 341 | + } |
| 342 | +#endif |
| 343 | + |
238 | 344 | readonly MySqlConnection m_connection;
|
239 | 345 | }
|
0 commit comments