Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbae committed Jun 27, 2018
1 parent bf880f1 commit 02ef8d8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ boolean passDataAsHex(int colNum) {
|| JDBCType.LONGVARBINARY == getColumn(colNum).getJdbctype());
}

private static String byteArrayToHex(byte[] a) {
private String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for(byte b: a)
sb.append(String.format("%02x", b));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class SqlDateTime extends SqlType {
Expand Down Expand Up @@ -41,4 +42,22 @@ public SqlDateTime() {
public Object createdata() {
return new Timestamp(ThreadLocalRandom.current().nextLong(((Timestamp) minvalue).getTime(), ((Timestamp) maxvalue).getTime()));
}

protected int generateRandomInt(int length) {
Random rnd = new Random();
int power = (int) Math.pow(10, length - 1);

//Example of how this works:
// if length is 3, then we add 100 + random number between 0~899, so that we get a random number between 100~999.
int randomNumeric;
if (length <= 0) {
randomNumeric = 0;
} else if (length == 1) {
randomNumeric = rnd.nextInt(10);
} else {
randomNumeric = power + rnd.nextInt((int) (9 * power));
}

return randomNumeric;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class SqlDateTime2 extends SqlDateTime {
Expand Down Expand Up @@ -47,19 +46,7 @@ public SqlDateTime2() {
public Object createdata() {
Timestamp temp = new Timestamp(ThreadLocalRandom.current().nextLong(((Timestamp) minvalue).getTime(), ((Timestamp) maxvalue).getTime()));
temp.setNanos(0);

Random rnd = new Random();
// this will generate a number that is this.precision number of digits.
int randomNumeric;
if (this.precision <= 0) {
randomNumeric = 0;
} else if (this.precision == 1) {
randomNumeric = rnd.nextInt(10);
} else {
randomNumeric = (int) Math.pow(10, this.precision - 1) + rnd.nextInt((int) (9 * Math.pow(10, this.precision - 1)));
}

String timeNano = temp.toString().substring(0, temp.toString().length() - 1) + randomNumeric;
String timeNano = temp.toString().substring(0, temp.toString().length() - 1) + generateRandomInt(this.precision);
return timeNano;
// can pass string rather than converting to LocalDateTime, but leaving
// it unchanged for now to handle prepared statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class SqlTime extends SqlDateTime {
Expand Down Expand Up @@ -49,18 +48,7 @@ public SqlTime() {

public Object createdata() {
Time temp = new Time(ThreadLocalRandom.current().nextLong(((Time) minvalue).getTime(), ((Time) maxvalue).getTime()));
Random rnd = new Random();
// this will generate a number that is this.scale number of digits.
int randomNumeric;
if (this.scale <= 0) {
randomNumeric = 0;
} else if (this.scale == 1) {
randomNumeric = rnd.nextInt(10);
} else {
randomNumeric = (int) Math.pow(10, this.scale - 1) + rnd.nextInt((int) (9 * Math.pow(10, this.scale - 1)));
}

String timeNano = temp.toString() + "." + randomNumeric;
String timeNano = temp.toString() + "." + generateRandomInt(this.scale);
return timeNano;

// can pass String rather than converting to loacTime, but leaving it
Expand Down

0 comments on commit 02ef8d8

Please sign in to comment.