Exemple #1
0
        public async Task <int> Update <T>(object viewModel,
                                           CancellationToken cancellationToken, SqlTransaction transaction = null)
        {
            var reflectionTable = await
                                  Task.Run(() => new TablesReflectionHelper()
                                           .GetReflectionTable(typeof(T)), cancellationToken);

            var query = new UpdateGenerator()
                        .GetUpdateQuery(reflectionTable, viewModel);
            int affectedRowsCount = 0;

            using (var connection = _connectionService.Create())
            {
                using (SqlCommand cmd = connection.CreateCommand())
                {
                    cmd.CommandText = query.Query;
                    cmd.Transaction = transaction;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddRange(query.Params.ToArray());
                    affectedRowsCount = await cmd.ExecuteNonQueryAsync(cancellationToken);
                }
            }

            return(affectedRowsCount);
        }
        protected override DbCommandDefinition CreateDbCommandDefinition(
            DbProviderManifest providerManifest, DbCommandTree commandTree)
        {
            if (commandTree == null)
                throw new ArgumentNullException("commandTree");

            SqlGenerator generator = null;
            if (commandTree is DbQueryCommandTree)
                generator = new SelectGenerator();
            else if (commandTree is DbInsertCommandTree)
                generator = new InsertGenerator();
            else if (commandTree is DbUpdateCommandTree)
                generator = new UpdateGenerator();
            else if (commandTree is DbDeleteCommandTree)
                generator = new DeleteGenerator();
            else if (commandTree is DbFunctionCommandTree)
                generator = new FunctionGenerator();

            string sql = generator.GenerateSQL(commandTree);

            EFMySqlCommand cmd = new EFMySqlCommand();
            cmd.CommandText = sql;
            if (generator is FunctionGenerator)
                cmd.CommandType = (generator as FunctionGenerator).CommandType;

            SetExpectedTypes(commandTree, cmd);

            EdmFunction function = null;
            if (commandTree is DbFunctionCommandTree)
                function = (commandTree as DbFunctionCommandTree).EdmFunction;

            // Now make sure we populate the command's parameters from the CQT's parameters:
            foreach (KeyValuePair<string, TypeUsage> queryParameter in commandTree.Parameters)
            {
                DbParameter parameter = cmd.CreateParameter();
                parameter.ParameterName = queryParameter.Key;
                parameter.Direction = ParameterDirection.Input;
                parameter.DbType = Metadata.GetDbType(queryParameter.Value);

                FunctionParameter funcParam;
                if (function != null &&
                    function.Parameters.TryGetValue(queryParameter.Key, false, out funcParam))
                {
                    parameter.ParameterName = funcParam.Name;
                    parameter.Direction = Metadata.ModeToDirection(funcParam.Mode);
                    parameter.DbType = Metadata.GetDbType(funcParam.TypeUsage);
                }
                cmd.Parameters.Add(parameter);
            }

            // Now add parameters added as part of SQL gen
            foreach (DbParameter p in generator.Parameters)
                cmd.Parameters.Add(p);

            return CreateCommandDefinition(cmd);
        }
Exemple #3
0
        public void Update()
        {
            var command = UpdateGenerator.Generate(new Foo
            {
                Id   = 5,
                Foo1 = "foo1",
                Foo2 = "foo2"
            });

            Assert.AreEqual("UPDATE Foo SET Foo1 = @Foo1, Foo2 = @Foo2 WHERE Id = @Id;", command);
        }
Exemple #4
0
        public static void AddGenerateCommand(this CommandLineApplication app, ConfigFile config)
        {
            app.Command("generate", generateCmd =>
            {
                generateCmd.Description = "Generate the C# Get- and UpdateConnector files for AfasClient";

                //generate will only work with a valid Afas Token.
                generateCmd.OnValidate((ctx) =>
                {
                    if ((config == null) || (!config.HasToken))
                    {
                        return(new ValidationResult("A valid configuration (with token) could not be found. Use [config].  "));
                    }
                    return(ValidationResult.Success);
                });


                generateCmd.OnExecuteAsync(async(cancellationToken) =>
                {
                    IAfasClient client = new AfasClient(config.MemberNumber, config.Token, (Environments)config.Environment);

                    //Make sure the folder exists.
                    System.IO.Directory.CreateDirectory(DestinationFolder);

                    var si = await client.GetSessionInfoAsync();

                    if (si.GetConnectors != null)
                    {
                        foreach (var connector in si.GetConnectors)
                        {
                            var meta = await client.GetMetaDataGetConAsync(connector.Id);
                            Console.WriteLine($"Processing : {meta.Name}");
                            SaveGetConnector(meta);
                        }
                    }

                    //UpdateConnector requires the use of the generator.
                    var updGenerator = new UpdateGenerator(client);
                    var updMetas     = updGenerator.GenerateList();
                    foreach (var meta in updMetas)
                    {
                        Console.WriteLine($"Processing : {meta.Name}");
                        SaveUpdateConnector(meta);
                    }
                });
            });
        }
Exemple #5
0
        /// <summary>
        /// Update a single row
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection"></param>
        /// <param name="param">object</param>
        /// <param name="table">Optional table name</param>
        /// <param name="commandTimeout">commandTimeout</param>
        /// <param name="transaction">transaction</param>
        /// <returns>Numbers of rows affected</returns>
        public static int UpdateSingle <T>(this IDbConnection connection, T param, int?commandTimeout = null, IDbTransaction transaction = null)
        {
            if (param == null)
            {
                throw new ArgumentNullException("param can not be null.");
            }

            if (param is IEnumerable)
            {
                throw new ArgumentException("param can not be a IEnumerable.");
            }

            var    type = typeof(T);
            string cachedCommand;
            var    value = StringCache.TryGetCommand(type, Operation.Update, out cachedCommand);

            if (string.IsNullOrEmpty(cachedCommand))
            {
                cachedCommand = UpdateGenerator.Generate(param);
                StringCache.Add(type, Operation.Update, cachedCommand);
            }

            return(connection.Execute(cachedCommand, param, commandTimeout: commandTimeout, transaction: transaction));
        }
Exemple #6
0
        protected override System.Data.Entity.Core.Common.DbCommandDefinition CreateDbCommandDefinition(
            System.Data.Entity.Core.Common.DbProviderManifest providerManifest, DbCommandTree commandTree)
        {
            if (commandTree == null)
            {
                throw new ArgumentNullException("commandTree");
            }

            SqlGenerator generator = null;

            if (commandTree is DbQueryCommandTree)
            {
                generator = new SelectGenerator();
            }
            else if (commandTree is DbInsertCommandTree)
            {
                generator = new InsertGenerator();
            }
            else if (commandTree is DbUpdateCommandTree)
            {
                generator = new UpdateGenerator();
            }
            else if (commandTree is DbDeleteCommandTree)
            {
                generator = new DeleteGenerator();
            }
            else if (commandTree is DbFunctionCommandTree)
            {
                generator = new FunctionGenerator();
            }

            string sql = generator.GenerateSQL(commandTree);

            EFMySqlCommand cmd = new EFMySqlCommand();

            cmd.CommandText = sql;
            if (generator is FunctionGenerator)
            {
                cmd.CommandType = (generator as FunctionGenerator).CommandType;
            }

            SetExpectedTypes(commandTree, cmd);

            EdmFunction function = null;

            if (commandTree is DbFunctionCommandTree)
            {
                function = (commandTree as DbFunctionCommandTree).EdmFunction;
            }

            // Now make sure we populate the command's parameters from the CQT's parameters:
            foreach (KeyValuePair <string, TypeUsage> queryParameter in commandTree.Parameters)
            {
                DbParameter parameter = cmd.CreateParameter();
                parameter.ParameterName = queryParameter.Key;
                parameter.Direction     = ParameterDirection.Input;
                parameter.DbType        = Metadata.GetDbType(queryParameter.Value);

#if NET_45_OR_GREATER
                if (queryParameter.Value.EdmType is PrimitiveType &&
                    ((PrimitiveType)queryParameter.Value.EdmType).PrimitiveTypeKind == PrimitiveTypeKind.Geometry)
                {
                    ((MySqlParameter)parameter).MySqlDbType = MySqlDbType.Geometry;
                }
#endif

                FunctionParameter funcParam;
                if (function != null &&
                    function.Parameters.TryGetValue(queryParameter.Key, false, out funcParam))
                {
                    parameter.ParameterName = funcParam.Name;
                    parameter.Direction     = Metadata.ModeToDirection(funcParam.Mode);
                    parameter.DbType        = Metadata.GetDbType(funcParam.TypeUsage);
                }
                cmd.Parameters.Add(parameter);
            }

            // Now add parameters added as part of SQL gen
            foreach (DbParameter p in generator.Parameters)
            {
                cmd.Parameters.Add(p);
            }

            return(CreateCommandDefinition(cmd));
        }
Exemple #7
0
        public void Update()
        {
            var generator = new UpdateGenerator <CompositeIdSample>();

            Assert.AreEqual("UPDATE `composite_id_table` SET `SomeValue` = ?p0, `SomeDate` = ?p1 WHERE `Id1` = ?p2 AND `Id2` = ?p3;", generator.GetSql());
        }