public function testExec() { $that = $this; $this->connectAndAuth(); $this->object->exec('uname -a', function ($stdio) use($that) { $that->assertInternalType('string', $stdio); }); }
/** * @param null $sql * * @return mixed * @throws SqlException */ public function execute($sql = null) { if (null == $sql) { $sql = $this->prepare(); } $result = $this->_connection->exec($sql); if (false === $result) { throw new SqlException($this->_connection->getErrorMessage()); } return $result; }
/** * Driver options: * - charset => character encoding to set (default is utf8) * - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html */ public function __construct(Connection $connection, array $options) { $this->connection = $connection; $charset = isset($options['charset']) ? $options['charset'] : 'utf8'; if ($charset) { $connection->exec("SET NAMES '{$charset}'"); } if (isset($options['sqlmode'])) { $connection->exec("SET sql_mode='{$options['sqlmode']}'"); } $connection->exec("SET time_zone='" . date('P') . "'"); }
/** * 链接数据库 * @param $config * @return Connection */ public static function connect($config) { $dsn = $config['driver'] . ':host=' . $config['host'] . ';port=' . $config['port']; $user = $config['user']; $pass = $config['pass']; $connection_options = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION); $pdo = new Connection($dsn, $user, $pass, $connection_options); if (!empty($config['collation'])) { $pdo->exec('SET NAMES ' . $config['charset'] . ' COLLATE ' . $config['collation']); } else { $pdo->exec('SET NAMES ' . $config['charset']); } return $pdo; }
/** * Import SQL dump from file - extreme fast. * @return int count of commands */ public static function loadFromFile(Connection $connection, $file) { @set_time_limit(0); // intentionally @ $handle = @fopen($file, 'r'); // intentionally @ if (!$handle) { throw new FileNotFoundException("Cannot open file '{$file}'."); } $count = 0; $sql = ''; while (!feof($handle)) { $s = fgets($handle); $sql .= $s; if (substr(rtrim($s), -1) === ';') { $connection->exec($sql); // native query without logging $sql = ''; $count++; } } if (trim($sql) !== '') { $connection->exec($sql); $count++; } fclose($handle); return $count; }