@Override public boolean doDelete(ColumnTypeHelper helper, DeleteRowsEvent event) { boolean need = false; String[] sqls = null; int index = 0; doBeforeDelete(helper, event); for (int i = 0; i < tables.size(); i++) { RDBSchnauzer rep = (RDBSchnauzer) tables.get(i); if (notEqualS(helper.databaseName, masterDb.dbname)) continue; if (notEqualS(helper.tableName, rep.getMasterTableName())) continue; List<Row> rows = event.getRows(); for (int j = 0; j < rows.size(); j++) { Row row = rows.get(j); List<Column> columns = row.getColumns(); if (sqls == null) { sqls = new String[rows.size() * tables.size() + 1]; sqls[0] = getStatusUpdateSql(helper); } need = true; try { sqls[++index] = rep.getDelete() + getWhereStr(rep, columns, helper); } catch (Exception e) { mailAndLog(Infos.Create + Infos.DoDelete + Infos.Failed, e.getMessage()); return false; } } } String[] errInfo = new String[1]; if (!need || !logSql(sqls)) { autoSetPosition(helper); return true; } if (dbhelper.excuteSqlByTransaction(sqls, errInfo)) { slaveDb.binlog = this.getBinLogName(helper); slaveDb.pos = helper.position; slaveDb.tablepos = helper.tableMapPos; LOGGER.info(Infos.DoDelete + Infos.OK); return true; } String sInfo = getWarning(sqls, errInfo); mailAndLog(Infos.DoDelete + Infos.Failed, sInfo); rollBackToTableEvent(helper); return false; }
@Override public boolean doUpdate(ColumnTypeHelper helper, UpdateRowsEvent event) { String preSql = ""; String aftSql = ""; boolean doOne = false; String[] sqls = null; int sqlsIndex = 0; doBeforeUpdate(helper, event); for (int i = 0; i < tables.size(); i++) { RDBSchnauzer rep = (RDBSchnauzer) tables.get(i); if (notEqualS(helper.databaseName, this.masterDb.dbname)) continue; if (notEqualS(helper.tableName, rep.getMasterTableName())) continue; List<Pair<Row>> rows = event.getRows(); String[] tableSqls = new String[rows.size() + 1]; int pos = 1; byte[] unsigntags = rep.getUnsignedTags(); for (int j = 0; j < rows.size(); j++) { String sql = "Update " + rep.getSlaveTableName() + " set "; Pair<Row> pRow = rows.get(j); Row row = pRow.getAfter(); Row preRow = pRow.getBefore(); List<Column> columns = row.getColumns(); List<Column> pcolumns = preRow.getColumns(); if (!rep.needReplicate(columns)) continue; try { preSql = helper.getUpdataStr(pcolumns, rep.getColumnNames(), unsigntags, rep.getFullFields()); aftSql = helper.getUpdataStr(columns, rep.getColumnNames(), unsigntags, rep.getFullFields()); } catch (Exception e) { mailAndLog(Infos.Create + Infos.DoUpdate + Infos.Failed, e.getMessage()); return false; } if (isEqualS(preSql, aftSql)) continue; sql += aftSql; try { sql += " where " + getWhereStr(rep, columns, helper); } catch (Exception e) { mailAndLog(Infos.Create + Infos.DoUpdate + Infos.Failed, e.getMessage()); return false; } tableSqls[pos++] = sql; doOne = true; } if (sqls == null) { sqls = new String[tables.size() * rows.size() + 1]; for (int k = 0; k < sqls.length; k++) sqls[k] = ""; sqls[0] = getStatusUpdateSql(helper); } int sIndex = 0; for (int j = 0; j < tableSqls.length; j++) { sIndex = (sqlsIndex + 1); if ((tableSqls[j] == null) || (tableSqls[j].isEmpty())) continue; if ((sqls[sIndex] != null) && (!sqls[sIndex].isEmpty())) sqls[i + 1] += ";"; sqls[++sqlsIndex] += tableSqls[j]; } } String[] errInfo = new String[1]; if (!doOne || !logSql(sqls)) { autoSetPosition(helper); return true; } if (dbhelper.excuteSqlByTransaction(sqls, errInfo)) { this.slaveDb.binlog = this.getBinLogName(helper); this.slaveDb.pos = helper.position; this.slaveDb.tablepos = helper.tableMapPos; LOGGER.info(Infos.DoUpdate + Infos.OK); return true; } String sInfo = getWarning(sqls, errInfo); mailAndLog(Infos.DoUpdate + Infos.Failed, sInfo); this.slaveDb.errorMsg = "Update command faild"; HeartBeatInfo hinfo = new HeartBeatInfo(); ConfigGetHelper conf = new ConfigGetHelper(Integer.toString(this.slaveDb.masterID)); conf.getHeartBeatSet(hinfo); if (hinfo.port > 0 && !hinfo.host.isEmpty()) { LOGGER.info(Infos.SendErrorInfo); LocalInfo info = LocalInfoGetter.getLocalInfo(); hinfo.SerialNo = info.getSNStr(); HeartBeatSender beatSender = new HeartBeatSender(this.masterDb, this.slaveDb, hinfo); beatSender.sendError(); } rollBackToTableEvent(helper); return false; }