Inheritance: SqlGenerator
    protected override DbCommandDefinition CreateDbCommandDefinition(
        DbProviderManifest providerManifest, DbCommandTree commandTree)
    {
      if (commandTree == null)
        throw new ArgumentNullException("commandTree");

      SqlGenerator generator = null;
      if (commandTree is DbQueryCommandTree)
        generator = new SelectGenerator();
      else if (commandTree is DbInsertCommandTree)
        generator = new InsertGenerator();
      else if (commandTree is DbUpdateCommandTree)
        generator = new UpdateGenerator();
      else if (commandTree is DbDeleteCommandTree)
        generator = new DeleteGenerator();
      else if (commandTree is DbFunctionCommandTree)
        generator = new FunctionGenerator();

      string sql = generator.GenerateSQL(commandTree);

      EFMySqlCommand cmd = new EFMySqlCommand();
      cmd.CommandText = sql;
      if (generator is FunctionGenerator)
        cmd.CommandType = (generator as FunctionGenerator).CommandType;

      SetExpectedTypes(commandTree, cmd);

      EdmFunction function = null;
      if (commandTree is DbFunctionCommandTree)
        function = (commandTree as DbFunctionCommandTree).EdmFunction;

      // Now make sure we populate the command's parameters from the CQT's parameters:
      foreach (KeyValuePair<string, TypeUsage> queryParameter in commandTree.Parameters)
      {
        DbParameter parameter = cmd.CreateParameter();
        parameter.ParameterName = queryParameter.Key;
        parameter.Direction = ParameterDirection.Input;
        parameter.DbType = Metadata.GetDbType(queryParameter.Value);

        FunctionParameter funcParam;
        if (function != null &&
            function.Parameters.TryGetValue(queryParameter.Key, false, out funcParam))
        {
          parameter.ParameterName = funcParam.Name;
          parameter.Direction = Metadata.ModeToDirection(funcParam.Mode);
          parameter.DbType = Metadata.GetDbType(funcParam.TypeUsage);
        }
        cmd.Parameters.Add(parameter);
      }

      // Now add parameters added as part of SQL gen 
      foreach (DbParameter p in generator.Parameters)
        cmd.Parameters.Add(p);

      return CreateCommandDefinition(cmd);
    }
Example #2
0
        protected virtual MigrationStatement Generate(HistoryOperation op)
        {
            if (op == null)
            {
                return(null);
            }

            MigrationStatement stmt = new MigrationStatement();

            var          cmdStr    = "";
            SqlGenerator generator = new SelectGenerator();

            foreach (var commandTree in op.CommandTrees)
            {
                switch (commandTree.CommandTreeKind)
                {
                case DbCommandTreeKind.Insert:
                    generator = new InsertGenerator();
                    break;

                case DbCommandTreeKind.Delete:
                    generator = new DeleteGenerator();
                    break;

                case DbCommandTreeKind.Update:
                    generator = new UpdateGenerator();
                    break;

                case DbCommandTreeKind.Query:
                    generator = new SelectGenerator();
                    break;

                case DbCommandTreeKind.Function:
                    generator = new FunctionGenerator();
                    break;

                default:
                    throw new NotImplementedException(commandTree.CommandTreeKind.ToString());
                }
                cmdStr = generator.GenerateSQL(commandTree);

                ReplaceParemeters(ref cmdStr, generator.Parameters);
                stmt.Sql += cmdStr.Replace("dbo", "") + ";";
            }
            return(stmt);
        }
Example #3
0
        public override string GenerateProcedureBody(ICollection <DbModificationCommandTree> commandTrees, string rowsAffectedParameter, string providerManifestToken)
        {
            MySqlConnection    con  = new MySqlConnection();
            MigrationStatement stmt = new MigrationStatement();

            _providerManifest = DbProviderServices.GetProviderServices(con).GetProviderManifest(providerManifestToken);

            var          cmdStr    = "";
            SqlGenerator generator = new SelectGenerator();

            foreach (var commandTree in commandTrees)
            {
                switch (commandTree.CommandTreeKind)
                {
                case DbCommandTreeKind.Insert:
                    generator = new InsertGenerator();
                    cmdStr    = generator.GenerateSQL(commandTree);
                    break;

                case DbCommandTreeKind.Delete:
                    generator = new DeleteGenerator();
                    cmdStr    = generator.GenerateSQL(commandTree);
                    break;

                case DbCommandTreeKind.Update:
                    generator = new UpdateGenerator();
                    cmdStr    = generator.GenerateSQL(commandTree);
                    break;

                case DbCommandTreeKind.Query:
                    generator = new SelectGenerator();
                    cmdStr    = generator.GenerateSQL(commandTree);
                    break;

                case DbCommandTreeKind.Function:
                    generator = new FunctionGenerator();
                    cmdStr    = generator.GenerateSQL(commandTree);
                    break;
                }
                stmt.Sql += cmdStr.Replace("dbo.", "") + ";";
            }
            return(stmt.Sql);
        }
    public override string GenerateProcedureBody(ICollection<DbModificationCommandTree> commandTrees, string rowsAffectedParameter, string providerManifestToken)
    {
      MySqlConnection con = new MySqlConnection();
      MigrationStatement stmt = new MigrationStatement();
      _providerManifest = DbProviderServices.GetProviderServices(con).GetProviderManifest(providerManifestToken);

      var cmdStr = "";
      SqlGenerator generator = new SelectGenerator();
      foreach (var commandTree in commandTrees)
      {
        switch (commandTree.CommandTreeKind)
        {
          case DbCommandTreeKind.Insert:
            generator = new InsertGenerator();
            cmdStr = generator.GenerateSQL(commandTree);
            break;
          case DbCommandTreeKind.Delete:
            generator = new DeleteGenerator();
            cmdStr = generator.GenerateSQL(commandTree);
            break;
          case DbCommandTreeKind.Update:
            generator = new UpdateGenerator();
            cmdStr = generator.GenerateSQL(commandTree);
            break;
          case DbCommandTreeKind.Query:
            generator = new SelectGenerator();
            cmdStr = generator.GenerateSQL(commandTree);
            break;
          case DbCommandTreeKind.Function:
            generator = new FunctionGenerator();
            cmdStr = generator.GenerateSQL(commandTree);
            break;
        }
        stmt.Sql += cmdStr.Replace("dbo.", "") + ";";
      }
      return stmt.Sql;
    }
    protected virtual MigrationStatement Generate(HistoryOperation op)
    {
      if (op == null) return null;

      MigrationStatement stmt = new MigrationStatement();

      var cmdStr = "";
      SqlGenerator generator = new SelectGenerator();
      foreach (var commandTree in op.CommandTrees)
      {
        switch (commandTree.CommandTreeKind)
        {
          case DbCommandTreeKind.Insert:
            generator = new InsertGenerator();
            break;
          case DbCommandTreeKind.Delete:
            generator = new DeleteGenerator();
            break;
          case DbCommandTreeKind.Update:
            generator = new UpdateGenerator();
            break;
          case DbCommandTreeKind.Query:
            generator = new SelectGenerator();
            break;
          case DbCommandTreeKind.Function:
            generator = new FunctionGenerator();
            break;
          default:
            throw new NotImplementedException(commandTree.CommandTreeKind.ToString());
        }
        cmdStr = generator.GenerateSQL(commandTree);

        ReplaceParemeters(ref cmdStr, generator.Parameters);
        stmt.Sql += cmdStr.Replace("dbo", "") + ";";
      }
      return stmt;
    }
    private MigrationStatement Generate(UpdateDatabaseOperation updateDatabaseOperation)
    {
      if (updateDatabaseOperation == null)
        throw new ArgumentNullException("UpdateDatabaseOperation");

      MigrationStatement statement = new MigrationStatement();
      StringBuilder sql = new StringBuilder();
      const string idempotentScriptName = "_idempotent_script";
      SelectGenerator generator = new SelectGenerator();

      if (!updateDatabaseOperation.Migrations.Any())
        return statement;

      sql.AppendFormat("DROP PROCEDURE IF EXISTS `{0}`;", idempotentScriptName);
      sql.AppendLine();
      sql.AppendLine();

      sql.AppendLine("DELIMITER //");
      sql.AppendLine();

      sql.AppendFormat("CREATE PROCEDURE `{0}`()", idempotentScriptName);
      sql.AppendLine();
      sql.AppendLine("BEGIN");
      sql.AppendLine("  DECLARE CurrentMigration TEXT;");
      sql.AppendLine();
      sql.AppendLine("  IF EXISTS(SELECT 1 FROM information_schema.tables ");
      sql.AppendLine("  WHERE table_name = '__MigrationHistory' ");
      sql.AppendLine("  AND table_schema = DATABASE()) THEN ");

      foreach (var historyQueryTree in updateDatabaseOperation.HistoryQueryTrees)
      {
        string historyQuery = generator.GenerateSQL(historyQueryTree);
        ReplaceParemeters(ref historyQuery, generator.Parameters);
        sql.AppendLine(@"    SET CurrentMigration = (" + historyQuery + ");");
        sql.AppendLine("  END IF;");
        sql.AppendLine();
      }

      sql.AppendLine("  IF CurrentMigration IS NULL THEN");
      sql.AppendLine("    SET CurrentMigration = '0';");
      sql.AppendLine("  END IF;");
      sql.AppendLine();

      // Migrations
      foreach (var migration in updateDatabaseOperation.Migrations)
      {
        if (migration.Operations.Count == 0)
          continue;

        sql.AppendLine("  IF CurrentMigration < '" + migration.MigrationId + "' THEN ");
        var statements = Generate(migration.Operations, _providerManifestToken);
        foreach (var migrationStatement in statements)
        {
          string sqlStatement = migrationStatement.Sql;
          if (!sqlStatement.EndsWith(";"))
            sqlStatement += ";";
          sql.AppendLine(sqlStatement);
        }
        sql.AppendLine("  END IF;");
        sql.AppendLine();
      }

      sql.AppendLine("END //");
      sql.AppendLine();
      sql.AppendLine("DELIMITER ;");
      sql.AppendLine();
      sql.AppendFormat("CALL `{0}`();", idempotentScriptName);
      sql.AppendLine();
      sql.AppendLine();
      sql.AppendFormat("DROP PROCEDURE IF EXISTS `{0}`;", idempotentScriptName);
      sql.AppendLine();

      statement.Sql = sql.ToString();

      return statement;
    }
Example #7
0
        private MigrationStatement Generate(UpdateDatabaseOperation updateDatabaseOperation)
        {
            if (updateDatabaseOperation == null)
            {
                throw new ArgumentNullException("UpdateDatabaseOperation");
            }

            MigrationStatement statement            = new MigrationStatement();
            StringBuilder      sql                  = new StringBuilder();
            const string       idempotentScriptName = "_idempotent_script";
            SelectGenerator    generator            = new SelectGenerator();

            if (!updateDatabaseOperation.Migrations.Any())
            {
                return(statement);
            }

            sql.AppendFormat("DROP PROCEDURE IF EXISTS `{0}`;", idempotentScriptName);
            sql.AppendLine();
            sql.AppendLine();

            sql.AppendLine("DELIMITER //");
            sql.AppendLine();

            sql.AppendFormat("CREATE PROCEDURE `{0}`()", idempotentScriptName);
            sql.AppendLine();
            sql.AppendLine("BEGIN");
            sql.AppendLine("  DECLARE CurrentMigration TEXT;");
            sql.AppendLine();
            sql.AppendLine("  IF EXISTS(SELECT 1 FROM information_schema.tables ");
            sql.AppendLine("  WHERE table_name = '__MigrationHistory' ");
            sql.AppendLine("  AND table_schema = DATABASE()) THEN ");

            foreach (var historyQueryTree in updateDatabaseOperation.HistoryQueryTrees)
            {
                string historyQuery = generator.GenerateSQL(historyQueryTree);
                ReplaceParemeters(ref historyQuery, generator.Parameters);
                sql.AppendLine(@"    SET CurrentMigration = (" + historyQuery + ");");
                sql.AppendLine("  END IF;");
                sql.AppendLine();
            }

            sql.AppendLine("  IF CurrentMigration IS NULL THEN");
            sql.AppendLine("    SET CurrentMigration = '0';");
            sql.AppendLine("  END IF;");
            sql.AppendLine();

            // Migrations
            foreach (var migration in updateDatabaseOperation.Migrations)
            {
                if (migration.Operations.Count == 0)
                {
                    continue;
                }

                sql.AppendLine("  IF CurrentMigration < '" + migration.MigrationId + "' THEN ");
                var statements = Generate(migration.Operations, _providerManifestToken);
                foreach (var migrationStatement in statements)
                {
                    string sqlStatement = migrationStatement.Sql;
                    if (!sqlStatement.EndsWith(";"))
                    {
                        sqlStatement += ";";
                    }
                    sql.AppendLine(sqlStatement);
                }
                sql.AppendLine("  END IF;");
                sql.AppendLine();
            }

            sql.AppendLine("END //");
            sql.AppendLine();
            sql.AppendLine("DELIMITER ;");
            sql.AppendLine();
            sql.AppendFormat("CALL `{0}`();", idempotentScriptName);
            sql.AppendLine();
            sql.AppendLine();
            sql.AppendFormat("DROP PROCEDURE IF EXISTS `{0}`;", idempotentScriptName);
            sql.AppendLine();

            statement.Sql = sql.ToString();

            return(statement);
        }