读取文件并查找是否存在字符串和 return 表名
read a file and find if a string exists and return the tablename
我正在尝试读取一个文件,如果文件有 'alter table' 那么它必须搜索 'modify' 如果修改存在那么它必须 return table 姓名。为此,我编写了以下代码并且它有效。
filename='modify_table.sql'
bol1="false"
File.foreach(filename).with_index do |line, line_num|
#convert all characters to upper case
if ( line =~ /[a-z]/ )
line = line.upcase
end
if (line =~ /ALTER TABLE/)
location = line.index("ALTER TABLE") + 11
subline = line[location..-1]
sublineParts = subline.split(" ")
tableName = sublineParts[0]
bol1 = line.include?("MODIFY")
if (bol1)
puts " found modify column on #{tableName}"
else
puts " no modify found"
end
end
end
我的文件包含:
begin
BEGIN EXECUTE IMMEDIATE 'alter table schemaname.tablename
modify (
abc VARCHAR2(200 BYTE),
xyz VARCHAR2(200 BYTE)
)'; EXCEPTION when others then if (SQLCODE != -01430 and SQLCODE != -942) then RAISE; end if; END;
end;
/
如果更改和修改在同一行,我的代码就可以工作。
在上面的文件中,两者都在差异行中。所以我写的代码 returns 没有找到修改,即使文件中有修改。
有人可以帮助我如何阅读下一行并找到 modify
这不是解决问题的最佳方法。但是这段代码会让它工作。
filename='modify_table.sql'
bol1 = false
lookformodify = false
tableName = ''
File.foreach(filename).with_index do |line, line_num|
# convert all characters to upper case
line = line.upcase if line =~ /[a-z]/
if !lookformodify && line =~ /ALTER TABLE/
lookformodify = true
location = line.index('ALTER TABLE') + 11
subline = line[location..-1]
sublineParts = subline.split(' ')
tableName = sublineParts[0]
bol1 = line.include?('MODIFY')
if bol1
puts " found modify column on #{tableName}"
lookformodify = false # found on same line. No need to look on other lines
end
elsif lookformodify && line =~ /MODIFY/
puts " found modify column on #{tableName}"
lookformodify = false # found on different line. No need to look on other lines
end
end
我正在尝试读取一个文件,如果文件有 'alter table' 那么它必须搜索 'modify' 如果修改存在那么它必须 return table 姓名。为此,我编写了以下代码并且它有效。
filename='modify_table.sql'
bol1="false"
File.foreach(filename).with_index do |line, line_num|
#convert all characters to upper case
if ( line =~ /[a-z]/ )
line = line.upcase
end
if (line =~ /ALTER TABLE/)
location = line.index("ALTER TABLE") + 11
subline = line[location..-1]
sublineParts = subline.split(" ")
tableName = sublineParts[0]
bol1 = line.include?("MODIFY")
if (bol1)
puts " found modify column on #{tableName}"
else
puts " no modify found"
end
end
end
我的文件包含:
begin
BEGIN EXECUTE IMMEDIATE 'alter table schemaname.tablename
modify (
abc VARCHAR2(200 BYTE),
xyz VARCHAR2(200 BYTE)
)'; EXCEPTION when others then if (SQLCODE != -01430 and SQLCODE != -942) then RAISE; end if; END;
end;
/
如果更改和修改在同一行,我的代码就可以工作。 在上面的文件中,两者都在差异行中。所以我写的代码 returns 没有找到修改,即使文件中有修改。 有人可以帮助我如何阅读下一行并找到 modify
这不是解决问题的最佳方法。但是这段代码会让它工作。
filename='modify_table.sql'
bol1 = false
lookformodify = false
tableName = ''
File.foreach(filename).with_index do |line, line_num|
# convert all characters to upper case
line = line.upcase if line =~ /[a-z]/
if !lookformodify && line =~ /ALTER TABLE/
lookformodify = true
location = line.index('ALTER TABLE') + 11
subline = line[location..-1]
sublineParts = subline.split(' ')
tableName = sublineParts[0]
bol1 = line.include?('MODIFY')
if bol1
puts " found modify column on #{tableName}"
lookformodify = false # found on same line. No need to look on other lines
end
elsif lookformodify && line =~ /MODIFY/
puts " found modify column on #{tableName}"
lookformodify = false # found on different line. No need to look on other lines
end
end