Esempio n. 1
0
 public bool Equals(DbColumn obj)
 {
     var eql = Name.Equals(obj.Name);
     eql &= Type.Equals(obj.Type);
     eql &= IsPrimaryKey.Equals(obj.IsPrimaryKey);
     eql &= Nullable.Equals(obj.Nullable);
     eql &= Precision.Equals(obj.Precision);
     eql &= Scale.Equals(obj.Scale);
     eql &= Limit.Equals(obj.Limit);
     eql &= AutoIncrement.Equals(obj.AutoIncrement);
     eql &= DefaultValue != null ? DefaultValue.Equals(obj.DefaultValue) : DefaultValue == obj.DefaultValue;
     return eql;
 }
Esempio n. 2
0
        public ObjectMapper(Type type)
        {
            var t = Objects.TableMapper.GetTableMapper(type);
            TableName = t.Name;

            var ignoreColumns = type.GetCustomAttributes(typeof(IgnoreColumnsAttribute), true).FirstOrDefault() as IgnoreColumnsAttribute;
            if (ignoreColumns != null) {
                IgnoreColumns = ignoreColumns.Columns;
            } else {
                IgnoreColumns = new List<string>();
            }
            IsSynchronizable = type.GetCustomAttributes(typeof(NotSynchronizableAttribute), true).Length == 0;

            Columns = new List<DbColumn>();
            foreach (var col in t.Columns) {
                var dbcolumn = new DbColumn();
                var columnAttribute = col.PropertyInfo.GetAttribute<ColumnAttribute>();
                dbcolumn.Name = columnAttribute == null ? col.Name : columnAttribute.Name;
                dbcolumn.Type = col.PropertyInfo.GetAttribute<DbTypeAttribute>(typeof(DbTypeAttribute), GetDbType).DbType;
                dbcolumn.IsPrimaryKey = col.PropertyInfo.HasAttribute(typeof(PrimaryKeyAttribute)) || col.Name.Equals("id") || (columnAttribute != null && columnAttribute.IsPrimaryKey);
                dbcolumn.Nullable = !col.PropertyInfo.HasAttribute(typeof(NotNullableAttribute));
                if (col.Name.Equals("id"))
                    dbcolumn.Nullable = false;
                if (col.PropertyInfo.HasAttribute(typeof(NumericAttribute))) {
                    var num = col.PropertyInfo.GetAttribute<NumericAttribute>();
                    dbcolumn.Scale = num.Scale;
                    dbcolumn.Precision = num.Precision;
                }
                if (col.PropertyInfo.HasAttribute(typeof(ColumnLimitAttribute))) {
                    dbcolumn.Limit = col.PropertyInfo.GetAttribute<ColumnLimitAttribute>().Limit;
                } else if (dbcolumn.Type == DbType.String) {
                    dbcolumn.Limit = 255;
                }
                dbcolumn.AutoIncrement = col.PropertyInfo.HasAttribute(typeof(AutoIncrementAttribute)) || col.Name.Equals("id");
                if (col.PropertyInfo.HasAttribute(typeof(DefaultValueAttribute))) {
                    dbcolumn.DefaultValue = col.PropertyInfo.GetAttribute<DefaultValueAttribute>().Value;
                }
                dbcolumn.IsSynchronizable = !col.PropertyInfo.HasAttribute(typeof(NotSynchronizableAttribute));
                Columns.Add(dbcolumn);
            }
        }
Esempio n. 3
0
        public override List<DbColumn> GetColumns(string tableName)
        {
            var list = new List<DbColumn>();
            using (var reader = ExecuteReader(string.Format("DESCRIBE {0};", tableName))) {
                while (reader.Read()) {
                    var column = new DbColumn();
                    column.Name = reader.GetString(0);
                    var type = Regex.Match(reader.GetString(1), @"(\w+)\(?(\d+)?,?(\d+)?\)?");
                    column.Type = GetTypeFromDb(type.Groups[1].Value);

                    if (column.Type == Migrate.DbType.String || column.Type == Migrate.DbType.Char) {
                        column.Limit = int.Parse(type.Groups[2].Value);
                    } else {
                        if (column.Type != DbType.Integer) {
                            if (!string.IsNullOrEmpty(type.Groups[2].Value)) {
                                column.Precision = int.Parse(type.Groups[2].Value);
                            }
                            if (!string.IsNullOrEmpty(type.Groups[3].Value)) {
                                column.Scale = int.Parse(type.Groups[3].Value);
                            }
                        }
                    }
                    if (column.Type == DbType.Short) {
                        if (column.Precision == 1) {
                            column.Type = DbType.Boolean;
                        }
                        column.Precision = 0;
                    }

                    column.Nullable = reader.GetString(2) == "YES";
                    column.IsPrimaryKey = reader.GetString(3) == "PRI";
                    column.DefaultValue = reader.IsDBNull(4) ? null : reader.GetString(4);
                    if (!string.IsNullOrEmpty(column.DefaultValue) && (column.Type == Migrate.DbType.String || column.Type == Migrate.DbType.Char)) {
                        column.DefaultValue = string.Format("'{0}'", column.DefaultValue);
                    }
                    column.AutoIncrement = reader.GetString(5) == "auto_increment";
                    list.Add(column);
                }
            }
            return list;
        }
Esempio n. 4
0
        public override List<DbColumn> GetColumns(string tableName)
        {
            var list = new List<DbColumn>();
            var pk = (string)ExecuteScalar(string.Format("SELECT c.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,INFORMATION_SCHEMA.KEY_COLUMN_USAGE c WHERE pk.TABLE_NAME = '{0}' and	CONSTRAINT_TYPE = 'PRIMARY KEY' and	c.TABLE_NAME = pk.TABLE_NAME and	c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME", tableName));
            using (var reader = ExecuteReader(string.Format("EXEC sp_columns '{0}'", tableName))) {
                while (reader.Read()) {
                    var column = new DbColumn();
                    column.Name = reader.GetString(3);
                    var type = reader.GetString(5);
                    if (type.Contains("identity")) {
                        column.AutoIncrement = true;
                        type = type.Replace("identity", "").Trim();
                    }
                    column.Type = GetTypeFromDb(type);

                    if (column.Type == Migrate.DbType.String || column.Type == Migrate.DbType.Char) {
                        column.Limit = reader.GetInt32(7);
                    } else {
                        if (column.Type != DbType.Integer && column.Type != DbType.DateTime) {
                            column.Precision = reader.GetInt32(6);
                            if (!reader.IsDBNull(7)) {
                                column.Scale = reader.GetInt32(7);
                            }
                        }
                    }
                    if (column.Type == DbType.Short) {
                        if (column.Precision == 1) {
                            column.Type = DbType.Boolean;
                        }
                        column.Precision = 0;
                    }
                    column.IsPrimaryKey = column.Name == pk;
                    Console.WriteLine(reader.GetValue(10).GetType().Name);
                    column.Nullable = reader.GetInt16(10) == 1;
                    column.DefaultValue = reader.IsDBNull(12) ? null : reader.GetString(12);
                    list.Add(column);
                }
            }
            return list;
        }