示例#1
0
        public static ColumnBuilder Build(string name, string typeName)
        {
            Type   type    = null;
            string tName   = typeName;
            bool   isArray = false;

            if (tName.EndsWith("[]"))
            {
                isArray = true;
                tName   = tName.TrimEnd("[]".ToCharArray());
            }


            type = Type.GetType(tName, false, true);

            if (type == null)
            {
                type = Type.GetType($"System.{tName}", false, true);
            }

            if (type == null)
            {
                type = PgSqlTypeManager.GetDotNetType(tName);
            }

            if (type == null)
            {
                throw new Exception($"Can't find Type '{typeName}'");
            }

            if (isArray)
            {
                type = typeof(List <>).MakeGenericType(type);
            }

            var col = Build(name, type);

            return(col);
        }
示例#2
0
        internal static TableDefinition<T> FromTable<T>(ITable table)
        {
            var td = TableDefinition.FromType<T>();
            try
            {
                var columns = new DbExecuter(table.GetConnectionString()).ExecuteReader(SQLStatements.GetColumns(table.GetConnectionString().TableName, table.GetConnectionString().SchemaName)).ToArray();
                foreach (var column in columns)
                {

                    Column dbCol = column.ToObject<Column>();

                    var col = td.GetColumnByDbName(dbCol.DbName) ?? td.GetColumnByClrName(dbCol.DbName);

                    if (col == null)
                    {
                        dbCol.DotNetType = PgSqlTypeManager.GetDotNetType(dbCol.PgType);
                        td.AddColumn(dbCol);

                    }
                    else
                    {
                        col.DbName = dbCol.DbName;
                        col.CanBeNull = dbCol.CanBeNull;
                        col.DefaultValue = dbCol.DefaultValue;
                        col.Position = dbCol.Position;
                        col.IsPrimaryKey = dbCol.IsPrimaryKey;
                        col.MustBeUnique = dbCol.MustBeUnique;
                    }

                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "GetTableSchema");
                throw;
            }

            return td;
        }
示例#3
0
        public static JToken ConvertFromDB(object data, NpgsqlDbColumn column)
        {
            if (data == null)
            {
                return(null);
            }

            if (data == DBNull.Value)
            {
                return(null);
            }

            var postgresType = column.DataTypeName;

            if (column.DataType == typeof(Array))
            {
                postgresType = $"{postgresType}[]";
            }

            var type       = PgSqlTypeManager.GetDotNetType(postgresType);
            var npgsqlType = PgSqlTypeManager.GetNpgsqlDbType(postgresType);


            JToken newObject = null;

            switch (npgsqlType)
            {
            case NpgsqlDbType.Json:
            case NpgsqlDbType.Jsonb:
            {
                if (type.IsListType())
                {
                    var list       = new JArray();
                    var enumerable = data as IEnumerable;
                    foreach (var o in enumerable)
                    {
                        var jt = JToken.Parse(o.ToString());
                        //var jo = jt.ToBasicDotNetObject();
                        list.Add(jt);
                    }
                    newObject = list;
                }
                else
                {
                    newObject = JToken.Parse(data.ToString());
                }
                break;
            }

            case NpgsqlDbType.Json | NpgsqlDbType.Array:
            case NpgsqlDbType.Jsonb | NpgsqlDbType.Array:
            {
                var list       = new JArray();
                var enumerable = data as IEnumerable;
                foreach (var o in enumerable)
                {
                    var jt = JToken.Parse(o.ToString());

                    list.Add(jt);
                }
                newObject = list;
                break;
            }

            default:
                newObject = JToken.FromObject(data);
                break;
            }

            return(newObject);
        }