欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个构造sql语句的类

程序员文章站 2022-05-30 08:51:56
...
  1. /**
  2. * @package Database Class
  3. * @author injection (mail:injection.mail@gmail.com)
  4. * @version 1.0
  5. */
  6. @ini_set( 'display_errors',0 );
  7. class DataBase{
  8. private $mDb_host,$mAb_user,$mAb_pwd,$mConn_No;
  9. function DataBase( $Conn_Obj ){
  10. $this->connectDb( $Conn_Obj );
  11. }
  12. function connectDb( $Conn_Obj ){
  13. $this->mDb_host = $Conn_Obj->host;
  14. $this->mAd_name = $Conn_Obj->user;
  15. $this->mAd_pwd = $Conn_Obj->pwd;
  16. $this->mConn_No = mysql_connect( $this->mDb_host, $this->mAd_name, $this->mAd_pwd );
  17. }
  18. function selectDb( $Conn_Obj ){
  19. $this->mDb_name = $Conn_Obj->dbname;
  20. mysql_select_db( $this->mDb_name );
  21. }
  22. }
  23. /**
  24. * @package Making Sqls Class exetends Database Class
  25. * @author injection (mail:injection.mail@gmail.com)
  26. * @version 1.0
  27. */
  28. class MakeSql extends DataBase{
  29. private $mSql;
  30. function MakeSql( $type,$arr_colum_list, $arr_sql_choice ){
  31. $this->MakeSqlType( $arr_colum_list, $arr_sql_choice );
  32. }
  33. #switch make list
  34. function MakeSqlType( $type, $arr_colum_list, $arr_sql_choice ){
  35. switch( $type ){
  36. case 'insert':
  37. return $this->makeInsert( $arr_colum_list, $arr_sql_choice );
  38. case 'select':
  39. return $this->makeSelect( $arr_colum_list, $arr_sql_choice );
  40. case 'update':
  41. return $this->makeUpdate( $arr_colum_list, $arr_sql_choice );
  42. case 'delete':
  43. return $this->makeDelete( $arr_colum_list, $arr_sql_choice );
  44. }
  45. }
  46. #make insert
  47. function makeInsert( $arr_colum_list,$arr_sql_choice ){
  48. $colum_key = array_keys( $arr_colum_list );
  49. $colum_value = array_values( $arr_colum_list );
  50. $this->mSql = "INSERT INTO ".$arr_sql_choice["tbl_name"]."( ".join( ',' , $colum_key )." ) VALUES( '".join( "','" , $colum_value )."')";
  51. return $this->mSql;
  52. }
  53. #making select
  54. function makeSelect( $arr_colum_list = '*' , $arr_sql_choice ){
  55. $colum_value = array_keys( $arr_colum_list );
  56. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  57. if( strcmp( $sql_key, 'tbl_name' ) == 0 ){
  58. if( strcmp($arr_colum_list, '*' ) !== 0 )
  59. $this->mSql = "SELECT ".join( ',' , $colum_value )." FROM ".$sql_value;
  60. else
  61. $this->mSql = "SELECT * FROM ".$sql_value;
  62. }
  63. else
  64. if( strcmp( $sql_value, '' ) !== 0 )
  65. if(strcmp( $sql_key, 'WHERE' ) === 0 && strcmp( $sql_value, 'colum' ) === 0 ){
  66. foreach($arr_colum_list As $colum_key => $colum_value )
  67. $this->mSql .= "$colum_key = '$colum_value' AND ";
  68. $this->mSql = rtrim( $this->mSql, " AND " );
  69. }
  70. else
  71. $this->mSql .= " $sql_key ".$sql_value;
  72. }
  73. return $this->mSql;
  74. }
  75. #making update
  76. function makeUpdate( $arr_colum_list, $arr_sql_choice ){
  77. $this->mSql = "UPDATE ".$arr_sql_choice['tbl_name']." SET ";
  78. foreach( $arr_colum_list as $colum_key => $colum_value )
  79. $this->mSql .= "$colum_key = '$colum_value',";
  80. $this->mSql = rtrim( $this->mSql , ',');
  81. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  82. if( strcmp( $sql_value, '' ) !== 0 && strcmp( $sql_key, 'tbl_name' ) !==0 && strcmp( $sql_key, 'ORDER BY' ) !== 0 )
  83. $this->mSql .= " $sql_key ".$sql_value;
  84. }
  85. return $this->mSql;
  86. }
  87. #making delete
  88. function makeDelete( $arr_colum_list, $arr_sql_choice ){
  89. $this->mSql = "DELETE FROM ".$arr_sql_choice['tbl_name'];
  90. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  91. if( strcmp( $sql_key, 'tbl_name' ) !== 0 && strcmp( $sql_value, '' ) !== 0 ){
  92. $this->mSql .= " $sql_key ".$sql_value;
  93. }
  94. }
  95. return $this->mSql;
  96. }
  97. }
复制代码