Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for setting variables in PostgresSQL and MySQL DboSource. #47

Closed
wants to merge 1 commit into from

5 participants

@labianchin

I've made some modification in the DboPostgres and DboMysqlBase in order to allow to set variables in the begining of the connection.

The sintax is described in:
http://www.postgresql.org/docs/9.0/static/sql-set.html
http://dev.mysql.com/doc/refman/5.1/en/set-option.html

The idea is very simple: In the database configuration, it could be set the key settings with an array of variables to set in the beginning of the connection.

One concrete example of use is to set a different datestyle.

PS: That's my first pull request, I hope everything is ok, if no, please tell me.
PS2: I know that there is a similar pull request of remidewitte (#41). I've done this one because this request is smaller and atomic.

@markstory
Owner

What would a use case for these settings be? I see the possible use in abstract, but I've yet to need to set SQL variables in a real world application.

@labianchin

As I said, a concrete use would be:
SET DateStyle to 'SQL', 'European'

In this way, the date fields would be in the format dd/mm/yyyy and the datetime fields in the format dd/mm/yyyy hh:mm:ss. It could be possible to read and write data in this format without needing to configure in the callbacks beforeValidate and afterFind of all models.

@josegonzalez
Collaborator

You could also configure the callback in just the AppModel, making setting the data come in in the format you want for all models.

@labianchin

Yes, that's what I do nowadays.
But, in some cases, it would be better that the data be formated directly from the database.

@h3u

Another use case i've to do recently for mysql:
If your DB server run uses UTC timezone but your app should use another:
SET time_zone = 'Europe/Berlin'

@josegonzalez
Collaborator

Your app and database should all be in the same timezone, preferably UTC, and then on the consumer facing end, you change the time appropriately if necessary. That will allow you the most flexibility since it does not rely on hoping there isn't a quirk when setting up a new machine and forgetting to set the correct timezone.

@h3u

Yes, your're right and so it is - all servers use UTC. Changing the timezone in cakePHP is quite simple, but how do I do this for a database (connection) without having administrative access?

@josegonzalez
Collaborator

You don't do it for the database connection. If you ever need to show the user a localized datetime, then you have a helper in the application-layer do the transformation. You also do the reverse transformation in the application layer. That way you're never accidentally saving data that you think is in, say, EST when in fact it is in PST, to a database that is UTC.

@h3u

We do this already but have plenty of gigs of data that have not been stored in UTC. Than switched the hosting provider, the dabase now runs in UTC (before in other timezone). For a grown application there's no way to fix this without patching cakePHP. Furthermore all database datetime functions couldn't be used.
Where's the problem implementing this pull?

@labianchin

Well, I agree with h3u and understand the concerns of josegonzalez and markstory. But I don't see any concrete reason to do not accept this pull request.

@remidewitte

+1 for this pull request.

DateStyle in Postgres only controls the way dates can be input and the way dates are output. And it has little to do with timezones. But it can be very convenient to set the timezone as well.

From different environnements, you can make sure sensible database configuration can be enforced by the application.

If this pull request were to be rejected, we should submit a generic callbak/hook called afterConnect for example.

And about implementing something in the application-layer to do the date transformation from YMD to DMY, I have done it for one project doing conversions in Model::beforeSave() and in Model::afterFind(), taking care of associations, primary result or not, etc. Believe me, when I found I could replace it with a one line patch, my colleagues and I were quite happy.
Moreover, at least for Postgres, you can keep on inputing date in YMD format even with a DMY DateStyle, so all the created/modified behavior from CakePHP is not broken.

@josegonzalez
Collaborator

I'd personally prefer a generic afterConnect() method - perhaps before/after methods for both connect() and disconnect() ? - that one could override by either subclassing the datasource, or by calling a model method.

The former would be faster, but in that case you could already do just such a thing and place the datasource in the https://github.com/cakephp/datasources plugin. The later would be slightly slower, and I don't think it would be added to 1.3 as it's a new feature, so most likely should be added to 2.0.

@labianchin labianchin closed this
@westernmagic westernmagic referenced this pull request from a commit
Divya Manian reducing font-size only on the body element fixes #47 4d022ee
@majna majna referenced this pull request from a commit in majna/cakephp
@majna majna Improve belongsTo and hasOne for unneeded queries when recursive > 1
Reuse already joined data for 'belongsTo' and 'hasOne' associations instead of running unneeded queries for each record.

Fixes #47
36c4012
@majna majna referenced this pull request from a commit in majna/cakephp
@majna majna Improve belongsTo and hasOne for unneeded queries when recursive > 1.
Reuse already joined data for 'belongsTo' and 'hasOne' associations instead of running unneeded queries for each record.
Fixes #47
47287a0
@majna majna referenced this pull request from a commit in majna/cakephp
@majna majna Improve belongsTo and hasOne for unneeded queries when recursive > 1.
Reuse already joined data for 'belongsTo' and 'hasOne' associations instead of running unneeded queries for each record.
Fixes #47
ce2fc6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 cake/libs/model/datasources/dbo/dbo_mysql.php
@@ -562,7 +562,14 @@ function connect() {
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
-
+
+ // Support any SET ... = ... (http://dev.mysql.com/doc/refman/5.1/en/set-option.html)
+ if (!empty($config['settings'])) {
+ foreach ($config['settings'] as $name => $value) {
+ $this->_execute("SET " . $name . " = " . $value);
+ }
+ }
+
$this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
return $this->connected;
View
6 cake/libs/model/datasources/dbo/dbo_postgres.php
@@ -129,6 +129,12 @@ function connect() {
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
+ // Support any SET ... TO ... (http://www.postgresql.org/docs/9.0/static/sql-set.html)
+ if (!empty($config['settings'])) {
+ foreach ($config['settings'] as $name => $value) {
+ $this->_execute("SET " . $name . " TO " . $value);
+ }
+ }
return $this->connected;
}
Something went wrong with that request. Please try again.