コード例 #1
0
        public async Task <T> ExecuteScalarAsync <T>(string connection, string sql, object parameters)
        {
            T result = default;

            using (var c = new MySqlData.MySqlConnection(connection))
            {
                _log.LogTrace <MySqlQuery>($"EXECUTE: {sql}. Parameters: {JsonConvert.SerializeObject(parameters)}");

                await c.OpenAsync();

                result = await c.ExecuteScalarAsync <T>(sql, parameters);

                await c.CloseAsync();
            }

            return(result);
        }
コード例 #2
0
ファイル: MySqlQuery.cs プロジェクト: mrogunlana/Panama.Core
        public void Insert <T>(T obj, Definition definition) where T : class
        {
            var connection = definition.Connection;

            if (string.IsNullOrEmpty(connection))
            {
                connection = _connection;
            }

            using (var c = new MySqlData.MySqlConnection(connection))
            {
                c.Open();

                //NOTE: we can not use DapperExtensions here as they do not support cancellation tokens
                var sql     = _sql.Insert(_sql.Configuration.GetMap <T>());
                var command = new CommandDefinition(sql, obj, cancellationToken: definition.Token, commandTimeout: definition.CommandTimeout);

                _log.LogTrace <MySqlQuery>($"INSERT: {definition.Sql}. Connection: {connection}. Object: {JsonConvert.SerializeObject(obj)}. Parameters: {JsonConvert.SerializeObject(definition.Parameters)}");

                c.ExecuteScalarAsync <T>(command).GetAwaiter().GetResult();
            }
        }
コード例 #3
0
ファイル: MySqlQuery.cs プロジェクト: mrogunlana/Panama.Core
        public T ExecuteScalar <T>(Definition definition)
        {
            var connection = definition.Connection;

            if (string.IsNullOrEmpty(connection))
            {
                connection = _connection;
            }

            T result = default;

            using (var c = new MySqlData.MySqlConnection(connection))
            {
                _log.LogTrace <MySqlQuery>($"EXECUTE: {definition.Sql}. Parameters: {JsonConvert.SerializeObject(definition.Parameters)}");

                c.Open();

                result = c.ExecuteScalarAsync <T>(new CommandDefinition(definition.Sql, definition.Parameters, cancellationToken: definition.Token, commandTimeout: definition.CommandTimeout)).GetAwaiter().GetResult();

                c.Close();
            }

            return(result);
        }
コード例 #4
0
ファイル: MySqlQuery.cs プロジェクト: mrogunlana/Panama.Core
        public void Delete <T>(T obj, Definition definition) where T : class, IModel
        {
            var connection = definition.Connection;

            if (string.IsNullOrEmpty(connection))
            {
                connection = _connection;
            }

            using (var c = new MySqlData.MySqlConnection(connection))
            {
                c.Open();

                //NOTE: we can not use DapperExtensions here as they do not support cancellation tokens
                var sql        = _sql.Delete(_sql.Configuration.GetMap <T>(), definition.Predicate, definition.Dictionary);
                var parameters = new DynamicParameters();
                var map        = _sql.Configuration.GetMap <T>();
                var columns    = map.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity));

                foreach (var property in ReflectionHelper.GetObjectValues(obj).Where(property => columns.Any(c => c.Name == property.Key)))
                {
                    parameters.Add(property.Key, property.Value);
                }

                foreach (var parameter in definition.Dictionary)
                {
                    parameters.Add(parameter.Key, parameter.Value);
                }

                var command = new CommandDefinition(sql, parameters, cancellationToken: definition.Token, commandTimeout: definition.CommandTimeout);

                _log.LogTrace <MySqlQuery>($"DELETE: {definition.Sql}. Connection: {connection}. Object: {JsonConvert.SerializeObject(obj)}. Parameters: {JsonConvert.SerializeObject(definition.Parameters)}");

                var result = c.ExecuteScalarAsync <T>(command).GetAwaiter().GetResult();
            }
        }