Example #1
0
        /// <summary>
        /// 获取表的约束信息
        /// </summary>
        /// <param name="connString"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static IEnumerable <Constraint> GetConstraints(ConnectString connString, string tableName)
        {
            DataGateway gateway = new DataGateway(OracleProvider.ManagedDataAccess);

            string sql = "select owner, constraint_name, constraint_type, TABLE_NAME, r_constraint_name, delete_rule, "
                         + "case when (status='ENABLED') then 1 else 0 end as enabled, "
                         + "case when (index_name is not null) then 1 else 0 end as using_index "
                         + $"from user_constraints where constraint_type!='C' and table_name='{tableName}'";

            IEnumerable <Constraint> result = gateway.Query <Constraint>(connString, sql);

            sql = $"select * from USER_CONS_COLUMNS where table_name='{tableName}' order by column_name,position";

            IEnumerable <ConstraintColumns> cols = gateway.Query <ConstraintColumns>(connString, sql);

            foreach (var con in result)
            {
                con.Columns = string.Join(",", cols.Where(item => item.Name == con.Name).OrderBy(item => item.Position).Select(item => item.ColumnName));

                if (con.Type == "R")
                {
                    con.RefConstraint = gateway.Query <ConstraintColumns>(connString, $"select * from USER_CONS_COLUMNS where constraint_name='{con.RefName}'").FirstOrDefault();
                }
            }

            return(result);
        }