/*
Program provadi sql dotazy podle dodaneho SQL dotazu z venku.
Spusteni: java doSQL dbname sqlstring
Volani z jineho Java programu: int sqlresult=doSQL.parse(sql_str,dbname);
Vystupem je int, v pripade prikazu SELECT udavajici pocet radek odpovidajici pruslusnemu SELCTu, v pripade jinych je vystupem kod-chybova hlaseni prosilusneho SQL dotazu
Jestlize je program spusten s prvni argumentem ver, vypise verzi programu
Priklad spusteni z batche:
java doSQL honzasport-test "CREATE TABLE Newtable (First_Col char(50), Second_Col char(50), Third_Col char(50), City char(50), Country char(25), Birth_Date date)"
java doSQL honzasport-test "INSERT INTO Newtable (First_Col, Second_Col, Third_Col) VALUES ('jsem_tu', 'na_prvnim_miste', '556')"
java doSQL honzasport-test "UPDATE Newtable SET First_Col='Ju a Hele jsou kamaradi' WHERE Newtable.First_Col='Ju a Hele'"
java doSQL honzasport-test "SELECT * FROM Newtable
Priklad volani z jineho programu:
int sqlresult=doSQL.parse(sql_str,dbname);
*/

import java.sql.*;
import java.io.*;


public class doSQL
{

    /*----------------------------------------------------------------------------------------------*/
    // zde se provadi vlastni SQL string	
    //
    /*----------------------------------------------------------------------------------------------*/														
	public static int parse(String sql_str, String whichURL){
	
		String url=new String("");
    	Connection db=null;
		int result=0;
	    
	    try {
    				//load and register a driver: Class.forName("foo.bah.Driver") od JDBC 2.0 pro jbdc-odbc v  baliku neni nutne , ale pro cizi drivery  jo
                  	//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                  		Class.forName("easysoft.sql.jobDriver").newInstance();


      		} catch (Exception e){System.out.println(">>>Java ODBC Driver Exception  "+e);}
      		//jdbc:podprotokol:databazedsn
    		//url = "jdbc:odbc:"+whichURL;
    		url = "jdbc:easysoft:"+whichURL;

     	 	try {
      			db = DriverManager.getConnection(url);
      			//System.out.println("Driver:"+DriverManager.getDriver(url) );





                 System.out.println("sql_str="+sql_str);
	    
   				try {
                	Statement sq_stmt = db.createStatement();

                    if (sql_str.indexOf("SELECT")!=-1) { //tato funkce umozni delat opakovane select, pokud by SQL bylo formulovane tak, ze vystuypem je vice radek

                    		int row=1;
                			ResultSet rs = sq_stmt.executeQuery(sql_str);
                			while (rs.next()){
                                    int columnIndex=1; //prvni sloupec je 1
                                   	//zjistujeme pocet sloupcu
                					ResultSetMetaData rsmd = rs.getMetaData();
    								int numberOfColumns = rsmd.getColumnCount();
                					while (columnIndex<=numberOfColumns){
	                					Reader datas=rs.getCharacterStream(columnIndex);
	                					//System.out.println("columnIndex="+columnIndex);
	                					columnIndex++;
                					}
                               		//System.out.println("row="+row);
                               		result=row;
                                	row=row+1;   //nefunguje row++?????
                                	//System.out.println("row="+row);
                                	//System.out.println("Cislo sekce="+CisloSekce+" MenuObrazek="+obrMenu+" AltMenuObrazek="+obrMenuAlt+" MenuText="+menu_text+" Odkaz="+menu_odkaz);
                                	
                			}//Konec while
                	}else {
                	       	//System.out.println("TADY");
                	        result = sq_stmt.executeUpdate(sql_str);

                	}


				} catch (SQLException e){System.out.println(">>>Database query error - sql="+sql_str);}

  			}  //try get connection
          	catch (SQLException e){System.out.println("\n"+">>>Database name not found:"+whichURL);}


        return result;
	}


    /*----------------------------------------------------------------------------------------------*/
    // main
    //
    /*----------------------------------------------------------------------------------------------*/
	public static void main(String args[]){
		String ver="1.0";
		String whichURL=null;//Jmeno databaze = systemove dsn ve Windows
    	
    
    	String sql_str=new String("");//prazdny retezec, aby se mohl behem cteni args nabalovat - tim se vytvori sql string


	    //zde se ctou argumenty
		int maxargs;
		if (args.length>=2) {
		 	
    		//for (maxargs=0; maxargs<args.length; maxargs++) {
    			//if (args[maxargs]!=null){

       					whichURL=new String(args[0]);
       					sql_str=args[1];

    			//}

    		//}



                //zde se provadi SQL
				int sqlresult=parse(sql_str, whichURL);
                System.out.println(sqlresult+" objects executed");

		}else if (args.length==1) {
			if (args[0].equals("ver")) {System.out.println(">>>Programme: doSQL.class version="+ver);}
		} else {System.out.println(">>>Usage: java doSQL dbname sqlstring or java doSQL ver or int sqlresult=doSQL.parse(sql_str,dbname) when calling from programme");}

	}

}
