Feature #7751

How to encapsulate File.delete and File.rename into one 'transaction'?

Added by Justin Peal about 1 year ago. Updated about 1 year ago.

[ruby-core:51726]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-

Description

Following program has a line: File.delete(sqlfile) and File.rename(tmpfile, sql_file)

It is not a 'transaction' which is I need. Has Ruby a feature to encapsulate File.delete and File.rename into one 'transaction'?

class String
def camelize
self.split('').map {|w| w.capitalize }.join('')
end
end

class Busql
WORD = /\A(\/*.?\\/|'(?:[']|'')'|"(?:["]|"")"|--.?\n|\n|([$]|\p{L})([$]|\p{L}|\p{N})|\p{M}+|\p{Z}+|\p{S}+|\p{N}+|\p{P}|\p{C}+)/m
OracleReservedWords = /\A(ACCESS|ADD|ALL|ALTER|AND|ANY|ARRAYLEN|AS|ASC|AUDIT)\z/i
OracleKeywords = /\A(ADMIN|AFTER|ALLOCATE|ANALYZE|ARCHIVE|ARCHIVELOG|AUTHORIZATION|AVG)\z/i
PL
SQLReservedWords = /\A(ABORT|ACCEPT|ACCESS|ADD|ALL|ALTER|AND|ANY|ARRAY|ARRAYLEN|AS|ASC|ASSERT|ASSIGN|AT|AUTHORIZATION|AVG)\z/i
DataTypes = /\A(VARCHAR2|NVARCHAR2|NUMBER|FLOAT|LONG|DATE|BINARYFLOAT|BINARYDOUBLE|TIMESTAMP|INTERVAL|YEAR|DAY|RAW|ROWID|UROWID|CHAR|NCHAR|CLOB|NCLOB|BLOB|BFILE)\z/i
Operators = /\A(PRIOR|CONNECT
BYROOT|UNION|ALL|INTERSECT|MINUS|MULTISET)\z/i
Joins = /\A(NATURAL|LEFT|RIGHT|FULL|INNER|OUTER|CROSS|JOIN)\z/i
SQL
Functions = /\A(ABS|ACOS|ADDMONTHS|APPENDCHILDXML|ASCII|ASCIISTR|ASIN|ATAN|ATAN2|AVG)\z/i
Pseudocolumns = /\A(CONNECT
BYISCYCLE|CONNECTBYISLEAF|LEVEL|CURRVAL|NEXTVAL|VERSIONSSTARTSCN|VERSIONSSTARTTIME|VERSIONSENDSCN|VERSIONSENDTIME|VERSIONSXID|VERSIONSOPERATION|COLUMNVALUE|OBJECTID|OBJECTVALUE|ORAROWSCN|ROWID|ROWNUM|XMLDATA)\z/i
NORMAL
WORDS = /\A([$_]|\p{L})/i

def changecase text
news = []
shr
text = text.dup
until shrtext.empty?
case word = shr
text[WORD, 1]
when OracleReservedWords, OracleKeywords, PLSQLReservedWords, DataTypes, Operators, Joins then word.upcase!()
when SQL
Functions, Pseudocolumns then word = word.camelize()
when NORMALWORDS then word.downcase!() if word.asciionly?
end
news << word
shr_text.slice!(0, word.size)
end
news.join
end

def changesql sqlfile
text = File.read(sqlfile, :mode => 'r:utf-8')
new
text = changecase(text)
if changed = (new
text != text)
File.write(tmpfile = sqlfile + '.t~m~p', newtext, :mode => 'wb')
File.delete(sql
file) and File.rename(tmpfile, sqlfile)
end
yield changed, text.size, newtext.size if blockgiven?
end

def beautify argv
argv.each do |arg|
Dir.glob(arg) do |sqlfile|
sql
file = sqlfile.forceencoding(Encoding::GB18030).encode(Encoding::UTF8)
change
sql(sqlfile) do |changed, oldlen, newlen|
yield sql
file, changed, oldlen, newlen if block_given?
end
end
end
end
end

if $0 == FILE
Busql.new.beautify(ARGV) do |sqlfile, changed, oldlen, newlen|
str = changed ? " #{old
len} ==> #{newlen}" : ''
print "#{sql
file}#{str}\n"
end
end

History

#1 Updated by Charlie Somerville about 1 year ago

File.rename can overwrite the destination file, so your File.delete is not necessary

On Tuesday, 29 January 2013 at 1:15 PM, mghomn (Justin Peal) wrote:

Issue #7751 has been reported by mghomn (Justin Peal).


Feature #7751: How to encapsulate File.delete and File.rename into one 'transaction'?
https://bugs.ruby-lang.org/issues/7751

Author: mghomn (Justin Peal)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:

Following program has a line: File.delete(sqlfile) and File.rename(tmpfile, sql_file)

It is not a 'transaction' which is I need. Has Ruby a feature to encapsulate File.delete and File.rename into one 'transaction'?

class String
def camelize
self.split('').map {|w| w.capitalize }.join('')
end
end

class Busql
WORD = /\A(\/*.?\\/|'(?:[']|'')'|"(?:["]|"")"|--.?\n|\n|([$]|\p{L})([$]|\p{L}|\p{N})|\p{M}+|\p{Z}+|\p{S}+|\p{N}+|\p{P}|\p{C}+)/m
OracleReservedWords = /\A(ACCESS|ADD|ALL|ALTER|AND|ANY|ARRAYLEN|AS|ASC|AUDIT)\z/i
OracleKeywords = /\A(ADMIN|AFTER|ALLOCATE|ANALYZE|ARCHIVE|ARCHIVELOG|AUTHORIZATION|AVG)\z/i
PL
SQLReservedWords = /\A(ABORT|ACCEPT|ACCESS|ADD|ALL|ALTER|AND|ANY|ARRAY|ARRAYLEN|AS|ASC|ASSERT|ASSIGN|AT|AUTHORIZATION|AVG)\z/i
DataTypes = /\A(VARCHAR2|NVARCHAR2|NUMBER|FLOAT|LONG|DATE|BINARYFLOAT|BINARYDOUBLE|TIMESTAMP|INTERVAL|YEAR|DAY|RAW|ROWID|UROWID|CHAR|NCHAR|CLOB|NCLOB|BLOB|BFILE)\z/i
Operators = /\A(PRIOR|CONNECT
BYROOT|UNION|ALL|INTERSECT|MINUS|MULTISET)\z/i
Joins = /\A(NATURAL|LEFT|RIGHT|FULL|INNER|OUTER|CROSS|JOIN)\z/i
SQL
Functions = /\A(ABS|ACOS|ADDMONTHS|APPENDCHILDXML|ASCII|ASCIISTR|ASIN|ATAN|ATAN2|AVG)\z/i
Pseudocolumns = /\A(CONNECT
BYISCYCLE|CONNECTBYISLEAF|LEVEL|CURRVAL|NEXTVAL|VERSIONSSTARTSCN|VERSIONSSTARTTIME|VERSIONSENDSCN|VERSIONSENDTIME|VERSIONSXID|VERSIONSOPERATION|COLUMNVALUE|OBJECTID|OBJECTVALUE|ORAROWSCN|ROWID|ROWNUM|XMLDATA)\z/i
NORMAL
WORDS = /\A([$_]|\p{L})/i

def changecase text
news = []
shr
text = text.dup
until shrtext.empty?
case word = shr
text[WORD, 1]
when OracleReservedWords, OracleKeywords, PLSQLReservedWords, DataTypes, Operators, Joins then word.upcase!()
when SQL
Functions, Pseudocolumns then word = word.camelize()
when NORMALWORDS then word.downcase!() if word.asciionly?
end
news << word
shr_text.slice!(0, word.size)
end
news.join
end

def changesql sqlfile
text = File.read(sqlfile, :mode => 'r:utf-8')
new
text = changecase(text)
if changed = (new
text != text)
File.write(tmpfile = sqlfile + '.t~m~p', newtext, :mode => 'wb')
File.delete(sql
file) and File.rename(tmpfile, sqlfile)
end
yield changed, text.size, newtext.size if blockgiven?
end

def beautify argv
argv.each do |arg|
Dir.glob(arg) do |sqlfile|
sql
file = sqlfile.forceencoding(Encoding::GB18030).encode(Encoding::UTF8)
change
sql(sqlfile) do |changed, oldlen, newlen|
yield sql
file, changed, oldlen, newlen if block_given?
end
end
end
end
end

if $0 == FILE
Busql.new.beautify(ARGV) do |sqlfile, changed, oldlen, newlen|
str = changed ? " #{old
len} ==> #{newlen}" : ''
print "#{sql
file}#{str}\n"
end
end

http://bugs.ruby-lang.org/

#2 Updated by Motohiro KOSAKI about 1 year ago

  • Status changed from Open to Rejected

#3 Updated by Justin Peal about 1 year ago

What a great function! I've confirm it is true.

irb(main):001:0> File.write('src', '000')
=> 3
irb(main):002:0> File.write('dest', '1111')
=> 4
irb(main):003:0> File.rename('src', 'dest')
=> 0
irb(main):004:0> File.read('dest')
=> "000"

It is sorry that the doc of Ruby (Ruby19.chm) didn't describe it:

rename(oldname, newname)
Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed.

File.rename("afile", "afile.bak") #=> 0

#4 Updated by Motohiro KOSAKI about 1 year ago

What a great function! I've confirm it is true.

irb(main):001:0> File.write('src', '000')
=> 3
irb(main):002:0> File.write('dest', '1111')
=> 4
irb(main):003:0> File.rename('src', 'dest')
=> 0
irb(main):004:0> File.read('dest')
=> "000"

It is sorry that the doc of Ruby (Ruby19.chm) didn't describe it:

rename(oldname, newname)
Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed.

File.rename("afile", "afile.bak") #=> 0

Because all File method behaviors depend on OS. It is not a part of
ruby responsibility.

Also available in: Atom PDF