use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.
the class QueryEditorPart method deleteResult.
/**
* Delete data through data OID
*
* @param strOid String[]
* @throws SQLException if failed
*/
public void deleteResult(String[] strOid) throws SQLException {
if (strOid == null || strOid.length <= 0)
return;
connection.checkAndConnectQuietly();
for (int i = 0; i < strOid.length; i++) {
CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) connection.getConnection(), strOid[i]).remove();
}
if (isAutocommit) {
queryAction(QUERY_ACTION.COMMIT);
}
setHaveActiveTransaction(true);
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.
the class DBAttrTypeFormatter method formatMuchValue.
/**
* Format much value to JDBC object, this value probably is from file, if it
* is file, convert it for JDBC object according to JDBC type
*
* @param str The much value
* @param type The JDBC type
* @param conn The Connection
* @param dbCharSet The database charset
* @param fileCharSet The file charset
* @param isUseNULLValueSetting
* @return the real JDBC object
*/
public static Object formatMuchValue(String str, String type, Connection conn, String dbCharSet, String fileCharSet, boolean isUseNULLValueSetting) {
String upperType = type.trim().toUpperCase();
boolean isString = upperType.startsWith(DataType.DATATYPE_VARCHAR) || upperType.startsWith(DataType.DATATYPE_CHAR) || upperType.startsWith(DataType.DATATYPE_STRING);
boolean isByte = upperType.startsWith(DataType.DATATYPE_BIT_VARYING) || upperType.startsWith(DataType.DATATYPE_BIT) || upperType.startsWith(DataType.DATATYPE_NCHAR) || upperType.startsWith(DataType.DATATYPE_NCHAR_VARYING);
int size = 0;
if (isString || isByte) {
size = DataType.getSize(type);
}
Object realObj = null;
String errorMsg = null;
File file = new File(str.replaceFirst(FILE_URL_PREFIX, ""));
if (upperType.startsWith(DataType.DATATYPE_BLOB)) {
try {
CUBRIDBlobProxy blob = new CUBRIDBlobProxy((CUBRIDConnectionProxy) conn);
if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
InputStream fin = null;
OutputStream out = null;
try {
fin = new FileInputStream(file);
out = blob.setBinaryStream(1);
byte[] data = new byte[512];
int count = -1;
while ((count = fin.read(data)) != -1) {
out.write(data, 0, count);
}
realObj = blob.getProxyObj();
} catch (IOException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} finally {
try {
fin.close();
} catch (IOException e) {
LOGGER.error("", e);
}
try {
out.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
} else {
try {
byte[] byteArr = str.getBytes(dbCharSet);
blob.setBytes(1, byteArr);
realObj = blob.getProxyObj();
} catch (UnsupportedEncodingException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
}
}
} catch (CUBRIDProxyException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} catch (SQLException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
}
} else if (upperType.startsWith(DataType.DATATYPE_CLOB)) {
try {
CUBRIDClobProxy clob = new CUBRIDClobProxy((CUBRIDConnectionProxy) conn, dbCharSet);
if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
BufferedReader reader = null;
Writer writer = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
writer = clob.setCharacterStream(1);
char[] charArr = new char[512];
int count = reader.read(charArr);
while (count > 0) {
writer.write(charArr, 0, count);
count = reader.read(charArr);
}
} catch (IOException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} finally {
try {
reader.close();
} catch (IOException e) {
LOGGER.error("", e);
}
try {
writer.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
} else {
clob.setString(1, str);
}
realObj = clob.getProxyObj();
} catch (CUBRIDProxyException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} catch (SQLException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
}
} else if (isString) {
if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
StringBuffer strBuffer = new StringBuffer();
char[] charArr = new char[512];
int count = reader.read(charArr);
int totalCount = count;
while (count > 0) {
if (count == 512) {
strBuffer.append(charArr);
} else {
char[] tmpChar = new char[count];
System.arraycopy(charArr, 0, tmpChar, 0, count);
strBuffer.append(tmpChar);
}
count = reader.read(charArr);
totalCount += count;
}
if (totalCount > size) {
errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
} else {
realObj = strBuffer.toString();
}
} catch (IOException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} finally {
try {
reader.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
} else {
realObj = str;
}
} else if (isByte) {
if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
InputStream fin = null;
try {
fin = new FileInputStream(file);
int length = fin.available();
if (length * 8 > size) {
errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
} else {
byte[] data = new byte[length];
if (fin.read(data) != -1) {
realObj = data;
}
}
} catch (IOException e) {
errorMsg = e.getMessage();
LOGGER.error("", e);
} finally {
try {
fin.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
} else {
FormatDataResult result = DBAttrTypeFormatter.format(type, str, false, dbCharSet, isUseNULLValueSetting);
byte[] byteArr = (byte[]) result.getFormatedJavaObj();
realObj = byteArr;
}
}
if (errorMsg != null) {
realObj = new Exception(errorMsg);
}
return realObj;
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.
the class ConnectDatabaseExecutor method internalConnect.
private boolean internalConnect(String brokerIP, String brokerPort, String dbName, String userName, String password, String charset, String jdbcAttrs, String driverVersion, boolean autoCommit, boolean isShard) {
Display display = Display.getDefault();
CUBRIDConnectionProxy connection = null;
try {
connection = (CUBRIDConnectionProxy) JDBCConnectionManager.getConnection(brokerIP, brokerPort, dbName, userName, password, charset, jdbcAttrs, driverVersion, autoCommit, isShard);
if (connection == null) {
display.syncExec(new Runnable() {
public void run() {
CommonUITool.openErrorBox(Messages.msgConnectBrokerFailure);
}
});
return false;
}
String jdbcVersion = connection.getJdbcVersion();
jdbcVersion = jdbcVersion == null ? "" : jdbcVersion.replaceAll("CUBRID-JDBC-", "");
if (jdbcVersion.lastIndexOf(".") > 0) {
jdbcVersion = jdbcVersion.substring(0, jdbcVersion.lastIndexOf("."));
}
String dbVersion = connection.getMetaData().getDatabaseProductVersion();
dbVersion = dbVersion == null ? "" : dbVersion;
String minorDbVersion = dbVersion;
if (minorDbVersion.lastIndexOf(".") > 0) {
minorDbVersion = minorDbVersion.substring(0, minorDbVersion.lastIndexOf("."));
}
if (CompatibleUtil.compareVersion(jdbcVersion, minorDbVersion) == 0) {
return true;
} else {
final String finalDbVersion = dbVersion;
display.syncExec(new Runnable() {
public void run() {
isConnectSuccess = CommonUITool.openConfirmBox(Messages.bind(Messages.tipNoSupportJdbcVersion, finalDbVersion));
}
});
return isConnectSuccess;
}
} catch (final SQLException e) {
LOGGER.error(e.getMessage(), e);
display.syncExec(new Runnable() {
public void run() {
CommonUITool.openErrorBox(Messages.bind(Messages.errCommonTip, e.getErrorCode(), e.getMessage()));
}
});
return false;
} catch (final Exception e) {
display.syncExec(new Runnable() {
public void run() {
CommonUITool.openErrorBox(e.getMessage());
}
});
return false;
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
LOGGER.error(e.getMessage());
}
}
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.
the class CubridWorkbenchContrItem method connectDatabaseWithErrMsg.
/**
*
* Connect the database
*
* @param dbInfo DatabaseInfo
* @return error messages String
*/
public static String connectDatabaseWithErrMsg(DatabaseInfo dbInfo) {
// FIXME extract
if (dbInfo == null || dbInfo.getServerInfo() == null) {
return "";
}
Map<String, String> jdbcVersionMap = CubridJdbcManager.getInstance().getLoadedJdbc();
if (jdbcVersionMap == null || jdbcVersionMap.get(dbInfo.getServerInfo().getJdbcDriverVersion()) == null) {
return Messages.errNoSupportDriver;
}
CUBRIDConnectionProxy connection = null;
try {
connection = (CUBRIDConnectionProxy) JDBCConnectionManager.getConnection(dbInfo, false);
DbUserInfo userInfo = dbInfo.getAuthLoginedDbUserInfo();
IsDBAUserTask checkTask = new IsDBAUserTask(dbInfo);
checkTask.execute();
userInfo.setDbaAuthority(checkTask.isDBAUser());
dbInfo.setLogined(true);
dbInfo.setRunningType(DbRunningType.CS);
dbInfo.getServerInfo().setConnected(true);
dbInfo.setLogined(true);
return null;
} catch (SQLException e) {
return Messages.bind(Messages.errCommonTip, e.getErrorCode(), e.getMessage());
} catch (Exception e) {
return e.getMessage();
} finally {
if (connection != null) {
try {
connection.close();
connection = null;
} catch (SQLException e) {
connection = null;
}
}
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.
the class RowDetailDialog method export.
/**
* Export data to file
*
* @param filePath String
* @param oidStr String
* @param value String
* @param fileCharset String
* @throws IOException The exception
* @throws SQLException The exception
*/
private void export(final String filePath, String oidStr, String value, String fileCharset) throws IOException, SQLException {
// FIXME move this logic to core module
OutputStream fs = null;
Writer writer = null;
InputStream in = null;
Reader reader = null;
ResultSet rs = null;
try {
ColumnInfo columnInfo = columnInfoList.get(selComboIndex);
String type = columnInfo.getType();
String completedType = columnInfo.getComleteType();
String colName = columnInfo.getName();
if (DataType.DATATYPE_BLOB.equals(type) && DataType.BLOB_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
Blob blob = rs.getBlob(colName);
in = blob.getBinaryStream();
fs = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bArr = new byte[512];
int count = in.read(bArr);
while (count > 0) {
fs.write(bArr, 0, count);
count = in.read(bArr);
}
fs.flush();
} else {
if (DataType.DATATYPE_CLOB.equals(type) && DataType.CLOB_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
Clob clob = rs.getClob(colName);
reader = clob.getCharacterStream();
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), fileCharset));
char[] charArr = new char[512];
int count = reader.read(charArr);
while (count > 0) {
writer.write(charArr, 0, count);
count = reader.read(charArr);
}
writer.flush();
} else if (DataType.DATATYPE_BIT.equals(type) || DataType.DATATYPE_BIT_VARYING.equals(type)) {
byte[] bArr = null;
if (DataType.BIT_EXPORT_FORMAT.equals(value)) {
CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
rs = oidPxory.getValues(new String[] { colName });
rs.next();
bArr = rs.getBytes(colName);
} else {
FormatDataResult result = new FormatDataResult();
DBAttrTypeFormatter.formatBit(completedType, value, result, dbCharset);
bArr = (byte[]) result.getFormatedJavaObj();
}
fs = new BufferedOutputStream(new FileOutputStream(filePath));
fs.write(bArr);
fs.flush();
} else {
byte[] bArr = value.getBytes(fileCharset);
fs = new BufferedOutputStream(new FileOutputStream(filePath));
fs.write(bArr);
fs.flush();
}
}
} finally {
if (fs != null) {
try {
fs.close();
fs = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (writer != null) {
try {
writer.close();
writer = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException e) {
LOGGER.error("", e);
}
}
if (in != null) {
try {
in.close();
in = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
}
Aggregations