/// <summary>
 /// Writes the Migration Up() and Down()
 /// </summary>
 /// <param name="context">The context that controls how the column should be generated</param>
 /// <param name="function">the Function to generate the migration for</param>
 /// <param name="migration">The migration index to apply</param>
 /// <param name="output">The output stream to append the C# code to</param>
 private void WriteToStream(SchemaMigrationContext context, FunctionDefinition function, int migration, StreamWriter output)
 {
     WriteMigration(output, context, migration
     , () => context.MigrationFunctionClassNamer(migration, function)
     , () => WriteView(context, function, output)
     , () => WriteDeleteView(context, function, output));
 }
 private static void WriteDeleteView(SchemaMigrationContext context, FunctionDefinition function, StreamWriter output)
 {
     if ( context.FromDatabaseType == DatabaseType.SqlServer)
     output.WriteLine("\t\t\tExecute.WithDatabaseType(DatabaseType.SqlServer).Sql(\"DROP FUNCTION [{0}].[{1}]\");", function.SchemaName, function.Name);
      else
     output.WriteLine("\t\t\tExecute.WithDatabaseType(DatabaseType.{0}).Sql(\"DROP FUNCTION {1}\");", context.FromDatabaseType, function.Name);
 }
 private void WriteView(SchemaMigrationContext context, FunctionDefinition function, StreamWriter output)
 {
     var scriptsDirectory = Path.Combine(context.WorkingDirectory, context.ScriptsDirectory);
      var scriptFile = Path.Combine(scriptsDirectory, string.Format("CreateFunction{0}_{1}.sql", function.Name, context.FromDatabaseType));
      if ( !File.Exists(scriptFile))
      {
     if (!Directory.Exists(scriptsDirectory))
        Directory.CreateDirectory(scriptsDirectory);
     File.WriteAllText(scriptFile, function.Sql);
      }
      output.WriteLine("\t\t\tExecute.WithDatabaseType(DatabaseType.{0}).Script(@\"{1}\");", context.FromDatabaseType, Path.Combine(context.ScriptsDirectory, Path.GetFileName(scriptFile)));
 }
        public IList<FunctionDefinition> ReadFunctions()
        {
            var procedures = new List<FunctionDefinition>();

             // NOTE: Query for SQL 2005 for greater ... would need to be refactored to support SQL Server 2000
             var query = Processor.Read("SELECT schema_Name(so.schema_id)  as  SchemaName, o.name, c.text FROM syscomments c, sysobjects o, sys.objects so WHERE c.id = o.id AND xtype = 'FN' AND so.object_id = o.id ORDER BY colid");

             foreach (DataRow dr in query.Tables[0].Rows)
             {
            var matches = (from t in procedures
                           where t.Name == dr["name"].ToString()
                           select t).ToList();

            FunctionDefinition functionDefinition = null;
            if (matches.Count > 0) functionDefinition = matches[0];

            // create the procedure if not found
            if (functionDefinition == null)
            {
               functionDefinition = new FunctionDefinition()
               {
                  SchemaName = dr["SchemaName"].ToString(),
                  Name = dr["Name"].ToString(),
                  Sql = dr["Text"].ToString()
               };
               procedures.Add(functionDefinition);
            }
            else
            {
               functionDefinition.Sql += dr["Text"].ToString();
            }
             }

             return procedures;
        }