'salesforce_api_drush_show_tables', 'description' => 'List tables in salesforce.', ); $items['soql describe'] = array( 'callback' => 'salesforce_api_drush_describe', 'description' => 'Describe an individual table in salesforce.', ); $items['soql'] = array( 'callback' => 'salesforce_api_drush_soql', 'description' => 'Execute a SOQL query.', ); return $items; } /** * Duplicates the functionality of mysql's SHOW TABLES */ function salesforce_api_drush_show_tables() { if($sf = salesforce_api_connect()) { try { $response = $sf->client->describeGlobal(); } catch (Exception $e) { drush_print($e->faultstring); return; } $rows = array(array(dt('Tables'))); foreach ($response->types as $type) { $rows[] = array($type); } drush_print_table($rows, 4, TRUE); drush_print("\n" . dt('!count rows in set.', array('!count' => (count($rows)-1)))); } else { drush_print('Could not connect to salesforce.'); } } /** * Examine a salesforce table. Allows for deep inspection using dot syntax * similar to sql databases. * * For example to see fields on contact: 'contact.fields' * To examin the first field on a contact 'contact.fields.1' */ function salesforce_api_drush_describe() { $tables = func_get_args(); if($sf = salesforce_api_connect()) { foreach ($tables as $t) { $info = explode('.', $t); try { $table = array_shift($info); $response = $sf->client->describeSObject($table); if (!empty($info)) { while ($i = array_shift($info)) { if (is_array($response)) { $response = $response[$i]; } else { $response = $response->$i; } } } $rows = array(array(dt('Key'), dt('Value'))); foreach ($response as $k => $v) { if (!is_array($v) && !is_object($v)) { $rows[] = array($k, $v); } else { if (is_object($v) && isset($v->name)) { // For fields $label = $v->name . ' [+]'; } elseif (is_object($v) && isset($v->childSObject)) { // For relationships $label = $v->childSObject. '.' .$v->field . ' [+]'; } else { $label = '[+]'; } $rows[] = array($k, $label); } } drush_print_table($rows, 4, TRUE); } catch (Exception $e) { drush_print($e->faultstring); } } } else { drush_print('Could not connect to salesforce.'); } } /** * Execute a SOQL query. */ function salesforce_api_drush_soql() { $args = func_get_args(); $command = implode(' ', $args); if($sf = salesforce_api_connect()) { try { timer_start('soql_query'); $result = $sf->client->query($command); $timer = timer_stop('soql_query'); foreach ($result->records as $r) { if (!isset($rows)) { $header = array(); foreach ($r as $k => $v) { $header[] = $k; } $rows = array($header); } $row = array(); foreach ($r as $k => $v) { $row[] = $v; } $rows[] = $row; } drush_print_table($rows, 4, TRUE); drush_print("\n" . dt('!count rows in set (!time sec)', array('!count' => $result->size, '!time' => number_format($timer['time']/1000, 2)))); } catch (Exception $e) { drush_print($e->faultstring); } } else { drush_print('Could not connect to salesforce.'); } }