So here is my problem. I know there is a fair bit of literature on this site about this type of issue but I am confused about how several of these issues intertwine for my problem. First I have an array of row data that needs to be updated or inserted based on a remote id value within that array, in this case value_c. This array corresponds to a row instance from table foo. Basically, if a record with a matching value_c exists in the database then update that record otherwise insert the new record payload. The data structure of the array corresponds to the row schema of table foo in our db. This is the schema (obfuscated for safety):
CREATE TABLE IF NOT EXISTS `foos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value_a` varchar(13) DEFAULT NULL,
`value_b` int(11) DEFAULT NULL,
`value_c` int(11) DEFAULT NULL,
.
.
.
.
`value_x` enum('enum_a','enum_b','enum_c','enum_d') DEFAULT NULL,
`value_y` text,
`value_z` enum('daily','monthly','weekly') DEFAULT NULL,
`value_aa` tinyint(4) NOT NULL,
`value_bb` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=829762 ;
There is a lot of data, and the plan in as followed. String-ify this data send it to a stored procedure that would then update or insert as needed. Something like the following (note that this will be happening in a model within codeigniter):
public function update_or_insert($event_array_payload)
{
// string-ify the data
$mod_payload = implode('<delimiter>', $event_array_payload)
//deal with NULLs in array
$mod_payload = $this->deal_with_nulls($mod_payload);
$this->stored_procedure_lib->update_or_insert_payload($mod_payload);
}
// then elsewhere in the stored procedure library
public function update_or_insert_payload($foo)
{
$this->_CI->db->query('CALL update_or_insert_foo(\'$foo\')');
}
My issue is as followed. A single string value is passed into the stored procedure. Then it needs to be parsed apart and places into either a single update or a single insert statement. I could create a variable for each column of the foo table and a loop to populate each variable and update/insert that way, but the foo table is very likely to be extended, and I do not want to create bugs further down the line. Is there a way to dynamically place the parsed apart contents of a string representation of an array into a single update or insert statement. I'm not sure if that is even possible, but I feel like a work around I do not know about might exist. Thank you for the help.