Exemplo n.º 1
0
        /// <summary>
        /// Constructs a DmColumn object based on a DmColumnSurrogate object.
        /// </summary>
        public DmColumn ConvertToDmColumn()
        {
            DmColumn dmColumn = DmColumn.CreateColumn(this.ColumnName, DmUtils.GetTypeFromAssemblyQualifiedName(this.DataType));

            dmColumn.dbTypeAllowed = this.dbTypeAllowed;
            if (dmColumn.dbTypeAllowed)
            {
                dmColumn.DbType = (DbType)this.DbType;
            }

            dmColumn.AllowDBNull        = this.AllowDBNull;
            dmColumn.IsReadOnly         = this.IsReadOnly;
            dmColumn.MaxLength          = this.MaxLength;
            dmColumn.IsAutoIncrement    = this.IsAutoIncrement;
            dmColumn.Precision          = this.Precision;
            dmColumn.PrecisionSpecified = this.PrecisionSpecified;
            dmColumn.Scale            = this.Scale;
            dmColumn.ScaleSpecified   = this.ScaleSpecified;
            dmColumn.IsUnique         = this.IsUnique;
            dmColumn.IsUnicode        = this.IsUnicode;
            dmColumn.IsCompute        = this.IsCompute;
            dmColumn.IsUnsigned       = this.IsUnsigned;
            dmColumn.OriginalDbType   = this.OriginalDbType;
            dmColumn.OriginalTypeName = this.OriginalTypeName;
            dmColumn.SetOrdinal(this.Ordinal);

            return(dmColumn);
        }
Exemplo n.º 2
0
        public void DmColumn_Create_Columns()
        {
            var tbl = new DmTable("ServiceTickets");

            var id = new DmColumn <Guid>("ServiceTicketID");

            tbl.Columns.Add(id);
            var key = new DmKey(new DmColumn[] { id });

            tbl.PrimaryKey = key;

            // if is PK, non null allowable
            Assert.False(id.AllowDBNull);
            Assert.Equal("ServiceTicketID", id.ColumnName);
            Assert.Equal(typeof(Guid), id.DataType);
            Assert.Equal(0, id.Ordinal);
            Assert.True(id.IsUnique);

            var titleColumn = new DmColumn <string>("Title");

            tbl.Columns.Add(titleColumn);
            Assert.Equal("Title", titleColumn.ColumnName);
            Assert.Equal(typeof(string), titleColumn.DataType);
            Assert.Equal(1, titleColumn.Ordinal);
            Assert.False(titleColumn.IsUnique);

            var sv = new DmColumn <int>("StatusValue");

            tbl.Columns.Add(sv);
            Assert.Equal("StatusValue", sv.ColumnName);
            Assert.Equal(typeof(Int32), sv.DataType);
            Assert.Equal(2, sv.Ordinal);
            Assert.False(sv.IsUnique);

            var opened = new DmColumn <DateTime>("Opened");

            tbl.Columns.Add(opened);
            Assert.Equal("Opened", opened.ColumnName);
            Assert.Equal(typeof(DateTime), opened.DataType);
            Assert.Equal(3, opened.Ordinal);
            Assert.False(opened.IsUnique);

            var closedColumn = DmColumn.CreateColumn("Closed", typeof(DateTime));

            tbl.Columns.Add(closedColumn);
            Assert.Equal("Closed", closedColumn.ColumnName);
            Assert.Equal(typeof(DateTime), closedColumn.DataType);
            Assert.Equal(4, closedColumn.Ordinal);
            Assert.False(closedColumn.IsUnique);
        }
Exemplo n.º 3
0
        public void DmColumn_Create_Columns()
        {
            var tbl = new DmTable("ServiceTickets");

            var id = new DmColumn <Guid>("ServiceTicketID");

            tbl.Columns.Add(id);
            var key = new DmKey(new DmColumn[] { id });

            tbl.PrimaryKey = key;

            // if is PK, non null allowable
            Assert.Equal(false, id.AllowDBNull);
            Assert.Equal("ServiceTicketID", id.ColumnName);
            Assert.Equal(id.DataType, typeof(Guid));
            Assert.Equal(id.Ordinal, 0);
            Assert.Equal(id.Unique, true);

            var titleColumn = new DmColumn <string>("Title");

            tbl.Columns.Add(titleColumn);
            Assert.Equal("Title", titleColumn.ColumnName);
            Assert.Equal(titleColumn.DataType, typeof(string));
            Assert.Equal(titleColumn.Ordinal, 1);
            Assert.Equal(titleColumn.Unique, false);

            var sv = new DmColumn <int>("StatusValue");

            tbl.Columns.Add(sv);
            Assert.Equal("StatusValue", sv.ColumnName);
            Assert.Equal(sv.DataType, typeof(Int32));
            Assert.Equal(sv.Ordinal, 2);
            Assert.Equal(sv.Unique, false);

            var opened = new DmColumn <DateTime>("Opened");

            tbl.Columns.Add(opened);
            Assert.Equal("Opened", opened.ColumnName);
            Assert.Equal(opened.DataType, typeof(DateTime));
            Assert.Equal(opened.Ordinal, 3);
            Assert.Equal(opened.Unique, false);

            var closedColumn = DmColumn.CreateColumn("Closed", typeof(DateTime));

            tbl.Columns.Add(closedColumn);
            Assert.Equal("Closed", closedColumn.ColumnName);
            Assert.Equal(closedColumn.DataType, typeof(DateTime));
            Assert.Equal(closedColumn.Ordinal, 4);
            Assert.Equal(closedColumn.Unique, false);
        }
Exemplo n.º 4
0
        public void DmColumn_SetOrdinal()
        {
            var tbl = new DmTable("ServiceTickets");

            var id = new DmColumn <Guid>("ServiceTicketID");

            tbl.Columns.Add(id);
            var key = new DmKey(new DmColumn[] { id });

            tbl.PrimaryKey = key;

            var titleColumn = new DmColumn <string>("Title");

            tbl.Columns.Add(titleColumn);
            var sv = new DmColumn <int>("StatusValue");

            tbl.Columns.Add(sv);
            var opened = new DmColumn <DateTime>("Opened");

            tbl.Columns.Add(opened);
            var closedColumn = DmColumn.CreateColumn("Closed", typeof(DateTime));

            tbl.Columns.Add(closedColumn);

            Assert.Equal(0, id.Ordinal);
            Assert.Equal(1, titleColumn.Ordinal);
            Assert.Equal(2, sv.Ordinal);
            Assert.Equal(3, opened.Ordinal);
            Assert.Equal(4, closedColumn.Ordinal);

            sv.SetOrdinal(1);

            Assert.Equal(0, id.Ordinal);
            Assert.Equal(1, sv.Ordinal);
            Assert.Equal(2, titleColumn.Ordinal);
            Assert.Equal(3, opened.Ordinal);
            Assert.Equal(4, closedColumn.Ordinal);

            closedColumn.SetOrdinal(0);

            Assert.Equal(0, closedColumn.Ordinal);
            Assert.Equal(1, id.Ordinal);
            Assert.Equal(2, sv.Ordinal);
            Assert.Equal(3, titleColumn.Ordinal);
            Assert.Equal(4, opened.Ordinal);
        }
Exemplo n.º 5
0
        public List <DmColumn> GetTableDefinition()
        {
            List <DmColumn> columns = new List <DmColumn>();

            // Get the columns definition
            var dmColumnsList        = PostgreSqlManagementUtils.ColumnsForTable(_sqlConnection, _sqlTransaction, TableName);
            var postgreSqlDbMetadata = new PostgreSqlDbMetadata();

            foreach (var c in dmColumnsList.Rows.OrderBy(r => (int)r["ordinal_position"]))
            {
                var typeName = c["data_type"].ToString();
                var name     = c["column_name"].ToString();


                // Gets the datastore owner dbType
                var datastoreDbType = (NpgsqlDbType)postgreSqlDbMetadata.ValidateOwnerDbType(typeName, false, false);
                // once we have the datastore type, we can have the managed type
                Type columnType = postgreSqlDbMetadata.ValidateType(datastoreDbType);

                var dbColumn = DmColumn.CreateColumn(name, columnType);
                dbColumn.OriginalTypeName = typeName;
                dbColumn.SetOrdinal(Convert.ToInt32(c["ordinal_position"]));

                var maxLengthLong = c["character_octet_length"] != DBNull.Value ? Convert.ToInt64(c["character_octet_length"]) : 0;
                dbColumn.MaxLength     = maxLengthLong > Int32.MaxValue ? Int32.MaxValue : (Int32)maxLengthLong;
                dbColumn.Precision     = c["numeric_precision"] != DBNull.Value ? Convert.ToByte(c["numeric_precision"]) : (byte)0;
                dbColumn.Scale         = c["numeric_scale"] != DBNull.Value ? Convert.ToByte(c["numeric_scale"]) : (byte)0;
                dbColumn.AllowDBNull   = (String)c["is_nullable"] != "NO";
                dbColumn.AutoIncrement = typeName.Contains("serial");
                dbColumn.IsUnsigned    = false;

                columns.Add(dbColumn);
            }

            return(columns);
        }