| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- // OdbcCommandBuilderTest.cs - NUnit Test Cases for testing the
- // OdbcCommandBuilder Test.
- //
- // Authors:
- // Sureshkumar T ([email protected])
- //
- // Copyright (c) 2004 Novell Inc., and the individuals listed on the
- // ChangeLog entries.
- //
- //
- // Permission is hereby granted, free of charge, to any person
- // obtaining a copy of this software and associated documentation
- // files (the "Software"), to deal in the Software without
- // restriction, including without limitation the rights to use, copy,
- // modify, merge, publish, distribute, sublicense, and/or sell copies
- // of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- // SOFTWARE.
- using System;
- using System.Data;
- using System.Data.Common;
- using System.Data.Odbc;
- using NUnit.Framework;
- namespace MonoTests.System.Data.Connected.Odbc
- {
- [TestFixture]
- [Category ("odbc")]
- public class OdbcCommandBuilderTest
- {
- [Test]
- public void GetInsertCommandTest ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb;
-
- cb = new OdbcCommandBuilder (da);
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
- cmd.CommandText, "#A1");
- Assert.AreSame (conn, cmd.Connection, "#A2");
- AssertInsertParameters (cmd, "#A3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual ("INSERT INTO \"employee (\"id, \"lname) VALUES (?, ?)",
- cmd.CommandText, "#B1");
- Assert.AreSame (conn, cmd.Connection, "#B2");
- AssertInsertParameters (cmd, "#B3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuoteSuffix = "´";
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual ("INSERT INTO employee´ (id´, lname´) VALUES (?, ?)",
- cmd.CommandText, "#C1");
- Assert.AreSame (conn, cmd.Connection, "#C2");
- AssertInsertParameters (cmd, "#C3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cb.QuoteSuffix = "´";
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual ("INSERT INTO \"employee´ (\"id´, \"lname´) VALUES (?, ?)",
- cmd.CommandText, "#D1");
- Assert.AreSame (conn, cmd.Connection, "#D2");
- AssertInsertParameters (cmd, "#D3:");
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetInsertCommandTestWithExpression ()
- {
- if (ConnectionManager.Instance.Odbc.EngineConfig.Type == EngineType.MySQL)
- Assert.Ignore ("Schema info from MySQL is incomplete");
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertInsertParameters (cmd, "#3:");
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetUpdateCommandTest ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#A1");
- Assert.AreSame (conn, cmd.Connection, "#A2");
- AssertUpdateParameters (cmd, "#A3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual ("UPDATE \"employee SET \"id = ?, \"lname = ? WHERE ((\"id = ?) AND ((? = 1 AND \"lname IS NULL) OR (\"lname = ?)))",
- cmd.CommandText, "#B1");
- Assert.AreSame (conn, cmd.Connection, "#B2");
- AssertUpdateParameters (cmd, "#B3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuoteSuffix = "´";
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual ("UPDATE employee´ SET id´ = ?, lname´ = ? WHERE ((id´ = ?) AND ((? = 1 AND lname´ IS NULL) OR (lname´ = ?)))",
- cmd.CommandText, "#C1");
- Assert.AreSame (conn, cmd.Connection, "#C2");
- AssertUpdateParameters (cmd, "#C3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cb.QuoteSuffix = "´";
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual ("UPDATE \"employee´ SET \"id´ = ?, \"lname´ = ? WHERE ((\"id´ = ?) AND ((? = 1 AND \"lname´ IS NULL) OR (\"lname´ = ?)))",
- cmd.CommandText, "#D1");
- Assert.AreSame (conn, cmd.Connection, "#D2");
- AssertUpdateParameters (cmd, "#D3:");
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("FIXME: Auto SQL generation during Update requires a valid SelectCommand")]
- public void GetUpdateCommandDBConcurrencyExceptionTest ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- Assert.IsNotNull (cb);
- DataRow [] rows = ds.Tables [0].Select ("id=1");
- rows [0] [0] = 6660; // non existent
- ds.Tables [0].AcceptChanges (); // moves 6660 to original value
- rows [0] [0] = 1; // moves 6660 as search key into db table
- try {
- da.Update (rows);
- Assert.Fail ("#1");
- } catch (DBConcurrencyException ex) {
- // Concurrency violation: the UpdateCommand
- // affected 0 records
- Assert.AreEqual (typeof (DBConcurrencyException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreSame (rows [0], ex.Row, "#5");
- Assert.AreEqual (1, ex.RowCount, "#6");
- }
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Category("NotWorking")]
- public void GetInsertCommandTest_option_true ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetInsertCommand (true);
- Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertInsertParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetInsertCommandTest_option_false ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetInsertCommand (false);
- Assert.AreEqual ("INSERT INTO employee (id, lname) VALUES (?, ?)",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertInsertParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Category("NotWorking")]
- public void GetUpdateCommandTest_option_true ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetUpdateCommand (true);
- Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertUpdateParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetUpdateCommandTest_option_false ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetUpdateCommand (false);
- Assert.AreEqual ("UPDATE employee SET id = ?, lname = ? WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertUpdateParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Category("NotWorking")]
- public void GetDeleteCommandTest_option_true ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetDeleteCommand (true);
- Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertDeleteParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetDeleteCommandTest_option_false ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- OdbcCommand cmd = cb.GetDeleteCommand (false);
- Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#1");
- Assert.AreSame (conn, cmd.Connection, "#2");
- AssertDeleteParameters (cmd, "#3:");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- public void GetDeleteCommandTest ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname, id+1 as next_id from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- Assert.AreEqual (1, ds.Tables.Count);
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual ("DELETE FROM employee WHERE ((id = ?) AND ((? = 1 AND lname IS NULL) OR (lname = ?)))",
- cmd.CommandText, "#A1");
- Assert.AreSame (conn, cmd.Connection, "#A2");
- AssertDeleteParameters (cmd, "#A3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual ("DELETE FROM \"employee WHERE ((\"id = ?) AND ((? = 1 AND \"lname IS NULL) OR (\"lname = ?)))",
- cmd.CommandText, "#B1");
- Assert.AreSame (conn, cmd.Connection, "#B2");
- AssertDeleteParameters (cmd, "#B3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuoteSuffix = "´";
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual ("DELETE FROM employee´ WHERE ((id´ = ?) AND ((? = 1 AND lname´ IS NULL) OR (lname´ = ?)))",
- cmd.CommandText, "#C1");
- Assert.AreSame (conn, cmd.Connection, "#C2");
- AssertDeleteParameters (cmd, "#C3:");
- cb = new OdbcCommandBuilder (da);
- cb.QuotePrefix = "\"";
- cb.QuoteSuffix = "´";
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual ("DELETE FROM \"employee´ WHERE ((\"id´ = ?) AND ((? = 1 AND \"lname´ IS NULL) OR (\"lname´ = ?)))",
- cmd.CommandText, "#D1");
- Assert.AreSame (conn, cmd.Connection, "#D2");
- AssertDeleteParameters (cmd, "#D3:");
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuotePrefix_DeleteCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
- try {
- cb.QuotePrefix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
- cb.RefreshSchema ();
- cb.QuotePrefix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuotePrefix_InsertCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
- try {
- cb.QuotePrefix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
- cb.RefreshSchema ();
- cb.QuotePrefix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuotePrefix_UpdateCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#1");
- try {
- cb.QuotePrefix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
- cb.RefreshSchema ();
- cb.QuotePrefix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuoteSuffix_DeleteCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetDeleteCommand ();
- Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
- try {
- cb.QuoteSuffix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#6");
- cb.RefreshSchema ();
- cb.QuoteSuffix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuoteSuffix_InsertCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetInsertCommand ();
- Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
- try {
- cb.QuoteSuffix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#6");
- cb.RefreshSchema ();
- cb.QuoteSuffix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test]
- [Ignore ("QuoteSuffix and QuotePrefix are now in DbCommandBuilder, while commands are in implementation classes. Result: we cannot perform this check until we refactor this.")]
- public void QuoteSuffix_UpdateCommand_Generated ()
- {
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- OdbcCommand cmd = null;
- try {
- string selectQuery = "select id, lname from employee where id = 3";
- OdbcDataAdapter da = new OdbcDataAdapter (selectQuery, conn);
- DataSet ds = new DataSet ();
- da.Fill (ds, "IntTest");
- OdbcCommandBuilder cb = new OdbcCommandBuilder (da);
- cmd = cb.GetUpdateCommand ();
- Assert.AreEqual (string.Empty, cb.QuoteSuffix, "#1");
- try {
- cb.QuoteSuffix = "";
- Assert.Fail ("#2");
- } catch (InvalidOperationException ex) {
- // The QuotePrefix and QuoteSuffix properties
- // cannot be changed once an Insert, Update, or
- // Delete command has been generated
- Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- Assert.AreEqual (string.Empty, cb.QuotePrefix, "#6");
- cb.RefreshSchema ();
- cb.QuoteSuffix = "";
- } finally {
- if (cmd != null)
- cmd.Dispose ();
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- [Test] // QuoteIdentifier (String, OdbcConnection)
- public void QuoteIdentifier2 ()
- {
- OdbcCommandBuilder cb;
- OdbcConnection conn = ConnectionManager.Instance.Odbc.Connection;
- string quote_char = ConnectionManager.Instance.Odbc.EngineConfig.QuoteCharacter;
- try {
- cb = new OdbcCommandBuilder ();
- Assert.AreEqual (quote_char + "mono" + quote_char, cb.QuoteIdentifier ("mono", conn), "#A1");
- Assert.AreEqual (quote_char + "Z" + quote_char, cb.QuoteIdentifier ("Z", conn), "#A2");
- Assert.AreEqual (quote_char + "abc" + quote_char, cb.QuoteIdentifier ("abc", conn), "#A3");
- Assert.AreEqual (quote_char + quote_char, cb.QuoteIdentifier (string.Empty, conn), "#A4");
- Assert.AreEqual (quote_char + " " + quote_char, cb.QuoteIdentifier (" ", conn), "#A5");
- Assert.AreEqual (quote_char + "\r" + quote_char, cb.QuoteIdentifier ("\r", conn), "#A6");
- cb.QuoteSuffix = "def";
- Assert.AreEqual (quote_char + "mono" + quote_char, cb.QuoteIdentifier ("mono", conn), "#A7");
- Assert.AreEqual (quote_char + "Z" + quote_char, cb.QuoteIdentifier ("Z", conn), "#A8");
- Assert.AreEqual (quote_char + "abc" + quote_char, cb.QuoteIdentifier ("abc", conn), "#A9");
- Assert.AreEqual (quote_char + quote_char, cb.QuoteIdentifier (string.Empty, conn), "#A10");
- Assert.AreEqual (quote_char + " " + quote_char, cb.QuoteIdentifier (" ", conn), "#A11");
- Assert.AreEqual (quote_char + "\r" + quote_char, cb.QuoteIdentifier ("\r", conn), "#A12");
- cb = new OdbcCommandBuilder ();
- cb.QuotePrefix = "abc";
- Assert.AreEqual ("abcmono", cb.QuoteIdentifier ("mono", conn), "#B1");
- Assert.AreEqual ("abcZ", cb.QuoteIdentifier ("Z", conn), "#B2");
- Assert.AreEqual ("abcabc", cb.QuoteIdentifier ("abc", conn), "#B3");
- Assert.AreEqual ("abc", cb.QuoteIdentifier (string.Empty, conn), "#B4");
- Assert.AreEqual ("abc ", cb.QuoteIdentifier (" ", conn), "#B5");
- Assert.AreEqual ("abc\r", cb.QuoteIdentifier ("\r", conn), "#B6");
- cb.QuoteSuffix = "def";
- Assert.AreEqual ("abcmonodef", cb.QuoteIdentifier ("mono", conn), "#B7");
- Assert.AreEqual ("abcZdef", cb.QuoteIdentifier ("Z", conn), "#B8");
- Assert.AreEqual ("abcabcdef", cb.QuoteIdentifier ("abc", conn), "#B9");
- Assert.AreEqual ("abcdef", cb.QuoteIdentifier (string.Empty, conn), "#B10");
- Assert.AreEqual ("abc def", cb.QuoteIdentifier (" ", conn), "#B11");
- Assert.AreEqual ("abc\rdef", cb.QuoteIdentifier ("\r", conn), "#B12");
- cb.QuotePrefix = string.Empty;
- cb = new OdbcCommandBuilder ();
- cb.QuotePrefix = "X";
- Assert.AreEqual ("Xmono", cb.QuoteIdentifier ("mono", conn), "#D1");
- Assert.AreEqual ("XZ", cb.QuoteIdentifier ("Z", conn), "#D2");
- Assert.AreEqual ("XX", cb.QuoteIdentifier ("X", conn), "#D3");
- Assert.AreEqual ("X", cb.QuoteIdentifier (string.Empty, conn), "#D4");
- Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D5");
- Assert.AreEqual ("X\r", cb.QuoteIdentifier ("\r", conn), "#D6");
- cb.QuoteSuffix = " ";
- Assert.AreEqual ("Xmono ", cb.QuoteIdentifier ("mono", conn), "#D7");
- Assert.AreEqual ("XZ ", cb.QuoteIdentifier ("Z", conn), "#D8");
- Assert.AreEqual ("XX ", cb.QuoteIdentifier ("X", conn), "#D9");
- Assert.AreEqual ("X ", cb.QuoteIdentifier (string.Empty, conn), "#D10");
- Assert.AreEqual ("X ", cb.QuoteIdentifier (" ", conn), "#D11");
- Assert.AreEqual ("X\r ", cb.QuoteIdentifier ("\r", conn), "#D12");
- cb = new OdbcCommandBuilder ();
- cb.QuotePrefix = " ";
- Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E1");
- Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E2");
- Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E3");
- Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E4");
- Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E5");
- Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E6");
- cb.QuoteSuffix = "def";
- Assert.AreEqual ("mono", cb.QuoteIdentifier ("mono", conn), "#E7");
- Assert.AreEqual ("Z", cb.QuoteIdentifier ("Z", conn), "#E8");
- Assert.AreEqual ("abc", cb.QuoteIdentifier ("abc", conn), "#E9");
- Assert.AreEqual (string.Empty, cb.QuoteIdentifier (string.Empty, conn), "#E10");
- Assert.AreEqual (" ", cb.QuoteIdentifier (" ", conn), "#E11");
- Assert.AreEqual ("\r", cb.QuoteIdentifier ("\r", conn), "#E12");
- } finally {
- ConnectionManager.Instance.Odbc.CloseConnection ();
- }
- }
- void AssertInsertParameters (OdbcCommand cmd, string prefix)
- {
- Assert.AreEqual (2, cmd.Parameters.Count, prefix + "Count");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
- Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
- Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
- Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
- Assert.AreEqual (DbType.String, cmd.Parameters [1].DbType, prefix + "DbType (1)");
- Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
- Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
- Assert.IsNull (cmd.Parameters [1].Value, prefix + "Value (1)");
- }
- void AssertUpdateParameters (OdbcCommand cmd, string prefix)
- {
- Assert.AreEqual (5, cmd.Parameters.Count, prefix + "Count");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
- Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
- Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
- Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
- Assert.AreEqual (DbType.String, cmd.Parameters [1].DbType, prefix + "DbType (1)");
- Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
- Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
- Assert.IsNull (cmd.Parameters [1].Value, prefix + "Value (1)");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [2].DbType, prefix + "DbType (2)");
- Assert.AreEqual ("p3", cmd.Parameters [2].ParameterName, prefix + "ParameterName (2)");
- Assert.AreEqual ("id", cmd.Parameters [2].SourceColumn, prefix + "SourceColumn (2)");
- Assert.IsNull (cmd.Parameters [2].Value, prefix + "Value (2)");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [3].DbType, prefix + "DbType (3)");
- Assert.AreEqual ("p4", cmd.Parameters [3].ParameterName, prefix + "ParameterName (3)");
- Assert.AreEqual ("lname", cmd.Parameters [3].SourceColumn, prefix + "SourceColumn (3)");
- Assert.AreEqual (1, cmd.Parameters [3].Value, prefix + "Value (3)");
- Assert.AreEqual (DbType.String, cmd.Parameters [4].DbType, prefix + "DbType (4)");
- Assert.AreEqual ("p5", cmd.Parameters [4].ParameterName, prefix + "ParameterName (4)");
- Assert.AreEqual ("lname", cmd.Parameters [4].SourceColumn, prefix + "SourceColumn (4)");
- Assert.IsNull (cmd.Parameters [4].Value, prefix + "Value (4)");
- }
- void AssertDeleteParameters (OdbcCommand cmd, string prefix)
- {
- Assert.AreEqual (3, cmd.Parameters.Count, prefix + "Count");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [0].DbType, prefix + "DbType (0)");
- Assert.AreEqual ("p1", cmd.Parameters [0].ParameterName, prefix + "ParameterName (0)");
- Assert.AreEqual ("id", cmd.Parameters [0].SourceColumn, prefix + "SourceColumn (0)");
- Assert.IsNull (cmd.Parameters [0].Value, prefix + "Value (0)");
- Assert.AreEqual (DbType.Int32, cmd.Parameters [1].DbType, prefix + "DbType (1)");
- Assert.AreEqual ("p2", cmd.Parameters [1].ParameterName, prefix + "ParameterName (1)");
- Assert.AreEqual ("lname", cmd.Parameters [1].SourceColumn, prefix + "SourceColumn (1)");
- Assert.AreEqual (1, cmd.Parameters [1].Value, prefix + "Value (1)");
- Assert.AreEqual (DbType.String, cmd.Parameters [2].DbType, prefix + "DbType (2)");
- Assert.AreEqual ("p3", cmd.Parameters [2].ParameterName, prefix + "ParameterName (2)");
- Assert.AreEqual ("lname", cmd.Parameters [2].SourceColumn, prefix + "SourceColumn (2)");
- Assert.IsNull (cmd.Parameters [2].Value, prefix + "Value (2)");
- }
- // FIXME: test SetAllValues
- // FIXME: Add tests for examining RowError
- // FIXME: Add test for ContinueUpdateOnError property
- }
- }
|