public void QuoteUnquote_CustomPrefixSuffix_Success()
        {
            RunTest((command, tableName) => {
                using (var cmd = (OleDbCommand)OleDbFactory.Instance.CreateCommand())
                {
                    cmd.Transaction = transaction;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = @"SELECT * FROM " + tableName;
                    cmd.Connection  = (OleDbConnection)OleDbFactory.Instance.CreateConnection();
                    cmd.Connection.ConnectionString = connection.ConnectionString;

                    using (var adapter = new OleDbDataAdapter(cmd.CommandText, connection))
                        using (var builder = new OleDbCommandBuilder(adapter))
                        {
                            // Custom prefix & suffix
                            builder.QuotePrefix = "'";
                            builder.QuoteSuffix = "'";

                            Assert.Equal(adapter, builder.DataAdapter);
                            Assert.Equal("'Test'", builder.QuoteIdentifier("Test", connection));
                            Assert.Equal("'Te''st'", builder.QuoteIdentifier("Te'st", connection));
                            Assert.Equal("Test", builder.UnquoteIdentifier("'Test'", connection));
                            Assert.Equal("Te'st", builder.UnquoteIdentifier("'Te''st'", connection));

                            // Ensure we don't need active connection:
                            Assert.Equal("'Test'", builder.QuoteIdentifier("Test", null));
                            Assert.Equal("Test", builder.UnquoteIdentifier("'Test'", null));

                            builder.QuotePrefix  = string.Empty;
                            string quoteErrMsg   = $"{nameof(builder.QuoteIdentifier)} requires open connection when the quote prefix has not been set.";
                            string unquoteErrMsg = $"{nameof(builder.UnquoteIdentifier)} requires open connection when the quote prefix has not been set.";

                            Assert.Equal("`Test`", builder.QuoteIdentifier("Test", connection));
                            Assert.Equal("Test", builder.UnquoteIdentifier("`Test`", connection));

                            Assert.NotNull(adapter.SelectCommand.Connection);
                            Assert.Equal("`Test`", builder.QuoteIdentifier("Test"));
                            Assert.Equal("Test", builder.UnquoteIdentifier("`Test`"));

                            adapter.SelectCommand.Connection = null;
                            AssertExtensions.Throws <InvalidOperationException>(() => builder.QuoteIdentifier("Test"), quoteErrMsg);
                            AssertExtensions.Throws <InvalidOperationException>(() => builder.UnquoteIdentifier("'Test'"), unquoteErrMsg);
                        }
                }
            });
        }
Example #2
0
        private static void Test_Command_QuoteIdentifier()
        {
            string           query       = "select id,name from t";
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, conn);

            OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);

            string sql_quote_conn = commandBuilder.QuoteIdentifier("select id,name from t", conn);

            Assert.AreEqual(sql_quote_conn, "\"select id,name from t\"");

            string sql_unquote_conn = commandBuilder.UnquoteIdentifier("\"select id,name from t\"", conn);

            Assert.AreEqual(sql_unquote_conn, "select id,name from t");
        }