Skip to content

Commit

Permalink
Support composite uniqueness verification
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Mar 21, 2017
1 parent 533406f commit f2a3a59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Expand Up @@ -19,9 +19,12 @@
*/
package org.neo4j.kernel.api.constraints;

import java.util.Arrays;

import org.neo4j.kernel.api.TokenNameLookup;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaUtil;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptorFactory;

Expand Down Expand Up @@ -60,15 +63,19 @@ public String userDescription( TokenNameLookup tokenNameLookup )
{
String labelName = labelName( tokenNameLookup );
String boundIdentifier = labelName.toLowerCase();
return String
.format( "CONSTRAINT ON ( %s:%s ) ASSERT %s.%s IS UNIQUE", boundIdentifier, labelName, boundIdentifier,
tokenNameLookup.propertyKeyGetName( descriptor.getPropertyId() ) );
String properties =
SchemaUtil.niceProperties( tokenNameLookup, descriptor.getPropertyIds(), boundIdentifier + "." );
if ( descriptor.getPropertyIds().length > 1 )
{
properties = "(" + properties + ")";
}
return String.format( "CONSTRAINT ON ( %s:%s ) ASSERT %s IS UNIQUE", boundIdentifier, labelName, properties );
}

@Override
public String toString()
{
return String.format( "CONSTRAINT ON ( n:label[%d] ) ASSERT n.property[%d] IS UNIQUE",
descriptor.getLabelId(), descriptor.getPropertyId() );
descriptor.getLabelId(), Arrays.toString( descriptor.getPropertyIds() ) );
}
}
Expand Up @@ -30,11 +30,16 @@ private SchemaUtil()
}

public static String niceProperties( TokenNameLookup tokenNameLookup, int[] propertyIds )
{
return niceProperties( tokenNameLookup, propertyIds, "" );
}

public static String niceProperties( TokenNameLookup tokenNameLookup, int[] propertyIds, String prefix )
{
String[] properties = new String[propertyIds.length];
for ( int i = 0; i < propertyIds.length; i++ )
{
properties[i] = tokenNameLookup.propertyKeyGetName( propertyIds[i] );
properties[i] = prefix + tokenNameLookup.propertyKeyGetName( propertyIds[i] );
}
return String.join( ", ", properties );
}
Expand Down

0 comments on commit f2a3a59

Please sign in to comment.