⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generatesql.java

📁 使用模式映射方法
💻 JAVA
字号:
/*
 * GenerateSQL.java
 *
 * Created on 2007年5月11日, 上午12:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package XRSystem.Process;

/**
 *
 * @author 王刚
 */
import java.lang.*;
import java.util.*;
import javax.swing.*;
import java.sql.*;

import XRSystem.Process.AnalysisXPath;
import XRSystem.Util.XPathNode;
import XRSystem.Util.ResultElement;
import XRSystem.DataAccess.SQLResultSet;

public class GenerateSQL {
    
    /** Creates a new instance of GenerateSQL */
    private String XPath;
    private ArrayList<XPathNode> NodeList = new ArrayList<XPathNode>();
    private ArrayList<ResultElement> ResultList = new ArrayList<ResultElement>();
    private int DocID;
    private String XMLView;
    
    public GenerateSQL(String XPath,String XMLView) {
        this.XPath = XPath;
        this.XMLView = XMLView;
    }
    
    private String GetViewFormat() {
        return "["+XMLView+"]";
    }
    
    private int GetDocID() {
        ResultSet rs = null;
        String DocSQL = "SELECT DocumentID FROM Document WHERE DocumentName ="+"'"+XMLView+"'";
        try{
            rs = new SQLResultSet().ExecuteSQL(DocSQL);
            while(rs.next()) {
                DocID = rs.getInt(1);
            }
        }catch(SQLException e){e.printStackTrace();} catch(Exception e){e.printStackTrace();}
        return DocID;
    }
    
    private String SQLExpession(int StepCount) {
        int n=StepCount;
        String XMLStructure = this.GetViewFormat();
        String SQLString="";
        String SELECTstr="SELECT DISTINCT "+XMLStructure+".子元素Pre,"+XMLStructure+".子元素Post,"+ XMLStructure+".子元素标记";
        String FROMstr=" FROM "+XMLStructure;
        String WHEREstr="";
        if(n==1) {
            WHEREstr=" "+GenerateWhereClause(n);
            SQLString = SELECTstr+FROMstr+WHEREstr;
        } else {
            FROMstr=FROMstr+","+"("+SQLExpession(n-1)+")"+"AS TempTable"+n;
            WHEREstr=" "+GenerateWhereClause(n);
            SQLString = SELECTstr+FROMstr+WHEREstr;
        }
        return SQLString;
    }
    
    private String GenerateWhereClause(int m) {
        int count=m;
        String XMLStructure = this.GetViewFormat();
        String WhereClause="";
        String TagName;
        String ContextNode;
        int SplitType = NodeList.get(count-1).GetSplitType();
        switch(SplitType) {
            case 0:
                if(count==1) {
                    TagName=NodeList.get(count-1).GetStepName().toString();
                    WhereClause="WHERE "+XMLStructure+".子元素标记="+"'"+TagName+"'"+" AND "+XMLStructure+".父元素Pre=0";
                } else {
                    TagName=NodeList.get(count-1).GetStepName().toString();
                    //ContextNode=NodeList.get(count-2).GetStepName().toString();
                    WhereClause="WHERE "+XMLStructure+".父元素Pre="+"TempTable"+count+".子元素Pre"+" AND "+XMLStructure+".父元素Post="+"TempTable"+count+".子元素Post"+ " AND "+XMLStructure+".子元素标记=" +"'"+TagName+"'";
                }
                break;
            case 1:
                if(count==1) {
                    TagName=NodeList.get(count-1).GetStepName().toString();
                    WhereClause="WHERE "+XMLStructure+".子元素标记="+"'"+TagName+"'";
                } else {
                    TagName=NodeList.get(count-1).GetStepName().toString();
                    //ContextNode=NodeList.get(count-2).GetStepName().toString();
                    WhereClause="WHERE "+XMLStructure+".子元素Pre>"+"TempTable"+count+".子元素Pre"+" AND "+XMLStructure+".子元素Post<"+"TempTable"+count+".子元素Post"+" AND "+XMLStructure+".子元素标记=" +"'"+TagName+"'";
                }
                break;
                
        }
        return WhereClause;
    }
    
    public void ExecuteQuery() {
        try{
            NodeList=new AnalysisXPath(XPath+"/").SplitXPath();
        } catch (StringIndexOutOfBoundsException e){e.printStackTrace();}
        
        String SQL=SQLExpession(NodeList.size());
        System.out.println("产生的SQL语句是:"+SQL);
        try{
            ResultSet QueryResultSet = new SQLResultSet().ExecuteSQL(SQL);
            while(QueryResultSet.next()) {
                ResultList.add(new ResultElement(this.GetDocID(),QueryResultSet.getInt(1),QueryResultSet.getString(3),QueryResultSet.getInt(2)));
            }
        } catch(SQLException e){e.printStackTrace();} catch(Exception e){e.printStackTrace();}
        this.DisplayResult(ResultList);
    }
    
    private void DisplayResult(ArrayList<ResultElement> TempList) {
        new DisplayResult(TempList).BuildResult();

    }
    
    public static void main(String[] args) {
        new GenerateSQL("//book//author","pub.xml").ExecuteQuery();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -