Example #1
0
        /// <summary>
        /// 指定された型情報から主キーによる更新クエリを生成します。
        /// </summary>
        /// <typeparam name="T">テーブルの型</typeparam>
        /// <param name="properties">更新対象の列</param>
        /// <returns>生成されたSQL</returns>
        /// <remarks>
        /// [作成例]
        ///
        ///  update TableName
        /// set
        ///     Column3 = :Column3
        ///     Column4 = :Column4
        ///  where Column1 = :Column1
        ///  and Column2 = :Column2
        /// </remarks>
        public static string CreateUpdateSql <T>(params Expression <Func <T, object> >[] properties)
        {
            var primaries = This.GetPrimaryKeyProperties <T>().Select(x => string.Format("{0} = :{0}", x));
            var separator = string.Format("{0}and ", Environment.NewLine);
            var builder   = new StringBuilder();

            builder.AppendLine(This.CreateUpdateAllSql <T>(properties));
            builder.Append("where ");
            builder.Append(string.Join(separator, primaries));
            return(builder.ToString());
        }
Example #2
0
        /// <summary>
        /// 指定された型情報から主キーによるレコード検索クエリを生成します。
        /// </summary>
        /// <typeparam name="T">テーブルの型</typeparam>
        /// <param name="properties">取得対象の列</param>
        /// <returns>生成されたSQL</returns>
        /// <remarks>
        /// [作成例]
        ///
        ///  select * from ACNSM.NSM_M_COMMON
        ///  where MASTER_CD_KBN = :MASTER_CD_KBN
        ///  and CD_KBN = :CD_KBN
        /// </remarks>
        public static string CreateSelectSql <T>(DB dB = DB.SQLServer, params Expression <Func <T, object> >[] properties)
        {
            IEnumerable <string> primaries = null;

            switch (dB)
            {
            case DB.SQLServer:
                primaries = This.GetPrimaryKeyProperties <T>().Select(x => string.Format("{0} = @{0}", x));
                break;

            case DB.Oracle:
                primaries = This.GetPrimaryKeyProperties <T>().Select(x => string.Format("{0} = :{0}", x));
                break;
            }
            //var primaries = This.GetPrimaryKeyProperties<T>().Select(x => string.Format("{0} = :{0}", x));
            var separator = string.Format("{0}and ", Environment.NewLine);
            var builder   = new StringBuilder();

            builder.AppendLine(This.CreateSelectAllSql <T>(properties));
            builder.Append(" where ");
            builder.Append(string.Join(separator, primaries));
            return(builder.ToString());
        }