BF [ 1824256 ] Convert sql casts

This commit is contained in:
teo_sarca 2007-11-02 06:26:59 +00:00
parent 12a8377fdd
commit 085c094efe
2 changed files with 76 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.StringTokenizer;
import org.compiere.util.CLogger;
@ -27,6 +28,8 @@ import org.compiere.util.Util;
* Convert Oracle SQL to PostgreSQL SQL
*
* @author Victor Perez, Low Heng Sin, Carlos Ruiz
* @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>BF [ 1824256 ] Convert sql casts
*/
public class Convert_PostgreSQL extends Convert_SQL92 {
/**
@ -167,8 +170,48 @@ public class Convert_PostgreSQL extends Convert_SQL92 {
if (index != -1 && retValue.indexOf("(+)", index) != -1)
retValue = convertOuterJoin(retValue);
// Convert datatypes from CAST(.. as datatypes):
retValue = convertCast(retValue);
return retValue;
} // convertComplexStatement
/**
* Convert datatypes from CAST sentences
* <pre>
* cast(NULL as NVARCHAR2(255))
* =&gt;cast(NULL as VARCHAR)
* </pre>
*/
private String convertCast(String sqlStatement) {
final String PATTERN_String = "\'([^']|(''))*\'";
final String PATTERN_DataType = "([\\w]+)(\\(\\d+\\))?";
final String pattern =
"CAST[\\s]*\\([\\s]*" // CAST<sp>(<sp>
+"(("+PATTERN_String+")|([^\\s]+))" // arg1 1(2,3)
+"[\\s]*AS[\\s]*" // <sp>AS<sp>
+"("+PATTERN_DataType+")" // arg2 (datatype) 4
+"\\s*\\)" // <sp>)
;
final int gidx_arg1 = 1;
final int gidx_arg2 = 7; // datatype w/o length
final Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(sqlStatement);
TreeMap<String, String> convertMap = (TreeMap<String, String>)getConvertMap();
StringBuffer retValue = new StringBuffer(sqlStatement.length());
while (m.find()) {
String arg1 = m.group(gidx_arg1);
String arg2 = m.group(gidx_arg2);
//
String datatype = convertMap.get("\\b"+arg2.toUpperCase()+"\\b");
if (datatype == null)
datatype = arg2;
m.appendReplacement(retValue, "cast("+arg1+" as "+datatype+")");
}
m.appendTail(retValue);
return retValue.toString();
}
/**
* Convert RowNum.

View File

@ -417,4 +417,37 @@ public final class Convert_PostgreSQLTest extends TestCase{
r = convert.convert(sql);
assertEquals(sqe, r[0]);
}
/**
* Test BF [ 1824256 ] Convert sql casts
*/
public void testCasts() {
String sql_begin = "SELECT ";
String[][] sql_tests = new String[][] {
// Oracle vs PostgreSQL
{"cast('N' as char)","cast('N' as char)"},
{"cast('this is a string' as nvarchar2(40))","cast('this is a string' as VARCHAR)"},
{"cast('this is a string as a ''string''' as nvarchar2(40))","cast('this is a string as a ''string''' as VARCHAR)"},
{"cast(tbl.IsView as char)","cast(tbl.IsView as char)"},
{"cast(trunc(tbl.Updated,'MONTH') as date)","cast(trunc(tbl.Updated,'MONTH') as TIMESTAMP)"},
{"cast(NULL as nvarchar2(255))","cast(NULL as VARCHAR)"},
{"cast(NULL as number(10))","cast(NULL as NUMERIC)"},
};
String sql_end = " FROM AD_Table tbl";
StringBuffer sql = new StringBuffer(sql_begin);
StringBuffer sqle = new StringBuffer(sql_begin);
for (int i = 0; i < sql_tests.length; i++) {
if (i > 0) {
sql.append(",");
sqle.append(",");
}
sql.append(sql_tests[i][0]);
sqle.append(sql_tests[i][1]);
}
sql.append(sql_end);
sqle.append(sql_end);
//
String[] r = convert.convert(sql.toString());
assertEquals(sqle.toString(), r[0]);
}
}