/** * Load schema meta data. * * @param dataSource data source * @param maxConnectionCount count of max connections permitted to use for this query * @param databaseType database type * @return schema meta data * @throws SQLException SQL exception */ public static SchemaMetaData Load(IDataSource dataSource, int maxConnectionCount, string databaseType) { List <string> tableNames; using (var connection = dataSource.CreateConnection()) { connection.Open(); tableNames = LoadAllTableNames(connection, databaseType); } Console.WriteLine($"Loading {tableNames.Count} tables' meta data."); if (tableNames.Count == 0) { return(new SchemaMetaData(new Dictionary <string, TableMetaData>(0))); } var groupInt = Math.Max(tableNames.Count / maxConnectionCount, 1); var tableGroups = tableNames.Select((o, i) => new { Index = i % groupInt, TableName = o }).GroupBy(o => o.Index).Select(o => o.Select(g => g.TableName).ToList()).ToList(); IDictionary <string, TableMetaData> tableMetaDataMap = 1 == tableGroups.Count ? Load(dataSource.CreateConnection(), tableGroups[0], databaseType) : AsyncLoad(dataSource, maxConnectionCount, tableNames, tableGroups, databaseType); return(new SchemaMetaData(tableMetaDataMap)); }
public static TableMetaData Load(IDataSource dataSource, string table, string databaseType) { using (var connection = dataSource.CreateConnection()) { connection.Open(); return(new TableMetaData(ColumnMetaDataLoader.Load(connection, table, databaseType), IndexMetaDataLoader.Load(connection, table, databaseType))); } }
public IDbConnection GetConnection(IDataSource ds) { if(!_isInTransaction) return null; IDbConnection con = _dbConnectionsByDataSource[ds] as IDbConnection; if(con == null) { con = ds.CreateConnection(); _dbConnectionsByDataSource.Add(ds, con); con.Open(); } return con; }
private static IDictionary <string, TableMetaData> AsyncLoad(IDataSource dataSource, int maxConnectionCount, List <string> tableNames, List <List <string> > tableGroups, string databaseType) { ConcurrentDictionary <string, TableMetaData> result = new ConcurrentDictionary <string, TableMetaData>(); var tasks = tableGroups.Select(o => Task.Run(() => { var tableMetaData = Load(dataSource.CreateConnection(), o, databaseType); foreach (var entry in tableMetaData) { result.TryAdd(entry.Key, entry.Value); } })).ToArray(); Task.WaitAll(tasks); return(result); }
public DacHelper() { _ds = DataSourceFactory.GetDataSource(); connstring = _ds.ConnString; conn = _ds.CreateConnection(); }