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.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dataMetaDom/mySql.rb', line 91

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



81
82
83
# File 'lib/dataMetaDom/mySql.rb', line 81

def couple
  @couple
end

#createObject (readonly)

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



71
72
73
# File 'lib/dataMetaDom/mySql.rb', line 71

def create
  @create
end

#dropObject (readonly)

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



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

def drop
  @drop
end

#uncoupleObject (readonly)

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



85
86
87
# File 'lib/dataMetaDom/mySql.rb', line 85

def uncouple
  @uncouple
end

Instance Method Details

#closeObject

Safely closes all the output files.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dataMetaDom/mySql.rb', line 119

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