public MetadataProperty CreateProperty(ApplicationObject owner, string name, SqlFieldInfo field)
        {
            MetadataProperty property = new MetadataProperty()
            {
                Name     = name,
                DbName   = field.COLUMN_NAME,
                FileName = Guid.Empty,
                Purpose  = PropertyPurpose.System
            };

            SetupPropertyType(owner, property, field);
            return(property);
        }
예제 #2
0
        private List <SqlFieldInfo> GetSqlFields(string tableName)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(@"SELECT");
            sb.AppendLine(@"    ORDINAL_POSITION, COLUMN_NAME, DATA_TYPE,");
            sb.AppendLine(@"    ISNULL(CHARACTER_MAXIMUM_LENGTH, 0) AS CHARACTER_MAXIMUM_LENGTH,");
            sb.AppendLine(@"    ISNULL(NUMERIC_PRECISION, 0) AS NUMERIC_PRECISION,");
            sb.AppendLine(@"    ISNULL(NUMERIC_SCALE, 0) AS NUMERIC_SCALE,");
            sb.AppendLine(@"    CASE WHEN IS_NULLABLE = 'NO' THEN CAST(0x00 AS bit) ELSE CAST(0x01 AS bit) END AS IS_NULLABLE");
            sb.AppendLine(@"FROM");
            sb.AppendLine(@"    INFORMATION_SCHEMA.COLUMNS");
            sb.AppendLine(@"WHERE");
            sb.AppendLine(@"    TABLE_NAME = N'{0}'");
            sb.AppendLine(@"ORDER BY");
            sb.AppendLine(@"    ORDINAL_POSITION ASC;");

            string sql = string.Format(sb.ToString(), tableName);

            List <SqlFieldInfo> list = new List <SqlFieldInfo>();

            using (SqlConnection connection = new SqlConnection(this.ConnectionString))
            {
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            SqlFieldInfo item = new SqlFieldInfo()
                            {
                                ORDINAL_POSITION         = reader.GetInt32(0),
                                COLUMN_NAME              = reader.GetString(1),
                                DATA_TYPE                = reader.GetString(2),
                                CHARACTER_MAXIMUM_LENGTH = reader.GetInt32(3),
                                NUMERIC_PRECISION        = reader.GetByte(4),
                                NUMERIC_SCALE            = reader.GetInt32(5),
                                IS_NULLABLE              = reader.GetBoolean(6)
                            };
                            list.Add(item);
                        }
                    }
                }
            }
            return(list);
        }
 public DatabaseField CreateField(SqlFieldInfo field)
 {
     return(new DatabaseField()
     {
         Name = field.COLUMN_NAME,
         TypeName = field.DATA_TYPE,
         Length = field.CHARACTER_MAXIMUM_LENGTH,
         Scale = field.NUMERIC_SCALE,
         Precision = field.NUMERIC_PRECISION,
         IsNullable = field.IS_NULLABLE,
         Purpose = (field.DATA_TYPE == "timestamp" ||
                    field.COLUMN_NAME == "_version" ||
                    field.COLUMN_NAME == "_Version")
                     ? FieldPurpose.Version
                     : FieldPurpose.Value
     });
 }
        private void SetupPropertyType(ApplicationObject owner, MetadataProperty property, SqlFieldInfo field)
        {
            // TODO: учесть именования типов PostgreSQL, например (mchar, mvarchar)

            if (field.DATA_TYPE == "nvarchar")
            {
                property.PropertyType.CanBeString = true;
            }
            else if (field.DATA_TYPE == "numeric")
            {
                property.PropertyType.CanBeNumeric = true;
            }
            else if (field.DATA_TYPE == "timestamp")
            {
                property.PropertyType.IsBinary = true;
            }
            else if (field.DATA_TYPE == "binary")
            {
                if (field.CHARACTER_MAXIMUM_LENGTH == 1)
                {
                    property.PropertyType.CanBeBoolean = true;
                }
                else if (field.CHARACTER_MAXIMUM_LENGTH == 16)
                {
                    if (field.COLUMN_NAME.ToLowerInvariant().TrimStart('_') == "idrref")
                    {
                        property.PropertyType.IsUuid = true;
                    }
                    else
                    {
                        property.PropertyType.CanBeReference = true;
                        if (owner is TablePart)
                        {
                            property.PropertyType.ReferenceTypeCode = ((TablePart)owner).Owner.TypeCode;
                        }
                        else
                        {
                            property.PropertyType.ReferenceTypeCode = owner.TypeCode;
                        }
                    }
                }
            }
        }
 public string GetPropertyName(SqlFieldInfo field)
 {
     return(LookupPropertyName(field.COLUMN_NAME));
 }