JDatabaseFactory Usage

Showing 1-3 of 3 messages
JDatabaseFactory Usage Amy Stephen 4/11/12 3:13 PM
Am I using the JDatabaseFactory, as intended?

1. Get an instance of JDatabaseFactory

$dbFactory = JDatabaseFactory::getInstance();

2. Create a database connection (like JFactory::getDatabase(); does now)

$options = array(
    'driver' => 'mysqli',
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'definitely not root because that would be crazy',
    'database' => 'db_name',
    'prefix' => 'xyz_');

$this->db = $dbFactory->getDriver('mysqli', $options);

Note: $this->db would be stored/retrieved as a static object for later reuse in queries, etc.

3. Create and use a query object

$this->query = $dbFactory->getQuery('Mysqli', $this->db);

$this->query->from($this->db->qn($this->table_name));

4. Interact with the database object (examples)

 $nullDate = $this->db->getNullDate();

 $columns = $this->db->getTableColumns($this->table_name, false);

$this->db->setQuery($this->query->__toString());

$results = $this->db->loadResult();

Note: these are simply examples -- not intended to be used together.

Re: JDatabaseFactory Usage Amy Stephen 4/11/12 4:18 PM
I am very confused about overlap I see between JDatabaseDriver and JDatabaseFactory.

These methods are 100% the same code and exist in both classes: getExporter, getImporter, getQuery.

This method is only in JDatabaseDriver, but I would expect it in JDatabaseFactory: getIterator.

Is JDatabaseFactory the "entry point" to driver, exporter, importer, query, and iterator? If so, can we remove the redundant methods from Driver (and add Iterator to Factory?)

Also - JDatabaseFactory is confusing. It would make more sense (IMO) - if an instance of JDatabaseFactory were statically made that included the JDatabaseDriver connection..

So, instead of:

$dbFactory = JDatabaseFactory::getInstance();  //which does nothing really

AND

$options = array(
    'driver' => 'mysqli',
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'definitely not root because that would be crazy',
    'database' => 'db_name',
    'prefix' => 'xyz_');

$this->db = $dbFactory->getDriver('mysqli'
, $options);

If the following were done:


$options = array(
    'driver' => 'mysqli',
    'host' => 'localhost',
    'user' => 'root',
    'password' => 'definitely not root because that would be crazy',
    'database' => 'db_name',
    'prefix' => 'xyz_');

$this->db = JDatabaseFactory::getInstance('mysqli', $options);

Then, $this->db stored as a static instance can then be used for queries, export, import, and iteration without having to create a new instance each time.

Does that make any sense? It seems like a cleaner, easier to understand way to create and reuse a connection to the database, keeping a single point of entry, and being able to reuse the connection (or create a new one, if desired.)



Re: JDatabaseFactory Usage Amy Stephen 4/11/12 4:49 PM
PR that will help explain https://github.com/joomla/joomla-platform/pull/1148