* @license PHP License * @package WB * @subpackage db */ WBClass::load('WBDatasource_SQLCommon'); /** * Send queries to database * * @version 0.1.0 * @package WB * @subpackage db */ class WBDatasource_SimpleQuery extends WBDatasource_SQLCommon { /** * database connection * @var WBDatasource_SQL */ protected $db; /** * constructor * * The parameter will be loaded from config.xml, section "db", you may * overwrite the list of tables. This will cause to load the table list * from a different file. * * Parameter: * - "tablelist": config file which contains the list of known tables. * * This class utilizes @link WBDatasource_SQL this implies that you may * use all constructor parameter of this class, as well. * * @param array $parameter */ public function __construct( $parameter = array() ) { parent::__construct( $parameter ); $this->db = WBClass::create('WBDatasource_SQL', $parameter); $this->loadTables( $parameter ); } /** * query database * * Send query to db and return result set as list. * Return empty array in case DB result is something fishi * * @param array|string $query * @param array $data * @return array */ public function query($query, $data = array()) { if (is_array($query)) { $query = implode(' ', $query); } if (!empty($data)) { $query = $this->populate($query, $data); } $res = $this->db->query($query); $list = array(); if (!is_object($res)) { return $list; } while ($l = $res->fetch_assoc()) { $list[] = $l; } return $list; } /** * Quote anything for SQL * * This is just an interface to the db-quote function * * @param mixed $value * @param bool $like whether the quote should be done for LIKE * @return string */ public function quote($value, $like = false) { return $this->db->quote($value, $like); } /** * Populate query with data * * Populate placeholders like in template engine. Also values will be quoted. * * * @see WBString::populate() * @param string $query * @param array $data * @return string */ public function populate($query, $data = array()) { if (!is_array($data) || empty($data)) { return $query; } foreach ($data as &$v) { $v = $this->quote($v); } return WBString::populate($query, $data); } }