Skip to content

Commit

Permalink
Add getters that never return null in Select
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Feb 18, 2015
1 parent 295d9c3 commit 008b070
Show file tree
Hide file tree
Showing 37 changed files with 208 additions and 140 deletions.
Expand Up @@ -37,46 +37,74 @@ static class Row {
} }


@CheckForNull @CheckForNull
public Long getLong(int columnIndex) throws SQLException { public Long getNullableLong(int columnIndex) throws SQLException {
long l = rs.getLong(columnIndex); long l = rs.getLong(columnIndex);
return rs.wasNull() ? null : l; return rs.wasNull() ? null : l;
} }


public long getLong(int columnIndex) throws SQLException {
return rs.getLong(columnIndex);
}

@CheckForNull @CheckForNull
public Double getDouble(int columnIndex) throws SQLException { public Double getNullableDouble(int columnIndex) throws SQLException {
double d = rs.getDouble(columnIndex); double d = rs.getDouble(columnIndex);
return rs.wasNull() ? null : d; return rs.wasNull() ? null : d;
} }


public double getDouble(int columnIndex) throws SQLException {
return rs.getDouble(columnIndex);
}

@CheckForNull @CheckForNull
public Integer getInt(int columnIndex) throws SQLException { public Integer getNullableInt(int columnIndex) throws SQLException {
int i = rs.getInt(columnIndex); int i = rs.getInt(columnIndex);
return rs.wasNull() ? null : i; return rs.wasNull() ? null : i;
} }


public int getInt(int columnIndex) throws SQLException {
return rs.getInt(columnIndex);
}

@CheckForNull @CheckForNull
public Boolean getBoolean(int columnIndex) throws SQLException { public Boolean getNullableBoolean(int columnIndex) throws SQLException {
boolean b = rs.getBoolean(columnIndex); boolean b = rs.getBoolean(columnIndex);
return rs.wasNull() ? null : b; return rs.wasNull() ? null : b;
} }


public boolean getBoolean(int columnIndex) throws SQLException {
return rs.getBoolean(columnIndex);
}

@CheckForNull @CheckForNull
public String getString(int columnIndex) throws SQLException { public String getNullableString(int columnIndex) throws SQLException {
String s = rs.getString(columnIndex); String s = rs.getString(columnIndex);
return rs.wasNull() ? null : s; return rs.wasNull() ? null : s;
} }


public String getString(int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}

@CheckForNull @CheckForNull
public Date getDate(int columnIndex) throws SQLException { public Date getNullableDate(int columnIndex) throws SQLException {
Timestamp t = rs.getTimestamp(columnIndex); Timestamp t = rs.getTimestamp(columnIndex);
return rs.wasNull() ? null : t; return rs.wasNull() ? null : t;
} }


public Date getDate(int columnIndex) throws SQLException {
return rs.getTimestamp(columnIndex);
}

@CheckForNull @CheckForNull
public byte[] getBytes(int columnIndex) throws SQLException { public byte[] getNullableBytes(int columnIndex) throws SQLException {
byte[] b = rs.getBytes(columnIndex); byte[] b = rs.getBytes(columnIndex);
return rs.wasNull() ? null : b; return rs.wasNull() ? null : b;
} }

public byte[] getBytes(int columnIndex) throws SQLException {
return rs.getBytes(columnIndex);
}
} }


static interface RowReader<T> { static interface RowReader<T> {
Expand All @@ -89,7 +117,7 @@ private LongReader() {


@Override @Override
public Long read(Row row) throws SQLException { public Long read(Row row) throws SQLException {
return row.getLong(1); return row.getNullableLong(1);
} }
} }


Expand All @@ -101,7 +129,7 @@ private StringReader() {


@Override @Override
public String read(Row row) throws SQLException { public String read(Row row) throws SQLException {
return row.getString(1); return row.getNullableString(1);
} }
} }


Expand Down
Expand Up @@ -46,8 +46,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long issueId = row.getLong(1); Long issueId = row.getNullableLong(1);
String ruleName = row.getString(2); String ruleName = row.getNullableString(2);


update.setString(1, ruleName); update.setString(1, ruleName);
update.setLong(2, issueId); update.setLong(2, issueId);
Expand Down
Expand Up @@ -50,8 +50,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
String key = row.getString(2); String key = row.getNullableString(2);
update.setString(1, convertKey(key)); update.setString(1, convertKey(key));
update.setLong(2, id); update.setLong(2, id);
return true; return true;
Expand Down
Expand Up @@ -63,9 +63,9 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long debt = row.getLong(2); Long debt = row.getNullableLong(2);
if (debt != null) { if (debt != null) {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
update.setLong(1, workDurationConvertor.createFromLong(debt)); update.setLong(1, workDurationConvertor.createFromLong(debt));
update.setDate(2, now); update.setDate(2, now);
update.setLong(3, id); update.setLong(3, id);
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.sonar.server.db.migrations.SqlStatement; import org.sonar.server.db.migrations.SqlStatement;


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;

import java.sql.SQLException; import java.sql.SQLException;


/** /**
Expand Down Expand Up @@ -56,8 +57,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
Double value = row.getDouble(2); Double value = row.getNullableDouble(2);


update.setString(1, convertDebtForDays(value)); update.setString(1, convertDebtForDays(value));
update.setLong(2, id); update.setLong(2, id);
Expand Down
Expand Up @@ -71,8 +71,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
String changeData = row.getString(2); String changeData = row.getNullableString(2);


update.setString(1, convertChangelog(changeData)); update.setString(1, convertChangelog(changeData));
update.setDate(2, now); update.setDate(2, now);
Expand Down
Expand Up @@ -56,7 +56,7 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
update.setString(1, Issue.STATUS_CLOSED); update.setString(1, Issue.STATUS_CLOSED);
update.setString(2, Issue.RESOLUTION_REMOVED); update.setString(2, Issue.RESOLUTION_REMOVED);
update.setDate(3, now); update.setDate(3, now);
Expand Down
Expand Up @@ -49,8 +49,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
Long ruleId = row.getLong(2); Long ruleId = row.getNullableLong(2);


update.setLong(1, ruleId); update.setLong(1, ruleId);
update.setLong(2, id); update.setLong(2, id);
Expand Down
Expand Up @@ -30,6 +30,7 @@


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;

import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;


Expand Down Expand Up @@ -78,13 +79,13 @@ public void execute(Context context) throws SQLException {
private class Converter implements MassUpdate.Handler { private class Converter implements MassUpdate.Handler {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
Double value = row.getDouble(2); Double value = row.getNullableDouble(2);
Double var1 = row.getDouble(3); Double var1 = row.getNullableDouble(3);
Double var2 = row.getDouble(4); Double var2 = row.getNullableDouble(4);
Double var3 = row.getDouble(5); Double var3 = row.getNullableDouble(5);
Double var4 = row.getDouble(6); Double var4 = row.getNullableDouble(6);
Double var5 = row.getDouble(7); Double var5 = row.getNullableDouble(7);


update.setLong(1, convertDebtForDays(value)); update.setLong(1, convertDebtForDays(value));
update.setLong(2, convertDebtForDays(var1)); update.setLong(2, convertDebtForDays(var1));
Expand Down
Expand Up @@ -55,9 +55,9 @@ private void updateKeys(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
String lang = row.getString(2); String lang = row.getNullableString(2);
String name = row.getString(3); String name = row.getNullableString(3);


update.setString(1, Slug.slugify(String.format("%s %s %s", lang, name, RandomStringUtils.randomNumeric(5)))); update.setString(1, Slug.slugify(String.format("%s %s %s", lang, name, RandomStringUtils.randomNumeric(5))));
update.setLong(2, id); update.setLong(2, id);
Expand All @@ -74,8 +74,8 @@ private void updateParentKeys(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
String parentKey = row.getString(2); String parentKey = row.getNullableString(2);


update.setString(1, parentKey); update.setString(1, parentKey);
update.setLong(2, id); update.setLong(2, id);
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);


update.setDate(1, now); update.setDate(1, now);
update.setLong(2, id); update.setLong(2, id);
Expand Down
Expand Up @@ -51,8 +51,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
Long measureId = row.getLong(2); Long measureId = row.getNullableLong(2);


update.setLong(1, id); update.setLong(1, id);
update.setLong(2, measureId); update.setLong(2, measureId);
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void execute(Context context) throws SQLException {
.list(new Select.RowReader<RuleParam>() { .list(new Select.RowReader<RuleParam>() {
@Override @Override
public RuleParam read(Select.Row row) throws SQLException { public RuleParam read(Select.Row row) throws SQLException {
return new RuleParam(row.getLong(1), row.getLong(2), row.getString(3), row.getString(4)); return new RuleParam(row.getNullableLong(1), row.getNullableLong(2), row.getNullableString(3), row.getNullableString(4));
} }
}); });


Expand All @@ -63,7 +63,7 @@ public RuleParam read(Select.Row row) throws SQLException {
.list(new Select.RowReader<ActiveRule>() { .list(new Select.RowReader<ActiveRule>() {
@Override @Override
public ActiveRule read(Select.Row row) throws SQLException { public ActiveRule read(Select.Row row) throws SQLException {
return new ActiveRule(row.getLong(1), row.getLong(2)); return new ActiveRule(row.getNullableLong(1), row.getNullableLong(2));
} }
}); });


Expand Down
Expand Up @@ -49,9 +49,9 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
String csv = row.getString(2); String csv = row.getNullableString(2);
if (isUnescaped(csv)) { if (isUnescaped(csv)) {
update.setLong(1, row.getLong(1)); update.setLong(1, row.getNullableLong(1));
return true; return true;
} }
return false; return false;
Expand Down
Expand Up @@ -150,36 +150,36 @@ public FileSourceBuilder(System2 system) {


@Override @Override
public boolean handle(Row row, SqlStatement update) throws SQLException { public boolean handle(Row row, SqlStatement update) throws SQLException {
String projectUuid = row.getString(1); String projectUuid = row.getNullableString(1);
String fileUuid = row.getString(2); String fileUuid = row.getNullableString(2);
String source = StringUtils.defaultIfBlank(row.getString(3), ""); String source = StringUtils.defaultIfBlank(row.getNullableString(3), "");
Date updatedAt = row.getDate(4); Date updatedAt = row.getNullableDate(4);
byte[] shortRevisions = row.getBytes(5); byte[] shortRevisions = row.getNullableBytes(5);
byte[] longRevisions = row.getBytes(6); byte[] longRevisions = row.getNullableBytes(6);
byte[] shortAuthors = row.getBytes(7); byte[] shortAuthors = row.getNullableBytes(7);
byte[] longAuthors = row.getBytes(8); byte[] longAuthors = row.getNullableBytes(8);
byte[] shortDates = row.getBytes(9); byte[] shortDates = row.getNullableBytes(9);
byte[] longDates = row.getBytes(10); byte[] longDates = row.getNullableBytes(10);
byte[] shortUtHits = row.getBytes(11); byte[] shortUtHits = row.getNullableBytes(11);
byte[] longUtHits = row.getBytes(12); byte[] longUtHits = row.getNullableBytes(12);
byte[] shortUtCond = row.getBytes(13); byte[] shortUtCond = row.getNullableBytes(13);
byte[] longUtCond = row.getBytes(14); byte[] longUtCond = row.getNullableBytes(14);
byte[] shortUtCovCond = row.getBytes(15); byte[] shortUtCovCond = row.getNullableBytes(15);
byte[] longUtCovCond = row.getBytes(16); byte[] longUtCovCond = row.getNullableBytes(16);
byte[] shortItHits = row.getBytes(17); byte[] shortItHits = row.getNullableBytes(17);
byte[] longItHits = row.getBytes(18); byte[] longItHits = row.getNullableBytes(18);
byte[] shortItCond = row.getBytes(19); byte[] shortItCond = row.getNullableBytes(19);
byte[] longItCond = row.getBytes(20); byte[] longItCond = row.getNullableBytes(20);
byte[] shortItCovCond = row.getBytes(21); byte[] shortItCovCond = row.getNullableBytes(21);
byte[] longItCovCond = row.getBytes(22); byte[] longItCovCond = row.getNullableBytes(22);
byte[] shortOverallHits = row.getBytes(23); byte[] shortOverallHits = row.getNullableBytes(23);
byte[] longOverallHits = row.getBytes(24); byte[] longOverallHits = row.getNullableBytes(24);
byte[] shortOverallCond = row.getBytes(25); byte[] shortOverallCond = row.getNullableBytes(25);
byte[] longOverallCond = row.getBytes(26); byte[] longOverallCond = row.getNullableBytes(26);
byte[] shortOverallCovCond = row.getBytes(27); byte[] shortOverallCovCond = row.getNullableBytes(27);
byte[] longOverallCovCond = row.getBytes(28); byte[] longOverallCovCond = row.getNullableBytes(28);
byte[] shortDuplicationData = row.getBytes(29); byte[] shortDuplicationData = row.getNullableBytes(29);
byte[] longDuplicationData = row.getBytes(30); byte[] longDuplicationData = row.getNullableBytes(30);


String[] sourceData = new FileSourceDto(source, String[] sourceData = new FileSourceDto(source,
ofNullableBytes(shortRevisions, longRevisions), ofNullableBytes(shortRevisions, longRevisions),
Expand Down Expand Up @@ -235,7 +235,7 @@ public void execute(Context context) throws SQLException {
RowReader<Long> simpleLongReader = new RowReader<Long>() { RowReader<Long> simpleLongReader = new RowReader<Long>() {
@Override @Override
public Long read(Row row) throws SQLException { public Long read(Row row) throws SQLException {
Long longValue = row.getLong(1); Long longValue = row.getNullableLong(1);
return longValue == null ? Long.valueOf(0L) : longValue; return longValue == null ? Long.valueOf(0L) : longValue;
} }
}; };
Expand Down
Expand Up @@ -49,9 +49,9 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
Date createdAt = row.getDate(2); Date createdAt = row.getNullableDate(2);
Date updatedAt = row.getDate(3); Date updatedAt = row.getNullableDate(3);


if (createdAt == null) { if (createdAt == null) {
update.setLong(1, now); update.setLong(1, now);
Expand Down
Expand Up @@ -52,8 +52,8 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
update.setDate(1, row.getDate(2)); update.setDate(1, row.getNullableDate(2));
update.setLong(2, row.getLong(1)); update.setLong(2, row.getNullableLong(1));
return true; return true;
} }
}); });
Expand Down
Expand Up @@ -54,7 +54,7 @@ public void execute(Context context) throws SQLException {
massUpdate.execute(new MassUpdate.Handler() { massUpdate.execute(new MassUpdate.Handler() {
@Override @Override
public boolean handle(Select.Row row, SqlStatement update) throws SQLException { public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
Long id = row.getLong(1); Long id = row.getNullableLong(1);
update.setLong(1, now); update.setLong(1, now);
update.setLong(2, id); update.setLong(2, id);
return true; return true;
Expand Down

0 comments on commit 008b070

Please sign in to comment.