/*
 * This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation.

 *  There is no copyright.

 *  MockwareTeam (C) 2009

 */

package core;

import java.sql.*;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node ;
import org.w3c.dom.NamedNodeMap ;



/**
 *
 * @author yetkin
 */
public class Query extends util {
    

    public Query(){}

    public static Vector <String> ColumnNames()
    {
        Vector <String> a = null;
        return a;
    }
    public static ResultSet getQuery(){
        String myQuery ="";
        JTable aTable = new JTable();
        try{


            docEle = doc.getDocumentElement() ;
			NodeList queries = docEle.getChildNodes() ; //root'dan ilk childları alıyo NodeList
	        NamedNodeMap dbNode = docEle.getAttributes() ;
            Node     query;

	        myQuery = "";
	        String insertQuery = "";
	        String value = "";
	        String nodeName = "";

	        for(int i = 1; i < queries.getLength(); i += 2)
	        {
	             query = queries.item(i); //bunun ilk child'ı (Node)

	             if(query.getNodeName().equalsIgnoreCase("Select"))
                     myQuery += Select (query);
                 else if (query.getNodeName().equalsIgnoreCase("From"))
                     myQuery += From (query);
                 else if (query.getNodeName().equalsIgnoreCase("Where"))
                     myQuery += Where (query);

                 }System.out.println(myQuery);
                 
    }catch( Exception e)
        {
            System.out.println("Cannot create the tables, invalid create table query!!!");
            System.out.println(e.getMessage());
        }
    Connection conn = db.dbConnect("jdbc:mysql://localhost:3306/Mockware", "root", "qropadb") ;

try {
        // Create a result set containing all data from my_table
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(myQuery);
        
 
System.out.println("mygimon");
         //Fetch each row from the result set
        while (rs.next()) {System.out.println("c'mon");
            // Get the data from the row using the column index
            String s = rs.getString(2);

            // Get the data from the row using the column name
           // s = rs.getString("col_string");

            System.out.println(s);return rs;
        }
    } catch (SQLException e) {
    }

   return null;
    
}
 public static String Select (Node Sel){
    NodeList objects = Sel.getChildNodes();
    String nodeName;
    String nodeValue;
    String slct="SELECT ";
    for(int i=1;i<objects.getLength();i += 2)
    {
        NamedNodeMap attr = objects.item(i).getAttributes();
         for(int k=0; k<attr.getLength(); k++){

			nodeName = attr.item(k).getNodeName() ;
			nodeValue = attr.item(k).getNodeValue() ;

			if(nodeName.equals("table")) slct += nodeValue + "." + objects.item(i).getTextContent();
            if (i+2 < objects.getLength()) slct+="," ;
            slct+=" ";
		} //bu loop bi nıde'un attributelarını almak için

    }

    return slct;
}
public static String From (Node Fr){
    NodeList objects = Fr.getChildNodes();
    String frm="FROM ";
    for(int i=1;i<objects.getLength();i += 2)
    {

			frm += objects.item(i).getTextContent();
            if (i+2 < objects.getLength()) frm+="," ;
            frm+=" ";


    }
    return frm;
}
public static String Where (Node Wh){

    NodeList objects = Wh.getChildNodes();
    String nodeName;
    String nodeValue;
    String whr="WHERE ";
    String lhs="";
    String opr="";
    for(int i=1;i<objects.getLength();i += 2)
    {
        NamedNodeMap attr = objects.item(i).getAttributes();

         for(int k=0; k<attr.getLength(); k++){

			nodeName = attr.item(k).getNodeName() ;
			nodeValue = attr.item(k).getNodeValue() ;

			if(nodeName.equals("lhs")) lhs = nodeValue;
            else if (nodeName.equals("opr")) opr =nodeValue;

            //if (i+2 < objects.getLength()) whr+="AND" ;
            //whr+=" ";
            //
		} //bu loop bi nıde'un attributelarını almak için
        whr += lhs + " " + opr + " " + objects.item(i).getTextContent();
        if (i+2 < objects.getLength()) whr+="AND" ;
            whr+=" ";
    }

    return whr;
}
}
