Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 1097 : Enhancement: Introduce the LiteralQueryField #1125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RepoDb.Extensions.QueryFields;

namespace RepoDb.UnitTests.Extensions.QueryFields
{
[TestClass]
public class LiteralQuerFieldTest
{
[TestMethod]
public void TestLiteralQueryFieldConstructor()
{
// Prepare
var literalQueryField = new LiteralQueryField("[Id] BETWEEN 10 AND 100");

// Assert
Assert.AreEqual("fake", literalQueryField.Field.Name);
Assert.AreEqual("[Id] BETWEEN 10 AND 100", literalQueryField.Literal);
}

[TestMethod]
public void TestLiteralQueryFieldGetString()
{
// Prepare
var literalQueryField = new LiteralQueryField("[Id] BETWEEN 10 AND 100");

// Assert
Assert.AreEqual("[Id] BETWEEN 10 AND 100", literalQueryField.GetString(0, null));
}
}
}

56 changes: 56 additions & 0 deletions RepoDb.Core/RepoDb/Extensions/QueryFields/LiteralQueryField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Data;
using RepoDb.Enumerations;
using RepoDb.Interfaces;

namespace RepoDb.Extensions.QueryFields
{
/// <summary>
/// Query field that uses exactl what is passed.
/// </summary>
/// <example>
/// See sample code below that uses a BETWEEN function.
/// <code>
/// var where = new LiteralQueryField("ColumnName BETWEEN 1 AND 10");
/// var result = connection.Query&lt;Entity&gt;(where);
/// </code>
/// </example>
public class LiteralQueryField : QueryField
{
#region Properties

public string Literal { get; set; }

#endregion

#region Constructors

/// <summary>
/// Creates a new instance of <see cref="FunctionalQueryField"/> object.
/// </summary>
/// <param name="literal">The content of the where as is.</param>
public LiteralQueryField(string literal)
// Both of the parameters are actually ignored.
: base("fake", Operation.Equal, null, null)
{
Literal = literal;
}

#endregion

#region Methods

/// <summary>
/// Gets the string representations (column-value pairs) of the current <see cref="QueryField"/> object with the formatted-function transformations.
/// </summary>
/// <param name="index">The target index.</param>
/// <param name="dbSetting">The database setting currently in used.</param>
/// <returns>The string representations of the current <see cref="QueryField"/> object using the LOWER function.</returns>
public override string GetString(int index,
IDbSetting dbSetting) =>
Literal;

#endregion
}
}