public function run($table) { if (!$table) { throw new Exception('Tool class table name has not been set.'); } $db = DbBase::createMaster(); $fieldList = $db->getConnection()->query('describe ' . $table)->fetchAll(\Pdo::FETCH_ASSOC); return $fieldList; }
/** * 转义SQL 语句中使用的字符串中的特殊字符 * * 数组转换:仅支持单维数组 * array('fafdas', 'fd', 'eeee', 'bbbbb') * ↓↓↓↓ * 'fafdas', 'fd', 'eeee', 'bbbbb' * * @param string|int|array $value * @return string */ public function escape($value) { $this->setAdapter(); if (is_array($value)) { $inArray = array(); foreach ($value as $item) { $inArray[] = $this->adapter->getConnection()->quote($item); } return implode(',', $inArray); } else { return $this->adapter->getConnection()->quote($value); } }
/** * 貌似Mysql执行过程中抛出PDOException,回退才有效 * * 在系统代码内部手工抛出异常并不能影响结果。 * @TODO 还需要进一步确认。不然就不能人工撤销了。 */ private function testMysql() { $dbAdapter = \HuiLib\Db\DbBase::createMaster(); try { $dbAdapter->beginTransaction(); $dbAdapter->getConnection()->query("insert into test.test set field1=22222, field2=3333"); $dbAdapter->getConnection()->query("insert into test.test set field1=0000, field0=00000"); //Field0不存在 $dbAdapter->commit(); } catch (\Exception $e) { print_r($e); $dbAdapter->rollback(); } }
protected function __construct($config) { $driverConfig = empty($config['driver']) ? '' : Front::getInstance()->getAppConfig()->getByKey($config['driver']); if (empty($config['driver']) || empty($driverConfig)) { throw new \HuiLib\Error\Exception('Log mysql driver ini error'); } $this->driver = DbBase::create($driverConfig); if (!$this->driver instanceof \HuiLib\Db\Adapter\Pdo\PdoBase) { throw new \HuiLib\Error\Exception('Log mysql driver initialized failed'); } if (empty($config['table'])) { throw new \HuiLib\Error\Exception('Log mysql driver table ini error'); } $this->table = $config['table']; parent::__construct($config); }
private function testSlave() { $salve = \HuiLib\Db\DbBase::createSlave(); $db = \HuiLib\Db\DbBase::createSlave()->getConnection(); $time = time(); var_dump($time % 2); try { $salve->beginTransaction(); $db->query("update test.test set field1='{$time}' where id=2 "); //! 优先级高于 % if (!($time % 2)) { throw new \Exception('error'); } $salve->commit(); } catch (\Exception $e) { $salve->rollBack(); echo 'error exception, time:' . $time . ' e:' . $e->getMessage(); } }
<?php //HuiLib Db库操作指南 //HuiLib Db库由连接器Adapter和查询器Query组成。Query封装了Delete, Insert, Select, Update, Where等相关概念。 //创建原始数据连接通过DbBase类。可以通过封装过的Query类实现快速查询。 //支持事务封装,提倡使用INNODB等支持事务的储存引擎。 //一、创建原始数据库连接 //1.1 以下创建数据库主库连接,返回Adapter对象: $db = \HuiLib\Db\DbBase::createMaster(); //1.2 获取数据库连接Pdo对象或实际连接对象: $connect = $db->getConnection(); //1.3 直接发起请求: $connect->query("select count(*) from test.test"); //二、Query组件查询 //2.1 Select部分 //2.1.1 创建select对象 $select = \HuiLib\Db\Query::select(); //2.1.2 设置查询表格等操作 $select->table('test'); //2.1.3 Where条件设置支持三种形式: //键值式(createPair) $select->where(Where::createPair('id', 2)); //=> id='2' //文本式(createPlain) $select->where(Where::createPlain("name is null")); //=>"name is null" //占位式(createQuote) $select->where(Where::createQuote('id=?', 2)); //=>"id='2'" //同时支持绑定式 $select->where(Where::createPlain('t.id=:id'));