/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package desktopapplication1;
import com.mysql.jdbc.Driver;
import java.sql.*;
import java.util.Iterator;
/**
*
* @author Ludger
*/
public class codeGene {
private String server;
private String username;
private String password;
public codeGene(String server, String username, String password) {
this.server = server;
this.username = username;
this.password = password;
}
public Queue<String> getDatabases()
{
try {
new Driver();
// buat koneksi
Connection mysqlConn = DriverManager.getConnection(
"jdbc:mysql://"+server+"/mysql",
username,
password);
Statement statement = mysqlConn.createStatement();
ResultSet resultSet = statement.executeQuery("show databases;");
Queue<String> temp = new Queue<String>();
while(resultSet.next())
{
String id = resultSet.getString(1);
temp.enqueue(id);
System.out.println("("+temp.size+")Database Name =" + id);
}
resultSet.close();
statement.close();
mysqlConn.close();
return temp;
} catch (Exception e) {
}
return null;
}
public Queue<String> getTables(String Database)
{
try {
new Driver();
// buat koneksi
Connection mysqlConn = DriverManager.getConnection(
"jdbc:mysql://"+server+"/"+Database,
username,
password);
Statement statement = mysqlConn.createStatement();
ResultSet resultSet = statement.executeQuery("show tables;");
Queue<String> temp = new Queue<String>();
while(resultSet.next())
{
String id = resultSet.getString(1);
temp.enqueue(id);
System.out.println("("+temp.size+")Table Name =" + id);
}
resultSet.close();
statement.close();
mysqlConn.close();
return temp;
} catch (Exception e) {
}
return null;
}
public String genCode(String Database,String DBName)
{
try {
new Driver();
// buat koneksi
Connection mysqlConn = DriverManager.getConnection(
"jdbc:mysql://"+server+"/"+Database,
username,
password);
Statement statement = mysqlConn.createStatement();
ResultSet resultSet = statement.executeQuery("SHOW COLUMNS FROM "+DBName);
Queue<String> temp = new Queue<String>();
while(resultSet.next())
{
String id = resultSet.getString(1);
temp.enqueue(id);
System.out.println("("+temp.size+")name=" + id);
}
String code = "class "+DBName+" \n { \n";
code += "//var data \n";
code += "var";
Iterator<String> itor = temp.iterator();
code += " $" + itor.next();
while(itor.hasNext())
{
code += ", $" + itor.next();
}
code += "; \n";
code += " public function __construct($id=-1) { \n";
code += " if($id == -1) \n";
code += " {\n";
code += " $this->id = 0; \n";
code += " }else \n";
code += " { \n";
code += " $this->id = $id; \n";
code += " $this->fetch_data(); \n";
code += " } \n";
code += " } \n";
code += " private function fetch_data() \n";
code += " { \n";
code += " global $db; \n";
code += " $sql = \"SELECT * FROM `"+DBName+"` WHERE `id` = $this->id;\";\n";
code += " $row = $db->get_row($sql);\n";
code += " if($row == null )\n";
code += " die(\"error no such Item\");\n";
itor = temp.iterator();
while(itor.hasNext())
{
String tttemp = itor.next();
code += " $this->"+tttemp+" = $row->"+tttemp+";" ;
}
code += " }\n";
code += " function submit_update()\n";
code += " {\n";
code += " global $db;\n";
code += " $sql = \"\n";
code += "UPDATE `"+DBName+"`\n";
code += "SET ";
itor = temp.iterator();
String tttemp = itor.next();
if(tttemp.equals("id")) tttemp = itor.next();
code += " `"+tttemp+"` = '$this->"+tttemp+"' \n";
while(itor.hasNext())
{
tttemp = itor.next();
if(!tttemp.equals("id") && !tttemp.equals("timestamp") && !tttemp.equals("created") && !tttemp.equals("updated") && !tttemp.equals("lastUpdated"))code += ", `"+tttemp+"` = '$this->"+tttemp+"' \n";
}
code += "WHERE `id` = '$this->id';\";\n";
code += " $db->query($sql);\n";
code += " }\n";
code += " function submit_new()\n";
code += " {\n";
code += " global $db;\n";
code += " $sql = \"INSERT INTO `"+DBName+"`\n";
code += " (\n";
itor = temp.iterator();
tttemp = itor.next();
if(tttemp.equals("id")) tttemp = itor.next();
code += " `"+tttemp+"`\n";
while(itor.hasNext())
{
tttemp = itor.next();
if(!tttemp.equals("id"))code += ", `"+tttemp+"`\n";
}
code += " )\n";
code += "VALUES (\n";
itor = temp.iterator();
tttemp = itor.next();
if(tttemp.equals("id")) tttemp = itor.next();
code += " '$this->"+tttemp+"'\n";
while(itor.hasNext())
{
tttemp = itor.next();
if(!tttemp.equals("id") && !tttemp.equals("timestamp") && !tttemp.equals("created") && !tttemp.equals("updated"))code += " , '$this->"+tttemp+"'\n";
if(tttemp.equals("timestamp") || tttemp.equals("created") || tttemp.equals("updated") || tttemp.equals("lastUpdated") )code +=" , NOW()\n";
}
code += " );\";\n";
code += " $db->query($sql);\n";
code += " return $db->insert_id;\n";
code += " }\n";
code += " function get_Post()\n";
code += " {\n";
itor = temp.iterator();
while(itor.hasNext())
{
tttemp = itor.next();
if(!tttemp.equals("id") && !tttemp.equals("timestamp") && !tttemp.equals("created") && !tttemp.equals("updated") && !tttemp.equals("lastUpdated"))code += "$this->"+tttemp+" = $_POST[\""+tttemp+"\"]; \n";
}
code += " }\n";
code += "}";
resultSet.close();
statement.close();
mysqlConn.close();
return code;
} catch (Exception e) {
System.out.println(e);
}
return "error";
}
public static void main(String[] args) {
}
}