throw new InvalidRoutineException(schemaName, routineName, "must have EXTERNAL NAME function_name");
}
}
TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator();
AISBuilder builder = new AISBuilder();
builder.routine(schemaName, routineName,
language, callingConvention);
for (int i = 0; i < aliasInfo.getParameterCount(); i++) {
String parameterName = aliasInfo.getParameterNames()[i];
Parameter.Direction direction;
switch (aliasInfo.getParameterModes()[i]) {
case ParameterMetaData.parameterModeIn:
default:
direction = Parameter.Direction.IN;
break;
case ParameterMetaData.parameterModeOut:
direction = Parameter.Direction.OUT;
break;
case ParameterMetaData.parameterModeInOut:
direction = Parameter.Direction.INOUT;
break;
}
TInstance type = typesTranslator.typeForSQLType(aliasInfo.getParameterTypes()[i],
schemaName, routineName, parameterName);
builder.parameter(schemaName, routineName, parameterName,
direction, type);
}
if (aliasInfo.getReturnType() != null) {
TInstance type = typesTranslator.typeForSQLType(aliasInfo.getReturnType(),
schemaName, routineName, "return value");
builder.parameter(schemaName, routineName, null,
Parameter.Direction.RETURN, type);
}
if (createAlias.getExternalName() != null) {
String className, methodName;
boolean checkJarName;
if (callingConvention == Routine.CallingConvention.JAVA) {
className = createAlias.getJavaClassName();
methodName = createAlias.getMethodName();
checkJarName = true;
}
else if (callingConvention == Routine.CallingConvention.LOADABLE_PLAN) {
// The whole class implements a standard interface.
className = createAlias.getExternalName();
methodName = null;
checkJarName = true;
}
else {
className = null;
methodName = createAlias.getExternalName();
checkJarName = false;
}
String jarSchema = null;
String jarName = null;
if (checkJarName) {
int idx = className.indexOf(':');
if (idx >= 0) {
jarName = className.substring(0, idx);
className = className.substring(idx + 1);
if (jarName.equals("thisjar")) {
TableName thisJar = (TableName)createAlias.getUserData();
jarSchema = thisJar.getSchemaName();
jarName = thisJar.getTableName();
}
else {
idx = jarName.indexOf('.');
if (idx < 0) {
jarSchema = defaultSchemaName;
}
else {
jarSchema = jarName.substring(0, idx);
jarName = jarName.substring(idx + 1);
}
}
}
}
if (jarName != null) {
AkibanInformationSchema ais = ddlFunctions.getAIS(session);
SQLJJar sqljJar = ais.getSQLJJar(jarSchema, jarName);
if (sqljJar == null)
throw new NoSuchSQLJJarException(jarSchema, jarName);
builder.sqljJar(jarSchema, jarName, sqljJar.getURL());
}
builder.routineExternalName(schemaName, routineName,
jarSchema, jarName,
className, methodName);
}
if (createAlias.getDefinition() != null) {
builder.routineDefinition(schemaName, routineName,
createAlias.getDefinition());
}
if (aliasInfo.getSQLAllowed() != null) {
Routine.SQLAllowed sqlAllowed;
switch (aliasInfo.getSQLAllowed()) {
case MODIFIES_SQL_DATA:
sqlAllowed = Routine.SQLAllowed.MODIFIES_SQL_DATA;
break;
case READS_SQL_DATA:
sqlAllowed = Routine.SQLAllowed.READS_SQL_DATA;
break;
case CONTAINS_SQL:
sqlAllowed = Routine.SQLAllowed.CONTAINS_SQL;
break;
case NO_SQL:
sqlAllowed = Routine.SQLAllowed.NO_SQL;
break;
default:
throw new InvalidRoutineException(schemaName, routineName, "unsupported " + aliasInfo.getSQLAllowed().getSQL());
}
builder.routineSQLAllowed(schemaName, routineName, sqlAllowed);
}
builder.routineDynamicResultSets(schemaName, routineName,
aliasInfo.getMaxDynamicResultSets());
builder.routineDeterministic(schemaName, routineName,
aliasInfo.isDeterministic());
builder.routineCalledOnNullInput(schemaName, routineName,
aliasInfo.calledOnNullInput());
Routine routine = builder.akibanInformationSchema().getRoutine(tableName);
boolean replaceExisting = createAlias.isCreateOrReplace();
ddlFunctions.createRoutine(session, routine, replaceExisting);
if (replaceExisting)
routineLoader.checkUnloadRoutine(session, tableName);
}