コード例 #1
0
        private long GetCount(MongoDbTransformContext context)
        {
            List <dynamic> sqlItems = new List <dynamic>();

            using (var connection = _sourceSqlDatabase.CreateConnection())
            {
                return(connection.ExecuteScalar <long>(context.CountSql));
            }
        }
コード例 #2
0
        private async Task <int> TokenizeLoopAsync(MongoDbTransformContext context)
        {
            int    take         = context.BundleSize;
            object indexId      = context.LastIndexId;
            int    totalIndices = 0;

            var collectionName     = context.CollectionName;
            var identityColumnName = context.IdentityColumnName;

            do
            {
                if (context.CancellationToken.IsCancellationRequested)
                {
                    break;
                }

                var predicateSql = $"SELECT TOP {take} {context.FieldPattern} FROM {context.SqlTableNameDialect()} " +
                                   $"WHERE {identityColumnName} > {indexId} ORDER BY {identityColumnName} ASC";

                List <dynamic> sqlItems = new List <dynamic>();
                using (var connection = _sourceSqlDatabase.CreateConnection())
                {
                    sqlItems = connection.Query(predicateSql).AsList();
                }

                var itemsCount = sqlItems.Count();
                totalIndices += itemsCount;
                if (itemsCount > 0)
                {
                    var bsonList = sqlItems.Select(p => ((IDictionary <string, object>)p).ToBsonDocument()).ToList();
                    var lastItem = bsonList.LastOrDefault();
                    indexId = lastItem.GetLastId(identityColumnName);

                    var collection = MongoDbQueryHelper.GetOrCreateCollection(_mongoDbDataContext.MongoDatabase, collectionName, context.Collation);

                    await collection.InsertManyAsync(bsonList, new InsertManyOptions
                    {
                        IsOrdered = false
                    });

                    await _connectionManager.WsLogAsync($"Table: {context.TableName} total: {totalIndices} record(s) progressed.");
                }

                if (totalIndices == 0)
                {
                    break;
                }
            } while (totalIndices < context.Count);

            return(totalIndices);
        }
コード例 #3
0
        public static object GetLatestId(this IMongoDatabase mongoDatabase, MongoDbTransformContext context)
        {
            var collection  = mongoDatabase.GetOrCreateCollection(context.CollectionName, context.Collation);
            var sortBuilder = Builders <BsonDocument> .Sort;
            var sort        = sortBuilder.Descending(context.IdentityColumnName);
            var item        = collection.Find(Builders <BsonDocument> .Filter.Empty).Sort(sort).Limit(1).FirstOrDefault();

            if (item == null)
            {
                return(0);
            }

            return(item.GetLastId(context.IdentityColumnName));
        }