Name

db_columns — retrieve column names from a database table

ATTRIBUTES

Attribute Pos. Req. Default Description
[ name | table ] Yes Name of the database table.
[ fields | columns ] Yes Manually specify columns to be returned.
joiner Yes \n String joiner to use if column list is requested in Perl scalar context.
passed_order 0 With columns=, return columns in the passed order instead of table order?
interpolate     0 interpolate input?
reparse     1 interpolate output?
hide     0 Hide the tag return value?

DESCRIPTION

[db_columns] returns the list of columns in a database table.

BEHAVIOR

This tag does not appear to be affected by, or affect, the rest of Interchange.

EXAMPLES

Example: looping over column list from the products table

<pre>
[loop list="[db-columns products]"]
  Column: [loop-code]
[/loop]
</pre>

Example: looping over column list, without using db_columns

It is possible to list table columns manually without the use of [db_columns]. The output will be satisfactory as long as you don't need db_column's columns= attribute.

[perl tables=products]
  $Scratch->{columns} = join ' ', $Db{products}->columns;
  return;
[/perl]

<pre>
[loop list="[scratch columns]"]
  Column: [loop-code]
[/loop]
</pre>

NOTES

A side effect of specifying passed_order=1 is the removal of invalid column names from the columns= list; column names not present in the database table are filtered out, instead of being returned regardless.

AVAILABILITY

db_columns is available in Interchange versions:

4.6.0-5.9.0 (git-head)

SOURCE

Interchange 5.9.0:

Source: code/UI_Tag/db_columns.coretag
Lines: 63


# Copyright 2002-2007 Interchange Development Group and others
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.
# 
# $Id: db_columns.coretag,v 1.5 2007-03-30 23:40:54 pajamian Exp $

UserTag db_columns  Order       name columns joiner passed_order
UserTag db_columns  AttrAlias   table name
UserTag db_columns  AttrAlias   fields columns
UserTag db_columns  Version     $Revision: 1.5 $
UserTag db_columns  Routine     <<EOR
sub {
my ($table,$columns, $joiner, $passed_order) = @_;
$table = $Values->{mv_data_table}
  unless $table;
my $db = Vend::Data::database_exists_ref($table)
  or return undef;
my $acl = UI::Primitive::get_ui_table_acl($table);
$db = $db->ref() unless $Vend::Interpolate::Db{$table};
my $key = $db->config('KEY');

$joiner = "\n" unless defined $joiner;

my @cols;
if(! $columns || $columns =~ /^[\s,\0]*$/) {
  @cols = $db->columns();
}
else {
  @cols = grep /\S/, split /[\s,\0]+/, $columns;
  my (@allcols) =  $db->columns();

  my %col;
  if($passed_order) {
    @col{@allcols} = @allcols;
    @allcols = @cols;
    my $found;
    for(@cols) {
      next unless $_ eq $key;
      $found = 1;
      last;
    }
    unshift (@allcols, $key) if ! $found;
  }
  else {
    @col{@cols} = @cols;
  }

  $col{$key} = $key if ! defined $col{$key};

  @cols = grep defined $col{$_}, @cols;
}

if($acl) {
  @cols = UI::Primitive::ui_acl_grep( $acl, 'fields', @cols);
}

return @cols if wantarray;
return join $joiner, @cols;
}
EOR

AUTHORS

Interchange Development Group

SEE ALSO

DocBook! Interchange!