osfameron has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing with CGI using CGI.pm, and recently decided to take another step - and downloaded MySQL and the DBI module.

SQL and DBI are excellent technologies: I feel very silly for having been so afraid of them for so long - it's a conceptual hurdle from text-based queries but it's way cool:

In a lazy (I was getting bored with manually creating forms) and hubris-fully over-ambitious moment yesterday, I started to work on a CGI script that:

  1. Connects to a specified database
  2. Allows you to select a table
  3. Creates a display with all the fields in that table, and suggstions for how to render them as a form. e.g. varchar(255) would be rendered as <input type=text maxlength=255>
  4. Allows you to choose which fields are displayed in the form, which are required etc.

This is all worked better, and quicker than expected, partly because I'm also using YACM - Yet Another Cool Module - the Template-Toolkit to create the HTML form for the interface.
Now all I need to do (;->) is render the forms (with Template.pm again of course)

My problems and/or questions to you are

  1. Has something like this been done before? I'm sure it has, but couldn't find references to it. I won't regret working on this, because it is a great learning experience, but I'd like to use something better/more standard if possible
  2. What advice do you have on the interface for creating the forms. For now, I've created a huge HTML <table> that scrolls off the screen with all the various check-boxes and text fields.
  3. What advice do you have on where to limit the scope of this: e.g. should I
    • Just create the HTML tables but no Perl code?
    • Create tables with hooks to a validator like Data::FormValidator?
    • Create tables with hooks to actions on creating and searching tickets...? complicated - but useful?: we could define snippets for each widget that generate a SELECT or INSERT SQL statement. Then we could think about code that allowed you to search/create entries that referred to multiple tables... now this makes my head hurt, so keep it simple (for now!)

I imagine that I'll rewrite the code to have each widget type as a class (something like CGI::DBI::{widget} with the widget name being either an SQL datatype name like 'text', 'varchar', 'enum' etc., an HTML input type like 'SELECT', 'TEXT', 'PASSWORD', or a custom type like 'Calendar', 'Time')

Each widget would then expose methods that create the HTML table snippets to start off with. When/if I move onto generating code or validations then I can just add new methods as I go along.

Apologies if I'm thinking out loud: I'd really appreciate some insight into the process of planning this!

Cheerio!
Osfameron

Replies are listed 'Best First'.
Re: CGI Form builder for DBI?
by davorg (Chancellor) on Oct 30, 2001 at 16:26 UTC

    I recommend you look for modules with "DBI" in their name at CPAN. A first glance finds the following modules that might be of interest to you:

    I'd also recommend that you take a close look at Class::DBI.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: CGI Form builder for DBI?
by zuqif (Hermit) on Oct 30, 2001 at 16:46 UTC
    Something I worked on previously seems to have gone down a similar path ..
    at the time I was producing loads of O-O cgi-form-dbi solutions, and they were beginning to look & feel the same.
    The solution : the ObjectCreate.pm module -> point it at a MySQL db $x, with tables $y & $z, and it'd write
    $x.pm $x::$y.pm & $x::$z.pm .. containting the necessary object code for the 4 sql actions.

    It hasn't been released, and isn't really in a state where I'd be happy todo so -
    many outstanding issues akin to the ones you mentioned above.
    It did however execute 90% of the typing for me (:

    Maybe not the most elegant solution, but it made perl-contracting a joy to finish 1 months 'work time' in a coupla hours (:
    the 'qif;
Re: CGI Form builder for DBI?
by growlf (Pilgrim) on Oct 30, 2001 at 18:15 UTC
    Take a look at what Gossamer Threads (http://gossamer-threads.com/scripts/mysqlman/index.htm) has done with their SQL interface - it is a generic frontend to MySQL that works very well and generates the forms based on the data types dynamicly even.

    Dunno if this helps much.
Re: CGI Form builder for DBI?
by osfameron (Hermit) on Oct 30, 2001 at 19:22 UTC
    Thanks for replies so far:
    • Checked out DBIx::* modules. I was sure I'd done a CPAN search already for DBI :( . Anyway I've downloaded modules to have a play with. And discovered the DBIx::Schema module, which I think might be useful for handling the abstraction of CGI views which encompass more than one schema. There's a lot there to evaluate... I'll try to post something interesting soon if I have time!
    • The Gossamer Threads link is very interesting: looks like a database admin type view of the database at table level, which is very useful. (though my initial idea was to facilitate making User focused displays that relate multiple tables...)
    • It did however execute 90% of the typing for me
      exactly!

    Update:And also found on CPAN CGI::FormBuilder which I wasn't aware of. Looks interesting and I love Nathan Wiger's rationale for creating it:

    I hate generating and processing forms. Hate it, hate it, hate it, hate it.

    Update 2: The original post has had a fairly good response, so I think some work or evaluation on this subject might be called for! I won't be able to spend a lot of time online this week, but I'll check replies and messages as soon as I can. Thanks all!

    Cheerio!
    Osfameron
      CGI::FormBuilder is awesome if you want more than what CGI.pm provides.