A class that constructs SQL statements

Source: Internet
Author: User
A class that constructs SQL statements
<无详细内容>

  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 ". $ 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 ')! = 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. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.