Replace parameters in a SQL query.
@param  string $sql
@param  array  $params
@return string
432604
protected function replaceParams($sql, array $params) {
		$i=0;
		return preg_replace_callback('/\?/', function() use(&$i, $params, $sql) {
			$rep = $params[$i++];
			if(!$rep instanceof Raw && !$rep instanceof static)
				return "'".addslashes($rep)."'";
			else
				return '?';
		}, $sql);
	}
1100848
public static SqlParamsPairs handleIn(String sql, Object[] params){
		
		//split with question mark placeholder
		String[] sqlPieces = sql.split("\\?");
		
		//question mark placeholder list
		String[] questionPlaceholders;
		if(sql.endsWith("?")){
			questionPlaceholders = new String[sqlPieces.length];
		}else{
			questionPlaceholders = new String[sqlPieces.length-1];
		}
		for(int i=0;i<questionPlaceholders.length;i++){
			questionPlaceholders[i]="?";
		}
		
		List<Object> plist = new ArrayList<Object>();
		for(int i=0; i<params.length; i++){
			Object p = params[i];
			if(p.getClass().equals(ArrayList.class)){
				
				//change ? => (?,?,?...)
				//change list to objects
				StringBuilder sb = new StringBuilder();
				sb.append("(");
				ArrayList inParams = (ArrayList)p;
				for(int j=0;j<inParams.size();j++){
					if(j!=0){
						sb.append(",");
					}
					sb.append("?");
					//split list to objects
					plist.add(inParams.get(j));
				}
				sb.append(")");
				questionPlaceholders[i] = sb.toString();
				
			}else{
				plist.add(p);
			}
		}
		
		//join sql
		StringBuilder sqlsb = new StringBuilder();
		for(int i=0;i<sqlPieces.length;i++){
			
			sqlsb.append(sqlPieces[i]);
			if(i<questionPlaceholders.length){
				sqlsb.append(questionPlaceholders[i]);
			}
		}
		
		
		SqlParamsPairs spPairs = new SqlParamsPairs(sqlsb.toString(),plist.toArray());
		
		return spPairs;
	}
555583
protected function fetch( $sql, $params = null ) {
		$stmt = $this->dbh->prepare( $sql );
		$this->bind( $stmt, $params );
		$stmt->execute();
		return $stmt->fetch();
	}
786722
public function sql(string $sql, array $params = []) {
        return $this->loadWiths($this->modelManager->sql($sql, $params));
    }
1197272
protected function replaceParamInSql()/*# : string */
    {
        $count = 0;
        $params = $this->params;
        return preg_replace_callback(
            '/\?|\:\w+/',
            function ($m) use ($count, $params) {
                if ('?' === $m[0]) {
                    $res = $params[$count++];
                } else {
                    $res = isset($params[$m[0]]) ? $params[$m[0]] :
                        $params[substr($m[0], 1)];
                }
                return $this->getDriver()->quote($res);
            },
            $this->sql
        );
    }
909186
protected function _modifyParams(&$sql, &$params) {
        if (is_array($sql)) {
            if (count($sql) < 2) {
                throw new \InvalidArgumentException('If $sql is array, it needs to have at least 2 elements: (0) sql query, string and (1) parameters (single value or array).');
            }
            if ((count($params) > 0)) {
                throw new \InvalidArgumentException('$sql is array, $params should be empty in this case.');
            }
            $params = $sql[1];
            $sql = $sql[0];
        }
        if (!is_array($params)) {
            $params = array ($params);
        }
    }
228031
public function raw(string $sql, string ...$params)
    {
        $this->addCondition('AND', '(' . $this->dialect()->quote($sql) . ')', $params);

        return $this;
    }
650653
protected function queryModify(string $sql, array $params = [], array $options = []): int {
        $options += [Db::OPTION_FETCH_MODE => 0];
        $stm = $this->query($sql, $params, $options);
        return $stm->rowCount();
    }
1155792
public function getValue($sql, $params = null)
    {
        $stmt = $this->execute($sql, $params);

        return $stmt->fetchColumn(0);
    }
1116729
public function debugSql(string $sql, array $params = []): string
    {
        if ($params) {
            
            $indexed = $params == array_values($params);
            
            foreach ($params as $k => $v) {
                
                if (is_object($v)) {
                    
                    if ($v instanceof \DateTime) {
                        $v = $v->format('Y-m-d H:i:s');
                    } else {
                        continue;
                    }
                } elseif (is_string($v)) {
                    $v = "'$v'";
                } elseif ($v === null) {
                    $v = 'NULL';
                } elseif (is_array($v)) {
                    $v = implode(',', $v);
                }
                
                if ($indexed) {
                    $sql = preg_replace('/\?/', $v, $sql, 1);
                } else {
                    $sql = str_replace($k, $v, $sql);
                }
            }
        }
        
        $sql = str_replace('{db_prefix}', $this->prefix, $sql);
        
        return $sql;
    }
1135163
public function insert($sql, $params = array()) {
		$this->query($sql, $params);

		return $this->pdo->lastInsertId();
	}
252981
public function createCommand($sql = null, $params = [])
    {
        /** @var Command $command */
        $command = new $this->commandClass([
            'db' => $this,
            'sql' => $sql,
        ]);

        return $command->bindValues($params);
    }
245278
public function setRaw(string $sql, string ...$params)
    {
        $this->addSet($this->dialect()->quote($sql), $params);

        return $this;
    }
117115
public function orWhereRaw(string $sql, string ...$params)
    {
        $instance = $this->whereStrategyInstance();

        $instance->whereStrategy()->orWhereRaw($sql, ...$params);

        return $instance;
    }
1068300
public static function inline_parameters($sql, $parameters)
    {
        $segments = preg_split('/\?/', $sql);
        $joined = '';
        $inString = false;
        $numSegments = count($segments);
        for ($i = 0; $i < $numSegments; $i++) {
            $input = $segments[$i];
            // Append next segment
            $joined .= $segments[$i];
            // Don't add placeholder after last segment
            if ($i === $numSegments - 1) {
                break;
            }
            // check string escape on previous fragment
            // Remove escaped backslashes, count them!
            $input = preg_replace('/\\\\\\\\/', '', $input);
            // Count quotes
            $totalQuotes = substr_count($input, "'"); // Includes double quote escaped quotes
            $escapedQuotes = substr_count($input, "\\'");
            if ((($totalQuotes - $escapedQuotes) % 2) !== 0) {
                $inString = !$inString;
            }
            // Append placeholder replacement
            if ($inString) {
                // Literal question mark
                $joined .= '?';
                continue;
            }

            // Encode and insert next parameter
            $next = array_shift($parameters);
            if (is_array($next) && isset($next['value'])) {
                $next = $next['value'];
            }
            if (is_bool($next)) {
                $value = $next ? '1' : '0';
            } elseif (is_int($next)) {
                $value = $next;
            } else {
                $value = (DB::get_conn() !== null) ? Convert::raw2sql($next, true) : $next;
            }
            $joined .= $value;
        }
        return $joined;
    }
268341
private function prepare(string $sql, array $params = []): \PDOStatement
    {
        return $this->pdo()->connectionProcess(function (Pdo $pdo) use ($sql, $params) {
            $stmt = $pdo->prepare($sql);

            if ($params) {
                $k = 1;

                foreach ($params as $key => $value) {
                    $stmt->bindValue(is_int($key) ? $k++ : $key, $value);
                }
            }

            $this->fire($sql, $params);

            $stmt->execute();

            $pdo->checkError();

            return $stmt;
        });
    }
976336
public function debugQuery($sql, $params = array())
    {
        $keys = array();

        # build a regular expression for each parameter
        foreach ($params as $key => &$value)
        {
            if (is_string($key))
            {
                $keys[] = '/:' . $key . '/';
            }
            else
            {
                $keys[] = '/[?]/';
            }
            $value = '[#' . $value . '#]';
        }

        $query = preg_replace($keys, $params, $sql, 1, $count);

        return $query;
    }