Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,16 @@ private void mapEntities() throws BalException {
}
});
entityBuilder.setKeys(keys);
entityBuilderMap.put(entityBuilder.getEntityName(), entityBuilder);

// Mark entities without primary keys as containing unsupported types
// This prevents IndexOutOfBoundsException during code generation
if (keys.isEmpty()) {
entityBuilder.setContainsUnsupportedTypes(true);
errStream.println("WARNING: Table '" + table.getTableName() +
"' does not have a primary key and will be excluded from entity generation.");
} else {
entityBuilderMap.put(entityBuilder.getEntityName(), entityBuilder);
}
});
HashMap<String, Integer> ownerFieldNames = new HashMap<>();
HashMap<String, Integer> assocFieldNames = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@

import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.persist.BalException;
import io.ballerina.persist.models.Entity;
import io.ballerina.persist.models.Module;
import io.ballerina.persist.nodegenerator.syntax.utils.BalSyntaxUtils;

public class DbModelGenSyntaxTree implements IntrospectSyntaxTree {

@Override
public SyntaxTree getDataModels(Module entityModule) throws BalException {
if (!entityModule.getEntityMap().values().isEmpty()) {
if (!entityModule.getEntityMap().isEmpty()) {
// Check if all entities have unsupported types
boolean allUnsupported = entityModule.getEntityMap().values().stream()
.allMatch(Entity::containsUnsupportedTypes);

if (allUnsupported) {
throw new BalException("No valid entities found in the database. " +
"All tables either lack primary keys or contain unsupported types.");
}

return BalSyntaxUtils.generateModelSyntaxTree(entityModule);
}
throw new BalException("No entities found in the database");
throw new BalException("No entities found in the database. " +
"The database may be empty or contain no accessible tables.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ private static Client getClientObject(Module entityModule, ClientSyntax dbClient
resource.addFunction(dbClientSyntax.getDeleteFunction(entity), true);
resourceList.add(resource);
}

// Check if all entities were filtered out
if (resourceList.isEmpty()) {
throw new BalException("No valid entities found for client generation. " +
"All entities contain unsupported types or are missing primary keys.");
}

resourceList.forEach(resource -> {
resource.getFunctions().forEach(function -> {
clientObject.addMember(function, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ public List<SqlTable> getSQLTables(String query) throws SQLException {
while (results.next()) {
tables.add(SqlTable.newBuilder(results.getString("table_name")).build());
}
if (tables.isEmpty()) {
throw new SQLException("No tables found in the database.");
}
// Return empty list instead of throwing exception for empty databases
// The error is handled in the calling function
return tables;
}
} catch (SQLException e) {
Expand Down
Loading