/// <summary> /// Builds commands for the given <see cref="JetCreateDatabaseOperation" /> /// by making calls on the given <see cref="MigrationCommandListBuilder" />. /// </summary> /// <param name="operation"> The operation. </param> /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param> /// <param name="builder"> The command builder to use to build the commands. </param> protected virtual void Generate( [NotNull] JetCreateDatabaseOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .Append("CREATE DATABASE ") .Append(_keepBreakingCharactersStringTypeMapping.GenerateSqlLiteral(operation.Name)); builder .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator) .EndCommand(suppressTransaction: true); }
/// <summary> /// 如果脚本不存在,则获得Begin脚本 /// </summary> /// <param name="migrationId">迁移ID</param> /// <returns></returns> public override string GetBeginIfExistsScript(string migrationId) { try { if (Check.IsTraceEnabled(m_oracleLogger?.Logger)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleHistoryRepository, OracleTraceFuncName.GetBeginIfExistsScript); } Check.NotEmpty(migrationId, nameof(migrationId)); RelationalTypeMapping mapping = Dependencies.TypeMappingSource.GetMapping(typeof(string)); return(new StringBuilder() .AppendLine("DECLARE") .AppendLine(" v_Count INTEGER;") .AppendLine("BEGIN") .Append("SELECT COUNT(*) INTO v_Count FROM ") .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema)) .Append(" WHERE ") .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName)) .Append(" = ") .AppendLine(mapping.GenerateSqlLiteral(migrationId)) .AppendLine("IF v_Count = 1 THEN") .ToString()); } catch (Exception ex) { if (Check.IsErrorEnabled(m_oracleLogger?.Logger)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleHistoryRepository, OracleTraceFuncName.GetBeginIfExistsScript, ex.ToString()); } throw; } finally { if (Check.IsTraceEnabled(m_oracleLogger?.Logger)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleHistoryRepository, OracleTraceFuncName.GetBeginIfExistsScript); } } }
protected virtual void ProcessMinimumLengthInternal( IConventionProperty property, MemberInfo memberInfo, string tableName, string columnName, StringBuilder sql, int minLength) { var lengthFunctionName = _databaseProvider.Name switch { "Microsoft.EntityFrameworkCore.SqlServer" => "LEN", "Microsoft.EntityFrameworkCore.Sqlite" => "LENGTH", "Npgsql.EntityFrameworkCore.PostgreSQL" => "LENGTH", "Pomelo.EntityFrameworkCore.MySQL" => "LENGTH", _ => null }; if (lengthFunctionName is null) { return; } sql.Clear(); sql .Append(lengthFunctionName) .Append('(') .Append(_sqlGenerationHelper.DelimitIdentifier(columnName)) .Append(')') .Append(" >= ") .Append(_intTypeMapping.GenerateSqlLiteral(minLength)); var constraintName = $"CK_{tableName}_{columnName}_MinLength"; property.DeclaringEntityType.AddCheckConstraint(constraintName, sql.ToString()); }
protected virtual void Test_GenerateSqlLiteral_helper( RelationalTypeMapping typeMapping, object value, string literalValue) => Assert.Equal(literalValue, typeMapping.GenerateSqlLiteral(value));