CRM-13123 - Add serialization metadata to schema
[civicrm-core.git] / CRM / Core / CodeGen / Specification.php
index be5b19b48a58f3ebfaf7f8a7468846caebe76574..63119cf49eb5eadfce80ed25c50b95116013af18 100644 (file)
@@ -242,13 +242,13 @@ class CRM_Core_CodeGen_Specification {
 
         $this->getIndex($indexXML, $fields, $index);
       }
+      CRM_Core_BAO_SchemaHandler::addIndexSignature($name, $index);
       $table['index'] = &$index;
     }
 
     if ($this->value('foreignKey', $tableXML)) {
       $foreign = array();
       foreach ($tableXML->foreignKey as $foreignXML) {
-        // print_r($foreignXML);
 
         if ($this->value('drop', $foreignXML, 0) > 0 and $this->value('drop', $foreignXML, 0) <= $this->buildVersion) {
           continue;
@@ -282,7 +282,7 @@ class CRM_Core_CodeGen_Specification {
    */
   public function getField(&$fieldXML, &$fields) {
     $name = trim((string ) $fieldXML->name);
-    $field = array('name' => $name, 'localizable' => $fieldXML->localizable);
+    $field = array('name' => $name, 'localizable' => ((bool) $fieldXML->localizable) ? 1 : 0);
     $type = (string ) $fieldXML->type;
     switch ($type) {
       case 'varchar':
@@ -361,6 +361,7 @@ class CRM_Core_CodeGen_Specification {
     $field['headerPattern'] = $this->value('headerPattern', $fieldXML);
     $field['dataPattern'] = $this->value('dataPattern', $fieldXML);
     $field['uniqueName'] = $this->value('uniqueName', $fieldXML);
+    $field['serialize'] = $this->value('serialize', $fieldXML);
     $field['html'] = $this->value('html', $fieldXML);
     if (!empty($field['html'])) {
       $validOptions = array(
@@ -470,19 +471,49 @@ class CRM_Core_CodeGen_Specification {
   public function getPrimaryKey(&$primaryXML, &$fields, &$table) {
     $name = trim((string ) $primaryXML->name);
 
-    /** need to make sure there is a field of type name */
-    if (!array_key_exists($name, $fields)) {
-      echo "primary key $name in {$table['name']} does not have a field definition, ignoring\n";
-      return;
-    }
-
     // set the autoincrement property of the field
     $auto = $this->value('autoincrement', $primaryXML);
+    if (isset($fields[$name])) {
+      $fields[$name]['autoincrement'] = $auto;
+    }
     $fields[$name]['autoincrement'] = $auto;
     $primaryKey = array(
       'name' => $name,
       'autoincrement' => $auto,
     );
+
+    // populate fields
+    foreach ($primaryXML->fieldName as $v) {
+      $fieldName = (string) ($v);
+      $length = (string) ($v['length']);
+      if (strlen($length) > 0) {
+        $fieldName = "$fieldName($length)";
+      }
+      $primaryKey['field'][] = $fieldName;
+    }
+
+    // when field array is empty set it to the name of the primary key.
+    if (empty($primaryKey['field'])) {
+      $primaryKey['field'][] = $name;
+    }
+
+    // all fieldnames have to be defined and should exist in schema.
+    foreach ($primaryKey['field'] as $fieldName) {
+      if (!$fieldName) {
+        echo "Invalid field defination for index $name\n";
+        return;
+      }
+      $parenOffset = strpos($fieldName, '(');
+      if ($parenOffset > 0) {
+        $fieldName = substr($fieldName, 0, $parenOffset);
+      }
+      if (!array_key_exists($fieldName, $fields)) {
+        echo "Table does not contain $fieldName\n";
+        print_r($fields);
+        exit();
+      }
+    }
+
     $table['primaryKey'] = &$primaryKey;
   }
 
@@ -524,9 +555,6 @@ class CRM_Core_CodeGen_Specification {
       $index['unique'] = TRUE;
     }
 
-    //echo "\$index = \n";
-    //print_r($index);
-
     // field array cannot be empty
     if (empty($index['field'])) {
       echo "No fields defined for index $indexName\n";