/** * {@inheritdoc} */ protected function writeResultHandler($routine) { // Validate number of column names and number of column types are equal. $n1 = count($routine['columns']); $n2 = count($routine['column_types']); if ($n1 != $n2) { throw new LogicException("Number of fields %d and number of columns %d don't match.", $n1, $n2); } $routine_args = $this->getRoutineArgs($routine); $this->codeStore->append('self::query(\'CALL ' . $routine['routine_name'] . '(' . $routine_args . ')\');'); $columns = ''; $fields = ''; foreach ($routine['columns'] as $i => $field) { if ($field != '_') { if ($columns) { $columns .= ','; } $columns .= '`' . $routine['fields'][$i] . '`'; if ($fields) { $fields .= ','; } $fields .= DataTypeHelper::escapePhpExpression(['data_type' => $routine['column_types'][$i]], '$row[\'' . $field . '\']', true); } } $this->codeStore->append('if (is_array($rows) && !empty($rows))'); $this->codeStore->append('{'); $this->codeStore->append('$sql = "INSERT INTO `' . $routine['table_name'] . '`(' . $columns . ')";'); $this->codeStore->append('$first = true;'); $this->codeStore->append('foreach($rows as $row)'); $this->codeStore->append('{'); $this->codeStore->append('if ($first) $sql .=\' values(' . $fields . ')\';'); $this->codeStore->append('else $sql .=\', (' . $fields . ')\';'); $this->codeStore->append('$first = false;'); $this->codeStore->append('}'); $this->codeStore->append('self::query($sql);'); $this->codeStore->append('}'); }
/** * Returns code for the arguments for calling the stored routine in a wrapper method. * * @param array $routine The metadata of the stored routine. * * @return string */ protected function getRoutineArgs($routine) { $ret = ''; foreach ($routine['parameters'] as $parameter_info) { $mangledName = $this->nameMangler->getParameterName($parameter_info['parameter_name']); if ($ret) { $ret .= ','; } $ret .= DataTypeHelper::escapePhpExpression($parameter_info, '$' . $mangledName, $this->lobAsStringFlag); } return $ret; }