Class: DataMetaDom::MySqlLexer::SqlOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/dataMetaDom/mySql.rb

Overview

Encapsulates 4 parts of DDL related SQL output:

  • Creates

  • Drops

  • Linking aka Coupling aka creating Foreign Keys

  • Unlinking aka Uncoupling aka dropping Foreign Keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sqlTargetDir) ⇒ SqlOutput

Creates an instance into the given target directory in which all 4 parts of the SQL DDL process will be created.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dataMetaDom/mySql.rb', line 83

def initialize(sqlTargetDir)
    @selTargetDir = sqlTargetDir
    @create = File.new("#{sqlTargetDir}/DDL-create.sql", 'wb')
    @drop = File.new("#{sqlTargetDir}/DDL-drop.sql", 'wb')
    @couple = File.new("#{sqlTargetDir}/DDL-couple.sql", 'wb')
    @uncouple = File.new("#{sqlTargetDir}/DDL-uncouple.sql", 'wb')
    @allScriptFiles = [@create, @drop, @couple, @uncouple]
    @dropScripts = [@uncouple, @drop]
    @allScriptFiles.each { |f|
        f.puts %q</* Generated by DataMeta DOM MySQL utility
DO NOT EDIT MANUALLY, update the DataMeta DOM source and regen.
*/
>
    }
    @dropScripts.each { |ds|
        ds.puts %q<
/* Disable all checks for safe dropping without any errors */
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

>
    }
end

Instance Attribute Details

#coupleObject (readonly)

Open output file into the couple SQL DDL statements, creating foreign keys



73
74
75
# File 'lib/dataMetaDom/mySql.rb', line 73

def couple
  @couple
end

#createObject (readonly)

Open output file into create SQL DDL statements (CREATE TABLE)



63
64
65
# File 'lib/dataMetaDom/mySql.rb', line 63

def create
  @create
end

#dropObject (readonly)

Open output file into drop SQL DDL statements (DROP TABLE)



68
69
70
# File 'lib/dataMetaDom/mySql.rb', line 68

def drop
  @drop
end

#uncoupleObject (readonly)

Open output file into the uncouple SQL DDL statements, dropping foreign keys



77
78
79
# File 'lib/dataMetaDom/mySql.rb', line 77

def uncouple
  @uncouple
end

Instance Method Details

#closeObject

Safely closes all the output files.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dataMetaDom/mySql.rb', line 111

def close
    @dropScripts.each { |ds|
        ds.puts %q<

/* Re-enable all checks disabled earlier */
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
>
    }
    @allScriptFiles.each { |f|
        begin
            f.close
        rescue Exception => x;
            $stderr.puts x.message
        end
    }
end