private static string ResolveByDataType(object value, PostgresDataType dataType)
        {
            switch (dataType)
            {
            case PostgresDataType.UUID:
                return(_converters[typeof(Guid)](value));

            case PostgresDataType.TEXT:
                return(_converters[typeof(string)](value));

            case PostgresDataType.INT:
            case PostgresDataType.FLOAT8:
                return(_converters[typeof(int)](value));

            case PostgresDataType.DATE:
                return($"'{((DateTimeOffset)value).ToUniversalTime().ToString("yyyy-MM-dd")}'");

            case PostgresDataType.TIMESTAMPTZ:
                return(_converters[typeof(DateTimeOffset)](value));

            case PostgresDataType.BOOLEAN:
                return(_converters[typeof(bool)](value));

            default:
                throw new ArgumentOutOfRangeException($"'{dataType}' is not a valid postgres data type.");
            }
        }
        private static object ResolveValue(object value,
                                           Type propertyType, PostgresDataType dataType)
        {
            object converted = value;

            switch (dataType)
            {
            case PostgresDataType.TEXT:
                if (propertyType.IsEnum)
                {
                    converted = Enum.Parse(propertyType, (string)value);
                }
                break;

            case PostgresDataType.DATE:
            case PostgresDataType.TIMESTAMPTZ:
                DateTime dt = (DateTime)value;
                dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);

                DateTimeOffset dtOffset = dt;
                converted = dtOffset;
                break;

            case PostgresDataType.UUID:
            case PostgresDataType.INT:
            case PostgresDataType.FLOAT8:
            case PostgresDataType.SQL_IDENTIFIER:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(dataType), $"'{dataType}' is an invalid '{nameof(PostgresDataType)}'.");
            }

            return(converted);
        }
Exemple #3
0
 public override Task CreateDatabaseAsync(Assembly assemblyOfModels)
 {
     return(DbConnection.CreateDatabase(assemblyOfModels,
                                        PostgresDataType.Init()
                                        .SetarRelationDataTypes()
                                        .ObterRelationDataTypes(),
                                        new PostgresDatabase()));
 }
        internal static object Map(object value,
                                   Type propertyType, PostgresDataType dataType)
        {
            if (propertyType.IsNullable())
            {
                return(ResolveValueForNullable(value, propertyType, dataType));
            }

            return(ResolveValue(value, propertyType, dataType));
        }
Exemple #5
0
        private static void SetNameDataType(TableColumn column, ColumnAttribute attr)
        {
            string name = attr != null && !string.IsNullOrWhiteSpace(attr.Name)
                                ? attr.Name
                                : column.PropertyType.Name;

            PostgresDataType dataType = attr != null && attr.DataType.HasValue
                                ? attr.DataType.Value
                                : ToPostgresDataTypeMapper.Map(column.PropertyType);

            column.Name     = name;
            column.DataType = dataType;
        }
        private static object ResolveValueForNullable(object value,
                                                      Type propertyType, PostgresDataType dataType)
        {
            if (value == null || value.GetType() == typeof(DBNull))
            {
                return(null);
            }

            if (propertyType.IsNullableEnum())
            {
                Type nullableType = Nullable.GetUnderlyingType(propertyType);
                return(Enum.Parse(nullableType, (string)value));
            }

            return(ResolveValue(value, propertyType, dataType));
        }
        internal static string Map(object value, PostgresDataType dataType)
        {
            if (value == null)
            {
                return("null");
            }

            switch (dataType)
            {
            case PostgresDataType.UUID:
            case PostgresDataType.SQL_IDENTIFIER:
                return(_converters[typeof(Guid)](value));

            case PostgresDataType.TEXT:
                return(_converters[typeof(string)](value));

            case PostgresDataType.INT:
            case PostgresDataType.FLOAT8:
                return(_converters[typeof(int)](value));

            case PostgresDataType.DATE:
                return($"'{((DateTimeOffset)value).ToUniversalTime().ToString("yyyy-MM-dd")}'");

            case PostgresDataType.TIMESTAMPTZ:
                return(_converters[typeof(DateTimeOffset)](value));

            case PostgresDataType.BOOLEAN:
                return(_converters[typeof(bool)](value));

            default:
                throw new ArgumentOutOfRangeException($"'{dataType}' is not a valid postgres data type.");
            }

            //if (column.DataType.HasValue)
            //{
            //	return ResolveByDataType(value, column.DataType.Value);
            //}

            //return ResolveByPropertyType(value, column);
        }
Exemple #8
0
        public static Func <NpgsqlDataReader, List <TResult> > Create <TResult>(Type propertyType, PostgresDataType dataType)
        {
            return(reader =>
            {
                var result = new List <object>();

                while (reader.Read())
                {
                    Debug.Assert(reader.FieldCount == 1, "Union queries only support selecting a single column.");

                    object value = reader.GetValue(0);
                    object resolvedValue = DbValueToObjectMapper.Map(value, propertyType, dataType);

                    result.Add(resolvedValue);
                }

                return result.Cast <TResult>().ToList();
            });
        }
Exemple #9
0
 public ColumnAttribute(string name, PostgresDataType dataType)
 {
     Name     = name;
     DataType = dataType;
 }