Fixes for MS SQL server
authorChristian Weiske <christian.weiske@netresearch.de>
Thu, 13 Feb 2014 07:34:15 +0000 (08:34 +0100)
committerChristian Weiske <christian.weiske@netresearch.de>
Thu, 13 Feb 2014 07:34:15 +0000 (08:34 +0100)
README.rst [new file with mode: 0644]
src/bdrem/Source/Sql.php

diff --git a/README.rst b/README.rst
new file mode 100644 (file)
index 0000000..42af37f
--- /dev/null
@@ -0,0 +1,35 @@
+*********************************
+bdrem - Birthday reminder by mail
+*********************************
+Birthday reminder that sends out mails (Text and HTML).
+
+It can also generate tables on your console/shell output and
+normal HTML pages.
+
+
+
+=============
+Configuration
+=============
+Copy ``data/bdrem.config.php.dist`` to ``data/bdrem.config.php`` and
+adjust it to your liking.
+
+
+MS SQL server
+=============
+Configure the date format in ``/etc/freedts/locales.conf``::
+
+    [default]
+        date format = %Y-%m-%d
+
+Also set the charset to UTF-8 in ``/etc/freedts/freedts.conf``::
+
+    [global]
+        # TDS protocol version                                                       tds version = 8.0
+        client charset = UTF-8
+
+Restart Apache afterwards.
+
+Use ``dblib`` in the DSN::
+
+    dblib:host=192.168.1.1;dbname=Databasename
index eb12ef256682ab24d28eccf5905e2eb88f8d0556..57f9a800b1e0281f4eaa6d91b79ea8e599254d55 100644 (file)
@@ -26,13 +26,22 @@ class Source_Sql
      */
     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
     {
-        $dbh = new \PDO($this->dsn, $this->user, $this->password);
+        $dbh = new \PDO(
+            $this->dsn, $this->user, $this->password,
+            array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
+        );
         $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
         $arEvents = array();
 
         foreach ($this->fields['date'] as $field => $typeName) {
-            $sqlMonth = 'EXTRACT(MONTH FROM ' . $field . ')';
-            $sqlDay = 'EXTRACT(DAY FROM ' . $field . ')';
+            if (substr($this->dsn, 0, 5) == 'dblib') {
+                //MS SQL Server does of course cook its own sht
+                $sqlMonth = 'DATEPART(month, ' . $field . ')';
+                $sqlDay = 'DATEPART(day, ' . $field . ')';
+            } else {
+                $sqlMonth = 'EXTRACT(MONTH FROM ' . $field . ')';
+                $sqlDay = 'EXTRACT(DAY FROM ' . $field . ')';
+            }
 
             $parts = array();
             foreach ($arDays as $month => $days) {
@@ -45,24 +54,26 @@ class Source_Sql
                     . ')';
             }
             $sql = 'SELECT ' . $field . ' AS e_date'
-                . ', ' . $this->fields['name'] . ' AS e_name'
+                . ', ' . implode(', ', (array) $this->fields['name'])
                 . ' FROM ' . $this->table
                 . ' WHERE '
                 . implode(' OR ', $parts);
 
             $res = $dbh->query($sql);
-            if ($res === false) {
-                $errorInfo = $dbh->errorInfo();
-                throw new \Exception(
-                    'SQL error #' . $errorInfo[0]
-                    . ': ' . $errorInfo[1]
-                    . ': ' . $errorInfo[2],
-                    (int) $errorInfo[1]
-                );
-            }
             while ($row = $res->fetchObject()) {
+                $arNameData = array();
+                foreach ((array) $this->fields['name'] as $fieldName) {
+                    $arNameData[] = $row->$fieldName;
+                }
+                $name = call_user_func_array(
+                    'sprintf',
+                    array_merge(
+                        array($this->fields['nameFormat']),
+                        $arNameData
+                    )
+                );
                 $event = new Event(
-                    $row->e_name, $typeName, 
+                    $name, $typeName,
                     str_replace('0000', '????', $row->e_date)
                 );
                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
@@ -72,7 +83,7 @@ class Source_Sql
         }
         return $arEvents;
     }
-    
+
     /**
      * @return array Key is the month, value an array of days
      */