int dataType = sqlType2DataType(columnSqlType);
if (dataType == DataTypes.TYPE_OBJECT)
throw new RuntimeException("Unsupport sql type "+columnSqlType+" for column '"+columnName+"'");
columnIndexs.put(columnName, lcolumnInfos.size());
lcolumnInfos.add(new ColumnInfo(columnName, dataType, isNullable));
}
ResultSet rstPrimaryKeys = dbMetaData.getPrimaryKeys(null, null, tableName);
List<String> keyColumns = new ArrayList<String>();
while (rstPrimaryKeys.next()) {
String columnName = rstPrimaryKeys.getString("COLUMN_NAME");
keyColumns.add(columnName);
}
if (nAutoIncrement >= 0) {
if (keyColumns.size() != 1)
throw new RuntimeException("AutoIncrement but no PrimaryKey");
if (! keyColumns.get(0).equals(lcolumnInfos.get(nAutoIncrement).getName()))
throw new RuntimeException("AutoIncrement only support on column of PrimaryKey");
}
// 将主键字段移到最前面
ColumnInfo[] columnInfos = lcolumnInfos.toArray(new ColumnInfo[lcolumnInfos.size()]);
for (int i=0; i < keyColumns.size(); i++) {
String columnName = keyColumns.get(i);
Integer columnIndex = columnIndexs.get(columnName);
if (columnIndex == null)
throw new RuntimeException("Unkown column '"+columnName+"' of PrimaryKey");
int n = columnIndex.intValue();
if (n == i) continue;
else if (n < i)
throw new RuntimeException("Error order of column '"+columnName+"' for PrimaryKey");
ColumnInfo tmp = columnInfos[i];
columnInfos[i] = columnInfos[n];
columnInfos[n] = tmp;
columnIndexs.put(columnInfos[i].getName(), i);
columnIndexs.put(columnInfos[n].getName(), n);
}