techsir 登陆 |注册|TS首页
  首页 快活林 猿氏悟语

动态网页技术PHP通过参数来生成MYSQL语句类

By: 冰客 发表于 2008-11-7 13:18:01 · 4282次点击   回复:0   
这个类可以通过具有参数的数组来构建MySQL查询语句。
  这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句。
  这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句。
  例子:

  1.    <? php
  2.    /* *******************************************************************
  3.   Example file
  4.   This example shows how to use the MyLibSQLGen class
  5.   
  6.   The example is based on the following MySQL table:
  7.   
  8.   CREATE TABLE customer (
  9.    id int(10) unsigned NOT NULL auto_increment,
  10.    name varchar(60) NOT NULL default '',
  11.    address varchar(60) NOT NULL default '',
  12.    city varchar(60) NOT NULL default '',
  13.    PRIMARY KEY (cust_id)
  14.   ) TYPE=MyISAM;
  15.   
  16.   ******************************************************************* */
  17.   
  18.    require_once ( " class_mylib_SQLGen-1.0.php " );
  19.   
  20.    $fields = Array ( " name " , " address " , " city " );
  21.    $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
  22.    $tables = Array ( " customer " );
  23.   
  24.    echo " <b>Result Generate Insert</b><br> " ;
  25.    $object = new MyLibSQLGen();
  26.    $object -> clear_all_assign(); // to refresh all property but it no need when first time execute
  27.    $object -> setFields( $fields );
  28.    $object -> setValues( $values );
  29.    $object -> setTables( $tables );
  30.   
  31.    if ( ! $object -> getInsertSQL()){ echo $object -> Error; exit ;}
  32.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  33.   
  34.   
  35.    echo " <b>Result Generate Update</b><br> " ;
  36.    $fields = Array ( " name " , " address " , " city " );
  37.    $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
  38.    $tables = Array ( " customer " );
  39.    $id = 1 ;
  40.    $conditions [ 0 ][ " condition " ] = " id='$id' " ;
  41.    $conditions [ 0 ][ " connection " ] = "" ;
  42.   
  43.    $object -> clear_all_assign();
  44.    $object -> setFields( $fields );
  45.    $object -> setValues( $values );
  46.    $object -> setTables( $tables );
  47.    $object -> setConditions( $conditions );
  48.   
  49.    if ( ! $object -> getUpdateSQL()){ echo $object -> Error; exit ;}
  50.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  51.   
  52.    echo " <b>Result Generate Delete</b><br> " ;
  53.    $tables = Array ( " customer " );
  54.    $conditions [ 0 ][ " condition " ] = " id='1' " ;
  55.    $conditions [ 0 ][ " connection " ] = " OR " ;
  56.    $conditions [ 1 ][ " condition " ] = " id='2' " ;
  57.    $conditions [ 1 ][ " connection " ] = " OR " ;
  58.    $conditions [ 2 ][ " condition " ] = " id='4' " ;
  59.    $conditions [ 2 ][ " connection " ] = "" ;
  60.   
  61.    $object -> clear_all_assign();
  62.    $object -> setTables( $tables );
  63.    $object -> setConditions( $conditions );
  64.   
  65.    if ( ! $object -> getDeleteSQL()){ echo $object -> Error; exit ;}
  66.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  67.   
  68.    echo " <b>Result Generate List</b><br> " ;
  69.    $fields = Array ( " id " , " name " , " address " , " city " );
  70.    $tables = Array ( " customer " );
  71.    $id = 1 ;
  72.    $conditions [ 0 ][ " condition " ] = " id='$id' " ;
  73.    $conditions [ 0 ][ " connection " ] = "" ;
  74.   
  75.    $object -> clear_all_assign();
  76.    $object -> setFields( $fields );
  77.    $object -> setTables( $tables );
  78.    $object -> setConditions( $conditions );
  79.   
  80.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  81.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  82.   
  83.    echo " <b>Result Generate List with search on all fields</b><br> " ;
  84.    $fields = Array ( " id " , " name " , " address " , " city " );
  85.    $tables = Array ( " customer " );
  86.    $id = 1 ;
  87.    $search = " Fadjar Nurswanto " ;
  88.    $object -> clear_all_assign();
  89.    $object -> setFields( $fields );
  90.    $object -> setTables( $tables );
  91.    $object -> setSearch( $search );
  92.   
  93.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  94.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  95.   
  96.    echo " <b>Result Generate List with search on some fields</b><br> " ;
  97.    $fields = Array ( " id " , " name " , " address " , " city " );
  98.    $tables = Array ( " customer " );
  99.    $id = 1 ;
  100.    $search = Array (
  101.    " name " => " Fadjar Nurswanto " ,
  102.    " address " => " Tomang Raya "
  103.    );
  104.   
  105.    $object -> clear_all_assign();
  106.    $object -> setFields( $fields );
  107.    $object -> setTables( $tables );
  108.    $object -> setSearch( $search );
  109.   
  110.    if ( ! $object -> getQuerySQL()){ echo $object -> Error; exit ;}
  111.    else { $sql = $object -> Result; echo $sql . " <br> " ;}
  112.    ?>
  113.   
复制代码
类代码:

  1.    <? php
  2.    /*
  3.   Created By : Fadjar Nurswanto <fajr_n@rindudendam.net>
  4.   DATE : 2006-08-02
  5.   PRODUCTNAME : class MyLibSQLGen
  6.   PRODUCTVERSION : 1.0.0
  7.   DESCRIPTION : class yang berfungsi untuk menggenerate SQL
  8.   DENPENCIES :
  9.    */
  10.    class MyLibSQLGen
  11.   {
  12.    var $Result ;
  13.    var $Tables = Array ();
  14.    var $Values = Array ();
  15.    var $Fields = Array ();
  16.    var $Conditions = Array ();
  17.    var $Condition ;
  18.    var $LeftJoin = Array ();
  19.    var $Search ;
  20.    var $Sort = " ASC " ;
  21.    var $Order ;
  22.    var $Error ;
  23.   
  24.    function MyLibSQLGen(){}
  25.    function BuildCondition()
  26.    {
  27.    $funct = " BuildCondition " ;
  28.    $className = get_class ( $this );
  29.    $conditions = $this -> getConditions();
  30.    if ( ! $conditions ){ $this -> dbgDone( $funct ); return true ;}
  31.    if ( ! is_array ( $conditions ))
  32.    {
  33.    $this -> Error = " $className::$funct \nVariable conditions not Array " ;
  34.    return ;
  35.    }
  36.    for ( $i = 0 ; $i < count ( $conditions ); $i ++ )
  37.    {
  38.    $this -> Condition .= $conditions [ $i ][ " condition " ] . " " . $conditions [ $i ][ " connection " ] . " " ;
  39.    }
  40.    return true ;
  41.    }
  42.    function BuildLeftJoin()
  43.    {
  44.    $funct = " BuildLeftJoin " ;
  45.    $className = get_class ( $this );
  46.    if ( ! $this -> getLeftJoin()){ $this -> Error = " $className::$funct \nProperty LeftJoin was empty " ; return ;}
  47.   
  48.    $LeftJoinVars = $this -> getLeftJoin();
  49.   
  50.    $hasil = false ;
  51.    foreach ( $LeftJoinVars as $LeftJoinVar )
  52.    {
  53.    @ $hasil .= " LEFT JOIN " . $LeftJoinVar [ " table " ];
  54.    foreach ( $LeftJoinVar [ " on " ] as $var )
  55.    {
  56.    @ $condvar .= $var [ " condition " ] . " " . $var [ " connection " ] . " " ;
  57.    }
  58.    $hasil .= " ON ( " . $condvar . " ) " ;
  59.    unset ( $condvar );
  60.    }
  61.   
  62.    $this -> ResultLeftJoin = $hasil ;
  63.   
  64.    return true ;
  65.    }
  66.    function BuildOrder()
  67.    {
  68.    $funct = " BuildOrder " ;
  69.    $className = get_class ( $this );
  70.    if ( ! $this -> getOrder()){ $this -> Error = " $className::$funct \nProperty Order was empty " ; return ;}
  71.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  72.   
  73.    $Fields = $this -> getFields();
  74.    $Orders = $this -> getOrder();
  75.    if ( ereg ( " , " , $Orders )){ $Orders = explode ( " , " , $Order );}
  76.    if ( ! is_array ( $Orders )){ $Orders = Array ( $Orders );}
  77.   
  78.    foreach ( $Orders as $Order )
  79.    {
  80.    if ( ! is_numeric ( $Order )){ $this -> Error = " $className::$funct \nProperty Order not Numeric " ; return ;}
  81.    if ( $Order > count ( $this -> Fields)){ $this -> Error = " $className::$funct \nMax value of property Sort is " . count ( $this -> Fields); return ;}
  82.   
  83.    @ $xorder .= $Fields [ $Order ] . " , " ;
  84.    }
  85.   
  86.    $this -> ResultOrder = " ORDER BY " . substr ( $xorder , 0 ,- 1 );
  87.   
  88.    return true ;
  89.    }
  90.    function BuildSearch()
  91.    {
  92.    $funct = " BuildSearch " ;
  93.    $className = get_class ( $this );
  94.   
  95.    if ( ! $this -> getSearch()){ $this -> Error = " $className::$funct \nProperty Search was empty " ; return ;}
  96.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  97.   
  98.    $Fields = $this -> getFields();
  99.    $xvalue = $this -> getSearch();
  100.   
  101.    if ( is_array ( $xvalue ))
  102.    {
  103.    foreach ( $Fields as $field )
  104.    {
  105.    if (@ $xvalue [ $field ])
  106.    {
  107.    $Values = explode ( " " , $xvalue [ $field ]);
  108.    foreach ( $Values as $Value )
  109.    {
  110.    @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
  111.    }
  112.    if ( $hasil )
  113.    {
  114.    @ $hasil_final .= " ( " . substr ( $hasil , 0 ,- 4 ) . " ) AND " ;
  115.    unset ( $hasil );
  116.    }
  117.    }
  118.    }
  119.    $hasil = $hasil_final ;
  120.    }
  121.    else
  122.    {
  123.    foreach ( $Fields as $field )
  124.    {
  125.    $Values = explode ( " " , $xvalue );
  126.    foreach ( $Values as $Value )
  127.    {
  128.    @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
  129.    }
  130.    }
  131.    }
  132.   
  133.    $this -> ResultSearch = substr ( $hasil , 0 ,- 4 );
  134.    return true ;
  135.    }
  136.    function clear_all_assign()
  137.    {
  138.    $this -> Result = null ;
  139.    $this -> ResultSearch = null ;
  140.    $this -> ResultLeftJoin = null ;
  141.    $this -> Result = null ;
  142.    $this -> Tables = Array ();
  143.    $this -> Values = Array ();
  144.    $this -> Fields = Array ();
  145.    $this -> Conditions = Array ();
  146.    $this -> Condition = null ;
  147.    $this -> LeftJoin = Array ();
  148.    $this -> Sort = " ASC " ;
  149.    $this -> Order = null ;
  150.    $this -> Search = null ;
  151.    $this -> fieldSQL = null ;
  152.    $this -> valueSQL = null ;
  153.    $this -> partSQL = null ;
  154.    $this -> Error = null ;
  155.    return true ;
  156.    }
  157.    function CombineFieldValue( $manual = false )
  158.    {
  159.    $funct = " CombineFieldsPostVar " ;
  160.    $className = get_class ( $this );
  161.    $fields = $this -> getFields();
  162.    $values = $this -> getValues();
  163.    if ( ! is_array ( $fields ))
  164.    {
  165.    $this -> Error = " $className::$funct \nVariable fields not Array " ;
  166.    return ;
  167.    }
  168.    if ( ! is_array ( $values ))
  169.    {
  170.    $this -> Error = " $className::$funct \nVariable values not Array " ;
  171.    return ;
  172.    }
  173.    if ( count ( $fields ) != count ( $values ))
  174.    {
  175.    $this -> Error = " $className::$funct \nCount of fields and values not match " ;
  176.    return ;
  177.    }
  178.    for ( $i = 0 ; $i < count ( $fields ); $i ++ )
  179.    {
  180.    @ $this -> fieldSQL .= $fields [ $i ] . " , " ;
  181.    if ( $fields [ $i ] == " pwd " || $fields [ $i ] == " password " || $fields [ $i ] == " pwd " )
  182.    {
  183.    @ $this -> valueSQL .= " password(' " . $values [ $i ] . " '), " ;
  184.    @ $this -> partSQL .= $fields [ $i ] . " =password(' " . $values [ $i ] . " '), " ;
  185.    }
  186.    else
  187.    {
  188.    if ( is_numeric ( $values [ $i ]))
  189.    {
  190.    @ $this -> valueSQL .= $values [ $i ] . " , " ;
  191.    @ $this -> partSQL .= $fields [ $i ] . " = " . $values [ $i ] . " , " ;
  192.    }
  193.    else
  194.    {
  195.    @ $this -> valueSQL .= " ' " . $values [ $i ] . " ', " ;
  196.    @ $this -> partSQL .= $fields [ $i ] . " =' " . $values [ $i ] . " ', " ;
  197.    }
  198.    }
  199.    }
  200.    $this -> fieldSQL = substr ( $this -> fieldSQL , 0 ,- 1 );
  201.    $this -> valueSQL = substr ( $this -> valueSQL , 0 ,- 1 );
  202.    $this -> partSQL = substr ( $this -> partSQL , 0 ,- 1 );
  203.    return true ;
  204.    }
  205.    function getDeleteSQL()
  206.    {
  207.    $funct = " getDeleteSQL " ;
  208.    $className = get_class ( $this );
  209.    $Tables = $this -> getTables();
  210.    if ( ! $Tables || ! count ( $Tables ))
  211.    {
  212.    $this -> dbgFailed( $funct );
  213.    $this -> Error = " $className::$funct \nTable was empty " ;
  214.    return ;
  215.    }
  216.    for ( $i = 0 ; $i < count ( $Tables ); $i ++ )
  217.    {
  218.    @ $Table .= $Tables [ $i ] . " , " ;
  219.    }
  220.    $Table = substr ( $Table , 0 ,- 1 );
  221.   
  222.    $sql = " DELETE FROM " . $Table ;
  223.   
  224.    if ( $this -> getConditions())
  225.    {
  226.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  227.    $sql .= " WHERE " . $this -> getCondition();
  228.    }
  229.    $this -> Result = $sql ;
  230.    return true ;
  231.    }
  232.    function getInsertSQL()
  233.    {
  234.    $funct = " getInsertSQL " ;
  235.    $className = get_class ( $this );
  236.    if ( ! $this -> getValues()){ $this -> Error = " $className::$funct \nProperty Values was empty " ; return ;}
  237.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  238.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  239.   
  240.    if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
  241.    $Tables = $this -> getTables();
  242.   
  243.    $sql = " INSERT INTO " . $Tables [ 0 ] . " ( " . $this -> fieldSQL . " ) VALUES ( " . $this -> valueSQL . " ) " ;
  244.   
  245.    $this -> Result = $sql ;
  246.   
  247.    return true ;
  248.    }
  249.    function getUpdateSQL()
  250.    {
  251.    $funct = " getUpdateSQL " ;
  252.    $className = get_class ( $this );
  253.   
  254.    if ( ! $this -> getValues()){ $this -> Error = " $className::$funct \nProperty Values was empty " ; return ;}
  255.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  256.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  257.   
  258.    if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
  259.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  260.    $Tables = $this -> getTables();
  261.   
  262.    $sql = " UPDATE " . $Tables [ 0 ] . " SET " . $this -> partSQL . " WHERE " . $this -> getCondition();
  263.   
  264.    $this -> Result = $sql ;
  265.   
  266.    return true ;
  267.    }
  268.    function getQuerySQL()
  269.    {
  270.    $funct = " getQuerySQL " ;
  271.    $className = get_class ( $this );
  272.   
  273.    if ( ! $this -> getFields()){ $this -> Error = " $className::$funct \nProperty Fields was empty " ; return ;}
  274.    if ( ! $this -> getTables()){ $this -> Error = " $className::$funct \nProperty Tables was empty " ; return ;}
  275.   
  276.    $Fields = $this -> getFields();
  277.    $Tables = $this -> getTables();
  278.    foreach ( $Fields as $Field ){@ $sql_raw .= $Field . " , " ;}
  279.    foreach ( $Tables as $Table ){@ $sql_table .= $Table . " , " ;}
  280.   
  281.    $this -> Result = " SELECT " . substr ( $sql_raw , 0 ,- 1 ) . " FROM " . substr ( $sql_table , 0 ,- 1 );
  282.   
  283.    if ( $this -> getLeftJoin())
  284.    {
  285.    if ( ! $this -> BuildLeftJoins()){ $this -> dbgFailed( $funct ); return ;}
  286.    $this -> Result .= " " . $this -> ResultLeftJoin;
  287.    }
  288.    if ( $this -> getConditions())
  289.    {
  290.    if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
  291.    $this -> Result .= " WHERE ( " . $this -> Condition . " ) " ;
  292.    }
  293.    if ( $this -> getSearch())
  294.    {
  295.    if ( ! $this -> BuildSearch()){ $this -> dbgFailed( $funct ); return ;}
  296.    if ( $this -> ResultSearch)
  297.    {
  298.    if ( eregi ( " WHERE " , $this -> Result)){ $this -> Result .= " AND " . $this -> ResultSearch;}
  299.    else { $this -> Result .= " WHERE " . $this -> ResultSearch;}
  300.    }
  301.    }
  302.    if ( $this -> getOrder())
  303.    {
  304.    if ( ! $this -> BuildOrder()){ $this -> dbgFailed( $funct ); return ;}
  305.    $this -> Result .= " " . $this -> ResultOrder;
  306.    }
  307.    if ( $this -> getSort())
  308.    {
  309.    if (@ $this -> ResultOrder)
  310.    {
  311.    $this -> Result .= " " . $this -> getSort();
  312.    }
  313.    }
  314.   
  315.    return true ;
  316.    }
  317.   
  318.    function getCondition(){ return @ $this -> Condition;}
  319.    function getConditions(){ if ( count (@ $this -> Conditions) && is_array (@ $this -> Conditions)){ return @ $this -> Conditions;}}
  320.    function getFields(){ if ( count (@ $this -> Fields) && is_array (@ $this -> Fields)){ return @ $this -> Fields;}}
  321.    function getLeftJoin(){ if ( count (@ $this -> LeftJoin) && is_array (@ $this -> LeftJoin)){ return @ $this -> LeftJoin;}}
  322.    function getOrder(){ return @ $this -> Order;}
  323.    function getSearch(){ return @ $this -> Search;}
  324.    function getSort(){ return @ $this -> Sort ;}
  325.    function getTables(){ if ( count (@ $this -> Tables) && is_array (@ $this -> Tables)){ return @ $this -> Tables;}}
  326.    function getValues(){ if ( count (@ $this -> Values) && is_array (@ $this -> Values)){ return @ $this -> Values;}}
  327.   
  328.    function setCondition( $input ){ $this -> Condition = $input ;}
  329.    function setConditions( $input )
  330.    {
  331.    if ( is_array ( $input )){ $this -> Conditions = $input ;}
  332.    else { $this -> Error = get_class ( $this ) . " ::setConditions \nParameter input not array " ; return ;}
  333.    }
  334.    function setFields( $input )
  335.    {
  336.    if ( is_array ( $input )){ $this -> Fields = $input ;}
  337.    else { $this -> Error = get_class ( $this ) . " ::setFields \nParameter input not array " ; return ;}
  338.    }
  339.    function setLeftJoin( $input )
  340.    {
  341.    if ( is_array ( $input )){ $this -> LeftJoin = $input ;}
  342.    else { $this -> Error = get_class ( $this ) . " ::setFields \nParameter input not array " ; return ;}
  343.    }
  344.    function setOrder( $input ){ $this -> Order = $input ;}
  345.    function setSearch( $input ){ $this -> Search = $input ;}
  346.    function setSort( $input ){ $this -> Sort = $input ;}
  347.    function setTables( $input )
  348.    {
  349.    if ( is_array ( $input )){ $this -> Tables = $input ;}
  350.    else { $this -> Error = get_class ( $this ) . " ::setTables \nParameter input not array " ; return ;}
  351.    }
  352.    function setValues( $input )
  353.    {
  354.    if ( is_array ( $input )){ $this -> Values = $input ;}
  355.    else { $this -> Error = get_class ( $this ) . " ::setValues \nParameter input not array " ; return ;}
  356.    }
  357.   }
  358.    ?>
复制代码
4282次点击
0个回复  |  直到 2008-11-7 13:18:01
添加一条新回复
您需要登录后才可以回帖 登录 | 成为会员 新浪微博登陆

标签云|手机版|科技先生 ( 京ICP备07036130号 Powered by Discuz! X )

GMT+8, 2024-5-19 13:19