Revision: 1759
Author: olavmrk
Date: Thu Sep 17 03:52:58 2009
Log: sqlauth: Set character set for connections.
http://code.google.com/p/simplesamlphp/source/detail?r=1759

Modified:
  /trunk/modules/sqlauth/lib/Auth/Source/SQL.php

=======================================
--- /trunk/modules/sqlauth/lib/Auth/Source/SQL.php      Mon Sep 22 04:55:51 2008
+++ /trunk/modules/sqlauth/lib/Auth/Source/SQL.php      Thu Sep 17 03:52:58 2009
@@ -125,6 +125,41 @@
                $this->password = $config['password'];
                $this->query = $config['query'];
        }
+
+
+       /**
+        * Create a database connection.
+        *
+        * @return PDO  The database connection.
+        */
+       private function connect() {
+               try {
+                       $db = new PDO($this->dsn, $this->username, 
$this->password);
+               } catch (PDOException $e) {
+                       throw new Exception('sqlauth:' . $this->authId . ': - 
Failed to connect  
to \'' .
+                               $this->dsn . '\': '. $e->getMessage());
+               }
+
+               $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+
+               $driver = explode(':', $this->dsn, 2);
+               $driver = strtolower($driver[0]);
+
+               /* Driver specific initialization. */
+               switch ($driver) {
+               case 'mysql':
+                       /* Use UTF-8. */
+                       $db->exec("SET NAMES 'utf8'");
+                       break;
+               case 'pgsql':
+                       /* Use UTF-8. */
+                       $db->exec("SET NAMES 'UTF8'");
+                       break;
+               }
+
+               return $db;
+       }


        /**
@@ -144,14 +179,7 @@
                assert('is_string($username)');
                assert('is_string($password)');

-               try {
-                       $db = new PDO($this->dsn, $this->username, 
$this->password);
-               } catch (PDOException $e) {
-                       throw new Exception('sqlauth:' . $this->authId . ': - 
Failed to connect  
to \'' .
-                               $this->dsn . '\': '. $e->getMessage());
-               }
-
-               $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+               $db = $this->connect();

                try {
                        $sth = $db->prepare($this->query);

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"simpleSAMLphp commits" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/simplesamlphp-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to