自动根据 db属性执行$this->db(xxx)方法; table/tablePrefix属性执行$this->db('xxx')->table('tablename', 'tablePrefix')方法

@return \Cml\Db\MySql\Pdo | \Cml\Db\MongoDB\MongoDB | \Cml\Db\Base
171606
public function mapDbAndTable()
    {
        return $this->db($this->getDbConf())->table($this->getTableName(), $this->tablePrefix);
    }
1109859
public function error() {
        if($this->PDOStatement) {
            $error = $this->PDOStatement->errorInfo();
            $this->error = $error[1].':'.$error[2];
        }else{
            $this->error = '';
        }
        if('' != $this->queryStr){
            $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
        }
        // 记录错误日志
        trace($this->error,'','ERR');
        if($this->config['debug']) {// 开启数据库调试模式
            E($this->error);
        }else{
            return $this->error;
        }
    }
680057
public static function getDbo()
	{
		if (!self::$database)
		{
			//get config values

			$host = Config::get()->get('database.host');
			$user = Config::get()->get('database.user');
			$password = Config::get()->get('database.password');
			$database = Config::get()->get('database.name');
			$prefix = Config::get()->get('database.prefix');
			$driver = Config::get()->get('database.driver');
			$debug = Config::get()->get('database.debug');

			//added extra code to prevent error when $driver is incorrect

			$options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);

			self::$database = DatabaseFactory::getInstance()->getDriver($driver, $options);

			//get the debug configuration setting
			self::$database->setDebug(Config::get()->get('debug'));
		}
		return self::$database;
	}
1138256
private function createUpdate(): string
    {
        // 处理字段名值数组
        list ($fields, $values) = $this->db->createRow($this->row);

        // 处理条件三项
        list ($whereSql, $wherePrepare, $whereParams) = $this->db->createWhere($this->where);
        if ($whereSql === false) {
            return '';
        }

        // 更新时未指定条件
        if (!$whereSql) {
            trigger_error('Update操作必须指定查询条件,不允许全表修改', E_USER_ERROR);
        }

        // 字段数组中的三项
        $set = $setPrepare = [];
        foreach ($fields as $k => $field) {
            $set[] = $this->db->createSet($field, $values[$k]);
            $setPrepare[] = $this->db->createSet($field, '?');
        }

        $this->sql = $this->db->createUpdate($this->table, $set, $whereSql);
        $this->prepare = $this->db->createUpdate($this->table, $setPrepare, $wherePrepare);
        $this->params = array_merge($values, $whereParams);

        $this->tables = [$this->table];
        return $this->prepare;
    }
312245
protected function buildSql()
    {
        $query = $this->getQuery();
        if (
            !isset($query['table']) || empty($query['table'])
            || !isset($query['where']) || empty($query['where'])
        ) {
            throw new Exception(str_cover('"{type}"查询参数不完整', [
                '{type}' => 'delete'
            ]), 101300401);
        }
        return 'DELETE FROM ' . $this->quoteTableName($query['table']) . ' WHERE ' . $query['where'];
    }
913317
public function getTable()
	{
		$schema_prefix = '';
		$schema = $this->getSchema();
		if (isset($schema) && $schema != '') $schema_prefix = $schema . '.';
		
		if (isset($this->table)) 	$table = $this->table;
		else 						$table = str_replace('\\', '', snake_case(class_basename($this)));
		
		if (stripos($table, '.') !== false)
			return $table;
		else
			return $schema_prefix . $table;
	}
111542
public static function db()
    {
        if (self::$db === null) {
            $rc = rcube::get_instance();
            $db = rcube_db::factory($rc->config->get('db_dsnw'));

            $db->set_debug((bool)$rc->config->get('sql_debug'));

            // Connect to database
            $db->db_connect('w');

            if (!$db->is_connected()) {
                rcube::raise_error("Error connecting to database: " . $db->is_error(), false, true);
            }

            self::$db = $db;
        }

        return self::$db;
    }
355093
public function getSql()
    {
        $sqlTemplate = "SELECT %s FROM %s %s";
        $fieldsCompiled = implode(',', $this->getFields());
        return trim(sprintf(
            $sqlTemplate,
            $fieldsCompiled,
            $this->getTable(),
            $this->getWhereStatement()
        ));
    }
86901
public function getPHP() {

        $result = '';

        // The type
        switch ($this->getType()) {
            case XMLDB_KEY_PRIMARY:
                $result .= 'XMLDB_KEY_PRIMARY' . ', ';
                break;
            case XMLDB_KEY_UNIQUE:
                $result .= 'XMLDB_KEY_UNIQUE' . ', ';
                break;
            case XMLDB_KEY_FOREIGN:
                $result .= 'XMLDB_KEY_FOREIGN' . ', ';
                break;
            case XMLDB_KEY_FOREIGN_UNIQUE:
                $result .= 'XMLDB_KEY_FOREIGN_UNIQUE' . ', ';
                break;
        }
        // The fields
        $keyfields = $this->getFields();
        if (!empty($keyfields)) {
            $result .= "['".  implode("', '", $keyfields) . "']";
        } else {
            $result .= 'null';
        }
        // The FKs attributes
        if ($this->getType() == XMLDB_KEY_FOREIGN ||
            $this->getType() == XMLDB_KEY_FOREIGN_UNIQUE) {
            // The reftable
            $reftable = $this->getRefTable();
            if (!empty($reftable)) {
                $result .= ", '" . $reftable . "', ";
            } else {
                $result .= 'null, ';
            }
            // The reffields
            $reffields = $this->getRefFields();
            if (!empty($reffields)) {
                $result .= "['".  implode("', '", $reffields) . "']";
            } else {
                $result .= 'null';
            }
        }
        // Return result
        return $result;
    }
393427
public function checkTablePrefix() {
        if (!$this->tablePrefix) {
            $this->setTablePrefix($this->in('What table prefix would you like to use?'));
        }

        $this->out(sprintf('Table Prefix: <info>%s</info>', $this->tablePrefix));

        $answer = strtoupper($this->in('Is this correct?', array('Y', 'N')));

        if ($answer === 'N') {
            $this->tablePrefix = null;
            $this->checkTablePrefix();
        }

        $this->out('<success>Table prefix set, proceeding...</success>');
        return true;
    }
1113743
public function toString()
    {
        $driver = $this->getDriver();
        $prefix = $driver->getTablePrefix();
        $query  = 'DELETE';

        if($this->table && $this->join) {
            $query .= ' '.$driver->quoteIdentifier(!is_numeric(key($this->table)) ? key($this->table) : current($this->table));
        }

        if($this->table) {
            $query .= ' FROM '.$driver->quoteIdentifier($prefix.current($this->table).(!is_numeric(key($this->table)) ? ' AS '.key($this->table) : ''));
        }

        if($this->join)
        {
            $joins = array();
            foreach($this->join as $alias => $join)
            {
                $tmp = '';

                if($join['type']) {
                    $tmp .= ' '.$join['type'];
                }

                if($join['table'] instanceof DatabaseQuerySelect) {
                    $tmp .= ' JOIN ('.$join['table'].')'.(is_string($alias) ? ' AS '.$driver->quoteIdentifier($alias) : '');
                } else {
                    $tmp .= ' JOIN '.$driver->quoteIdentifier($prefix.$join['table'].(is_string($alias) ? ' AS '.$alias : ''));
                }

                if($join['condition']) {
                    $tmp .= ' ON ('.$driver->quoteIdentifier($join['condition']).')';
                }

                $joins[] = $tmp;
            }

            $query .= implode('', $joins);
        }

        if($this->where)
        {
            $query .= ' WHERE';

            foreach($this->where as $where)
            {
                if(!empty($where['combination'])) {
                    $query .= ' '.$where['combination'];
                }

                $query .= ' '.$driver->quoteIdentifier($where['condition']);
            }
        }

        if($this->order)
        {
            $query .= ' ORDER BY ';

            $list = array();
            foreach($this->order as $order) {
                $list[] = $driver->quoteIdentifier($order['column']).' '.$order['direction'];
            }

            $query .= implode(' , ', $list);
        }

        if($this->limit) {
            $query .= ' LIMIT '.$this->offset.' , '.$this->limit;
        }

        if($this->_parameters) {
            $query = $this->_replaceParams($query);
        }

        return $query;
    }
1209496
private function createQuery(): string
    {
        // 已经直接传递了SQL,Prepare与SQL相同
        $this->prepare = $this->sql;

        // 查找其中的表名
        $this->tables = $this->db->getNameFromQuery($this->sql);

        return $this->prepare;
    }
409267
protected function _getTables()
    {
        $prefix = \JFactory::getApplication()->get('dbprefix');
        $dbo    = \JFactory::getDbo();
        $tables = array();

        foreach (\JFactory::getDbo()->getTableList() as $table)
        {
            // Only check in tables with a prefix
            if (stripos($table, $prefix) === 0)
            {
                $columns = $dbo->getTableColumns($table);

                // Make sure that the table has the check in columns
                if (!isset($columns[$this->_user_column]) || !isset($columns[$this->_date_column])) {
                    continue;
                }

                // Check the column's types.
                if (stripos($columns[$this->_user_column], 'int') !== 0 || stripos($columns[$this->_date_column], 'date') !== 0) {
                    continue;
                }

                $query = $dbo->getQuery(true)
                             ->select('COUNT(*)')
                             ->from($dbo->quoteName($table))
                             ->where(sprintf('%s > 0', $this->_user_column));

                $dbo->setQuery($query);

                // Only include tables that need to be checked in
                if (!$dbo->execute() || !$dbo->loadResult()) {
                    continue;
                }

                $tables[] = str_replace($prefix, '', $table);
            }
        }

        return $tables;
    }
160232
public function getDbTableShema()
    {
        if ($this->_dbTableShema === null) {
            $this->_dbTableShema = Yii::$app->db->getTableSchema($this->dbTableName, true);
        }
        
        return $this->_dbTableShema;
    }
1301509
protected function opendb() {
    // Only do one open
    
    if($this->db) {
      return $this->db;
    }
    //echo "$this->host, $this->user, $this->password";
    try {
      $db = @new PDO($this->host, $this->user, $this->password);
    } catch(PDOException $e) {
      $this->errno = $e->getCode();
      $this->error = $e->getMessage();
      throw new SqlException(__METHOD__.  " Connection failed: $this->host", $this);
    }

    $this->db = $db; // set this right away so if we get an error below $this->db is valid

    return $db;
  }
906163
public function query()
	{
		if ($this->query_sql === null) {
			$this->compile();
		}

		if (empty($this->query_params)) {
			$t = microtime(true);
			try {
				$result = $this->pdo->query($this->query_sql);
				if ($this->log_query) {
					error_log(sprintf("SQL Query time: %F ms (query), %d rows.", (microtime(true) - $t) * 1000, $result->rowCount()));
				}
			}
			catch (\PDOException $ex) {
				throw new FlupdoException($ex->getMessage(), $ex->getCode(), $ex, $this->query_sql, $this->query_params);
			}
			if (!$result) {
				throw new FlupdoException(vsprintf("SQLSTATE[%s]: Error %s: %s", $this->pdo->errorInfo()),
					$this->pdo->errorCode(), null, $this->query_sql, $this->query_params);
			}
			return $result;
		} else {
			$t = microtime(true);
			try {
				$stmt = $this->prepare();
			}
			catch (\PDOException $ex) {
				throw new FlupdoException($ex->getMessage(), $ex->getCode(), $ex, $this->query_sql, $this->query_params);
			}
			if ($stmt === FALSE) {
				throw new FlupdoException(vsprintf("SQLSTATE[%s]: Error %s: %s", $this->pdo->errorInfo()),
					$this->pdo->errorCode(), null, $this->query_sql, $this->query_params);
			}

			$i = 1;
			foreach ($this->query_params as $param) {
				if (is_bool($param)) {
					$stmt->bindValue($i, $param, \PDO::PARAM_BOOL);
				} else if (is_null($param)) {
					$stmt->bindValue($i, $param, \PDO::PARAM_NULL);
				} else if (is_int($param)) {
					$stmt->bindValue($i, $param, \PDO::PARAM_INT);
				} else {
					// ignore locales when converting to string
					$stmt->bindValue($i, strval($param), \PDO::PARAM_STR);
				}
				$i++;
			}

			try {
				$success = $stmt->execute();
			}
			catch (\PDOException $ex) {
				throw new FlupdoException($ex->getMessage(), $ex->getCode(), $ex, $this->query_sql, $this->query_params);
			}
			if ($success === FALSE) {
				throw new FlupdoException(vsprintf("SQLSTATE[%s]: Error %s: %s", $stmt->errorInfo()),
					$stmt->errorCode(), null, $this->query_sql, $this->query_params);
			}
			if ($this->log_query) {
				error_log(sprintf("SQL Query time: %F ms (prepare + execute), %d rows.", (microtime(true) - $t) * 1000, $stmt->rowCount()));
			}
			if ($this->can_explain && $this->log_explain) {
				$this->explain();
			}
			return $stmt;
		}
	}
696707
public function getTable()
    {
        if (isset($this->table)) {
            $table = $this->table;
        } else {
            $table = str_replace('\\', '', snake_case(str_plural(class_basename($this))));
        }

        return $this->getConnection()->db->prefix.$table;
    }