Esempio n. 1
0
        public static T ExecuteScalar <T>(this DbConnection connection, string commandText, object[] parameters = null,
                                          DbTransaction transaction = null, IExceptionProcessor exceptionProcessor = null)
        {
            var result = ExecuteScalar(connection, commandText, parameters, transaction, exceptionProcessor);

            return(ValueMapper.ProcessValue <T>(result));
        }
Esempio n. 2
0
        public void ProcessValue_byteArray_to_string()
        {
            var targetType = typeof(string);
            var expected   = "something";

            var value = Encoding.UTF8.GetBytes(expected);

            ValueMapper.ProcessValue(targetType, value).Should().Be(expected);
        }
Esempio n. 3
0
        public void ProcessValue_byteArray_to_Guid()
        {
            var targetType = typeof(Guid);
            var expected   = Guid.NewGuid();

            var value = expected.ToByteArray();

            ValueMapper.ProcessValue(targetType, value).Should().Be(expected);
        }
Esempio n. 4
0
        public void ProcessValue_empty_string_to_NullableType(Type targetType)
        {
            var value  = "";
            var result = ValueMapper.ProcessValue(targetType, value);

            Output.WriteLine(result?.ToString() ?? "<null>");
            result.Should().BeNull();
            ValidateIsAssignable(targetType, result);
        }
Esempio n. 5
0
        private static object Insert(this DbConnection connection, object data, string tableName,
                                     IFieldInfoCollection fields, DbTransaction transaction = null,
                                     OnConflictOption option                = OnConflictOption.Default,
                                     ICommandBuilder commandBuilder         = null,
                                     IExceptionProcessor exceptionProcessor = null,
                                     object keyValue      = null,
                                     bool persistKeyvalue = true)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            using (var command = connection.CreateCommand())
            {
                commandBuilder.BuildInsert(command, data, tableName, fields,
                                           option: option,
                                           keyValue: keyValue,
                                           persistKeyvalue: persistKeyvalue);

                try
                {
                    var ptData = data as IPersistanceTracking;

                    ptData?.OnInserting();
                    var id = command.ExecuteScalar();
                    ptData?.OnInserted();

                    var pkField = fields.PrimaryKeyField;
                    if (pkField != null)
                    {
                        var value = ValueMapper.ProcessValue(pkField.RunTimeType, id);
                        pkField.SetFieldValue(data, value);

                        return(value);
                    }
                    else
                    {
                        return(id);
                    }
                }
                catch (Exception ex)
                {
                    if (exceptionProcessor is null)
                    {
                        throw;
                    }
                    else
                    {
                        throw exceptionProcessor.ProcessException(ex, connection, (string)null, transaction);
                    }
                }
            }
        }
Esempio n. 6
0
        public void ProcessValue_string_to_DateTime(string value)
        {
            var targetType = typeof(DateTime);
            var expected   = DateTime.Parse(value);

            var result = ValueMapper.ProcessValue(targetType, value);

            result.Should().BeOfType <DateTime>();

            ((DateTime)result).Should().BeCloseTo(expected, 1000);
        }
Esempio n. 7
0
        // ReadData depends on this behavior
        public void ProcessValue_jiberrishString_to_NullableGuid()
        {
            var value = "jibberish";

            Action a = () =>
            {
                ValueMapper.ProcessValue(typeof(Guid?), value).Should().BeNull();
            };

            a.Should().Throw <FormatException>();
        }
Esempio n. 8
0
        public void ProcessValue_empty_string_to_DateTime()
        {
            var targetType = typeof(DateTime);
            var expected   = default(DateTime);

            var value = "";

            var result = ValueMapper.ProcessValue(targetType, value);

            result.Should().BeOfType <DateTime>();

            ((DateTime)result).Should().BeCloseTo(expected, 1000);
        }
Esempio n. 9
0
        public void ProcessValue_value_to_Type(Type targetType, object value, object expected)
        {
            var result = ValueMapper.ProcessValue(targetType, value);

            result.Should().Be(expected);

            if (result != null)
            {
                result.GetType().Should().Be(Nullable.GetUnderlyingType(targetType) ?? targetType);
            }

            ValidateIsAssignable(targetType, result);
        }
Esempio n. 10
0
        public void ProcessValue_DBnull_to_valueType(Type targetType)
        {
            var value    = DBNull.Value;
            var expected = Activator.CreateInstance(targetType);
            var result   = ValueMapper.ProcessValue(targetType, value);

            ValidateIsAssignable(targetType, result);

            if (expected == null)
            {
                expected = value;
            }
            result.Should().Be(expected);
            Output.WriteLine(result?.ToString() ?? "<null>");
        }
Esempio n. 11
0
        public static IEnumerable <TResult> QueryScalar2 <TResult>(this DbConnection connection, string commandText,
                                                                   object paramaters         = null,
                                                                   DbTransaction transaction = null,
                                                                   IExceptionProcessor exceptionProcessor = null)
        {
            var targetType = typeof(TResult);

            using (var command = connection.CreateCommand())
            {
                command.CommandText = commandText;
                command.AddParams(paramaters);

                using (var reader = connection.ExecuteReader(command, transaction: transaction, exceptionProcessor: exceptionProcessor))
                {
                    while (reader.Read())
                    {
                        TResult value = default(TResult);
                        try
                        {
                            var dbValue = reader.GetValue(0);
                            value = (TResult)ValueMapper.ProcessValue(targetType, dbValue);
                        }
                        catch (Exception e)
                        {
                            if (exceptionProcessor != null)
                            {
                                throw exceptionProcessor.ProcessException(e, connection, commandText, transaction);
                            }
                            else
                            {
                                throw;
                            }
                        }

                        yield return(value);
                    }
                }
            }
        }
Esempio n. 12
0
        public void ProcessValue_jiberrishString_to_NullableValue(Type targetType)
        {
            var value = "jibberish";

            ValueMapper.ProcessValue(targetType, value).Should().BeNull();
        }
Esempio n. 13
0
        public void ProcessValue_DBNull_to_DateTime()
        {
            var result = ValueMapper.ProcessValue(typeof(DateTime), DBNull.Value);

            result.Should().Be(default(DateTime));
        }
Esempio n. 14
0
        public void ProcessValue_DBNull_to_string()
        {
            var result = ValueMapper.ProcessValue(typeof(string), DBNull.Value);

            result.Should().Be(null);
        }