/// <summary> /// Gets the associations. /// </summary> /// <param name="table">The table.</param> /// <returns></returns> public virtual TableSchemaList GetAssociations(TableSchema table) { TableSchemaList result = new TableSchemaList(); //find all tables that have 2 foreign keys, only 2 columns, and NO primary key var assocTables = from t in this.GetTableSchemas() where t.PrimaryKey == null && t.Columns.Count == 2 && t.ForeignKeys.Count == 2 select t; foreach (var tbl in assocTables) { if (tbl.ForeignKeys[0].PrimaryKeyTable.Name == table.Name || tbl.ForeignKeys[1].PrimaryKeyTable.Name == table.Name) { result.Add(tbl); } } return result; }
/// <summary> /// Gets a list of tables whose names begin with the specified prefix. /// </summary> /// <param name="prefix">The prefix of the tables to find.</param> /// <returns>List of tables whose names begin with the specified prefix.</returns> public TableSchemaList GetTableSchemas(string prefix) { TableSchemaList result = null; string cmdtext = String.Format(@"select table_name from information_schema.tables where table_name like '{0}%' ", prefix); using (IDataReader reader = this._helper.ExecuteReader(cmdtext)) { while (reader.Read()) { if (result == null) result = new TableSchemaList(); result.Add(new TableSchema(this, reader["TABLE_NAME"].ToString())); } } return result; }
/// <summary> /// Gets all of the tables in a database. /// </summary> /// <returns></returns> public virtual TableSchemaList GetTableSchemas() { TableSchemaList result = null; string cmdtext = @"select * from information_schema.tables where table_name not like 'sys%'"; using (IDataReader reader = this._helper.ExecuteReader(cmdtext)) { while (reader.Read()) { if (result == null) result = new TableSchemaList(); result.Add(new TableSchema(this, reader["TABLE_NAME"].ToString())); } } return result; }
private TableSchemaList GetTableSchemas() { WriteLine("Retrieving table schemas"); TableSchemaList tableSchemas = null; if (_options.OnlyTablesWithPrefix.Count == 0) { WriteLine("TablesWithPrefix was empty."); tableSchemas = _codeRunnerConfig.DbProvider.GetTableSchemas(); } else { tableSchemas = new TableSchemaList(); foreach (string prefix in _options.OnlyTablesWithPrefix) { WriteLine("Adding tables with prefix '{0}'", prefix); tableSchemas.AddRange(_codeRunnerConfig.DbProvider.GetTableSchemas(prefix)); } } return tableSchemas; }