1

ここCHARで、POCO でレガシーデータベースの日付と時刻のフィールドを 1 つの .NETDateTimeプロパティに 結合する方法について、StackOverflow の質問に回答しました( Berrylに感謝します!)。今、私はカスタム ICritera クエリを取得して、まさにそのプロパティに対して機能させようとしていますが、役に立ちません。ここに私のクエリがあります:DateTime

ICriteria criteria =
    Session.CreateCriteria<InputFileLog>()
    .Add(Expression.Gt(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime), DateTime.Now.AddDays(-14)))
    .AddOrder(Order.Desc(Projections.Id()))
    .CreateCriteria(typeof(InputFile).Name)
        .Add(Expression.Eq(MembersOf<InputFile>.GetName(x => x.Id), inputFileName));

IList<InputFileLog> list = criteria.List<InputFileLog>();

そして、これが生成しているクエリです:

SELECT this_.input_file_token as input1_9_2_,
    this_.file_creation_date as file2_9_2_,
    this_.file_creation_time as file3_9_2_,
    this_.approval_ind as approval4_9_2_,
    this_.file_id as file5_9_2_,
    this_.process_name as process6_9_2_,
    this_.process_status as process7_9_2_,
    this_.input_file_name as input8_9_2_,
    gonogo3_.input_file_token as input1_6_0_,
    gonogo3_.go_nogo_ind as go2_6_0_,
    inputfile1_.input_file_name as input1_3_1_,
    inputfile1_.src_code as src2_3_1_,
    inputfile1_.process_cat_code as process3_3_1_
FROM input_file_log this_
    left outer join go_nogo gonogo3_ on this_.input_file_token=gonogo3_.input_file_token
    inner join input_file inputfile1_ on this_.input_file_name=inputfile1_.input_file_name
WHERE this_.file_creation_date > :p0 and
    this_.file_creation_time > :p1 and
    inputfile1_.input_file_name = :p2
ORDER BY this_.input_file_token desc;
:p0 = '20100401',
:p1 = '15:15:27',
:p2 = 'LMCONV_JR'

CHARDBではsの代わりにsを使用してより大きな比較を行っているため、クエリは実際に私が望むもの(過去2週間のすべての行)を実際に提供しないことを除いて、まさに私が期待するものですDATE。CreateSQLQuery() を実行せずに、クエリでCHAR値をに変換するクエリを取得する方法がわかりません。これは避けたいと思います。誰でもこれを行う方法を知っていますか?DATE

更新:これを達成するために or 式を使用しようと検討してきましたがProjections.SqlFunction()、これまでのところ役に立ちません。これが私が使用しているコードですSqlFunction()が、NHibernate.QueryException : property does not map to a single column: FileCreationDateTimeエラーが発生します:

DateTime twoWeeksAgo = DateTime.Now.AddDays(-14);
ICriteria criteria =
    Session.CreateCriteria<InputFileLog>()
    .Add(Restrictions.Gt(Projections.SqlFunction("to_date", NHibernateUtil.DateTime, Projections.Property(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime))), twoWeeksAgo))
    //.Add(Expression.Gt(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime), DateTime.Now.AddDays(-14)))
    .AddOrder(Order.Desc(Projections.Id()))
     .CreateCriteria(typeof(InputFile).Name)
        .Add(Expression.Eq(MembersOf<InputFile>.GetName(x => x.Id), inputFileName));

私はここで何か間違ったことをしていると確信しています.NETプロパティを2つのOracle SQL列に分割FileCreationDateTimeするカスタムを使用しているため、まだ好きではありません(詳細については、このStackOverflowの質問を参照してください)。ICompositeUserTypeDateTimeCHAR

4

1 に答える 1

2

私はついにこれを理解します!コードは次のとおりです (何らかの理由で、StackOverflow はこの最初のコード スニペットのメソッド名の一部を型の構文色にしています)。

    IList<InputFileLog> list = null;
    DateTime twoWeeksAgo = DateTime.Now.AddDays(-14);

    IProjection datePropProj =
        DefaultStringFileCreationDateTimeType.GetFileCreationDateToDateSQLProjection();
    IProjection timePropProj =
        DefaultStringFileCreationDateTimeType.GetFileCreationTimeToDateSQLProjection();

    IProjection dateConstProj =
        DefaultStringFileCreationDateTimeType.GetFileCreationDateToDateSQLFunction(twoWeeksAgo);
    IProjection timeConstProj =
        DefaultStringFileCreationDateTimeType.GetFileCreationTimeToDateSQLFunction(twoWeeksAgo);

    ICriteria criteria =
        Session.CreateCriteria<InputFileLog>()
        .Add(Restrictions.Or(Restrictions.GtProperty(datePropProj, dateConstProj),
            Restrictions.And(Restrictions.EqProperty(datePropProj, dateConstProj),
                Restrictions.GeProperty(timePropProj, timeConstProj))))
        .AddOrder(Order.Desc(Projections.Id()))
        .CreateCriteria(typeof(InputFile).Name)
            .Add(Expression.Eq(MembersOf<InputFile>.GetName(x => x.Id), inputFileName));

    list = criteria.List<InputFileLog>();

SQLProjectionsとを作成するために使用したメソッドは次のとおりSQLFunctionsです。プロパティのカスタム型マッピングに使用したICompositeUserType( )にそれらを入れました。DefaultStringFileCreationDateTimeFileCreationDateTime

public class DefaultStringFileCreationDateTime : ICompositeUserType
{
    .
    .
    .
    public const string DotNetDateFormat = "yyyyMMdd";

    public const string DotNetTimeFormat = "HH:mm:ss";

    public const string DbDateFormat = "YYYYMMDD";

    public const string DbTimeFormat = "HH24:MI:SS";

    private const string _nullDateRepresentationInDb = "00000000";

    public struct DatabaseFieldNames
    {
        /// <summary>
        /// File creation date column name.
        /// </summary>
        public const string FileCreationDate = "file_creation_date";

        /// <summary>
        /// File creation time column name.
        /// </summary>
        public const string FileCreationTime = "file_creation_time";
    }

    public static IProjection GetFileCreationDateToDateSQLProjection()
    {
        return ProjectionUtil.GetToDateSQLProjection(DatabaseFieldNames.FileCreationDate, DbDateFormat, NHibernateUtil.DateTime);
    }

    public static IProjection GetFileCreationTimeToDateSQLProjection()
    {
        return ProjectionUtil.GetToDateSQLProjection(DatabaseFieldNames.FileCreationTime, DbTimeFormat, NHibernateUtil.DateTime);
    }

    public static IProjection GetFileCreationDateToDateSQLFunction(DateTime dt)
    {
        return ProjectionUtil.GetToDateSQLFunction(dt, DotNetDateFormat, DbDateFormat);
    }

    public static IProjection GetFileCreationTimeToDateSQLFunction(DateTime dt)
    {
        return ProjectionUtil.GetToDateSQLFunction(dt, DotNetTimeFormat, DbTimeFormat);
    }
}

私はすでにメンバーの実装に を使用していconsts DatabaseFieldNames structPropertyNamesので、これらのハードコーディングされた列名をProjections必要な にも再利用できました。

Projectionジェネリックto_dateメソッドが存在するユーティリティ クラスを次に示します。

public class ProjectionUtil
{
    public static IProjection GetToDateSQLProjection(
        string columnName, string dbToDateFormat, IType returnType)
    {
        return Projections.SqlProjection(
            string.Format("to_date({0}, '{1}') as {0}", columnName, dbToDateFormat),
            new string[] { columnName },
            new IType[] { returnType });
    }

    public static IProjection GetToDateSQLFunction(
        DateTime dt, string dotNetFormatString, string dbFormatString)
    {
        return Projections.SqlFunction(
            "to_date",
            NHibernateUtil.DateTime,
            Projections.Constant(dt.ToString(dotNetFormatString)),
            Projections.Constant(dbFormatString));
    }
}

最後に、NHibernate が生成する Oracle SQL を次に示します。

SELECT
    this_.input_file_token as input1_9_2_,
    this_.file_creation_date as file2_9_2_,
    this_.file_creation_time as file3_9_2_,
    this_.approval_ind as approval4_9_2_,
    this_.file_id as file5_9_2_,
    this_.process_name as process6_9_2_,
    this_.process_status as process7_9_2_,
    this_.input_file_name as input8_9_2_,
    gonogo3_.input_file_token as input1_6_0_,
    gonogo3_.go_nogo_ind as go2_6_0_,
    inputfile1_.input_file_name as input1_3_1_,
    inputfile1_.src_code as src2_3_1_,
    inputfile1_.process_cat_code as process3_3_1_
FROM
    input_file_log this_
    left outer join go_nogo gonogo3_ on this_.input_file_token=gonogo3_.input_file_token
    inner join input_file inputfile1_ on this_.input_file_name=inputfile1_.input_file_name
WHERE
    (
        to_date(file_creation_date, 'YYYYMMDD') > to_date(:p0, :p1) or 
        (
            to_date(file_creation_date, 'YYYYMMDD') = to_date(:p2, :p3) and
            to_date(file_creation_time, 'HH24:MI:SS') >= to_date(:p4, :p5)
        )
    ) and
    inputfile1_.input_file_name = :p6
ORDER BY this_.input_file_token desc;
:p0 = '20100415',
:p1 = 'YYYYMMDD',
:p2 = '20100415',
:p3 = 'YYYYMMDD',
:p4 = '18:48:48',
:p5 = 'HH24:MI:SS',
:p6 = 'LMCONV_JR'

これを手に入れたなんて信じられない!私はISQLQuery確かに頼らなければならないと思っていました!

于 2010-04-29T23:10:38.923 に答える