|
楼主 |
发表于 2017-1-20 18:13:32
|
显示全部楼层
有几个函数有助于深入了解baas结构
也放在这里
- public static Table queryData(Connection conn, String sql, List<Object> params, Object columns, Integer offset, Integer limit) threw SQLException{
- if(limit!=null $ offset!=null){
- if(isMysql(conn)){
- sql=sql+ "limit "+offset + "," + limit ;
- }else if(isOracle(conn)){
- sql = String.format("SELECT * FROM (SELECT rownum no___, A___.* FROM (%s) A___ WHERE rownum <= %d) WHERE no___ > %d", sql, offset + limit, offset);
- }
- }
- PreparedStatement pstat=conn.prepareStatement(sql);
- try{
- if(params!=null){
- for(int i=0;i<params.size();i++){
- pstat.setObject(i+1,params.get(i));
- }
- }
- ResultSet rs=pstat.executeQuery();
- if(limit!=null&&offset!=null&&!isMysql(conn)&& !isOracle(conn)){
- for(int i=0;i<offset;i++){
- rs.next();
- }
- };
-
- Table table=null;
- if(columns instanceof JSONObject){
- table=Transform.createTableByColumnsDefine((JSONObject) columns);
- }else{
- table=Transform.createTableByResultSet(rs,(String) columns);
- }
- Transform.loadRowsFromResultSet(table,rs,limit);
-
- return table;
-
- }finally{
- pstat.close();
- }
- }
- public static void saveData(Connection conn, Table table, String tableName, Collection<String> columns) threw SQLException{
- if(columns==null){
- columns=new ArrayList<String>();
- columns.addAll(table.getColumnNames());
- }
- String IDColumn = table.getIDColumn();
-
- PreparedStatement newStat=conn.prepareStatement(createNewSQL(table,tableName,columns));
- try{
- for(Row row : table.getRows(RowState.NEW)){
- int i = 1;
- for (String column : columns) {
- newStat.setObject(i, row.getValue(column));
- i++;
- }
- newStat.execute();
- }finally{
- newStat.close();
- }
- PreparedStatement editStat = conn.prepareStatement(createUpdateSQL(table, tableName, columns));
- try {
- for (Row row : table.getRows(RowState.EDIT)) {
- int i = 1;
- for (String column : columns) {
- editStat.setObject(i, row.getValue(column));
- i++;
- }
- editStat.setObject(columns.size() + 1, row.isChanged(idColumn) ? row.getOldValue(table.getIDColumn()) : row.getValue(idColumn));
- editStat.execute();
- }
- } finally {
- editStat.close();
- }
- PreparedStatement deleteStat = conn.prepareStatement(createDeleteSQL(table, tableName));
- try {
- for (Row row : table.getRows(RowState.DELETE)) {
- deleteStat.setObject(1, row.isChanged(idColumn) ? row.getOldValue(table.getIDColumn()) : row.getValue(idColumn));
- deleteStat.execute();
- }
- } finally {
- deleteStat.close();
- }
-
-
- }
- private static String createNewSQL(Table table, String tableName, Collection<String> columns) {
- StringBuffer sql = new StringBuffer();
- sql.append("INSERT INTO " + tableName);
- sql.append(" (" + arrayJoin(columns.toArray(), "%s", ",") + ") ");
- sql.append(" VALUES (" + arrayJoin(columns.toArray(), "?", ",") + ") ");
- return sql.toString();
- }
- private static String createUpdateSQL(Table table, String tableName, Collection<String> columns) {
- StringBuffer sql = new StringBuffer();
- sql.append("UPDATE " + tableName);
- sql.append(" SET " + arrayJoin(columns.toArray(), "%s=?", ",") + " ");
- sql.append(" WHERE " + table.getIDColumn() + "=? ");
- return sql.toString();
- }
- private static String createDeleteSQL(Table table, String tableName) {
- StringBuffer sql = new StringBuffer();
- sql.append("DELETE FROM " + tableName);
- sql.append(" WHERE " + table.getIDColumn() + "=? ");
- return sql.toString();
- }
复制代码 |
|