1 11 package com.nilostep.xlsql.sql.mysql; 12 13 import com.nilostep.xlsql.sql.xlSqlWriter; 14 15 import java.sql.*; 16 import java.text.*; 17 import java.util.*; 18 19 22 public class xlMySQLWriter extends xlSqlWriter { 23 30 public String wCreateSchema(String s) { 31 return "CREATE DATABASE " + s + ";"; 32 } 33 34 44 public String wCreateTable(String s, 45 String t, 46 String [] co, 47 String [] ty) { 48 String sql = "CREATE TABLE " + s + "." + t + " ( "; 49 boolean firstcolumn = true; 50 51 for (int i = 0; i < co.length; i++) { 52 if (firstcolumn) { 53 firstcolumn = false; 54 } else { 55 sql = sql + ","; 56 } 57 58 sql = sql + co[i] + " "; 59 60 if (ty[i].equalsIgnoreCase("VARCHAR")) { 61 sql = sql + "TEXT"; 62 } else if (ty[i].equalsIgnoreCase("BIT")) { 63 sql = sql + "CHAR(1)"; 64 } else { 65 sql = sql + ty[i]; 66 } 67 } 68 69 sql = sql + " );"; 70 71 return sql; 72 } 73 74 82 public String wDropTable(String s, 83 String t) { 84 return "DROP TABLE IF EXISTS " + s + "." + t + ";"; 85 } 86 87 98 public String wInsert(String s, 99 String t, 100 String [] co, 101 String [] ty, 102 String [] va) { 103 String sql = "INSERT INTO " + s + "." + t + " VALUES ("; 104 boolean firstcolumn = true; 105 106 for (int i = 0; i < co.length; i++) { 107 if (firstcolumn) { 108 firstcolumn = false; 109 } else { 110 sql = sql + ","; 111 } 112 113 if (va[i] == null) { 114 sql = sql + "null"; 115 116 continue; 117 } 118 119 if ("DOUBLE".equals(ty[i])) { 120 sql = sql + va[i]; 121 } else if ("BIT".equals(ty[i])) { 122 sql = sql + "'Y'"; 123 } else if ("DATE".equals(ty[i])) { 124 try { 125 SimpleDateFormat dateFormat = 126 new SimpleDateFormat("dd/MM/yyyy"); 127 java.util.Date d; 128 d = dateFormat.parse(va[i]); 129 dateFormat.applyPattern("yyyy-MM-dd"); 130 sql = sql + "'" + dateFormat.format(d) + "'"; 131 } catch (ParseException pe) { 132 sql = sql + "null"; 133 } 134 } else { 135 sql = sql + "'" + va[i] + "'"; 136 } 137 } 138 139 sql = sql + " );"; 140 141 return sql; 142 } 143 144 protected String getTableName(String schema, 145 String table) { 146 return null; 147 } 148 } | Popular Tags |