Browse Source

[indexer] Choose unique key field more intelligently

Choose by type and name:
find field named 'id' first, then look for long types, then int types.
Abraham Elmahrek 11 years ago
parent
commit
f6f9de4
2 changed files with 34 additions and 11 deletions
  1. 1 11
      desktop/libs/indexer/static/js/collections.js
  2. 33 0
      desktop/libs/indexer/static/js/lib.js

+ 1 - 11
desktop/libs/indexer/static/js/collections.js

@@ -223,17 +223,7 @@ var CreateCollectionViewModel = function() {
       }).done(function(data) {
         if (data.status == 0) {
           self.collection.fields(inferFields(data.data, self.collection));
-
-          // Find unique key default field
-          var message = null;
-          var uniqueKeyFields = ko.utils.arrayFilter(self.collection.fields(), function(field) {
-            return field.indexed();
-          });
-          if (uniqueKeyFields.length > 0) {
-            self.collection.uniqueKeyField(uniqueKeyFields[0].name());
-          } else if (self.collection.fields().length > 0) {
-            self.collection.uniqueKeyField(self.collection.fields()[0].name());
-          }
+          chooseUniqueKey(self.collection);
         } else {
           $(document).trigger("error", data.message);
         }

+ 33 - 0
desktop/libs/indexer/static/js/lib.js

@@ -296,6 +296,39 @@ ko.bindingHandlers.editableText = {
   }
 };
 
+function chooseUniqueKey(collection) {
+  function fieldChooser(fields) {
+    if (fields.length > 0) {
+      fields[0].uniqueKeyField(true);
+      return true;
+    }
+    return false;
+  }
+
+  // Find a field named "ID"
+  if (fieldChooser(ko.utils.arrayFilter(collection.fields(), function(field) {
+    return field.name().toLowerCase() == 'id';
+  }))) return;
+
+  // Find a long
+  if (fieldChooser(ko.utils.arrayFilter(collection.fields(), function(field) {
+    return $.inArray(field.type().toLowerCase(), ['long', 'tlong', 'plong']) != -1;
+  }))) return;
+
+  // Find an integer
+  if (fieldChooser(ko.utils.arrayFilter(collection.fields(), function(field) {
+    return $.inArray(field.type().toLowerCase(), ['int', 'tint', 'pint']) != -1;
+  }))) return;
+
+  // Find first indexed field
+  if (fieldChooser(ko.utils.arrayFilter(collection.fields(), function(field) {
+    return field.indexed();
+  }))) return;
+
+  // Choose a field
+  fieldChooser(collection.fields());
+}
+
 function getCharacterLabel(character) {
   var LABELS = {
     '\t': '\\t'