Exemplo n.º 1
0
        void SetCreateCommand()
        {
            StringBuilder stringBuilder = new StringBuilder(MySqlConnection.StringBuilderCapacity);

            stringBuilder
            .Append("CREATE TABLE ")
            .Append(MySqlConnection.SchemaCreationPriority == SchemaCreationPriority.DatabaseFirst ? "IF NOT EXISTS " : "")
            .Append(Entity.Table.Name)
            .AppendLine("(");

            foreach (EntityField field in Entity.Resource.All)
            {
                stringBuilder
                .Append(field.Names[Entity])
                .Append(" ")
                .Append(MySqlTypeConverter.ConvertToMySqlTypeAsString(field))
                .Append((field.NotNull != null || field.PrimaryKey != null) ? " NOT NULL," : ",")
                .AppendLine();
            }

            stringBuilder
            .Append("PRIMARY KEY (");

            foreach (EntityField field in Entity.Resource.Primary)
            {
                stringBuilder
                .Append(field.Names[Entity])
                .Append(",");
            }

            stringBuilder = CutLastComma(stringBuilder);

            stringBuilder
            .Append("),")
            .AppendLine();

            foreach (Entity foreignEntity in Entity.Resource.Dictionary.Keys)
            {
                stringBuilder
                .Append("FOREIGN KEY (");

                foreach (EntityField foreignField in Entity.Resource.Dictionary[foreignEntity])
                {
                    stringBuilder
                    .Append(foreignField.Names[Entity])
                    .Append(",");
                }

                stringBuilder = CutLastComma(stringBuilder);
                stringBuilder
                .Append(") REFERENCES ")
                .Append(foreignEntity.Table.Name)
                .Append("(");

                foreach (EntityField foreignField in Entity.Resource.Dictionary[foreignEntity])
                {
                    stringBuilder
                    .Append(foreignField.Names[foreignEntity])
                    .Append(",");
                }

                stringBuilder = CutLastComma(stringBuilder);
                stringBuilder
                .Append("),")
                .AppendLine();
            }

            stringBuilder = CutLastComma(stringBuilder);

            stringBuilder.AppendLine(")");

            CreateCommand             = new SqlCommand(stringBuilder.ToString(), MySqlConnection.Connection);
            CreateCommand.CommandType = CommandType.Text;
        }
        void SetCreateCommand()
        {
            StringBuilder createCommandBuilder = new StringBuilder(MySqlDatabase.StringBuilderCapacity);

            createCommandBuilder
            .Append("CREATE TABLE ")
            .Append("IF NOT EXISTS ")
            .Append(Entity.Table.Name)
            .AppendLine("(");

            foreach (EntityField field in Entity.Resource.All)
            {
                createCommandBuilder
                .Append(field.Names[Entity])
                .Append(" ")
                .Append(MySqlTypeConverter.ConvertToMySqlTypeAsString(field))
                .Append((field.NotNull != null || field.PrimaryKey != null) ? " NOT NULL," : ",")
                .AppendLine();
            }

            createCommandBuilder
            .Append("PRIMARY KEY (");

            foreach (EntityField field in Entity.Resource.Primary)
            {
                createCommandBuilder
                .Append(field.Names[Entity])
                .Append(",");
            }

            createCommandBuilder = CutLast(createCommandBuilder, ",");

            createCommandBuilder
            .Append("),")
            .AppendLine();

            //foreach(Entity foreignEntity in Entity.Resource.ForeignEntityPrimaryFields.Keys)

            foreach (Entity foreignEntity in Entity.Resource.ForeignEntityPrimaryFields.Keys)
            {
                createCommandBuilder
                .Append("FOREIGN KEY (");

                foreach (EntityField foreignField in Entity.Resource.ForeignEntityPrimaryFields[foreignEntity])
                {
                    createCommandBuilder
                    .Append(foreignField.Names[Entity])
                    .Append(",");
                }

                createCommandBuilder = CutLast(createCommandBuilder, ",");
                createCommandBuilder
                .Append(") REFERENCES ")
                .Append(foreignEntity.Table.Name)
                .Append("(");

                foreach (EntityField foreignField in Entity.Resource.ForeignEntityPrimaryFields[foreignEntity])
                {
                    createCommandBuilder
                    .Append(foreignField.Names[foreignEntity])
                    .Append(",");
                }

                createCommandBuilder = CutLast(createCommandBuilder, ",");
                createCommandBuilder
                .Append("),")
                .AppendLine();
            }

            createCommandBuilder = CutLast(createCommandBuilder, ",");

            createCommandBuilder.AppendLine(")");

            CreateCommand             = new MySqlCommand(createCommandBuilder.ToString(), MySqlConnection.Connection);
            CreateCommand.CommandType = CommandType.Text;
        }