背景音乐

天使动漫论坛 - 梦开始的地方

查看: 7202|回复: 1
打印 上一主题 下一主题

关于discuz x2支持x2.5以上db调用写法的考虑

跳转到指定楼层
1#
初音家二小姐 发表于 2016-2-18 16:27:43 来自手机 |只看该作者 |倒序浏览
本帖最后由 初音家二小姐 于 2016-2-18 17:06 编辑

前段时间因为qq接口qqconnect的问题尝试把qq接口更新到connect1.8,从结果来看移植成功了但是仍然频繁报这种错误(1013 Failed to fetch tmp_token)无法解决这个问题
作为该修改的副产物,如何让discuz x2支持discuz x2.5+的插件就成了我们需要观察的目标。其中最明显的差距是在db调用上。

discuz x2.5+以后对db的调用方法类似这样C::t('table_name')->fetch(100);其中100是primarykey
而原有的discuz x2则是用DB类下的静态方法,因此需要简略hack一下让他支持

这个分为两部分,1是引入新的class,这部分可以单独放出一个文件,方便在用的时候现require。考虑到这个行为只是强行让他支持dzx2.5+的写法,平时不应该加载。
2是class_core的db支持,有少量在discuz x2.5+中出现的DB类下方法没有,可以直接添加
3是对这两者的适应性改造。像在新的class里,class_table要求每个表都写一个类支持他,提供如什么键是主键的这种写死在code里面。需要对其小改一下,例如主键手动获取。
4对mysql 需要支持php7的mysqli_lib
2#
初音家二小姐 发表于 2016-2-21 09:32:00 来自手机 |只看该作者
关于(2),添加到class db中的方法



  1. //add function field to adjust discuz x3+
  2.        
  3.         public static function quote($str, $noarray = false) {

  4.                 if (is_string($str))
  5.                         return '\'' . self::mysqli_escape($str) . '\'';

  6.                 if (is_int($str) or is_float($str))
  7.                         return '\'' . $str . '\'';

  8.                 if (is_array($str)) {
  9.                         if($noarray === false) {
  10.                                 foreach ($str as &$v) {
  11.                                         $v = self::quote($v, true);
  12.                                 }
  13.                                 return $str;
  14.                         } else {
  15.                                 return '\'\'';
  16.                         }
  17.                 }

  18.                 if (is_bool($str))
  19.                         return $str ? '1' : '0';

  20.                 return '\'\'';
  21.         }

  22.         public static function quote_field($field) {
  23.                 if (is_array($field)) {
  24.                         foreach ($field as $k => $v) {
  25.                                 $field[$k] = self::quote_field($v);
  26.                         }
  27.                 } else {
  28.                         if (strpos($field, '`') !== false)
  29.                                 $field = str_replace('`', '', $field);
  30.                         $field = '`' . $field . '`';
  31.                 }
  32.                 return $field;
  33.         }
  34.        
  35.         public static function field($field, $val, $glue = '=') {

  36.                 $field = self::quote_field($field);

  37.                 if (is_array($val)) {
  38.                         $glue = $glue == 'notin' ? 'notin' : 'in';
  39.                 } elseif ($glue == 'in') {
  40.                         $glue = '=';
  41.                 }

  42.                 switch ($glue) {
  43.                         case '=':
  44.                                 return $field . $glue . self::quote($val);
  45.                                 break;
  46.                         case '-':
  47.                         case '+':
  48.                                 return $field . '=' . $field . $glue . self::quote((string) $val);
  49.                                 break;
  50.                         case '|':
  51.                         case '&':
  52.                         case '^':
  53.                                 return $field . '=' . $field . $glue . self::quote($val);
  54.                                 break;
  55.                         case '>':
  56.                         case '<':
  57.                         case '<>':
  58.                         case '<=':
  59.                         case '>=':
  60.                                 return $field . $glue . self::quote($val);
  61.                                 break;

  62.                         case 'like':
  63.                                 return $field . ' LIKE(' . self::quote($val) . ')';
  64.                                 break;

  65.                         case 'in':
  66.                         case 'notin':
  67.                                 $val = $val ? implode(',', self::quote($val)) : '\'\'';
  68.                                 return $field . ($glue == 'notin' ? ' NOT' : '') . ' IN(' . $val . ')';
  69.                                 break;

  70.                         default:
  71.                                 //throw new DbException('Not allow this glue between field and value: "' . $glue . '"');
  72.                                 //fuck x3...
  73.                                 exit('Not allow this glue between field and value: "');
  74.                 }
  75.         }
  76.        
  77.         //discuz x3+ function area ends.


复制代码

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

Archiver|手机版|WAP| 天使动漫论坛

【免責聲明】【删除申请】所有內容資源來自網絡&網友分享,僅供日語學習試用,請于24小時內銷毀。如侵犯您的權益請告知,將會第壹時間刪除。我的邮箱

GMT+8, 2026-3-1 22:12 , Processed in 0.768454 second(s), 26 queries , Gzip On.

Powered by Discuz! X2

© 2010-2017 天使动漫论坛

回顶部