tctable
This commit is contained in:
@ -9,6 +9,7 @@ public class TCField
|
||||
{
|
||||
//Типы данных
|
||||
public static final int BD_NULL = 1000; //Столбец со значением всегда NULL
|
||||
public static final int BD_UINT1_B = 27; //1 байт без знаковый Boolean
|
||||
public static final int BD_UINT1 = 0; //1 байт без знаковый
|
||||
public static final int BD_UINT2 = 1; //2 байта без знаковый
|
||||
public static final int BD_UINT4 = 3; //4 байта без знаковый
|
||||
@ -23,11 +24,17 @@ public class TCField
|
||||
public static final int BD_FLOAT8 = 22; //8 байт double
|
||||
public static final int BD_SFLOAT8 = 26; //Почти тоже самое что и BD_UTF8_1 но с строке передаётся число
|
||||
public static final int BD_UTF8_1 = 100; //100 - utf8_1 string 1й байт размер строки в байтах
|
||||
public static final int BD_UTF8_1_UUID = 103; //103 - utf8_1 string 1й байт размер строки в байтах
|
||||
public static final int BD_UTF8_1_TIMESTAMP = 105; //103 - utf8_1 string 1й байт размер строки в байтах
|
||||
public static final int BD_UTF8_2 = 101; //101 - utf8_2 string 1х 2 байта размер строки в байтах
|
||||
public static final int BD_UTF8_4 = 102; //102 - utf8_4 string 1х 4 байта размер строки в байтах
|
||||
public static final int BD_UTF8_4_JSONB = 104; //102 - utf8_4 string 1х 4 байта размер строки в байтах
|
||||
public static final int BD_BLOB_2 = 141;
|
||||
public static final int BD_BLOB_4 = 143; //Двоичные данные uint4 количество байт
|
||||
|
||||
public boolean u_exists=false; //Пользовательское поле (Для упрощения экспорта данных из базы в базу)
|
||||
public String u_type=""; //Пользовательское поле
|
||||
|
||||
public String name=""; //Название столбца
|
||||
public int type=-1; //Тип данных
|
||||
public byte[] value=null; //Запакованые данные
|
||||
@ -46,16 +53,19 @@ public class TCField
|
||||
this.type= TCField.BD_UTF8_2; //Defalt type
|
||||
//From PostgreSQL and MySQL
|
||||
if(type.equals("null")) { this.type= TCField.BD_NULL; } else
|
||||
if(type.equals("bool") || type.equals("tinyint")) { this.type= TCField.BD_UINT1; } else
|
||||
if(type.equals("bool")) { this.type= TCField.BD_UINT1_B; } else
|
||||
if(type.equals("tinyint")) { this.type= TCField.BD_UINT1; } else
|
||||
if(type.equals("int4") || type.equals("int") || type.equals("serial") || type.equals("bigint")) { this.type= TCField.BD_INT4; } else //bigint немного неправильно потому что это 64 бита но для андрод приложения не сделал
|
||||
if(type.equals("int8")) { this.type= TCField.BD_INT8; } else
|
||||
if(type.equals("float4") || type.equals("float")) { this.type= TCField.BD_FLOAT4; } else
|
||||
if(type.equals("float8") || type.equals("NUMBER")) { this.type= TCField.BD_FLOAT8; } else
|
||||
if(type.equals("varchar") || type.equals("VARCHAR2")) { this.type= TCField.BD_UTF8_2; } else
|
||||
if(type.equals("text")) { this.type= TCField.BD_UTF8_4; } else
|
||||
if(type.equals("jsonb")) { this.type= TCField.BD_UTF8_4_JSONB; } else
|
||||
if(type.equals("bytea") || type.equals("longblob")) { this.type= TCField.BD_BLOB_4; } else
|
||||
if(type.equals("timestamptz")) { this.type= TCField.BD_UTF8_1; } else
|
||||
if(type.equals("timestamp")) { this.type= TCField.BD_UTF8_1; } else
|
||||
if(type.equals("uuid")) { this.type= TCField.BD_UTF8_1_UUID; } else
|
||||
if(type.equals("timestamp")) { this.type= TCField.BD_UTF8_1_TIMESTAMP; } else
|
||||
if(type.equals("date")) { this.type= TCField.BD_UTF8_1; }
|
||||
}
|
||||
|
||||
@ -82,7 +92,7 @@ public class TCField
|
||||
//Прочитать значение из потока в соответствии с типом
|
||||
public void ReadValue(InputStream fileHandle) throws IOException
|
||||
{
|
||||
if(this.type== TCField.BD_UINT1)
|
||||
if(this.type == TCField.BD_UINT1 || this.type == TCField.BD_UINT1_B)
|
||||
{
|
||||
value=new byte[1];
|
||||
fileHandle.read(value);
|
||||
@ -132,7 +142,7 @@ public class TCField
|
||||
value=new byte[8];
|
||||
fileHandle.read(value);
|
||||
}else
|
||||
if(this.type== TCField.BD_UTF8_1 || this.type== TCField.BD_SUINT8 || this.type== TCField.BD_SINT8 || this.type== TCField.BD_SFLOAT8)
|
||||
if(this.type== TCField.BD_UTF8_1 || this.type== TCField.BD_UTF8_1_UUID || this.type== TCField.BD_UTF8_1_TIMESTAMP || this.type== TCField.BD_SUINT8 || this.type== TCField.BD_SINT8 || this.type== TCField.BD_SFLOAT8)
|
||||
{
|
||||
byte[] s=new byte[1];
|
||||
fileHandle.read(s);
|
||||
@ -153,7 +163,7 @@ public class TCField
|
||||
fileHandle.read(value);
|
||||
}
|
||||
}else
|
||||
if(this.type== TCField.BD_UTF8_4)
|
||||
if(this.type== TCField.BD_UTF8_4 || this.type== TCField.BD_UTF8_4_JSONB)
|
||||
{
|
||||
long s=Tools.readUInt(fileHandle);
|
||||
if(s==0) value=null;
|
||||
@ -183,14 +193,14 @@ public class TCField
|
||||
try
|
||||
{
|
||||
if(value==null) result = "";
|
||||
if(type== TCField.BD_UINT1) result = String.valueOf(getUByteVal());
|
||||
if(type== TCField.BD_UINT1 || type== TCField.BD_UINT1_B) result = String.valueOf(getUByteVal());
|
||||
if(type== TCField.BD_UINT4) result = String.valueOf(getUIntVal());
|
||||
if(type== TCField.BD_UINT8) result = String.valueOf(getULongVal());
|
||||
if(type== TCField.BD_INT4) result = String.valueOf(getIntVal());
|
||||
if(type== TCField.BD_INT8) result = String.valueOf(getLongVal());
|
||||
if(type== TCField.BD_FLOAT4) result = String.valueOf(getFloatVal());
|
||||
if(type== TCField.BD_FLOAT8) result = String.valueOf(getDoubleVal());
|
||||
if(type== TCField.BD_UTF8_1 || type== TCField.BD_UTF8_2 || type== TCField.BD_UTF8_4 || type== TCField.BD_SUINT8 || type== TCField.BD_SINT8 || type== TCField.BD_SFLOAT8)
|
||||
if(type== TCField.BD_UTF8_1 || type== TCField.BD_UTF8_1_UUID || type== TCField.BD_UTF8_1_TIMESTAMP || type== TCField.BD_UTF8_2 || type== TCField.BD_UTF8_4 || type== TCField.BD_UTF8_4_JSONB || type== TCField.BD_SUINT8 || type== TCField.BD_SINT8 || type== TCField.BD_SFLOAT8)
|
||||
{
|
||||
result = new String(value, "UTF8");
|
||||
}
|
||||
@ -309,7 +319,7 @@ public class TCField
|
||||
public short getUByteVal()
|
||||
{
|
||||
short val=0;
|
||||
if(type==BD_UINT1) {
|
||||
if(type==BD_UINT1 || type==BD_UINT1_B) {
|
||||
val = (short) (value[0] & 0xff);
|
||||
}else{
|
||||
String sVal = getStrVal();
|
||||
@ -401,7 +411,7 @@ public class TCField
|
||||
boolean result=true;
|
||||
if(val==null)
|
||||
{ value=null;
|
||||
}else if(type== TCField.BD_UINT1)
|
||||
}else if(type== TCField.BD_UINT1 || type== TCField.BD_UINT1_B)
|
||||
{
|
||||
if(val.equals("f") || val.equals("false")) val="0";
|
||||
else if(val.equals("t") || val.equals("true")) val="1";
|
||||
@ -481,7 +491,7 @@ public class TCField
|
||||
}else if(type== TCField.BD_FLOAT8)
|
||||
{
|
||||
setDoubleVal(Double.parseDouble(val));
|
||||
}else if(type== TCField.BD_UTF8_1 || this.type== TCField.BD_SUINT8 || this.type== TCField.BD_SINT8 || this.type== TCField.BD_SFLOAT8)
|
||||
}else if(type== TCField.BD_UTF8_1 || type== TCField.BD_UTF8_1_UUID || type== TCField.BD_UTF8_1_TIMESTAMP || this.type== TCField.BD_SUINT8 || this.type== TCField.BD_SINT8 || this.type== TCField.BD_SFLOAT8)
|
||||
{
|
||||
value=null;
|
||||
if(val!=null && !val.equals(""))
|
||||
@ -528,7 +538,7 @@ public class TCField
|
||||
}
|
||||
}
|
||||
}
|
||||
}else if(type== TCField.BD_UTF8_4)
|
||||
}else if(type== TCField.BD_UTF8_4 || type== TCField.BD_UTF8_4_JSONB)
|
||||
{
|
||||
value=null;
|
||||
if(val!=null && !val.equals(""))
|
||||
|
||||
@ -40,7 +40,7 @@ public class TCTableTools {
|
||||
}
|
||||
|
||||
//Напиши функцию: Получить ассоциативный массив название полей таблицы и их тип
|
||||
public static Map<String, String> getTableSchema(Connection connection, String schemaName, String tableName) {
|
||||
public static Map<String, String> getTableSchema(Connection connection, String tableName) {
|
||||
Map<String, String> schemaMap = new HashMap<>();
|
||||
String query = """
|
||||
SELECT c.column_name,
|
||||
@ -56,8 +56,10 @@ public class TCTableTools {
|
||||
AND c.table_name = ?
|
||||
""";
|
||||
try (PreparedStatement statement = connection.prepareStatement(query)) {
|
||||
statement.setString(1, schemaName);
|
||||
statement.setString(2, tableName);
|
||||
String schema = Tools.beforeFirst(tableName,".");
|
||||
String table = Tools.afterLast(tableName,".");
|
||||
statement.setString(1, schema);
|
||||
statement.setString(2, table);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
while (resultSet.next()) {
|
||||
String columnName = resultSet.getString("column_name");
|
||||
@ -71,4 +73,73 @@ public class TCTableTools {
|
||||
return schemaMap;
|
||||
}
|
||||
|
||||
public static String getSQLUpdate(TCTable tbl) {
|
||||
StringBuilder sql;
|
||||
sql = new StringBuilder("update " + tbl.name + " set ");
|
||||
for(int i=0;i<tbl.fields.size();i++)
|
||||
{
|
||||
if(tbl.fields.get(i).u_exists){
|
||||
if(!tbl.fields.get(i).name.equals("uid")) {
|
||||
sql.append(tbl.fields.get(i).name);
|
||||
switch (tbl.fields.get(i).type){
|
||||
case TCField.BD_UTF8_1_UUID:{
|
||||
sql.append("=main.strtouuid(?),");
|
||||
break;
|
||||
}
|
||||
case TCField.BD_UTF8_4_JSONB:{
|
||||
sql.append("=?::jsonb,");
|
||||
break;
|
||||
}
|
||||
case TCField.BD_UTF8_1_TIMESTAMP:{
|
||||
sql.append("=?::timestamp,");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
sql.append("=?,");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sql = new StringBuilder(sql.substring(0, sql.length() - 1));
|
||||
sql.append(" where uid=main.strtouuid(?)");
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
public static String getSQLInsert(TCTable tbl)
|
||||
{
|
||||
String sql="insert into "+tbl.name+"(";
|
||||
StringBuilder subSql1= new StringBuilder(" ");
|
||||
StringBuilder subSql2= new StringBuilder(" ");
|
||||
for(int i=0;i<tbl.fields.size();i++)
|
||||
{
|
||||
if(tbl.fields.get(i).u_exists){
|
||||
if(!tbl.fields.get(i).name.equals("uid")) {
|
||||
subSql1.append(tbl.fields.get(i).name);
|
||||
subSql1.append(",");
|
||||
switch (tbl.fields.get(i).type){
|
||||
case TCField.BD_UTF8_1_UUID:{
|
||||
subSql2.append("main.strtouuid(?),");
|
||||
break;
|
||||
}
|
||||
case TCField.BD_UTF8_4_JSONB:{
|
||||
subSql2.append("?::jsonb,");
|
||||
break;
|
||||
}
|
||||
case TCField.BD_UTF8_1_TIMESTAMP:{
|
||||
subSql2.append("?::timestamp,");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
subSql2.append("?,");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
subSql1.append("uid");
|
||||
subSql2.append("main.strtouuid(?)");
|
||||
sql+=subSql1+")values("+subSql2+");";
|
||||
return sql;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user