SEARCH CFQL方法中的Optionnal参数不是varchar

时间:2016-03-10 14:49:30

标签: codefluent

是否可以使用允许传递除varchar之外的可选参数的搜索功能(即DateTime,decimal ..)?

我需要搜索日期范围内的行。用户应该只能搜索日期,或最多日期,甚至是从FROM-TO范围。

我的方法是使用Date参数创建SEARCH方法。这些在CFE中被正确转换为DateTime对象,但我无法传递空值。

我设法用未经检查的部分创建了一个SEARCH方法,但这些部分在函数体中不会获得选项。

如何将fromDate和toDate参数设置为可选参数?

CFQL正文

SEARCH(string amount, string fromDate, string toDate, string otherEntityId, date date) WHERE [$Entity::Date$ >= CAST(@fromDate AS DATE)] AND [$Entity::Date$ <= CAST(@toDate AS DATE)] AND OtherEntity.OtherEntityId = @otherEntityId AND Amount = @amount and date = @date

生成的正文程序如下。注意未经检查的部分不是选项

CREATE PROCEDURE [Schema].[Entity_NameOfProcedure]
(
 @amount [nvarchar] (256) = NULL,
 @fromDate [nvarchar] (256) = NULL,
 @toDate [nvarchar] (256) = NULL,
 @OtherEntityId [nvarchar] (256) = NULL,
 @date [date] = NULL,
 @_orderBy0 [nvarchar] (64) = NULL,
 @_orderByDirection0 [bit] = 0
)
AS
SET NOCOUNT ON
DECLARE @sql nvarchar(max), @paramlist nvarchar(max)

SELECT @sql=
'SELECT DISTINCT * /*on purpose for stackoverflow */
    FROM [Schema].[Entity]
        LEFT OUTER JOIN [Schema].[OtherEntity] ON ([Schema].[Entity].[Entity_OtherEntity_OtherEntityId] = [Schema].[OtherEntity].[OtherEntity_OtherEntityId]) 
    WHERE (([Entity].[Entity_Date] >= CAST(@fromDate AS DATE) AND ([Entity].[Entity_Date] <= CAST(@toDate AS DATE) AND ((1 = 1) AND ((1 = 1) AND (([Schema].[OtherEntity].[OtherEntity_NumberInt] LIKE ''6%'') AND (1 = 1)))))) AND (1 = 1))'
SELECT @paramlist = '@amount nvarchar (256), 
    @fromDate nvarchar (256), 
    @toDate nvarchar (256), 
    @otherEntityId nvarchar (256), 
    @date date, 
    @_orderBy0 nvarchar (64), 
    @_orderByDirection0 bit'
IF @amount IS NOT NULL
    SELECT @sql = @sql + ' AND (([Schema].[Entity].[Entity_amount] = @amount))'
IF @otherEntityId IS NOT NULL
    SELECT @sql = @sql + ' AND (([Schema].[OtherEntity].[OtherEntity_OtherEntityId] = @OtherEntityId))'
IF @date IS NOT NULL
    SELECT @sql = @sql + ' AND (([Schema].[Entity].[Entity_Date] = @date))'
EXEC sp_executesql @sql, @paramlist,
    @amount, 
    @fromDate, 
    @toDate, 
    @OtherEntityId, 
    @date, 
    @_orderBy0, 
    @_orderByDirection0

RETURN
GO

感谢您的回答

1 个答案:

答案 0 :(得分:1)

您可以使用可选的日期参数创建SEARCH方法:

SEARCH(date fromDate, date toDate)
WHERE CreationDate >= @fromDate AND CreationDate <= @toDate

这会生成以下SQL:

SELECT @sql=
'SELECT DISTINCT [Employee_Id], [Employee_FirstName], [Employee_CreationDate] 
    FROM [Employee]
    WHERE (1 = 1)'
SELECT @paramlist = '@fromDate datetime, @toDate datetime'
IF @fromDate IS NOT NULL
    SELECT @sql = @sql + ' AND (([Employee_CreationDate] >= @fromDate))'
IF @toDate IS NOT NULL
    SELECT @sql = @sql + ' AND (([Employee_CreationDate] <= @toDate))'

生成的BOM中fromDatetoDate参数不可为空。但是,如果您使用默认值(默认值等于CodeFluentPersistence.DefaultDateTimeValueMore about default values),则NULL将被发送到商店过程。

如果您愿意DateTime?,只需将参数类型从date更改为date?

SEARCH(date? fromDate, date? toDate)
WHERE CreationDate >= @fromDate AND CreationDate <= @toDate

生成以下C#方法:

public static EmployeeCollection Search(DateTime? fromDate, DateTime? toDate)