|
@@ -7,12 +7,16 @@ import org.dbsyncer.connector.constant.DatabaseConstant;
|
|
|
import org.dbsyncer.connector.database.AbstractDatabaseConnector;
|
|
|
import org.dbsyncer.connector.model.PageSql;
|
|
|
import org.dbsyncer.connector.util.PrimaryKeyUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public final class MysqlConnector extends AbstractDatabaseConnector {
|
|
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
+
|
|
|
@Override
|
|
|
public String buildSqlWithQuotation() {
|
|
|
return "`";
|
|
@@ -37,6 +41,12 @@ public final class MysqlConnector extends AbstractDatabaseConnector {
|
|
|
final SqlBuilderConfig sqlBuilderConfig = config.getSqlBuilderConfig();
|
|
|
Map<String, Integer> typeAliases = PrimaryKeyUtil.findPrimaryKeyType(sqlBuilderConfig.getFields());
|
|
|
|
|
|
+ // 不支持游标查询
|
|
|
+ if (!PrimaryKeyUtil.isSupportedCursor(typeAliases, primaryKeys)) {
|
|
|
+ logger.warn("不支持游标查询,主键包含非数字类型");
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
// select * from test.`my_user` where `id` > ? and `uid` > ? order by `id`,`uid` limit ?,?
|
|
|
StringBuilder sql = new StringBuilder(config.getQuerySql());
|
|
|
boolean skipFirst = false;
|
|
@@ -45,7 +55,7 @@ public final class MysqlConnector extends AbstractDatabaseConnector {
|
|
|
skipFirst = true;
|
|
|
sql.append(" WHERE ");
|
|
|
}
|
|
|
- PrimaryKeyUtil.buildCursorSql(sql, primaryKeys, quotation, " AND ", typeAliases, skipFirst);
|
|
|
+ PrimaryKeyUtil.buildSql(sql, primaryKeys, quotation, " AND ", " > ? ", skipFirst);
|
|
|
sql.append(" ORDER BY ");
|
|
|
PrimaryKeyUtil.buildSql(sql, primaryKeys, quotation, ",", "", true);
|
|
|
sql.append(DatabaseConstant.MYSQL_PAGE_SQL);
|
|
@@ -55,16 +65,22 @@ public final class MysqlConnector extends AbstractDatabaseConnector {
|
|
|
@Override
|
|
|
public Object[] getPageArgs(ReaderConfig config) {
|
|
|
int pageSize = config.getPageSize();
|
|
|
- Object[] cursors = config.getCursors();
|
|
|
- if (null == cursors) {
|
|
|
- return new Object[]{0, pageSize};
|
|
|
+ // 通过游标分页
|
|
|
+ if (config.isSupportedCursor()) {
|
|
|
+ Object[] cursors = config.getCursors();
|
|
|
+ if (null == cursors) {
|
|
|
+ return new Object[]{0, pageSize};
|
|
|
+ }
|
|
|
+ int cursorsLen = cursors.length;
|
|
|
+ Object[] newCursors = new Object[cursorsLen + 2];
|
|
|
+ System.arraycopy(cursors, 0, newCursors, 0, cursorsLen);
|
|
|
+ newCursors[cursorsLen] = 0;
|
|
|
+ newCursors[cursorsLen + 1] = pageSize;
|
|
|
+ return newCursors;
|
|
|
}
|
|
|
- int cursorsLen = cursors.length;
|
|
|
- Object[] newCursors = new Object[cursorsLen + 2];
|
|
|
- System.arraycopy(cursors, 0, newCursors, 0, cursorsLen);
|
|
|
- newCursors[cursorsLen] = 0;
|
|
|
- newCursors[cursorsLen + 1] = pageSize;
|
|
|
- return newCursors;
|
|
|
+
|
|
|
+ // 普通分页
|
|
|
+ return new Object[]{(config.getPageIndex() - 1) * pageSize, pageSize};
|
|
|
}
|
|
|
|
|
|
@Override
|