table.name = name.toLowerCase();
try
{
for ( JsonValue jsoninfo : jsontable.get( "fields" ).asArray() )
{
JsonObject info = (JsonObject) jsoninfo;
String type = info.get( "type" ).asString().toLowerCase();
if ( type.equals( "autoincrement" ) )
{
table.setPrimaryKey( type, info.get( "name" ).asString() );
}
ArrayList<Constraint> constraints = new ArrayList<Constraint>();
if ( info.names().contains( "constraints" ) )
{
for ( JsonValue jsonconstaint : info.get( "constraints" ).asArray() )
{
JsonObject constraint = (JsonObject) jsonconstaint;
String definition = constraint.get( "definition" ).asString();
if ( definition.toLowerCase().contains( "primary key" ) )
{
// skip if primary key already set
if ( table.hasPrimaryKey() )
{
continue;
}
final Field field = table.getFieldByName( constraint.get( "name" ).asString() );
if ( field != null )
{
table.setPrimaryKey( field );
}
}
constraints.add( new Constraint( constraint.names().contains( "name" ) ? constraint.get( "name" ).asString() : null, definition ) );
}
}
table.addField( type, info.get( "name" ).asString(), constraints );
}
} catch ( NullPointerException e )
{
throw new ParserException( "Couldn't parse the fields in this table." );
}
if ( jsontable.names().contains( "constraints" ) )
{
try
{
for ( JsonValue jsoninfo : jsontable.get( "constraints" ).asArray() )
{
JsonObject info = (JsonObject) jsoninfo;
String definition = info.get( "definition" ).asString();
if ( definition.toLowerCase().contains( "primary key" ) )
{
// skip if primary key already set
if ( table.hasPrimaryKey() )
{
continue;
}
final Field field = table.getFieldByName( info.get( "name" ).asString() );
if ( field != null )
{
table.setPrimaryKey( field );
}
}
table.addConstraint( info.names().contains( "name" ) ? info.get( "name" ).asString() : null, definition );
}
} catch ( NullPointerException e )
{
throw new ParserException( "Couldn't parse the constraints in this table." );
}