In this post I try to make more clear previous post Magical SQL and try to distilate idea.

Problem: During more than 3 years of WEB development we found about 70% of CGI code consist of the same kind very simple SQL queries. With many field lists duplication. This is very annoying to write and difficult to support.

Requirements: Proposed solution should :

1) Be very lazy (!) And work magically

2) Cover 90-95% percent of all typical SQL queries. We don't want to duplicate SQL functioanality. SQL is an excellent standart. But we found usually uses ≤10% of the SQL abilites / features

3) Be secure enough

4) Work with MySQL. Nor I neither Alex didn't use in our projects other then MySQL database (but this is topic for other post :) )

Research : We test several others solutions (DBIx::*) :

But non of tested soulution doesn't cover our requirmnets because :

1) They are too complicated and so they are not lazy

2) They try to cover all SQL features and gonna to be API between perl code and SQL

3) They require additional efforts to prepare parameters, and don't allow to pass parameters directly from user as-is

So all this modules was development for other tasks and don't fit our ones.

Idea : 1) This is wrappers to DBI functions like Select, Update, Insert, Replace

2) All this functions have similar input parameters : Table_List and reference to hash with parameters

3) SQL query prepared based on this data and executed

Example of this calls are

@Rec = $dbh->Select('Members', \%Q)
There %Q - list of parameters usually supplied from user via form or directly. For example
%Q = (login__contain=>'alex')

4) Security :

4.1) Override significiant fields rule. E.g. run query as

@Rec = $dbh->Select('Members', {%Q, id_m=>1})
In this case not depending on user conditions only record(s) with id_m=1 will be allowed

4.2) Field name convention rule. All significiant fields should be started with undescore sign. And all fields started from '_' will be removed from user input.

Realization Please check previous post to more example of this idea's realization

Disclaimer: This is still only idea. Altghough this is working idea and working solution. Several projects already done based on this technique and works fine.

Edit ar0n -- added readmore tag


In reply to Magical SQL 2 by asdfgroup

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.