Example #1
0
 static function baseDB($dsn, $options = array())
 {
     $opts = $options + array('username' => NULL, 'password' => NULL, 'db params' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
     if ($dsn instanceof PDO) {
         self::$con = $dsn;
         return;
     }
     self::$con = new PDO($dsn, $opts['username'], $opts['password'], $opts['db params']);
 }
Example #2
0
 public function setUp()
 {
     $this->db = QPDB::getBaseDB();
     $this->db->exec('CREATE TABLE IF NOT EXISTS qpdb_test (colOne, colTwo, colThree)');
     $stmt = $this->db->prepare('INSERT INTO qpdb_test (colOne, colTwo, colThree) VALUES (:one, :two, :three)');
     for ($i = 0; $i < 5; ++$i) {
         $vals = array(':one' => 'Title ' . $i, ':two' => 'Body ' . $i, ':three' => 'Footer ' . $i);
         $stmt->execute($vals);
         $stmt->closeCursor();
     }
 }
<?php

/**
 * Use QueryPath's database extension to import XML data into a database.
 *
 * 
 * @author M Butcher <*****@*****.**>
 * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
 */
require_once '../src/QueryPath/QueryPath.php';
require_once '../src/QueryPath/Extension/QPDB.php';
// Set the default database.
QPDB::baseDB('sqlite:../test/db/qpTest.db');
// To begin, let's create a new database. We can do this outside
// of QueryPath:
$db = QPDB::getBaseDB();
$db->exec('CREATE TABLE IF NOT EXISTS qpdb_article (title, author, body)');
// Here's our sample article:
$article = '<?xml version="1.0"?>
<article>
  <title>Use QueryPath for Fun and Profit</title>
  <author>
    <first>Matt</first>
    <last>Butcher</last>
  </author>
  <body>
  <![CDATA[
  <p>QueryPath is a great tool.</p>
  <p>Use it in many ways.</p>
  ]]>
  </body>
Example #4
0
 /**
  * Create a new database instance for all QueryPath objects to share.
  *
  * This method need be called only once. From there, other QPDB instances
  * will (by default) share the same database instance.
  *
  * Normally, a DSN should be passed in. Username, password, and db params
  * are all passed in using the options array.
  *
  * On rare occasions, it may be more fitting to pass in an existing database
  * connection (which must be a {@link PDO} object). In such cases, the $dsn
  * parameter can take a PDO object instead of a DSN string. The standard options
  * will be ignored, though.
  *
  * <b>Warning:</b> If you pass in a PDO object that is configured to NOT throw
  * exceptions, you will need to handle error checking differently.
  *
  * <b>Remember to always use {@link QPDB::doneWithQuery()} when you are done
  * with a query. It gives PDO a chance to clean up open connections that may
  * prevent other instances from accessing or modifying data.</b>
  *
  * @param string $dsn
  *  The DSN of the database to connect to. You can also pass in a PDO object, which
  *  will set the QPDB object's database to the one passed in.
  * @param array $options
  *  An array of configuration options. The following options are currently supported:
  *  - username => (string)
  *  - password => (string)
  *  - db params => (array) These will be passed into the new PDO object.
  *    See the PDO documentation for a list of options. By default, the
  *    only flag set is {@link PDO::ATTR_ERRMODE}, which is set to 
  *    {@link PDO::ERRMODE_EXCEPTION}.
  * @throws PDOException
  *  An exception may be thrown if the connection cannot be made.
  */
 static function baseDB($dsn, $options = array())
 {
     $opts = $options + array('username' => NULL, 'password' => NULL, 'db params' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
     // Allow this to handle the case where an outside
     // connection does the initialization.
     if ($dsn instanceof PDO) {
         self::$con = $dsn;
         return;
     }
     self::$con = new PDO($dsn, $opts['username'], $opts['password'], $opts['db params']);
 }