Example #1
0
        public virtual DBForeignKey Generate()
        {
            if (ReferenceType == null ||
                Table == null || Table.Schema == null ||
                Column == null || Column.Column == null)
            {
                throw new Exception($"{nameof(ReferenceAttribute)} is not initialized!");
            }
            if (ForeignKey == null)
            {
                var referenceTable = CheckReference();

                ForeignKey = new DBForeignKey()
                {
                    Table     = Table.Table,
                    Column    = Column.Column,
                    Reference = referenceTable.PrimaryKey,
                    Name      = Attribute.Name,
                };
                Table.Table.Foreigns.Add(ForeignKey);
            }
            Column.Column.IsReference = true;
            ForeignKey.Property       = PropertyInfo.Name;
            ForeignKey.PropertyInfo   = PropertyInfo;
            if (ForeignKey.PropertyInvoker == null)
            {
                ForeignKey.PropertyInvoker = EmitInvoker.Initialize(PropertyInfo, true);
            }
            return(ForeignKey);
        }
Example #2
0
            public void Parse(DBSchema schema, DBTable table)
            {
                QResult list = schema.Connection.ExecuteQResult(Filter);

                int iName      = list.GetIndex(Name);
                int iSchema    = list.GetIndex(Schema);
                int iTable     = list.GetIndex(Table);
                int iColumn    = list.GetIndex(Column);
                int iRefSchema = list.GetIndex(ReferenceSchema);
                int iRefTable  = list.GetIndex(ReferenceTable);
                int iRefColumn = list.GetIndex(ReferenceColumn);

                foreach (object[] item in list.Values)
                {
                    var name = item[iName].ToString();
                    var tab  = table ?? schema.ParseTable(item[iTable].ToString());
                    var col  = tab?.ParseColumn(item[iName].ToString());
                    var rtab = schema.ParseTable(item[iRefTable].ToString());
                    var rcol = rtab?.ParseColumn(item[iRefColumn].ToString());
                    if (col != null && rcol != null)
                    {
                        var reference = col.Table.Foreigns.GetByColumns(col, rcol);
                        if (reference == null)
                        {
                            reference = new DBForeignKey
                            {
                                Column    = col,
                                Reference = rcol
                            };
                            col.Table.Foreigns.Add(reference);
                        }
                        reference.Name = name;
                    }
                }
            }