Esempio n. 1
0
        /// <summary>
        /// 主キーをエンティティから抜いてDBから取得する。なければnullを返す
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataEntity"></param>
        /// <returns></returns>
        public T Select <T>(DataEntity <T> dataEntity) where T : DataEntity <T>, new()
        {
            DbTableAttribute attributes = DataEntity <T> .GetTableAttribute();

            var keys = DataEntity <T> .GetKeyElements();

            if (keys.Count == 0)
            {
                throw new ApplicationException("主キーの無いテーブルに選択クエリを発行しようとしました。");
            }

            string sql = @" SELECT * FROM " + attributes.Name + " " + CreateWhereClause <T>(keys, dataEntity);

            using (IDbConnection connection = this.CreateConnection())
            {
                connection.Open();
                using (IDbCommand command = connection.CreateCommand())
                {
                    command.CommandTimeout = this.CommandTimeout;
                    command.CommandText    = sql;
                    ts.TraceInformation(sql);
                    using (IDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (!reader.Read())
                        {
                            return(default(T));
                        }

                        return(this.CreateInstance <T>(reader));
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// エンティティの主キーを元に、テーブルの列を削除します。
        /// </summary>
        /// <typeparam name="T">エンティティを指定します。</typeparam>
        /// <param name="dataEntity">データエンティティを指定します。</param>
        public void Delete <T>(DataEntity <T> dataEntity) where T : DataEntity <T>, new()
        {
            var keys = DataEntity <T> .GetKeyElements();

            if (keys.Count == 0)
            {
                throw new ApplicationException("主キーの無いテーブルへのDELETE文の発行は許可されていません。");
            }

            DbTableAttribute table = DataEntity <T> .GetTableAttribute();

            string sql = @"DELETE FROM " + table.Name + " " + CreateWhereClause <T>(keys, dataEntity);

            this.ExecuteNonQuery(sql);
        }