Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unbuffered mode, insert tablename remove `` #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/PDO.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function SetFailureFlag()
public function closeConnection()
{
$this->pdo = null;
}
}

private function Init($query, $parameters = null, $driverOptions = array())
{
Expand Down Expand Up @@ -211,6 +211,17 @@ public function inTransaction()
return $this->pdo->inTransaction();
}

/**
* mysql unbuffered mode, suitable for reading huge data source
* @see https://www.php.net/manual/en/mysqlinfo.concepts.buffering.php
*
* @return void
*/
public function unbuffered()
{
$this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}

Comment on lines +214 to +224
Copy link
Contributor Author

@jjasoncool jjasoncool Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. buffered mode is used to read/write huge data.
    See https://www.php.net/manual/en/mysqlinfo.concepts.buffering.php
    I think there is no need to add setAttribute() method to this class.
    So I add a function that can set connection to unbuffered mode.

/**
* execute a sql query, returns an result array in the select operation, and returns the number of rows affected in other operations
* @param string $query
Expand Down Expand Up @@ -264,7 +275,7 @@ public function insert($tableName, $params = null)
{
$keys = array_keys($params);
$rowCount = $this->query(
'INSERT INTO `' . $tableName . '` (`' . implode('`,`', $keys) . '`)
'INSERT INTO ' . $tableName . ' (`' . implode('`,`', $keys) . '`)
Copy link
Contributor Author

@jjasoncool jjasoncool Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Insert schema.table will show error exception.
    If the program across two db.
    When use $db->insert('db2.table', para);
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.db2.table' doesn't exist.

If remove `` these quotes, I don't need to add new DB(Host, DBPort, **another_db_name**, DBUser, DBPassword) to solve this problem.
But I don't know if this is a good practice.

VALUES (:' . implode(',:', $keys) . ')',
$params
);
Expand Down Expand Up @@ -349,7 +360,6 @@ public function lastInsertId()
return $this->pdo->lastInsertId();
}


/**
* @param $query
* @param null $params
Expand Down