pdo.func.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function pdo() {
  8. global $_W;
  9. static $db;
  10. if(empty($db)) {
  11. if($_W['config']['db']['slave_status'] == true && !empty($_W['config']['db']['slave'])) {
  12. load()->classs('slave.db');
  13. $db = new SlaveDb('master');
  14. } else {
  15. load()->classs('db');
  16. if(empty($_W['config']['db']['master'])) {
  17. $_W['config']['db']['master'] = $GLOBALS['_W']['config']['db'];
  18. $db = new DB($_W['config']['db']);
  19. } else {
  20. $db = new DB('master');
  21. }
  22. }
  23. }
  24. return $db;
  25. }
  26. function pdos($table = '') {
  27. return load()->singleton('Query');
  28. }
  29. function pdo_query($sql, $params = array()) {
  30. return pdo()->query($sql, $params);
  31. }
  32. function pdo_fetchcolumn($sql, $params = array(), $column = 0) {
  33. return pdo()->fetchcolumn($sql, $params, $column);
  34. }
  35. function pdo_fetch($sql, $params = array()) {
  36. return pdo()->fetch($sql, $params);
  37. }
  38. function pdo_fetchall($sql, $params = array(), $keyfield = '') {
  39. return pdo()->fetchall($sql, $params, $keyfield);
  40. }
  41. function pdo_get($tablename, $condition = array(), $fields = array()) {
  42. return pdo()->get($tablename, $condition, $fields);
  43. }
  44. function pdo_getall($tablename, $condition = array(), $fields = array(), $keyfield = '', $orderby = array(), $limit = array()) {
  45. return pdo()->getall($tablename, $condition, $fields, $keyfield, $orderby, $limit);
  46. }
  47. function pdo_getslice($tablename, $condition = array(), $limit = array(), &$total = null, $fields = array(), $keyfield = '', $orderby = array()) {
  48. return pdo()->getslice($tablename, $condition, $limit, $total, $fields, $keyfield, $orderby);
  49. }
  50. function pdo_getcolumn($tablename, $condition = array(), $field) {
  51. return pdo()->getcolumn($tablename, $condition, $field);
  52. }
  53. function pdo_exists($tablename, $condition = array()) {
  54. return pdo()->exists($tablename, $condition);
  55. }
  56. function pdo_count($tablename, $condition = array(), $cachetime = 15) {
  57. return pdo()->count($tablename, $condition, $cachetime);
  58. }
  59. function pdo_update($table, $data = array(), $params = array(), $glue = 'AND') {
  60. return pdo()->update($table, $data, $params, $glue);
  61. }
  62. function pdo_insert($table, $data = array(), $replace = FALSE) {
  63. return pdo()->insert($table, $data, $replace);
  64. }
  65. function pdo_delete($table, $params = array(), $glue = 'AND') {
  66. return pdo()->delete($table, $params, $glue);
  67. }
  68. function pdo_insertid() {
  69. return pdo()->insertid();
  70. }
  71. function pdo_begin() {
  72. pdo()->begin();
  73. }
  74. function pdo_commit() {
  75. pdo()->commit();
  76. }
  77. function pdo_rollback() {
  78. pdo()->rollBack();
  79. }
  80. function pdo_debug($output = true, $append = array()) {
  81. return pdo()->debug($output, $append);
  82. }
  83. function pdo_run($sql) {
  84. return pdo()->run($sql);
  85. }
  86. function pdo_fieldexists($tablename, $fieldname = '') {
  87. return pdo()->fieldexists($tablename, $fieldname);
  88. }
  89. function pdo_fieldmatch($tablename, $fieldname, $datatype = '', $length = '') {
  90. return pdo()->fieldmatch($tablename, $fieldname, $datatype, $length);
  91. }
  92. function pdo_indexexists($tablename, $indexname = '') {
  93. return pdo()->indexexists($tablename, $indexname);
  94. }
  95. function pdo_fetchallfields($tablename){
  96. $fields = pdo_fetchall("DESCRIBE {$tablename}", array(), 'Field');
  97. $fields = array_keys($fields);
  98. return $fields;
  99. }
  100. function pdo_tableexists($tablename){
  101. return pdo()->tableexists($tablename);
  102. }