/** * Asking function for create or not stored procedure. * * @param string $spType Stored procedure type {insert|update|delete|select}. * @param string $tableName The table name. */ private function askForCreateSP($spType, $tableName) { $question = sprintf('Create SP for <dbo>%s</dbo> ? (default Yes): ', $spType); $question = new ConfirmationQuestion($question, true); if ($this->helper->ask($this->input, $this->output, $question)) { $defaultSpName = strtolower(sprintf('%s_%s', $tableName, $spType)); $fileName = sprintf('%s/%s.psql', $this->sourceDirectory, $defaultSpName); $question = new Question(sprintf('Please enter filename (%s): ', $fileName), $fileName); $spName = $this->helper->ask($this->input, $this->output, $question); if ($spName !== $fileName) { $spName = strtolower($spName); $fileName = sprintf('%s/%s.psql', $this->sourceDirectory, $spName); } else { $spName = $defaultSpName; } if (file_exists($fileName)) { $this->io->writeln(sprintf('File <fso>%s</fso> already exists', $fileName)); $question = 'Overwrite it ? (default No): '; $question = new ConfirmationQuestion($question, false); if ($this->helper->ask($this->input, $this->output, $question)) { $code = $this->generateSP($tableName, $spType, $spName); $this->writeTwoPhases($fileName, $code); } } else { $code = $this->generateSP($tableName, $spType, $spName); $this->writeTwoPhases($fileName, $code); } } }
/** * Executes the actual PhpStratum program. Returns 0 is everything went fine. Otherwise, returns non-zero. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $this->io = new StratumStyle($input, $output); $command = $this->getApplication()->find('constants'); $ret = $command->execute($input, $output); if ($ret != 0) { return $ret; } $command = $this->getApplication()->find('loader'); $ret = $command->execute($input, $output); if ($ret != 0) { return $ret; } $command = $this->getApplication()->find('wrapper'); $ret = $command->execute($input, $output); $this->io->writeln(''); return $ret; }
/** * Compares the tables listed in the config file and the tables found in the data schema. */ public function unknownTables() { foreach ($this->dataSchemaTables as $table) { if (isset($this->config['tables'][$table['table_name']])) { if (!isset($this->config['tables'][$table['table_name']]['audit'])) { $this->io->writeln(sprintf('<info>audit is not set for table %s</info>', $table['table_name'])); } else { if ($this->config['tables'][$table['table_name']]['audit']) { if (!isset($this->config['tables'][$table['table_name']]['alias'])) { $this->config['tables'][$table['table_name']]['alias'] = AuditTable::getRandomAlias(); } } } } else { $this->io->writeln(sprintf('<info>Found new table %s</info>', $table['table_name'])); $this->config['tables'][$table['table_name']] = ['audit' => false, 'alias' => null, 'skip' => null]; } } }
/** * Generate main part with name and params. * * @param array[] $columns Columns from table. * @param string|null $spType Stored procedure type {insert|update|delete|select}. * * @return array[]|null */ protected function checkUniqueKeys($columns, $spType = null) { $primaryKeys = DataLayer::getTablePrimaryKeys($this->dataSchema, $this->tableName); $uniqueKeys = DataLayer::getTableUniqueKeys($this->dataSchema, $this->tableName); $resultColumns = []; if (!isset($spType)) { if (count($uniqueKeys) <= 0 && count($primaryKeys) <= 0) { return null; } else { return $columns; } } if (count($primaryKeys) > 0) { foreach ($columns as $column) { $check = StaticDataLayer::searchInRowSet('Column_name', $column['column_name'], $primaryKeys); if (isset($check)) { $resultColumns[] = $column; } } return $resultColumns; } else { if (count($uniqueKeys) > 0) { reset($uniqueKeys); $first = key($uniqueKeys); if (count($uniqueKeys) > 1) { $this->io->writeln(sprintf('Table <dbo>%s</dbo> has more than one unique key.', $this->tableName)); $array = []; foreach ($uniqueKeys as $column) { if (isset($array[$column['Key_name']])) { $array[$column['Key_name']] .= ','; $array[$column['Key_name']] .= $column['Column_name']; } else { $array[$column['Key_name']] = $column['Column_name']; } } $tableArray = []; foreach ($array as $key => $column) { $tableArray[] = [$key, $column]; } $table = new Table($this->output); $table->setHeaders(['Name', 'Keys']); $table->setRows($tableArray); $table->render(); $question = new Question(sprintf('What unique keys use in statement?(%s): ', $uniqueKeys[$first]['Key_name']), $uniqueKeys[$first]['Key_name']); $uniqueKeys = $this->helper->ask($this->input, $this->output, $question); $uniqueKeys = explode(',', $array[$uniqueKeys]); foreach ($uniqueKeys as $column) { $resultColumns[] = ['column_name' => $column]; } return $resultColumns; } else { foreach ($uniqueKeys as $column) { $resultColumns[] = ['column_name' => $column['Column_name']]; } return $resultColumns; } } else { return null; } } }