// 00380 write insert statement part 1
// 00400 reading table structure
// 00500 data type handling
// 00510 from byte to byte array
// 00520 write insert statement part 2
// 00750 uint Little-Endian handle
// 00800 int16 Little-Endian Handle
// 00820 decode the Binary *
// 00830 Read UTF16-LE
// 00840 CONVERT TO UTF-8
// 00900 Millseconds to date
import java.util.*;
import java.sql.*;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.BitSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.nio.charset.Charset;
import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
public class inserttable {
boolean ViewSource = false;
boolean ViewDataStructure = false;
boolean ViewDetail = false ;
boolean ViewMySQLInsertDetail = false ;
int ViewMySQLInsert = 0 ;
int ViewMessageNumber=999999;
int MsgSeqNo = 0;
static String v_data = "";
static String v_tableName = "";
static String v_create;
static String v_delimiter;
static String v_type;
static String v_unsigned;
static tables stru = new tables();
static jdbcmysql MySql = new jdbcmysql();
int BeginByte = 0;
int EndByte = 0;
ByteArrayInputStream bin=null;
inserttable(){}
String toBinary( byte[] bytes )
{
StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
for( int i = 0; i < Byte.SIZE * bytes.length; i++ )
sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
return sb.toString();
}
private byte[] getBytes (char[] chars) {
// Charset cs = Charset.forName ("UTF16"); //not ok
// Charset cs = Charset.forName ("BIG5"); //not ok
Charset cs = Charset.forName ("UTF-16LE"); //not ok
CharBuffer cb = CharBuffer.allocate (chars.length);
cb.put (chars);
cb.flip ();
ByteBuffer bb = cs.encode (cb);
return bb.array();
}
private char[] getChars (byte[] bytes) {
// Charset cs = Charset.forName ("UTF8"); // not ok
Charset cs = Charset.forName ("UTF-16LE"); // not ok
ByteBuffer bb = ByteBuffer.allocate (bytes.length);
bb.put (bytes);
bb.flip ();
CharBuffer cb = cs.decode (bb);
return cb.array();
}
public static short byteArrayToShortLE(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer.getShort();
}
public static int byteArrayToIntLE(byte[] data) {
ByteBuffer buffer = ByteBuffer.wrap(data);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return buffer.getInt();
}
public static String byteArrayToString(byte[] data) {
return (data == null) ? null : new String(data);
} // endof byteArrayToString
public byte[] intToBytes(int my_int) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeInt(my_int);
out.close();
byte[] int_bytes = bos.toByteArray();
bos.close();
return int_bytes;
}
public void run() throws Exception {
// data come from readbyte which can change to byte value . 0-255
String _Byte[] = v_data.split(",");
int MsgSize = Integer.valueOf(_Byte[0])+ Integer.valueOf(_Byte[1]) * 256;
int v_tableNumber = Integer.valueOf(_Byte[2])+ Integer.valueOf(_Byte[3]) * 256;
int n = 1;
int Col=0;
if (stru.tables.length >= v_tableNumber && stru.tables[v_tableNumber].length >= 12) {
if (ViewSource) {System.out.println("Data : " + v_data);}
String v_tableName = stru.tables[v_tableNumber][1];
int common=0;int data=0;
String S_ = stru.tables[v_tableNumber][2];
if(S_ !="Dummy" && S_ != null && !S_.isEmpty()){
//System.out.println(S_);
String B_[] = S_.split(",");
common = Integer.valueOf(B_[0]);
data = Integer.valueOf(B_[1]);
n=(MsgSize-common)/data;
}
for (int count=0; count< n; count++){
if (ViewDataStructure) {
System.out.println("Table : " + v_tableName + "("
+ v_tableNumber + ")");
}
String InsertPart1 = "";
String Variable = "";
char Delimiter = ' ';
///////////////////////////////////////////////////
//
//
// 00380 write insert statement part 1
//
//
///////////////////////////////////////////////////
for (int j = 1; j < stru.tables[v_tableNumber].length; j++) {
if (j >= 9 && j % 3 == 0) {
String FieldName = stru.tables[v_tableNumber][j];
if (FieldName == "Filler" || FieldName == "Filler2"
|| FieldName == "Filler3" || FieldName == "Filler4") {
} else {
InsertPart1 = InsertPart1 + Delimiter + FieldName;
Variable = Variable + Delimiter + "?";
Delimiter = ',';
}
}
}
String InsertPart2 = "";
Delimiter = ' ';
String S_TYPE = " ";
String S_CharValue = "";
long v_intValue = 0;
String v_strvalue = "";
String v_strvalue_ = "";
byte DBString[];
int i88 = 65;
char c88 = (char) i88;
int BeginByte = 4;
v_delimiter = "";
//////////////////////////////////////////////////////////////////////
//
//
// 00400 reading table structure for part 2
//
//
//////////////////////////////////////////////////////////////////////
Col=0;
// String INSERT_="insert into "+v_tableName +" ("+InsertPart1+",time_stamp,MsgSeqNo) value ("+Variable+",?,?);";
// if(1==0){System.out.println("\n"+INSERT_);}
for (int j = 1; j < stru.tables[v_tableNumber].length; j++) {
if (v_tableNumber==ViewMessageNumber) {
ViewSource = true;
ViewDataStructure = true;
ViewDetail = true ;
ViewMySQLInsertDetail = true ;
ViewMySQLInsert = 1 ;
}
if (j >= 9 && j % 3 == 0) {
if (ViewDataStructure || ViewDetail) {
System.out.print("\n " + stru.tables[v_tableNumber][j]);
} // find the field name
String v_Type = stru.tables[v_tableNumber][j + 1];
int fieldLen = Integer.valueOf(stru.tables[v_tableNumber][j + 2]);
EndByte = fieldLen + BeginByte - 1;
if (ViewDetail) {
System.out.print(" <");
System.out.print(BeginByte + " to ");
System.out.print(EndByte);
System.out.print("> ");
System.out.print("> ");
System.out.print(" " + v_Type + " ");
}
long v_power = 1;
// 00500 data type handling
if (EndByte <= _Byte.length) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// "NumberOfOrders"
for (int BytePointer = BeginByte; BytePointer <= EndByte; BytePointer++) {
int BP=BytePointer;
if (n>1 && BP>common){BP=data*count+BP;}
// 00510 from byte to byte array
if (ViewDetail) {
System.out.print(_Byte[BP] + ",");
}
if (v_Type == "Binary" || v_Type == "String") {
//00820 decode the Binary
int i = Integer.valueOf(_Byte[BP]);
outputStream.write(i); // had check the method write which can write by integer
} // endif
else if (v_Type == "Int16" || v_Type == "Int32"
|| v_Type == "Int64" || v_Type == "Uint16"
|| v_Type == "Uint32" || v_Type == "Uint64") {
// 00750 uint Little-Endian handle
int xx = Integer.valueOf(_Byte[BP]);
v_intValue = v_intValue + xx * v_power;
v_power = v_power * 256;
} // endelse
} // endfor BytePointer to BeginByte
// 00800 int16 Little-Endian Handle
if (v_Type == "Uint16") {
} else if (v_Type == "Uint16") {
} else if (v_Type == "Uint32") {
} else if (v_Type == "Uint64") {
} else if (v_Type == "Int16") {
if (v_intValue > (2 ^ 15)) {
v_intValue = v_intValue - (2 ^ 15) * 2;
}
if (v_intValue < 0) {
v_intValue=2^15+-v_intValue;
}
} else if (v_Type == "Int32") {
if (v_intValue > (2 ^ 31)) {
v_intValue = v_intValue - (2 ^ 31) * 2;
}
if (v_intValue < 0) {
v_intValue=2^31+-v_intValue;
}
} else if (v_Type == "Int64") {
if (v_intValue > (2 ^ 63)) {
v_intValue = v_intValue - (2 ^ 63) * 2;
}
if (v_intValue < 0) {
long _l=2^63;
v_intValue=-v_intValue+_l;
//v_intValue = v_intValue + 9223372036854775808 ;
v_intValue=0;
}
} else if (v_Type == "String") {
DBString = outputStream.toByteArray();
v_strvalue =new String(DBString);
} else if (v_Type == "Binary") {
DBString = outputStream.toByteArray();
try { v_strvalue =new String(DBString,"UTF-16LE");} catch (UnsupportedEncodingException e){v_strvalue = "dEcode error";}
}
}
//////////////////////////////////////
//
//
// 00520 write insert statement part 2
//
//
//////////////////////////////////////
String FieldName = stru.tables[v_tableNumber][j];
if (1==0){
if (FieldName == "Filler" || FieldName == "Filler2" || FieldName == "Filler3" || FieldName == "Filler4") {}
else {
Col++;
if (v_Type == "String") {System.out.println("ps.setString("+ Col+","+v_intValue+")");}
else {System.out.println("ps.setInt("+Col+","+'"'+v_strvalue+'"'+")");}
}}
if (FieldName == "Filler" || FieldName == "Filler2"
|| FieldName == "Filler3" || FieldName == "Filler4") {
}
else {
if (v_Type == "String" || v_Type == "Binary") {
if (ViewDetail) {
System.out.print(" :+++++++++++: " + v_strvalue);
}
if (true || v_Type == "String") {
if (v_strvalue.matches(".*\'.*"))
{InsertPart2 = InsertPart2 + v_delimiter + "\"" + v_strvalue + "\"";}
else {InsertPart2 = InsertPart2 + v_delimiter + "\'" + v_strvalue + "\'";}
} else {InsertPart2 = InsertPart2 + v_delimiter + "\'" + bin + "\'";} // else if v_type=="String"
v_strvalue = "";
} else if (v_Type == "Int16" || v_Type == "Int32"
|| v_Type == "Int64" || v_Type == "Uint16"
|| v_Type == "Uint32" || v_Type == "Uint64") {
if (ViewDetail) {
System.out.print(" Num0325 -:: " + v_intValue);
DateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmssSSSS"); //00900 Millseconds to date
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(v_intValue/1000000);
long _a=Long.valueOf(formatter.format(calendar.getTime()));
System.out.print(" = " + _a);
System.out.print(" ==> "+ stru.tables[v_tableNumber][j]);
String _f=stru.tables[v_tableNumber][j];
if (_f=="StartDateTime" || _f=="EndDateTime" || _f=="ReleaseTime" || _f=="TradeTime" || _f=="IndexTime"){v_intValue=_a;}
}
InsertPart2 = InsertPart2 + v_delimiter + v_intValue;
v_intValue = 0;
} else {
if (ViewDetail) {
System.out.print(" =x: " + v_intValue +" Type "+v_Type);
}
InsertPart2 = InsertPart2 + v_delimiter + v_intValue;
v_intValue = 0;
}
v_delimiter = ",";
}
BeginByte = EndByte + 1; // when the field is finsh set the new BeginBute
}
}
Long UniqueNum = System.currentTimeMillis();
UniqueNum=(Long) UniqueNum*1000000+MsgSeqNo*100+count;
String SQL_insert = "Insert into " + v_tableName + " (" + InsertPart1
+ ",UniqueNum,MsgSeqNo) value (" + InsertPart2 + ","+UniqueNum+ ","+MsgSeqNo+")";
if (ViewMySQLInsert == 1) {
System.out.print("\n\n " + SQL_insert);
}
MySql.createdbSQL = SQL_insert;
MySql.RunMySql();
} // endfor int count
} //endif (stru.tables.length >= v_tableNumber && stru.tables[v_tableNumber].length >= 12)
} // endMethodRun
} // endClass
// end of file
沒有留言:
張貼留言