Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Talk:DB layer 2.0: Difference between revisions

From MoodleDocs
Line 72: Line 72:
My +1 to return array() if nothing found and false if error, I do not think that if ($resultarray =get_records()) {} is wrong. [[User:Skodak|Skodak]]
My +1 to return array() if nothing found and false if error, I do not think that if ($resultarray =get_records()) {} is wrong. [[User:Skodak|Skodak]]


== default $mdb object? ==
== default $DB object ==


In the above examples there is:
Martin proposed to use global $DB instead of function returning $mdb instance or factory methods. Nicolas confirmed that it should be suitable for unit testing purposes.


  $mdb = get_moodle_db(); // or static factory method
sample code:
 
<code>
To make things simpler for the 99% of developers who won't need anything more, can I suggest we do this once by default somewhere in setup.php?  We already have a $db object by default, perhaps we can even re-use it to make things read nicely and reduce confusion, so developers can just do:
  function xyz($userid, $courseid) {
 
    global $DB, $CFG;
$blah = $db->get_records(...)
    if ($records = get_records_sql('SELECT * FROM {$CFG->prefix}abc WHERE userid=? AND courseid=?', array($userid, $courseid))) {
        //...
    }
}
</code>


Things like $db->debug = true; can be changed to  
Things like $db->debug = true; can be changed to  
 
  $DB->set_debug(true);  // sets the adodb debug var
  $db->debug(true);  // sets the adodb debug var
 
 
Hmm, I am not sure having global $mdb is good for unit testing [[User:Skodak|Skodak]]

Revision as of 22:11, 5 May 2008

Requirements

  • OOP
  • easy unit testing
  • full abstraction (AdoDB, PDO)
  • easy to use
  • code must not know on which db it is running (no condifional db family hacks anymore!)
  • no magic quotes and slashing anymore

Various Notes (before 20080501)

  • AdoDB, AdoDB over PDO, PDO. (my initial +1 to continue using underlying ADOdb).
  • Placeholder types: :named or ? (will require PHP parsing under some DBs).
  • Multiple connections supported (auto-contained DB object).
  • PERF debugging available.
  • Logging available (to detect wrong uses).


  • Total breakage (all contrib uses)
  • Total breakage (current dmllib 1.0 functions definition).
  • Development mode (branch with radical replacement, some compatibility layer to minimize breakage - current dmllib instantiating new dmllib + stripping slashes ?)


  • Mahara experience. ( it's also worthwhile to note that a long long time ago I also did this for elgg which involved doing the addslashes/stripslashes audit through the code base - we didn't have to do this with mahara as we had placeholders and no slashes right from the start - Penny )
  • Interface example.
  • dmllib 2.0 tests since the beginning.
  • dmllib 2.0 documentation since the beginning.
  • Classes implementation order (PG, MySQL, MSSQL, Oracle).
  • More ideas.


  • Next meeting. TODO: document MDM20080501, AdoDb/PDO decision, Interface + PHP documentation + Docs documentation, define tests to perform. ADDRESS IT for... 20080508 ?

Eloy Lafuente (stronk7) 19:00, 29 April 2008 (CDT)

PDO/ADODB

I think it's interesting to discuss the potential of moving to PDO instead of ADODB but I'm not convinced it needs to be in the same conversation - we should be able to switch out ADODB and use PDO instead with completely no change outside dml - surely nowhere in the code is using $db directly - should always be using execute_sql

Penny Leach

Yup 100% agree, underlying stuff will be completely hidden, so it's a non-priority decision. We'll be able "replace" it more or less easily or, alternatively, create new "Moodle drivers" using different internal stuff. Just prospecting your opinion about PDO. I've done some (basic) research and it seems not to be 100% ready for production under some DBs. But wanted to know if you have some experience with it (or at least you initial feeling).
Eloy Lafuente (stronk7)

Interface proposal

sample implementaion with nearly full API definition in MDL-14679


Parameter types - :name and ?

The :name style seems to be much more flexible than ?.

Pros (name:)

  • order not important
  • possible validation

Pros (?)

  • supported by all backends except Ora
  • no probelm with overwriting

AdoDB

  • expects ? except for ora (not sure)

PDO

  • supports both

API changes

Donal suggested that get_records and families should return an empty array rather than false so you can iterate straight over them.

My +1 to return array() if nothing found and false if error, I do not think that if ($resultarray =get_records()) {} is wrong. Petr Škoda (škoďák)

default $DB object

Martin proposed to use global $DB instead of function returning $mdb instance or factory methods. Nicolas confirmed that it should be suitable for unit testing purposes.

sample code:

function xyz($userid, $courseid) {
    global $DB, $CFG;
    if ($records = get_records_sql('SELECT * FROM {$CFG->prefix}abc WHERE userid=? AND courseid=?', array($userid, $courseid))) {
        //...
    }
}

Things like $db->debug = true; can be changed to

$DB->set_debug(true);  // sets the adodb debug var