[#1011112] Use Annontations instead of DDL Manifest

View Trackers | Feature Requests | Download .csv | Monitor

Date:
2011-11-08 08:34
Priority:
3
State:
Open
Submitted by:
Christian Meier (cikic)
Assigned to:
Johann Oskarsson (myrkraverk)
Category:
PL/Java
Group:
Summary:
Use Annontations instead of DDL Manifest

Detailed description
With annontations you could build the correct sql from class/method data via reflection. Today you have to keep your DDL manifest and methods in sync.

Here is an example I use:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQL {
public String schema() default "";
public String name() default "";
public String[] paramInOutAndNames() default {};
public String language() default "java";
public String returnType() default "";
}

And a Method to generate the SQL Code:
public static void genSqlCode() throws SQLException{
for (Class c : classes) {
for (Method method : c.getMethods()){
if (method.isAnnotationPresent(SQL.class)) {
SQL a = method.getAnnotation(SQL.class);
Type returnType = method.getReturnType();

String funcName;
if (a.name().equals(""))
funcName = method.getName();
else
funcName = a.name();

String sqlReturnType;
if (a.returnType().equals(""))
sqlReturnType = ClassUtils.TYPE_MAPPING.get(returnType)[0].toString();
else
sqlReturnType = a.returnType();

String schemaName = "";
if (!a.schema().equals(""))
schemaName = a.schema() + ".";

String params = "";

Class[] paramClasses = method.getParameterTypes();
for (int i=0;i<paramClasses.length;i++) {
try {
params += a.paramInOutAndNames()[i] + " ";
} catch (IndexOutOfBoundsException ioe) {}

params += ClassUtils.TYPE_MAPPING.get(paramClasses[i])[0].toString() + ",";
}

if (params.length()>0) params = params.substring(0,params.length()-1);

String sql = " create or replace function " + schemaName + funcName + " (" + params + ")\n" +
"\t returns " + sqlReturnType + "\n" +
"\t as '" + c.getName() + "." + method.getName() + "'\n" +
"\t language " + a.language() + ";\n";

DatabaseUtils.execute(sql);
}
}
}
}
In the easiest way you just have to declare a method for a sql function with:
@SQL
public static String version() {
return "0.0.1";
}

Or you can do a more complex
@SQL(schema="public" name="foo", language="javau", paramInOutAndNames={"IN i_lala"}, returnType="varchar")
public static String foo(String lala) {
return lala;
}

When someone installs a jar simply screeen all classes in the jar for the annontation and fire the generated sql.

Followup

No Followups Have Been Posted

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge