commit ab66741834bfd63bb73ea99895cc042b692f2d89
Author: Daniel Black <daniel.black@openquery.com.au>
Date:   Fri Apr 4 00:13:30 2014 +0000

    Issue #710940 by danblack: Add binary datatypes to database layer

diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
index e6940dc..b8f0699 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
@@ -137,11 +137,11 @@ protected function createTableSql($name, $table) {
   protected function createFieldSql($name, $spec) {
     $sql = "`" . $name . "` " . $spec['mysql_type'];
 
-    if (in_array($spec['mysql_type'], array('VARCHAR', 'CHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'TEXT'))) {
+    if (in_array($spec['mysql_type'], array('VARCHAR', 'CHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'TEXT', 'VARBINARY', 'BINARY'))) {
       if (isset($spec['length'])) {
         $sql .= '(' . $spec['length'] . ')';
       }
-      if (!empty($spec['binary'])) {
+      if (!empty($spec['binary']) && !in_array($spec['mysql_type'], array('VARBINARY', 'BINARY'))) {
         $sql .= ' BINARY';
       }
     }
@@ -227,6 +227,9 @@ public function getFieldTypeMap() {
       'varchar:normal'  => 'VARCHAR',
       'char:normal'     => 'CHAR',
 
+      'varbinary:normal'  => 'VARBINARY',
+      'binary:normal'     => 'BINARY',
+
       'text:tiny'       => 'TINYTEXT',
       'text:small'      => 'TINYTEXT',
       'text:medium'     => 'MEDIUMTEXT',
@@ -389,7 +392,7 @@ public function fieldSetDefault($table, $field, $default) {
       $default = 'NULL';
     }
     else {
-      $default = is_string($default) ? "'$default'" : $default;
+      $default = is_string($default) ? $this->connection->quote($default) : $default;
     }
 
     $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
index 72686ea..269cc56 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
@@ -273,6 +273,9 @@ function getFieldTypeMap() {
       'varchar:normal' => 'varchar',
       'char:normal' => 'character',
 
+      'varbinary:normal' => 'bytea',
+      'binary:normal' => 'bytea',
+
       'text:tiny' => 'text',
       'text:small' => 'text',
       'text:medium' => 'text',
diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
index 45149da..9c9d5ac 100644
--- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php
@@ -204,6 +204,9 @@ public function getFieldTypeMap() {
       'varchar:normal'  => 'VARCHAR',
       'char:normal'     => 'CHAR',
 
+      'varbinary:normal'  => 'BLOB',
+      'binary:normal'     => 'BLOB',
+
       'text:tiny'       => 'TEXT',
       'text:small'      => 'TEXT',
       'text:medium'     => 'TEXT',
diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php
index 2442875..0190215 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php
@@ -339,6 +339,41 @@ function testSchemaAddField() {
         }
       }
     }
+
+    // Test binary types
+    foreach (array('binary', 'varbinary') as $bintype) {
+      $base_field_spec = array(
+         'type' => $bintype,
+      );
+      $variations = array();
+      foreach (array(NULL, 2, 50) as $length) {
+        if ($bintype == 'varbinary' && empty($length)) {
+          $length = 1;
+        }
+        foreach (array(TRUE, FALSE) as $binary) {
+          $variation = array(
+             'length' => $length,
+             'binary' => $binary,
+          );
+          $field_spec = $variation + $base_field_spec;
+          $this->assertFieldAdditionRemoval($field_spec);
+        }
+      }
+    }
+
+    $field_spec['default'] = hex2bin('002701FF7F808100020304');
+    $table_name = 'test_table_' . ($this->counter++);
+    $table_spec = array(
+      'fields' => array(
+        'id' => $field_spec,
+      ),
+    );
+    db_create_table($table_name, $table_spec);
+    $this->pass("Table $table_name created.");
+    $this->assertTrue($this->tryInsert($table_name), 'Insert with binary default succeeded.');
+
+    // Clean-up.
+    db_drop_table($table_name);
   }
 
   /**
