/** * {@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('}'); }
/** * Generates the DocBlock parts to be used by the wrapper generator. */ private function getDocBlockPartsWrapper() { // Get the DocBlock parts from the source of the stored routine. $this->getDocBlockPartsSource(); // Generate the parameters parts of the DocBlock to be used by the wrapper. $parameters = []; foreach ($this->parameters as $parameter_info) { $parameters[] = ['parameter_name' => $parameter_info['parameter_name'], 'php_type' => DataTypeHelper::columnTypeToPhpType($parameter_info), 'data_type_descriptor' => $parameter_info['data_type_descriptor'], 'description' => $this->getParameterDocDescription($parameter_info['parameter_name'])]; } // Compose all the DocBlock parts to be used by the wrapper generator. $this->docBlockPartsWrapper = ['sort_description' => $this->docBlockPartsSource['sort_description'], 'long_description' => $this->docBlockPartsSource['long_description'], 'parameters' => $parameters]; }
/** * 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; }
/** * Loads the width of all columns in the MySQL schema into $columns. */ private function getColumns() { $rows = DataLayer::getAllTableColumns(); foreach ($rows as $row) { $row['length'] = DataTypeHelper::deriveFieldLength($row); $this->columns[$row['table_name']][$row['column_name']] = $row; } }