Esempio n. 1
0
        public DbColumnMapping Add(string columnName, string propertyName)
        {
            string delimited = string.Format(_delimiterFormatString, columnName);
              var mapping = new DbColumnMapping(columnName, propertyName, delimited);

              // add the same instance to both dictionaries:
              this.ByColumn.Add(mapping.ColumnName, mapping);
              this.ByProperty.Add(mapping.PropertyName, mapping);
              return mapping;
        }
Esempio n. 2
0
        public BiggyDocumentStore(DbCache dbCache)
        {
            this.DbCache = dbCache;
            this.Model   = this.getModel();

            //this.Model = new BiggyRelationalStore<dynamic>(context);
            this.TableMapping      = this.getTableMappingForT();
            this.PrimaryKeyMapping = this.TableMapping.PrimaryKeyMapping[0];
            SetFullTextColumns();
            TryLoadData();
        }
Esempio n. 3
0
        public BiggyRelationalStore(DbCache dbCache)
        {
            this.Cache        = dbCache;
            this.tableMapping = this.getTableMappingForT();

            // Is there a PK? If so, set the member variable:
            if (this.tableMapping.PrimaryKeyMapping.Count == 1)
            {
                this.PrimaryKeyMapping = this.tableMapping.PrimaryKeyMapping[0];
            }
        }
Esempio n. 4
0
        List <DbColumnMapping> getPrimaryKeyForT()
        {
            List <DbColumnMapping> result = new List <DbColumnMapping>();
            string newTableName           = this.DecideTableName();
            var    baseName       = this.GetBaseName();
            var    acceptableKeys = new string[] { "ID", baseName + "ID" };
            //var props = typeof(T).GetProperties();

            var item     = new T();
            var itemType = item.GetType();
            var props    = itemType.GetProperties();

            // Check for custom attributes first - for doc stores, this should be
            // the primary way to define keys:
            var foundProps = props.Where(p => p.GetCustomAttributes(false)
                                         .Any(a => a.GetType() == typeof(PrimaryKeyAttribute)));

            if (foundProps != null && foundProps.Count() > 0)
            {
                foreach (var pk in foundProps)
                {
                    var attribute   = pk.GetCustomAttributes(false).First(a => a.GetType() == typeof(PrimaryKeyAttribute));
                    var pkAttribute = attribute as PrimaryKeyAttribute;
                    //PrimaryKeyAttribute pkAttribute = pk.GetCustomAttributes(typeof(PrimaryKeyAttribute), false).FirstOrDefault() as PrimaryKeyAttribute;
                    var newMapping = new DbColumnMapping(this.DbCache.DbDelimiterFormatString);
                    newMapping.TableName         = newTableName;
                    newMapping.ColumnName        = pk.Name;
                    newMapping.DataType          = pk.PropertyType;
                    newMapping.PropertyName      = pk.Name;
                    newMapping.IsPrimaryKey      = true;
                    newMapping.IsAutoIncementing = pkAttribute.IsAutoIncrementing;
                    result.Add(newMapping);
                }
            }
            else
            {
                // No custom attributes were found. Do your best with column names:
                var conventionalKey = props.FirstOrDefault(x => x.Name.Equals("id", StringComparison.OrdinalIgnoreCase)) ??
                                      props.FirstOrDefault(x => x.Name.Equals(baseName + "ID", StringComparison.OrdinalIgnoreCase));

                var newMapping = new DbColumnMapping(this.DbCache.DbDelimiterFormatString);
                newMapping.DataType          = typeof(int);
                newMapping.ColumnName        = conventionalKey.Name;
                newMapping.PropertyName      = conventionalKey.Name;
                newMapping.IsPrimaryKey      = true;
                newMapping.IsAutoIncementing = newMapping.DataType == typeof(int);
                result.Add(newMapping);
            }
            if (result.Count == 0)
            {
                throw new InvalidOperationException("Can't tell what the primary key is. You can use ID, " + baseName + "ID, or specify with the PrimaryKey attribute");
            }
            return(result);
        }
Esempio n. 5
0
        public DbColumnMapping Add(string columnName, string propertyName)
        {
            string delimited = string.Format(_delimiterFormatString, columnName);
            var    mapping   = new DbColumnMapping(_delimiterFormatString);

            mapping.ColumnName   = columnName;
            mapping.PropertyName = propertyName;

            // add the same instance to both dictionaries:
            this.ByColumn.Add(mapping.ColumnName, mapping);
            this.ByProperty.Add(mapping.PropertyName, mapping);
            return(mapping);
        }
Esempio n. 6
0
        DbColumnMapping getPrimaryKeyForT()
        {
            DbColumnMapping result = new DbColumnMapping(this.DbCache.DbDelimiterFormatString);

            result.TableName = this.DecideTableName();
            var baseName        = this.GetBaseName();
            var acceptableKeys  = new string[] { "ID", baseName + "ID" };
            var props           = typeof(T).GetProperties();
            var conventionalKey = props.FirstOrDefault(x => x.Name.Equals("id", StringComparison.OrdinalIgnoreCase)) ??
                                  props.FirstOrDefault(x => x.Name.Equals(baseName + "ID", StringComparison.OrdinalIgnoreCase));

            if (conventionalKey == null)
            {
                var foundProp = props
                                .FirstOrDefault(p => p.GetCustomAttributes(false)
                                                .Any(a => a.GetType() == typeof(PrimaryKeyAttribute)));

                if (foundProp != null)
                {
                    result.ColumnName   = foundProp.Name;
                    result.DataType     = foundProp.PropertyType;
                    result.PropertyName = foundProp.Name;
                }
            }
            else
            {
                result.DataType     = typeof(int);
                result.ColumnName   = conventionalKey.Name;
                result.PropertyName = conventionalKey.Name;
            }
            result.IsPrimaryKey      = true;
            result.IsAutoIncementing = result.DataType == typeof(int);
            if (String.IsNullOrWhiteSpace(result.ColumnName))
            {
                throw new InvalidOperationException("Can't tell what the primary key is. You can use ID, " + baseName + "ID, or specify with the PrimaryKey attribute");
            }
            return(result);
        }
Esempio n. 7
0
 public DbColumnMapping Add(DbColumnMapping mapping)
 {
     this.ByColumn.Add(mapping.ColumnName, mapping);
       this.ByProperty.Add(mapping.PropertyName, mapping);
       return mapping;
 }
Esempio n. 8
0
        public virtual DBTableMapping getTableMappingFor <T>() where T : new()
        {
            var result     = new DBTableMapping(this.DbDelimiterFormatString);
            var item       = new T();
            var itemType   = item.GetType();
            var properties = itemType.GetProperties();

            string replaceString = "[^a-zA-Z1-9]";
            var    rgx           = new Regex(replaceString);

            string flattenedItemTypeName = rgx.Replace(itemType.Name.ToLower(), "");
            string plural      = Inflector.Inflector.Pluralize(flattenedItemTypeName);
            var    dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == flattenedItemTypeName);

            if (dbTableName == null)
            {
                dbTableName = this.DbTableNames.FirstOrDefault(t => rgx.Replace(t.ToLower(), "") == plural);
            }
            else
            {
                var tableNameAttribute = itemType.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbTableAttribute)) as DbTableAttribute;
                if (tableNameAttribute != null)
                {
                    dbTableName = tableNameAttribute.Name;
                }
            }

            result.DBTableName    = dbTableName;
            result.MappedTypeName = itemType.Name;

            var dbColumnInfo = from c in this.DbColumnsList where c.TableName == dbTableName select c;

            foreach (var property in properties)
            {
                string          flattenedPropertyName = rgx.Replace(property.Name.ToLower(), "");
                DbColumnMapping columnMapping         = dbColumnInfo.FirstOrDefault(c => rgx.Replace(c.ColumnName.ToLower(), "") == flattenedPropertyName);
                if (columnMapping != null)
                {
                    columnMapping.PropertyName = property.Name;
                    columnMapping.DataType     = itemType;
                }
                else
                {
                    // Look for a custom column name attribute:
                    DbColumnAttribute mappedColumnAttribute = null;
                    var attribute = property.GetCustomAttributes(false).FirstOrDefault(a => a.GetType() == typeof(DbColumnAttribute));
                    if (attribute != null)
                    {
                        // Use the column name found in the attribute:
                        mappedColumnAttribute = attribute as DbColumnAttribute;
                        string matchColumnName = mappedColumnAttribute.Name;
                        columnMapping = dbColumnInfo.FirstOrDefault(c => c.ColumnName == matchColumnName);
                        columnMapping.PropertyName = property.Name;
                        columnMapping.DataType     = itemType;
                    }
                }
                if (columnMapping != null)
                {
                    result.ColumnMappings.Add(columnMapping);
                    if (columnMapping.IsPrimaryKey)
                    {
                        result.PrimaryKeyMapping.Add(columnMapping);
                    }
                }
            }
            return(result);
        }
Esempio n. 9
0
 public DbColumnMapping Add(DbColumnMapping mapping)
 {
     this.ByColumn.Add(mapping.ColumnName, mapping);
     this.ByProperty.Add(mapping.PropertyName, mapping);
     return(mapping);
 }