public function FeedsSQLParser::configFormValidate in Feeds SQL 7
Validate entered query to make sure it works.
File
- plugins/
FeedsSQLParser.inc, line 134
Class
- FeedsSQLParser
- Parses data from an SQL database.
Code
public function configFormValidate(&$config) {
$results = array();
$config['query'] = trim($config['query']);
$query = token_replace($config['query']);
// Make sure there is only one query
if ($end = strpos($query, ';')) {
$query = substr($query, 0, $end + 1);
}
// Verify the query is a SELECT statement
$select = strtoupper(substr($query, 0, 6));
if ($select != 'SELECT') {
form_set_error('query', t('SQL query has to be of the form "SELECT field1, field2 FROM table WHERE conditions"'));
return;
}
try {
// Switch to the selected database
db_set_active($config['database']);
// Test the query
$count = 0;
$result = @db_query($query);
foreach ($result as $record) {
$count++;
// Store only up to 10 results
if ($count <= 10) {
$results[] = (array) $record;
}
}
// Switch back to the default database
db_set_active();
} catch (Exception $error) {
form_set_error('query', $error
->getMessage());
db_set_active();
}
// Create mapping sources
$mapping = array();
if (is_array($results) && !empty($results[0])) {
foreach ($results[0] as $field => $value) {
$mapping[$field] = $field;
}
}
// Display some of the results
$config['results'] = theme('form_element_label', array(
'element' => array(
'#title' => t('Test results'),
'#title_display' => 'before',
),
)) . theme('item_list', array(
'items' => array(
t('Total number of records: !records', array(
'!records' => $count,
)),
t('Total number of columns: !columns', array(
'!columns' => count($mapping),
)),
),
)) . theme('table', array(
'header' => $mapping,
'rows' => $results,
)) . t('Verify that the results from the query are what you expect. Note that this is limited to 10 results.') . '<br />';
ksort($mapping);
$config['mapping'] = $mapping;
$this
->addConfig($config);
}