Skip to content

Commit e699ab8

Browse files
committed
序号从1开始
1 parent 8839dde commit e699ab8

6 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/main/java/io/sqlman/SqlScript.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public interface SqlScript {
2020

2121
/**
2222
* 获取该脚本指定序号的SQL语句
23+
* 序号从{@code 1}开始
2324
*
2425
* @param ordinal SQL语句序号
2526
* @return 指定序号的SQL语句

src/main/java/io/sqlman/SqlVersionManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface SqlVersionManager {
2424
void upgrade() throws SQLException;
2525

2626
/**
27-
* 执行指定SQL脚本的升级
27+
* 执行指定SQL脚本的升级
2828
*
2929
* @param script 指定脚本
3030
* @throws SQLException SQL异常
@@ -33,7 +33,7 @@ public interface SqlVersionManager {
3333
void upgrade(SqlScript script) throws SQLException;
3434

3535
/**
36-
* 执行指定SQL脚本指定语句序号的升级
36+
* 执行指定SQL脚本指定语句序号的升级,语句序号从{@code 1}开始
3737
*
3838
* @param script 指定脚本
3939
* @param ordinal 指定语句序号
@@ -45,7 +45,7 @@ public interface SqlVersionManager {
4545

4646
/**
4747
* 获取所有SQL脚本
48-
* 实现类返回的结果必须遵循脚本版本的先后顺序
48+
* 实现类返回的结果必须遵循脚本版本的先后顺序
4949
* 实现类当发现脚本资源中包含重复的SQL脚本版本时应当抛出
5050
*
5151
* @return 所有SQL脚本

src/main/java/io/sqlman/script/DruidScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public int sqls() {
3939

4040
@Override
4141
public SqlSentence sentence(int ordinal) {
42-
return sentences.get(ordinal);
42+
return sentences.get(ordinal - 1);
4343
}
4444

4545
@Override

src/main/java/io/sqlman/script/DruidScriptResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public SqlScript resolve(SqlSource source) throws IncorrectSyntaxException, IOEx
4949
String text = SqlUtils.stringify(in, charset);
5050
List<SQLStatement> statements = SQLUtils.parseStatements(text, dialect.toLowerCase());
5151
List<SqlSentence> sentences = new ArrayList<>(statements.size());
52-
for (int ordinal = 0; ordinal < statements.size(); ordinal++) {
53-
SQLStatement statement = statements.get(ordinal);
52+
for (int index = 0; index < statements.size(); index++) {
53+
SQLStatement statement = statements.get(index);
5454
String sql = statement.toString();
5555
sql = sql.trim();
5656
String suffix = ";";
5757
while (sql.endsWith(suffix)) {
5858
sql = sql.substring(0, sql.length() - 1);
5959
}
60-
SqlSentence sentence = new DruidSentence(ordinal, sql);
60+
SqlSentence sentence = new DruidSentence(index + 1, sql);
6161
sentences.add(sentence);
6262
}
6363
return new DruidScript(source.name(), source.version(), source.parameters(), source.description(), sentences);

src/main/java/io/sqlman/script/DruidSentence.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class DruidSentence implements SqlSentence {
1313
private final String value;
1414

1515
public DruidSentence(int ordinal, String value) {
16-
if (ordinal < 0) {
17-
throw new IllegalArgumentException("ordinal must not be negative");
16+
if (ordinal < 1) {
17+
throw new IllegalArgumentException("ordinal must not lesser than 1");
1818
}
1919
if (value == null || value.trim().isEmpty()) {
2020
throw new IllegalArgumentException("value must not be null or blank string");

src/main/java/io/sqlman/version/JdbcVersionManager.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public void upgrade() throws SQLException {
5151
logger.info("Schema current version is {}", current);
5252

5353
String version = current != null ? current.getVersion() : null;
54-
int ordinal = current != null ? current.getSuccess() ? current.getOrdinal() + 1 : current.getOrdinal() : 0;
55-
boolean included = current == null || !current.getSuccess() || ordinal < current.getSqlQuantity() - 1;
54+
int ordinal = current != null ? current.getSuccess() ? current.getOrdinal() + 1 : current.getOrdinal() : 1;
55+
boolean included = current == null || !current.getSuccess() || ordinal < current.getSqlQuantity();
5656
Enumeration<SqlSource> sources = current != null ? acquire(version, included) : acquire();
5757
if (!included) {
58-
ordinal = 0;
58+
ordinal = 1;
5959
}
6060

6161
if (sources.hasMoreElements()) {
@@ -65,14 +65,14 @@ public void upgrade() throws SQLException {
6565
while (sources.hasMoreElements()) {
6666
SqlSource source = sources.nextElement();
6767
SqlScript script = resolve(source);
68-
if (ordinal == 0) {
68+
if (ordinal == 1) {
6969
upgrade(script);
7070
} else {
7171
int sqls = script.sqls();
72-
for (int index = ordinal; index < sqls; index++) {
72+
for (int index = ordinal; index <= sqls; index++) {
7373
upgrade(script, index);
7474
}
75-
ordinal = 0;
75+
ordinal = 1;
7676
}
7777
}
7878

@@ -102,7 +102,7 @@ public void upgrade(final SqlScript script) throws SQLException {
102102
@Override
103103
public void perform(Connection connection) throws SQLException {
104104
int sqls = script.sqls();
105-
for (int ordinal = 0; ordinal < sqls; ordinal++) {
105+
for (int ordinal = 1; ordinal <= sqls; ordinal++) {
106106
upgrade(script, ordinal);
107107
}
108108
}
@@ -112,7 +112,7 @@ public void perform(Connection connection) throws SQLException {
112112
else {
113113
logger.info("Executing script {} non-atomically", script.name());
114114
int sqls = script.sqls();
115-
for (int ordinal = 0; ordinal < sqls; ordinal++) {
115+
for (int ordinal = 1; ordinal <= sqls; ordinal++) {
116116
upgrade(script, ordinal);
117117
}
118118
}
@@ -150,7 +150,7 @@ public Integer execute(Connection connection) throws SQLException {
150150
SqlSentence sentence = script.sentence(ordinal);
151151
String sql = sentence.value();
152152

153-
logger.info("Executing sentence {}/{} of script version {} in {} transaction isolation level : {}", ordinal + 1, script.sqls(), script.version(), isolation, sql.replaceAll("\\s+", " "));
153+
logger.info("Executing sentence {}/{} of script version {} in {} transaction isolation level : {}", ordinal, script.sqls(), script.version(), isolation, sql.replaceAll("\\s+", " "));
154154

155155
PreparedStatement statement = connection.prepareStatement(sql);
156156
int rows = statement.executeUpdate();

0 commit comments

Comments
 (0)