/** * Returns the columns of a given table and database. * @param array $params get params of the request * @return array */ public function cols(array $params = array()) { // get db and table from params if (empty($params['db']) || empty($params['table'])) { return array('status' => 'error'); } else { $db = $params['db']; $table = $params['table']; } // init table $this->getResource()->init($params['db'], $params['table']); // get columns from the database $colnames = array_keys($this->getResource()->fetchCols()); // obtain table metadata $tablesResource = new Data_Model_Resource_Tables(); $tableMeta = $tablesResource->fetchRowByName($db, $table, true); if ($tableMeta === false) { // this table is not in the metadata table - let's see if we can get // further information from the table itself $descResource = new Data_Model_Resource_Description(); $descResource->init($params['db']); $tableMeta = $descResource->describeTable($params['table']); } // construct metadata array $meta = array(); foreach ($tableMeta['columns'] as $key => $colMeta) { $meta[$colMeta['name']] = array('id' => $key, 'ucd' => explode(';', str_replace(' ', '', $colMeta['ucd']))); } // return columns of this table $cols = array(); foreach ($colnames as $colname) { $col = array('id' => $meta[$colname]['id'], 'name' => $colname, 'sortable' => true, 'ucfirst' => false, 'ucd' => $meta[$colname]['ucd']); // add removenewline flag if this is set in the config if (Daiquiri_Config::getInstance()->data->viewer->columnWidth) { $col['width'] = Daiquiri_Config::getInstance()->data->viewer->columnWidth; } else { $col['width'] = 100; } // add removenewline flag if this is set in the config if (Daiquiri_Config::getInstance()->data->viewer->removeNewline) { $col['format'] = array('removeNewline' => true); } // append col to cols array $cols[] = $col; } return array('status' => 'ok', 'cols' => $cols); }
function _parseSqlAll_getColsDaiquiri(&$sqlTree, &$node, $zendAdapter, $table, $alias) { $resParts = $this->_parseSqlAll_parseResourceName($table); // process the alias name $aliasParts = $this->_parseSqlAll_parseResourceName($alias); unset($aliasParts[0]); $aliasName = ""; foreach ($aliasParts as $part) { if ($aliasName === "") { $aliasName .= "`" . $part . "`"; } else { $aliasName .= ".`" . $part . "`"; } } // check if the given table resource is composed of DATABASE.TABLE if (count($resParts) !== 3) { throw new Exception("Cannot resolve table columns, table name is not valid."); } // check if this is a table of the user database $username = Daiquiri_Auth::getInstance()->getCurrentUsername(); if ($resParts[1] === Daiquiri_Config::getInstance()->getUserDbName($username)) { $resource = new Data_Model_Resource_Viewer(); $resource->init($resParts[1], $resParts[2]); $tableData = array('columns' => array()); foreach ($resource->fetchCols() as $col => $value) { if ($col !== 'row_id') { $tableData['columns'][] = array('name' => $col); } } } else { $tableResource = new Data_Model_Resource_Tables(); $tableData = $tableResource->fetchRowByName($resParts[1], $resParts[2], true); } if (empty($tableData)) { throw new Exception("Table {$table} does not exist."); } foreach ($tableData['columns'] as $count => $row) { if ($count == 0) { // this is the item we change if ($alias === false || empty($alias)) { $node['base_expr'] = "`" . $row['name'] . "`"; $node['no_quotes'] = array("delim" => ".", "parts" => array($row['name'])); } else { $node['base_expr'] = $aliasName . ".`" . $row['name'] . "`"; $node['no_quotes'] = array("delim" => ".", "parts" => array_merge($aliasParts, array($row['name']))); $node['alias'] = array("as" => true, "name" => "`" . str_replace(".", "__", str_replace("`", "", $node['base_expr'])) . "`", "base_expr" => "as `" . str_replace(".", "__", str_replace("`", "", $node['base_expr'])) . "`", "no_quotes" => array("delim" => ".", "parts" => array(str_replace(".", "__", str_replace("`", "", $node['base_expr']))))); } $node['delim'] = ","; $nodeTemplate = $node; array_push($sqlTree['SELECT'], $node); } else { $newNode = $nodeTemplate; // this is set on the first passing when count is 0 if ($alias === false || empty($alias)) { $newNode['base_expr'] = "`" . $row['name'] . "`"; $newNode['no_quotes'] = array("delim" => ".", "parts" => array($row['name'])); } else { $newNode['base_expr'] = $aliasName . ".`" . $row['name'] . "`"; $newNode['no_quotes'] = array("delim" => ".", "parts" => array_merge($aliasParts, array($row['name']))); $newNode['alias'] = array("as" => true, "name" => "`" . str_replace(".", "__", str_replace("`", "", $newNode['base_expr'])) . "`", "base_expr" => "as `" . str_replace(".", "__", str_replace("`", "", $newNode['base_expr'])) . "`", "no_quotes" => array("delim" => ".", "parts" => array(str_replace(".", "__", str_replace("`", "", $newNode['base_expr']))))); } array_push($sqlTree['SELECT'], $newNode); } } }