Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/table_syncer.rb

Instance Method Summary collapse

Instance Method Details

#to_sql_create_query(table_name) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/table_syncer.rb', line 162

def to_sql_create_query(table_name)
	query = "insert into #{table_name} ("
	comma = ''
	self.each_key { |key_name|
		query += "#{comma}#{key_name} "
		comma = ','
	}
	query += ") values ( "
	comma = ''
	self.each_key { |key_name|
		query += "#{comma} #{self[key_name] ? "'" + self[key_name].gsub("'", "\\\\'") + "'" : 'NULL'}" # assume it will leave the others are null, I guess
		comma = ','
	}
 	query += ");"
end

#to_sql_update_query(table_name, nonmatching_keys) ⇒ Object

ltodo take some ‘params’ :)



151
152
153
154
155
156
157
158
159
160
# File 'lib/table_syncer.rb', line 151

def to_sql_update_query(table_name, nonmatching_keys) # ltodo take some 'params' :)
	raise unless self['id']
	query = "update #{table_name} set"
		comma = ''
	self.each_key do |key|
		query << "#{comma} #{key} = #{self[key] ? "'" + self[key].gsub("'", "\\\\'") + "'": 'NULL'}" if nonmatching_keys.include? key
		comma = ',' if nonmatching_keys.include? key
	end
	query << " where id = #{self['id']}"
end