# Some tests insert some tuples with the id values set. In other words, the sequence
# is not used to generate a value for the primary key column named id. When a new tuple
# it to be inserted, where the id value is not set explicitly, a primary key violation will
# be raised because the generated from the sequence value is the same as one of the existing
# id values. This happens in unit tests quite often. So this function serves that the unit tests
# pass. However it is very expensive( sends 4 queries to the server) and probably not suitable for
# production code. Check the implementation for further info/details.
defmake_sure_pk_works(table_name,name)
# Ensure the auto-generated id will not violate the primary key constraint.
# This is expensive and it's used so that the tests pass. Comment out for production code(?).
# Assume that table name has one primary key column named id that is associated with a sequence,
# otherwise return
hdl=nil
sequence_name=extract_sequence_name(select_value("select \"default\" from _columns where table_id in (select id from _tables where name = '#{table_name}') and name='id';"))
returnifsequence_name.blank?
max_id=select_value("select max(id) from #{table_name}").to_i
next_seq_val=select_value("select next value for #{sequence_name}").to_i
if(max_id>next_seq_val)
hdl=execute("ALTER SEQUENCE #{sequence_name} RESTART WITH #{max_id+1}",name)
else
hdl=execute("ALTER SEQUENCE #{sequence_name} RESTART WITH #{next_seq_val+1}",name)
end
end
# Auxiliary function that extracts the table name from an insertion query
# It's called by insert_sql in order to assist at make_sure_pk_works.
# Ideally, if make_sure_pk_works is commented out for production code, this
# function will be never called.
defextract_table_name_from_insertion_query(sql)
$1ifsql=~/INSERT INTO "(.*)" \(/
end
# Auxiliary function that extracts the sequence name.
# It's called by make_sure_pk_works.
# Ideally, if make_sure_pk_works is commented out for production code, this
# function will be never called.
defextract_sequence_name(seqStr)
$1ifseqStr=~/\."(.*)"/
end
private
defconnect
puts"ACTIVERECORD\n"
if(@connection)
#@connection.connect(user = "monetdb", passwd = "monetdb", lang = "sql", host="127.0.0.1", port = 50000, db_name = "ruby", auth_type = "SHA1")