Generate the SQL for a query on the model
@param array $params Parameter array
@param \PDO $db Database connection to use, default db
@return string
439167
public static function _genSQL ($params = array (), &$db = FALSE)
	{
		
		// Set select

		if (!isset ($params['select']))
		{
			$params['select']	= '*';
		}
		
		// Set from

		if (!isset ($params['from']))
		{
			$params['from']		= '`' . static::$dbTable . '`';
		}
		
		// Get database slave for read

		if ($db === FALSE)
		{
			$db	=& self::_getDbSlave ();
		}
		
		if (!($db instanceof \PDO))
		{
			throw new Exception ('Invalid or no database resource set');
		}
		
		// Return value

		return $db->genSQL ($params);

	}
1214629
protected function debug_query($sql, $params = null) {
        echo '<hr /> (', $this->get_dbtype(), '): ',  htmlentities($sql, ENT_QUOTES, 'UTF-8');
        if($params) {
            echo ' (parameters ';
            print_r($params);
            echo ')';
        }
        echo '<hr />';
    }
1211662
public function execute($sql, array $params=null) {
        list($sql, $params, $type) = $this->fix_sql_params($sql, $params);

        $result = true;
        $this->query_start($sql, $params, SQL_QUERY_UPDATE);

        try {
            $sth = $this->pdb->prepare($sql);
            $sth->execute($params);
        } catch (PDOException $ex) {
            $this->lastError = $ex->getMessage();
            $result = false;
        }

        $this->query_end($result);
        return $result;
    }
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);
	}
48339
public function query($sql, array $params = []) {
        $start_time = microtime(true);
        $this->sql  = $sql;
        $this->query->build()->reset();
        #准备sql
        $sth = $this->getLink(false)->prepare($sql);
        #设置保存数据
        $sth->setFetchMode(PDO::FETCH_ASSOC);
        #绑定参数
        $params = $this->setParamsSort($params);
        foreach ((array) $params as $key => $value) {
            $sth->bindParam($key, $params[$key], is_numeric($params[$key]) ? PDO::PARAM_INT : PDO::PARAM_STR);
        }
        try {
            #执行查询
            $sth->execute();
            $this->affectedRow = $sth->rowCount();
            $data              = $sth->fetchAll() ?: [];
            #记录日志
            if (DEBUG) {
                $end_time = microtime(true);

                self::$queryLog[] = array(
                    'sql'  => sql_processing($sql, $params),
                    'data' => to_json($data),
                    'time' => ($end_time - $start_time) . 'μs',
                );
            }
            return $data;
        } catch (Exception $e) {
            $error = $sth->errorInfo();
            throw new Exception($sql . " ;BindParams:" . var_export($params, true) . implode(';', $error));
        }
    }
1288454
public function query(string $sql, array $params = [])
    {
        $prepared = $this->prepare($sql, $params);
        $this->execute($prepared);
        return new ResultSet($prepared['statement']);
    }
1141130
public function queryColumn($sql = null,$params = array())
	{
		if(!isset($this->operationOption['params']))
		{
			$this->operationOption['params'] = array();
		}
		$this->prepareQuery($sql ? $sql : $this->buildSelectSQL(),$params);
		if(false === $this->lastStmt)
		{
			return false;
		}
		else
		{
			return $this->lastStmt->fetchAll(PDO::FETCH_COLUMN);
		}
	}
1287492
public function setSql($sql, array $params = [])
    {
        $this->sql = (string) $sql;

        $this->setSqlParameters($params);
    }
487188
public function createSqlParts( $params )
    {
        $returnArray = array( 'tables' => '', 'joins'  => '', 'columns' => '' );

        if ( !isset( $params['parent_tag_id'] ) )
        {
            return $returnArray;
        }

        if ( is_array( $params['parent_tag_id'] ) )
        {
            $parentTagIDsArray = $params['parent_tag_id'];
        }
        else if ( (int) $params['parent_tag_id'] > 0 )
        {
            $parentTagIDsArray = array( (int) $params['parent_tag_id'] );
        }
        else
        {
            return $returnArray;
        }

        $suffix = '/%';
        if ( isset( $params['exclude_parent'] ) && $params['exclude_parent'] )
        {
            $suffix = '/%/%';
        }

        $returnArray['tables'] = " INNER JOIN eztags_attribute_link i1 ON (i1.object_id = ezcontentobject.id AND i1.objectattribute_version = ezcontentobject.current_version)
                                   INNER JOIN eztags i2 ON (i1.keyword_id = i2.id)
                                   INNER JOIN eztags_keyword i3 ON (i2.id = i3.keyword_id)";

        $db = eZDB::instance();
        $dbStrings = array();

        if ( isset( $params['type'] ) && strtolower( $params['type'] ) == 'and' )
        {
            foreach ( $parentTagIDsArray as $parentTagID )
            {
                $dbStrings[] = "EXISTS (
                    SELECT 1
                    FROM
                        eztags_attribute_link j1,
                        ezcontentobject j2,
                        eztags j3
                    WHERE j3.path_string LIKE \"%/" . $db->escapeString( $parentTagID . $suffix ) . "
                    AND j1.object_id = j2.id
                    AND j2.id = ezcontentobject.id
                    AND j1.objectattribute_version = j2.current_version
                    AND j3.id = j1.keyword_id
                )";
            }

            $dbString = implode( ' AND ', $dbStrings );
        }
        else
        {
            foreach ( $parentTagIDsArray as $parentTagID )
            {
                $dbStrings[] = ' i2.path_string LIKE "%/' . $db->escapeString( $parentTagID . $suffix ) . '"';
            }

            $dbString = implode( ' OR ', $dbStrings );
        }

        if ( isset( $params['language'] ) )
        {
            $language = $params['language'];
            if ( !is_array( $language ) )
            {
                $language = array( $language );
            }

            eZContentLanguage::setPrioritizedLanguages( $language );
        }


        $returnArray['joins'] = " ( $dbString )
                                  AND " . eZContentLanguage::languagesSQLFilter( 'i2' ) . " AND " .
                                  eZContentLanguage::sqlFilter( 'i3', 'i2' ) . " AND ";

        if ( isset( $params['language'] ) )
        {
            eZContentLanguage::clearPrioritizedLanguages();
        }

        return $returnArray;
    }
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;
        });
    }
368018
public function getRow($sql, $params = array())
    {
        $stmt = $this->execute($sql, $params);
        return $stmt->fetch($this->fetchMode);
    }
1080740
public function bind($sql, array $params=array())
    {
        return $this->_bindInto('\?', $sql, $params, function($binder, $param) use ($sql) {
            return $binder->sqlValue($param);
        });
    }
1293740
public function buildSql($params)
    {
        $sql = '';

        if (isset($params['fields'])) {

            $sql .= 'SELECT ';
            if (isset($params['limit']) && !isset($params['offset'])) {
                $sql .= 'TOP ' . $params['limit'] . ' ';
            }
            if (isset($params['distinct'])) {
                $sql .= 'DISTINCT ';
            }

            $sql .= $params['fields'];
            if (isset($params['limit'], $params['offset'])) {
                if (!isset($params['order'])) {
                    throw new PreconditionException('if use offset CLAUSE, must provide order CLAUSE.');
                }

                $sql .= ', ROW_NUMBER() OVER (ORDER BY ' . (isset($params['order']) ? $params['order'] : 'rand()') . ') AS _row_number_';
            }
        }

        if (isset($params['from'])) {
            $sql .= ' FROM ' . $params['from'];
        }

        if (isset($params['join'])) {
            $sql .= $params['join'];
        }

        if (isset($params['where'])) {
            $sql .= ' WHERE ' . $params['where'];
        }

        if (isset($params['limit'], $params['offset'])) {
            $sql = 'SELECT' . ' t.* FROM (' . $sql . ') as t WHERE t._row_number_ BETWEEN ' . $params['offset'] . ' + 1 AND ' . $params['offset'] . ' + ' . $params['limit'];
        }

        if (isset($params['group'])) {
            $sql .= ' GROUP BY ' . $params['group'];
        }

        if (isset($params['having'])) {
            $sql .= ' HAVING ' . $params['having'];
        }

        if (isset($params['order']) && !isset($params['offset'])) {
            $sql .= ' ORDER BY ' . $params['order'];
        }

        if (isset($params['forUpdate'])) {
            $sql .= 'FOR UPDATE';
        }

        return $sql;
    }
343621
public static function getSqlWithParams(string $sql, array $params = []): string
    {
        if (! empty($params)) {
            $indexed = ($params === array_values($params));
            foreach ($params as $k => $v) {
                if (is_string($v)) {
                    $v = "'${v}'";
                }
                if (is_array($v)) {
                    $v = "'" . implode("','", $v) . "'";
                }
                if ($indexed) {
                    $sql = preg_replace('/\?/', $v, $sql, 1);
                } else {
                    $sql = str_replace(":${k}", $v, $sql);
                }
            }
        }

        return str_replace(PHP_EOL, '', $sql);
    }
1151066
public function execute($params = [])
    {
        return $this->getDb()
            ->createCommand()
            ->setText($this->buildSql())
            ->execute(array_merge($this->getParams(), $params));
    }
422689
public function query($statement, $params = array())
    {
        $returnValue = null;
        $this->preparedExec = false;

       
        if (count($params) > 0){
        	$sth = $this->dbConnector->prepare($statement);
        	$sth->execute($params);
        }
        else{
        	$sth = $this->dbConnector->query($statement);
        }
        
		
        if (!empty($sth)){
        	$returnValue = $sth;
        }

        return $returnValue;
    }
1159356
public static function _exists ($params, &$db = FALSE)
	{
	
		if (!is_array ($params))
		{
			$params	= array (
				'where'	=> array (
					array (static::$pk, $params)
				)
			);
		}
		
		return self::_count ($params, $db) > 0;
		
	}