Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDEMPIERE-6133 : Support Export Blob Column for Export SQL Insert Scr… #2342

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions migration/iD12/oracle/202405080546_IDEMPIERE-6133.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SET SQLBLANKLINES ON
SET DEFINE OFF

-- May 8, 2024, 5:46:45 AM WIB
INSERT INTO AD_SysConfig (AD_SysConfig_ID,AD_Client_ID,AD_Org_ID,Created,Updated,CreatedBy,UpdatedBy,IsActive,Name,Value,Description,EntityType,ConfigurationLevel,AD_SysConfig_UU) VALUES (200246,0,0,TO_TIMESTAMP('2024-05-08 05:46:45','YYYY-MM-DD HH24:MI:SS'),TO_TIMESTAMP('2024-05-08 05:46:45','YYYY-MM-DD HH24:MI:SS'),100,100,'Y','EXPORT_BLOB_COLUMN_FOR_INSERT','Y','include blob column when export record using sql insert script option','D','C','c63dc8f1-c098-4f93-b84a-0897c0cf9c6a')
;

-- IDEMPIERE-6133
SELECT register_migration_script('202405080546_IDEMPIERE-6133.sql') FROM dual;
6 changes: 6 additions & 0 deletions migration/iD12/postgresql/202405080546_IDEMPIERE-6133.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- May 8, 2024, 5:46:45 AM WIB
INSERT INTO AD_SysConfig (AD_SysConfig_ID,AD_Client_ID,AD_Org_ID,Created,Updated,CreatedBy,UpdatedBy,IsActive,Name,Value,Description,EntityType,ConfigurationLevel,AD_SysConfig_UU) VALUES (200246,0,0,TO_TIMESTAMP('2024-05-08 05:46:45','YYYY-MM-DD HH24:MI:SS'),TO_TIMESTAMP('2024-05-08 05:46:45','YYYY-MM-DD HH24:MI:SS'),100,100,'Y','EXPORT_BLOB_COLUMN_FOR_INSERT','Y','include blob column when export record using sql insert script option','D','C','c63dc8f1-c098-4f93-b84a-0897c0cf9c6a')
;

-- IDEMPIERE-6133
SELECT register_migration_script('202405080546_IDEMPIERE-6133.sql') FROM dual;
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,8 @@ public void export(GridTab gridTab, List<GridTab> childs, boolean isCurrentRowOn
* @param pgs list to add postgresql insert script
*/
protected void addSQLInsert(PO po, List<String> oracles, List<String> pgs) {
String sql = po.toInsertSQL();
String oracle = Database.getDatabase(Database.DB_ORACLE).convertStatement(sql);
String pg = Database.getDatabase(Database.DB_POSTGRESQL).convertStatement(sql);
String oracle = Database.getDatabase(Database.DB_ORACLE).convertStatement(po.toInsertSQL(Database.DB_ORACLE));
String pg = Database.getDatabase(Database.DB_POSTGRESQL).convertStatement(po.toInsertSQL(Database.DB_POSTGRESQL));
oracles.add(oracle);
pgs.add(pg);
}
Expand Down
7 changes: 7 additions & 0 deletions org.adempiere.base/src/org/compiere/db/AdempiereDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ public default String getSQLCreate(MTable table)
return sb.toString();
} // getSQLCreate

/**
* Convert blob to hex encoded string and return SQL function that will convert the hex encoded string back to blob
* @param hexString
uthadehikaru marked this conversation as resolved.
Show resolved Hide resolved
* @return SQL statement
*/
public String TO_Blob(byte[] blob);

/**
* @param column
* @return DDL SQL statement for column
Expand Down
1 change: 1 addition & 0 deletions org.adempiere.base/src/org/compiere/model/MSysConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class MSysConfig extends X_AD_SysConfig
public static final String ENABLE_SSO = "ENABLE_SSO";
public static final String ENABLE_SSO_OSGI_CONSOLE = "ENABLE_SSO_OSGI_CONSOLE";
public static final String ENABLE_SSO_IDEMPIERE_MONITOR = "ENABLE_SSO_IDEMPIERE_MONITOR";
public static final String EXPORT_BLOB_COLUMN_FOR_INSERT = "EXPORT_BLOB_COLUMN_FOR_INSERT";
public static final String FEEDBACK_EMAIL_CC = "FEEDBACK_EMAIL_CC";
public static final String FEEDBACK_EMAIL_TO = "FEEDBACK_EMAIL_TO";
public static final String FORCE_POSTING_PRIOR_TO_PERIOD_CLOSE = "FORCE_POSTING_PRIOR_TO_PERIOD_CLOSE";
Expand Down
28 changes: 16 additions & 12 deletions org.adempiere.base/src/org/compiere/model/PO.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.compiere.Adempiere;
import org.compiere.acct.Doc;
import org.compiere.db.AdempiereDatabase;
import org.compiere.db.Database;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.CCache;
import org.compiere.util.CLogMgt;
Expand Down Expand Up @@ -3464,7 +3465,7 @@ private boolean doInsert(boolean withValues) {

// SQL
StringBuilder sqlInsert = new StringBuilder();
AD_ChangeLog_ID = buildInsertSQL(sqlInsert, withValues, params, session, AD_ChangeLog_ID, false);
AD_ChangeLog_ID = buildInsertSQL(sqlInsert, withValues, params, session, AD_ChangeLog_ID, false, null);
//
int no = withValues ? DB.executeUpdate(sqlInsert.toString(), m_trxName)
: DB.executeUpdate(sqlInsert.toString(), params.toArray(), false, m_trxName);
Expand Down Expand Up @@ -3533,12 +3534,13 @@ private boolean doInsert(boolean withValues) {

/**
* Export data as insert SQL statement
* @param database
* @return SQL insert statement
*/
public String toInsertSQL()
public String toInsertSQL(String database)
{
StringBuilder sqlInsert = new StringBuilder();
buildInsertSQL(sqlInsert, true, null, null, 0, true);
buildInsertSQL(sqlInsert, true, null, null, 0, true, database);
return sqlInsert.toString();
}

Expand All @@ -3553,7 +3555,7 @@ public String toInsertSQL()
* @return last AD_ChangeLog_ID
*/
protected int buildInsertSQL(StringBuilder sqlInsert, boolean withValues, List<Object> params, MSession session,
int AD_ChangeLog_ID, boolean generateScriptOnly) {
int AD_ChangeLog_ID, boolean generateScriptOnly, String database) {
sqlInsert.append("INSERT INTO ");
sqlInsert.append(p_info.getTableName()).append(" (");
StringBuilder sqlValues = new StringBuilder(") VALUES (");
Expand All @@ -3572,8 +3574,6 @@ protected int buildInsertSQL(StringBuilder sqlInsert, boolean withValues, List<O
if (DisplayType.isLOB(dt))
{
lobAdd (value, i, dt);
if (!p_info.isColumnMandatory(i))
continue;
}

//do not export secure column
Expand Down Expand Up @@ -3682,14 +3682,18 @@ else if (c == String.class)
sqlValues.append (encrypt(i,DB.TO_STRING ((String)value)));
else if (DisplayType.isLOB(dt))
{
if (p_info.isColumnMandatory(i))
{
sqlValues.append("''"); // no db dependent stuff here -- at this point value is known to be not null
}
else
if(database!=null && MSysConfig.getBooleanValue(MSysConfig.EXPORT_BLOB_COLUMN_FOR_INSERT, true, getAD_Client_ID()))
{
sqlValues.append("null");
sqlValues.append (Database.getDatabase(database).TO_Blob((byte[]) value));
}
else if (p_info.isColumnMandatory(i))
{
sqlValues.append("''"); // no db dependent stuff here -- at this point value is known to be not null
}
else
{
sqlValues.append("null");
}
}
else
sqlValues.append (saveNewSpecial (value, i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HexFormat;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -369,6 +370,7 @@ public String getStatus()
*/
public String convertStatement (String oraStatement)
{

Convert.logMigrationScript(oraStatement, null);
if (SystemProperties.isDBDebug()) {
String filterOrDebug = SystemProperties.getDBDebugFilter();
Expand Down Expand Up @@ -1216,4 +1218,9 @@ public boolean isQueryTimeout(SQLException ex) {
public ITablePartitionService getTablePartitionService() {
return new TablePartitionService();
}

@Override
public String TO_Blob(byte[] blob) {
return "HEXTORAW('"+HexFormat.of().formatHex(blob)+"')";
}
} // DB_Oracle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -1405,4 +1406,9 @@ public boolean isQueryTimeout(SQLException ex) {
public ITablePartitionService getTablePartitionService() {
return new TablePartitionService();
}

@Override
public String TO_Blob(byte[] blob) {
return "decode('"+HexFormat.of().formatHex(blob)+"','hex')";
}
} // DB_PostgreSQL