/// <inheritdoc />
        public IEnumerable <IDbViewFieldEntity> GetFields(IDbViewEntity view)
        {
            ISqlObject     owner       = new SqlServerSource("INFORMATION_SCHEMA");
            ISqlExpression tableName   = new SqlServerConstantExpressionFactory().Create(view.Name);
            ISqlExpression tableSchema = new SqlServerConstantExpressionFactory().Create(view.Schema);
            ISqlObject     columnsView = new SqlServerSource(owner, "COLUMNS");

            ISqlObject nameField       = new SqlServerField(columnsView, "COLUMN_NAME", "Name");
            ISqlObject indexField      = new SqlServerField(columnsView, "ORDINAL_POSITION", "Index");
            ISqlObject typeField       = new SqlServerField(columnsView, "DATA_TYPE", "TypeName");
            ISqlObject maxLengthField  = new SqlServerField(columnsView, "CHARACTER_MAXIMUM_LENGTH", "MaxLength");
            ISqlObject isNullableField = new SqlServerField(columnsView, "IS_NULLABLE", "IsNullable");

            ISqlSelectStatement statement = new SqlServerSelectStatement();

            statement.SelectClause.AddExpressions(nameField, indexField, typeField, maxLengthField, isNullableField);
            statement.FromClause.SetSource(columnsView);
            statement.WhereClause.Condition = new SqlServerField(columnsView, "TABLE_NAME", null).Equal(tableName).And(new SqlServerField(columnsView, "TABLE_SCHEMA", null).Equal(tableSchema));
            statement.OrderClause.AddExpression(indexField, SqlOrder.Asc);

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

            builder.DataSource     = view.Database.Source.Name;
            builder.InitialCatalog = view.Database.Name;
            if (!(builder.IntegratedSecurity = view.Database.Source.DbCredential.IntegratedSecurity))
            {
                builder.UserID   = view.Database.Source.DbCredential.UserName;
                builder.Password = view.Database.Source.DbCredential.Password;
            }

            ICollection <IDbViewFieldEntity> fields       = new List <IDbViewFieldEntity>();
            IList <ISqlExpression>           selectFields = new List <ISqlExpression>(statement.SelectClause.Expressions);

            using (IDataReader reader = SqlHelper.ExecuteSelect(new SqlConnection(builder.ConnectionString), statement)) {
                while (reader.Read())
                {
                    fields.Add(new DbViewFieldEntity(reader.GetString(selectFields.IndexOf(nameField)),
                                                     reader.GetInt32(selectFields.IndexOf(indexField)),
                                                     SqlServerUtility.GetRuntimeType(reader.GetString(selectFields.IndexOf(typeField))),
                                                     SqlServerUtility.GetDbType(reader.GetString(selectFields.IndexOf(typeField))),
                                                     (int)SqlServerUtility.GetSqlDbType(reader.GetString(selectFields.IndexOf(typeField))),
                                                     reader.GetString(selectFields.IndexOf(typeField)),
                                                     reader.IsDBNull(selectFields.IndexOf(maxLengthField)) ? 0 : reader.GetInt32(selectFields.IndexOf(maxLengthField)),
                                                     reader.GetString(selectFields.IndexOf(isNullableField)).Equals("YES", StringComparison.OrdinalIgnoreCase),
                                                     view,
                                                     null,
                                                     null));
                }
            }

            return(fields);
        }
Пример #2
0
 /// <inheritdoc />
 public CodeExpression GetIdentityStatement(CodeExpression name)
 {
     return(new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(SqlServerUtility)), TypeUtility.GetMemberName <object, ISqlSelectStatement>((obj) => SqlServerUtility.GetIdentityStatement(null)), name));
 }
Пример #3
0
 /// <inheritdoc />
 public ISqlExpression CreateStartWith(string value)
 {
     return(this.Create(SqlServerUtility.GetBeginLikeString(value)));
 }
Пример #4
0
 /// <inheritdoc />
 public ISqlExpression Create(string value)
 {
     return(new SqlStringExpression(value != null ? string.Format("'{0}'", SqlServerUtility.EncodeString(value)) : string.Empty));
 }
Пример #5
0
 /// <inheritdoc />
 public ISqlExpression CreateContain(string value)
 {
     return(this.Create(SqlServerUtility.GetContainLikeString(value)));
 }
Пример #6
0
 /// <inheritdoc />
 public ISqlExpression CreateEndWith(string value)
 {
     return(this.Create(SqlServerUtility.GetEndLikeString(value)));
 }
Пример #7
0
        /// <summary>
        /// Creates fields from the specified field items.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="items"></param>
        private void CreateFileds(IDbTableEntity table, IDictionary <string, DbConstraintEntity> constraints, IEnumerable <FieldItem> items)
        {
            DbTableFieldEntity  field                       = null;
            DbConstraintEntity  constraint                  = null;
            IDbTableEntity      referencedTable             = null;
            IDbTableFieldEntity referencedField             = null;
            IDictionary <string, DbTableFieldEntity> fields = g_fieldsCache.ContainsKey(table) ?
                                                              g_fieldsCache[table] :
                                                              (g_fieldsCache[table] = new Dictionary <string, DbTableFieldEntity>());

            foreach (FieldItem item in items)
            {
                //determine referenced table and field
                referencedTable = string.IsNullOrWhiteSpace(item.ReferenceTable) ?
                                  null :
                                  (from t in table.Database.Tables
                                   where t.Name.Equals(item.ReferenceTable, StringComparison.OrdinalIgnoreCase)
                                   select t).FirstOrDefault();

                referencedField = string.IsNullOrWhiteSpace(item.ReferenceField) ?
                                  null :
                                  g_fieldsCache.ContainsKey(referencedTable) ?
                                  g_fieldsCache[referencedTable][item.ReferenceField] :
                                  (from f in referencedTable.TableFields
                                   where f.Name.Equals(item.ReferenceField, StringComparison.OrdinalIgnoreCase)
                                   select f).FirstOrDefault();

                //determine field constraint
                if (item.ConstraintType == DbConstraintType.None)
                {
                    constraint = null;
                }
                else
                {
                    if (constraints.ContainsKey(item.ConstraintName))
                    {
                        constraint = constraints[item.ConstraintName];
                    }
                    else
                    {
                        constraints[item.ConstraintName] = constraint = new DbConstraintEntity(item.ConstraintName, table, item.ConstraintType, referencedTable);
                    }
                }

                //check field name
                if (fields.ContainsKey(item.Name))
                {
                    field = fields[item.Name];
                    if (field.ReferencedField == null)
                    {
                        field.ReferencedField = referencedField;
                    }
                }
                else
                {
                    fields[item.Name] = field = new DbTableFieldEntity(item.Name,
                                                                       item.Index,
                                                                       SqlServerUtility.GetRuntimeType(item.Type),
                                                                       SqlServerUtility.GetDbType(item.Type),
                                                                       (int)SqlServerUtility.GetSqlDbType(item.Type),
                                                                       item.Type,
                                                                       item.MaxLength,
                                                                       item.IsNullable,
                                                                       table,
                                                                       item.Description,
                                                                       item.HasDefault,
                                                                       item.IsIdentity,
                                                                       this.m_isReadOnlyCache.ContainsKey(item.Name.ToLower()) ?
                                                                       this.m_isReadOnlyCache[item.Name.ToLower()] :
                                                                       false,
                                                                       referencedField);
                }
                if (constraint != null)
                {
                    field.AddConstraints(constraint);
                    constraint.AddFields(field);
                }
            }
        }