public CreateTableStatement BuildStatement()
        {
            var keyMembers = entitySet.ElementType.KeyMembers.Cast <EdmProperty>().ToArray();

            // Only create a CompositePrimaryKeyStatement if there is a composite primary key.
            // If there is just one key member this is handled using a constraint.
            CompositePrimaryKeyStatement compositePrimaryKeyStatement = null;

            if (keyMembers.Length > 1)
            {
                compositePrimaryKeyStatement = new CompositePrimaryKeyStatementBuilder(keyMembers).BuildStatement();
            }

            var simpleColumnCollection = new ColumnStatementCollectionBuilder(entitySet.ElementType.Properties, keyMembers).BuildStatement();
            var foreignKeyCollection   = new ForeignKeyStatementBuilder(associationTypeContainer.GetAssociationTypes(entitySet.Name)).BuildStatement();

            var columnStatements = new List <IStatement>();

            columnStatements.AddRange(simpleColumnCollection);
            columnStatements.AddIfNotNull(compositePrimaryKeyStatement);
            columnStatements.AddRange(foreignKeyCollection);

            return(new CreateTableStatement
            {
                TableName = NameCreator.EscapeName(entitySet.Table),
                ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
            });
        }
Exemplo n.º 2
0
        public InsertStatement BuildStatement()
        {
            var columnNameCollection = new ColumnNameStatementCollectionBuilder(_entityMapping).BuildStatement();

            var columnNameStatements = new List <ISqlStatement>();

            columnNameStatements.AddRange(columnNameCollection);

            var columnParametersBuilder = new ColumnParameterStatementCollectionBuilder(_entity, _entityMapping);;

            var columnParameterStatements = new List <ISqlStatement>();

            columnParameterStatements.AddRange(columnParametersBuilder.BuildStatement());

            return(new InsertStatement
            {
                TableName = NameCreator.EscapeName(_entityMapping.TableName),

                ColumnNameStatementCollection = new ColumnNameStatementCollection(columnNameStatements),

                ColumnParameterStatementCollection = new ColumnParameterStatementCollection(columnParameterStatements),

                Parameters = columnParametersBuilder.Parameters
            });
        }
        public CreateIndexStatementCollection BuildStatement()
        {
            IDictionary <string, CreateIndexStatement> createIndexStatements = new Dictionary <string, CreateIndexStatement>();

            string tableName = NameCreator.EscapeName(_entityMapping.TableName);

            foreach (var indexMapping in _entityMapping.IndexMappings)
            {
                string indexName = GetIndexName(indexMapping);

                // There is a single CreateIndexStatement per indexName
                if (!createIndexStatements.TryGetValue(indexName, out CreateIndexStatement createIndexStatement))
                {
                    createIndexStatement = new CreateIndexStatement
                    {
                        IsUnique  = indexMapping.IsUnique,
                        IndexName = indexName,
                        TableName = tableName,
                        Columns   = new Collection <CreateIndexStatement.IndexColumn>()
                    };

                    createIndexStatements.Add(indexName, createIndexStatement);
                }

                createIndexStatement.Columns.Add(new CreateIndexStatement.IndexColumn
                {
                    ColumnName = indexMapping.PropertyName, // FIXME:
                    Order      = indexMapping.Order
                });
            }

            return(new CreateIndexStatementCollection(createIndexStatements.Values));
        }
Exemplo n.º 4
0
        public CreateTableStatement BuildStatement()
        {
            var keyMembers = _entityMapping.ColumnMappings.Where(p => p.IsKey).ToList();

            // Only create a CompositePrimaryKeyStatement if there is a composite primary key.
            // If there is just one key member this is handled using a constraint.
            CompositePrimaryKeyStatement compositePrimaryKeyStatement = null;

            if (keyMembers.Count > 1)
            {
                compositePrimaryKeyStatement = new CompositePrimaryKeyStatementBuilder(keyMembers).BuildStatement();
            }

            var simpleColumnCollection = new ColumnStatementCollectionBuilder(_entityMapping.ColumnMappings, keyMembers).BuildStatement();

            var foreignKeyCollection = new ForeignKeyStatementBuilder(_model, _entityMapping.ForeignKeyMappings).BuildStatement();

            var columnStatements = new List <ISqlStatement>();

            columnStatements.AddRange(simpleColumnCollection);
            columnStatements.AddIfNotNull(compositePrimaryKeyStatement);
            columnStatements.AddRange(foreignKeyCollection);

            return(new CreateTableStatement
            {
                TableName = NameCreator.EscapeName(_entityMapping.TableName),
                ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
            });
        }
Exemplo n.º 5
0
        public DeleteStatement BuildStatement()
        {
            var keyMembers = _entityMapping.ColumnMappings.Where(p => p.IsKey).ToList();

            var pk      = keyMembers.First();
            var pkValue = _entityMapping.GetColumn(_entity, pk.PropertyName);

            var pkParameter = _parameters.Add(pkValue);

            return(new DeleteStatement
            {
                TableName = NameCreator.EscapeName(_entityMapping.TableName),
                PkName = pk.ColumnName,
                PkParameterName = pkParameter.Name,
                Parameters = _parameters
            });
        }
Exemplo n.º 6
0
        public static string GetTableName(this EntityType entityType)
        {
            MetadataProperty metadataProperty;

            if (!entityType.MetadataProperties.TryGetValue("TableName", false, out metadataProperty))
            {
                return(entityType.Name);
            }

            if (metadataProperty.Value.GetType().Name != "DatabaseName")
            {
                return(entityType.Name);
            }

            object metadataPropertyValue     = metadataProperty.Value;
            Type   metadataPropertyValueType = metadataProperty.Value.GetType();

            // The type DatabaseName is internal. So we need reflection...
            // GetValue() overload with one value was introduces in .net 4.5 so use the overload with two parameters.
            var name = (string)metadataPropertyValueType.GetProperty("Name").GetValue(metadataPropertyValue, null);

            return(NameCreator.EscapeName(name));
        }
        public UpdateStatement BuildStatement()
        {
            var columnSetCollection = new ColumnSetStatementCollectionBuilder(_entity, _entityMapping, _parameters).BuildStatement();

            var columnSetStatements = new List <ISqlStatement>();

            columnSetStatements.AddRange(columnSetCollection);

            var keyMembers = _entityMapping.ColumnMappings.Where(p => p.IsKey).ToList();
            var pk         = keyMembers.First();
            var pkValue    = _entityMapping.GetColumn(_entity, pk.PropertyName);

            var pkParameter = _parameters.Add(pkValue);

            return(new UpdateStatement
            {
                TableName = NameCreator.EscapeName(_entityMapping.TableName),
                ColumnSetStatementCollection = new ColumnSetStatementCollection(columnSetStatements),
                PkColumn = pk.ColumnName,
                PkParameter = pkParameter.Name,
                Parameters = _parameters
            });
        }
        public CreateIndexStatementCollection BuildStatement()
        {
            IDictionary <string, CreateIndexStatement> createIndexStatments = new Dictionary <string, CreateIndexStatement>();

            foreach (var edmProperty in entitySet.ElementType.Properties)
            {
                var indexAnnotations = edmProperty.MetadataProperties
                                       .Select(x => x.Value)
                                       .OfType <IndexAnnotation>();

                string tableName = NameCreator.EscapeName(entitySet.Table);
                foreach (var index in indexAnnotations.SelectMany(ia => ia.Indexes))
                {
                    CreateIndexStatement createIndexStatement;
                    string indexName = GetIndexName(index, edmProperty);
                    if (!createIndexStatments.TryGetValue(indexName, out createIndexStatement))
                    {
                        createIndexStatement = new CreateIndexStatement
                        {
                            IsUnique = index.IsUnique,
                            Name     = indexName,
                            Table    = tableName,
                            Columns  = new Collection <CreateIndexStatement.IndexColumn>()
                        };
                        createIndexStatments.Add(indexName, createIndexStatement);
                    }

                    createIndexStatement.Columns.Add(new CreateIndexStatement.IndexColumn
                    {
                        Name  = edmProperty.Name,
                        Order = index.Order
                    });
                }
            }

            return(new CreateIndexStatementCollection(createIndexStatments.Values));
        }
        public SqliteAssociationType(AssociationType associationType, EntityContainer container)
        {
            FromRoleEntitySetName = associationType.Constraint.FromRole.Name;
            ToRoleEntitySetName   = associationType.Constraint.ToRole.Name;

            string fromTable = container.GetEntitySetByName(FromRoleEntitySetName, true).Table;
            string toTable;

            if (IsSelfReferencing(associationType))
            {
                toTable             = fromTable;
                ToRoleEntitySetName = FromRoleEntitySetName;
            }
            else
            {
                toTable = container.GetEntitySetByName(ToRoleEntitySetName, true).Table;
            }

            FromTableName     = NameCreator.EscapeName(fromTable);
            ToTableName       = NameCreator.EscapeName(toTable);
            ForeignKey        = associationType.Constraint.ToProperties.Select(x => x.Name);
            ForeignPrimaryKey = associationType.Constraint.FromProperties.Select(x => x.Name);
            CascadeDelete     = associationType.Constraint.FromRole.DeleteBehavior == OperationAction.Cascade;
        }
Exemplo n.º 10
0
        public void CreateTableNameTest()
        {
            string result = NameCreator.EscapeName("Test");

            Assert.AreEqual("\"Test\"", result);
        }
 private string GetIndexName(IndexAttribute index, EdmProperty property)
 {
     return(index.Name == null?IndexNameCreator.CreateName(entitySet.ElementType.GetTableName(), property.Name) : NameCreator.EscapeName(index.Name));
 }