CDbConnection inheritance

I want to point a question for Qiang and Yii team.

The actual CDbConnection actually is not inherited by any driver, we always use an instance of the CDbConnection.

However, I was thinking if it is not better USE the inheritance, so we would have an CDbConnection (defaults to mysql), CPgsqlConnection, CMssqlConnection, OOciConnection, etc.

I explain: This would make the customizing for a particular database more flexible. the actual CDbConnection is with coding like

    if($driver==='mssql' || $driver==='dblib')

I think it is not a good approach to make diferente codes in CdbConnection based on the driver type.

For example, I am improving Oracle driver for set the letter-case and the date format. I could do this directly into CdbConnection (and put one more exception into the code), but I preferred to extend the CdbConnection class like this:

    class COciConnection extends CDbConnection{

protected function initConnection($pdo)


{


	parent::initConnection($pdo);		

                // set the case

	$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);

                //set date format

	$stmt=$pdo->prepare("alter session set NLS_DATE_FORMAT='dd/MM/yyyy HH24:MI:SS'");


	$stmt->execute();		


}

    }

   

So, I have escapsulated the especific code for Oracle into this new class. I am sure that will be usefull for MsSQL implementation, too.

Also, extending CDbConnection and having CMysqlConnection, COciConnection and etc. can allows Yii to make connections in config.php easier. Instead of:

'db'=>array(

            'connectionString'=>'oci:dbname=MYDB',

            'username'=>'USER',

            'password'=>'PASS',

            'class'=>'COciConnection',

            ),

we could have

'db'=>array(

            'class'=>'COciConnection',

    'dbname'=MYDB',

            'username'=>'USER',

            'password'=>'PASS',           

            ),

Ok, the former way is not complicated, but I guess the last way is a bit more clear.

I know that ACTUALLY is possible to do what I proposed, I am wondering if this approach could be the default behavior in the future Yii db implementation (1.1)

Yes, inheriting CDbConnection as you did is good. We are not going to add CMysqlConnection etc. though because it wouldn't add any new things and would break BC. Users can always extend CDbConnection (just as you did) if new features are needed. For the oracle driver, we may consider adding COciConnection because it is new and we don't need to worry too much about BC.

Sorry, what is BC?

It is not my intention to polemize, I just think that CDbConnection has to be driver-independent. However, its initConnection method has a $stmt=$pdo->prepare('SET NAMES ?'), which does not means anything for oracle and to other databases, i guess. Is is especific for MySQL.

Anyway, it is just an idea.

BC = Backward compatibility.

SET NAMES is a standard SQL statement. It is supported by most DBMS. If Oracle doesn't support it, then we need special handling, and that's why you want to extend CDbConnection.