Ejemplo n.º 1
0
        private async Task SaveManyCoreMany(IDbFactory dbFactory,
                                            IEnumerable <StateEntity> entities)
        {
            var array = StateEntity.DistinctWithVersion(entities).ToArray();
            var items = array
                        .Select(x => new RelationalStateEntity
            {
                claptrap_id        = x.ClaptrapId,
                claptrap_type_code = x.ClaptrapTypeCode,
                version            = x.Version,
                state_data         = x.StateData,
                updated_time       = x.UpdatedTime
            })
                        .AsParallel()
                        .ToArray();

            var sql = GetUpsertSql(_schemaName, _tableName, items.Length);

            using var db = dbFactory.GetConnection(_connectionName);
            var ps = new DynamicParameters();

            for (var i = 0; i < array.Length; i++)
            {
                foreach (var(parameterName, valueFunc) in RelationalStateEntity.ValueFactories())
                {
                    var entity = items[i];
                    var name   = _sqlTemplateCache.GetParameterName(parameterName, i);
                    ps.Add(name, valueFunc(entity));
                }
            }

            await db.ExecuteAsync(sql, ps);
        }
Ejemplo n.º 2
0
        private string GetUpsertSql(string schemaName, string tableName, int count)
        {
            var sql = _upsertSqlCache.GetOrAdd(count, CreateUpsertSql);

            return(sql);


            string CreateUpsertSql(int valueCount)
            {
                string upsertManySqlHeader =
                    $"REPLACE INTO {schemaName}.{tableName} (claptrap_type_code,claptrap_id,version,state_data,updated_time) VALUES ";
                var sb        = new StringBuilder(upsertManySqlHeader);
                var valuesSql = Enumerable.Range(0, count)
                                .Select(x =>
                                        ValuePartFactory(RelationalStateEntity.ParameterNames(), x))
                                .ToArray();

                sb.Append(string.Join(",", valuesSql));
                return(sb.ToString());

                string ValuePartFactory(IEnumerable <string> parameters, int index)
                {
                    var values = string.Join(",", parameters.Select(x => _sqlTemplateCache.GetParameterName(x, index)));
                    var re     = $" ({values}) ";

                    return(re);
                }
            }
        }
Ejemplo n.º 3
0
 public static void RegisterParameters(ISqlTemplateCache sqlTemplateCache, int maxCount)
 {
     foreach (var name in RelationalStateEntity.ParameterNames())
     {
         for (var i = 0; i < maxCount; i++)
         {
             sqlTemplateCache.AddParameterName(name, i);
         }
     }
 }