public TableKeySchema[] GetTableKeys(string connectionString, TableSchema table)
        {
            var keys               = new List <TableKeySchema>();
            var foreignColumns     = new List <string>();
            var primaryColumns     = new List <string>();
            var extendedProperties = new List <ExtendedProperty>();

            using (IVistaDBDatabase vistaDb = GetDatabase(connectionString))
            {
                if (vistaDb == null)
                {
                    return(keys.ToArray());
                }

                IVistaDBTableSchema vistaTable = vistaDb.TableSchema(table.Name);
                if (vistaTable == null)
                {
                    return(keys.ToArray());
                }

                foreach (IVistaDBRelationshipInformation vistaKey in vistaTable.ForeignKeys)
                {
                    foreignColumns.Clear();
                    foreignColumns.AddRange(vistaKey.ForeignKey.Split(
                                                new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));

                    IVistaDBTableSchema vistaPrimaryTable = vistaDb.TableSchema(vistaKey.PrimaryTable);
                    if (vistaPrimaryTable == null)
                    {
                        continue;
                    }

                    primaryColumns.Clear();
                    //find primary key index
                    foreach (IVistaDBIndexInformation i in vistaPrimaryTable.Indexes)
                    {
                        if (i.Primary)
                        {
                            primaryColumns.AddRange(i.KeyExpression.Split(
                                                        new[] { ';' }, StringSplitOptions.RemoveEmptyEntries));
                        }
                    }

                    extendedProperties.Clear();
                    extendedProperties.Add(ExtendedProperty.Readonly(ExtendedPropertyNames.Description, vistaKey.Description));

                    var key = new TableKeySchema(
                        table.Database, vistaKey.Name,
                        foreignColumns.ToArray(),
                        string.Empty, vistaKey.ForeignTable,
                        primaryColumns.ToArray(),
                        string.Empty, vistaKey.PrimaryTable,
                        extendedProperties.ToArray());

                    keys.Add(key);
                }
            }

            return(keys.ToArray());
        }
示例#2
0
        public TableKeySchema[] GetTableKeys(string connectionString, TableSchema table)
        {
            // TODO: Verify that this is returning the correct PrimaryKeys.
            //0, 2, 3; catalog, table, key name
            DataTable dataTable = GetSchemaData(connectionString,
                                                ForeignKeys,
                                                null /*restrictions[0] - catalog*/,
                                                null /*restrictions[1] - unused*/,
                                                table.Name /*restrictions[2] - table*/,
                                                null /*restrictions[3] - key name*/);

            var keys     = new TableKeySchema[dataTable.Rows.Count];
            int keyIndex = 0;

            foreach (DataRow dr in dataTable.Rows)
            {
                string   name = (string)dr[ForeignKeysNameColumn];
                string[] foreignKeyColumns = new[] { (string)dr[ForeignKeysFromColColumn] };
                string   foreignKeyTable   = (string)dr[ForeignKeysFromTableColumn];
                string[] primaryKeyColumns = new[] { (string)dr[ForeignKeysToColColumn] };
                string   primaryKeyTable   = (string)dr[ForeignKeysToTableColumn];

                var key = new TableKeySchema(table.Database, name,
                                             foreignKeyColumns, foreignKeyTable,
                                             primaryKeyColumns, primaryKeyTable);

                keys[keyIndex++] = key;
            }

            return(keys);
        }