Created
September 17, 2015 00:47
-
-
Save akehoyayoi/2dd5cb67944e3588c0e4 to your computer and use it in GitHub Desktop.
convert ddl from sqlserver to mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convertLine(line) | |
# 可読性のため一次変数に受ける | |
result = line.gsub("]","").gsub("[","").gsub("UNIQUEIDENTIFIER","NVARCHAR (38)") | |
result = result.gsub("NVARCHAR \(MAX\)","NVARCHAR \(256\)") | |
result.gsub("DATETIMEOFFSET (7)","TIMESTAMP").gsub("sys.geography","geometry") | |
end | |
def convertDDL(path) | |
@inDDL = false | |
@isGEO = false | |
@isINDEX = false | |
File.open(path, 'r:utf-8') do |f| | |
f.each_line do |line| | |
if line.include?("CREATE") | |
@isDDL = true | |
end | |
if line.include?("INDEX") | |
@isINDEX = true | |
end | |
# 一次変換 | |
if @isDDL | |
line = convertLine(line) | |
# GEO情報があるか否か,あればstorage engineをMYISAMで出力する | |
if line.include?("geometry") | |
@isGEO = true | |
end | |
if @isINDEX | |
line = line.gsub("CLUSTERED","") | |
end | |
# 末尾か否か | |
if line.include?(");") && @isINDEX == false | |
if @isGEO | |
puts line.gsub(");",") ENGINE = MYISAM;") | |
else | |
puts line.gsub(");",") ENGINE = INNODB;") | |
end | |
@isDDL = false | |
@isGEO = false | |
else | |
puts line | |
end | |
end | |
end | |
end | |
end | |
convertDDL(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment