public ColumInfo(string name, FieldType type, ColumConstraintsInfo inf, string domainName, bool nullFlag, bool fieldNullFlag, string defaltSource) { ColumName = name; ColumType = type; ConstraintsInf = inf; DomainName = domainName; NullFlag = nullFlag; FieldNullFlag = fieldNullFlag; DefaultSource = defaltSource; }
public IEnumerable<ColumInfo> GetColums(FbConnection con) { var constraints = GetConstrains(con); using (var command = con.CreateCommand()) { command.CommandText = $"select trim(rf.rdb$field_name) Name, f.rdb$field_type Type, f.rdb$field_sub_type SubType , f.rdb$character_length CharSize, trim(rf.rdb$field_source) FieldSource, rf.rdb$null_flag NullFlag, f.rdb$null_flag fieldNullFlag, f.rdb$field_precision FieldPrecision, f.rdb$field_scale FieldScale, f.rdb$field_length FieldLength, coalesce(rf.rdb$default_source, '') DefaultSource " + "from rdb$relation_fields rf " + "join rdb$relations r on rf.rdb$relation_name = r.rdb$relation_name " + "and r.rdb$view_blr is null " + $"and r.rdb$relation_type = 0 and r.rdb$system_flag = {SystemFlag} " + "join rdb$fields f on f.rdb$field_name = rf.rdb$field_source " + $"where rf.rdb$relation_name = '{this.TableName}' " + "order by rf.rdb$field_position; "; var reader = command.ExecuteReader(); while (reader.Read()) { var name = (string)reader["Name"]; var size = (reader["CharSize"] == DBNull.Value) ? null : (short?)reader["CharSize"]; var subType = (reader["SubType"] == DBNull.Value) ? null : (short?)reader["SubType"]; var nullFlag = reader["NullFlag"] == DBNull.Value; var fieldNullFlag = reader["FieldNullFlag"] == DBNull.Value; var precision = (reader["FieldPrecision"] == DBNull.Value) ? null : (short?)reader["FieldPrecision"]; var scale = (reader["FieldScale"] == DBNull.Value) ? null : (short?)reader["FieldScale"]; var fieldLength = (reader["FieldLength"] == DBNull.Value) ? null : (short?)reader["FieldLength"]; var type = new FieldType((short)reader["Type"], subType, size, precision, scale, fieldLength); var defaultSource = (string)reader["DefaultSource"]; var constraintInfo = new ColumConstraintsInfo(); if (constraints.ContainsKey(name)) { constraintInfo = constraints[name]; } yield return new ColumInfo(name, type, constraintInfo, (string)reader["FieldSource"], nullFlag, fieldNullFlag, defaultSource); } } }
private Dictionary<string, ColumConstraintsInfo> GetConstrains(FbConnection con) { var dic = new Dictionary<string, ColumConstraintsInfo>(); using (var command = con.CreateCommand()) { command.CommandText = "select trim(seg.rdb$field_name) FieldName, rel.rdb$constraint_type Type, trim(idx.foreign_key_table) ForeignKeyTable " + "from rdb$relation_constraints rel " + "left outer join( " + "select idx.rdb$index_name, idx.rdb$relation_name, idx2.rdb$relation_name foreign_key_table " + "from rdb$indices idx " + "left outer join rdb$indices idx2 on idx.rdb$foreign_key = idx2.rdb$index_name) idx " + "on rel.rdb$index_name = idx.rdb$index_name " + "left outer join rdb$index_segments seg on idx.rdb$index_name = seg.rdb$index_name " + $"where rel.rdb$relation_name = '{this.TableName}' and seg.rdb$field_name != '' "; var reader = command.ExecuteReader(); while (reader.Read()) { var name = (string)reader["FieldName"]; ColumConstraintsInfo inf; if (!dic.TryGetValue(name, out inf)) { inf = new ColumConstraintsInfo(); dic.Add(name, inf); } var type = GetConstraintType((string)reader["Type"]); inf.SetKind(type); if (type == ConstraintsKind.Foreign) { inf.ForeignKeyTableName = (string)reader["ForeignKeyTable"]; } } } return dic; }