public function magicBind($sql, array $params = array()) { return $this->_bindInto('(?:`\\w+`|\\w+)\\s*(?:!=|=|<>)\\s*\\?|\\?', $sql, $params, function ($binder, $param, $token) use($sql) { if ($token == '?') { return $binder->sqlValue($param); } else { if (!preg_match("/^(.+?)(\\s*(?:!=|=|<>)\\s*)(.+?)\$/", $token, $m)) { throw new \InvalidArgumentException("Failed to parse magic token {$token}"); } $lhs = $m[1]; $op = trim($m[2]); $rhs = $m[3]; if (($op == '!=' || $op == '<>') && is_array($param)) { return $lhs . ' NOT IN ' . $binder->reduce($param); } if ($op == '=' && is_array($param)) { return $lhs . ' IN ' . $binder->reduce($param); } if (is_null($param)) { return $lhs . ' IS' . ($op == '=' ? '' : ' NOT') . ' NULL'; } return $lhs . $m[2] . $binder->sqlValue($param); } }); }
public function testBindWithBackquote() { $binder = new Binder(); $this->assertEquals($binder->magicBind('`id`=?', array(1)), "`id`='1'"); }