Skip to content

Commit

Permalink
Create a new Databases faker module
Browse files Browse the repository at this point in the history
  • Loading branch information
mboisnard authored Mar 9, 2024
1 parent ea859b5 commit 0522533
Show file tree
Hide file tree
Showing 33 changed files with 936 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ The class needs to implement `YamlFakeDataProvider`, and override a few properti

[source,kotlin]
----
class Name internal constructor(fakerService: FakerService) : AbstractFakeDataProvider<Name>(fakerService) {
class Name internal constructor(fakerService: FakerService) : YamlFakeDataProvider<Name>(fakerService) {
override val yamlCategory = YamlCategory.NAME
override val localUniqueDataProvider = LocalUniqueDataProvider<Name>()
override val unique by UniqueProviderDelegate(localUniqueDataProvider, fakerService)
Expand Down
1 change: 1 addition & 0 deletions cli-bot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ val fakers = listOf(
"books",
"commerce",
"creatures",
"databases",
"edu",
"games",
"humor",
Expand Down
45 changes: 45 additions & 0 deletions core/src/main/kotlin/io/github/serpro69/kfaker/IRandom.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.serpro69.kfaker

import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*

/**
Expand Down Expand Up @@ -84,6 +87,18 @@ interface IRandom {
*/
fun nextLong(bound: Long): Long

/**
* Returns a pseudorandom, uniformly distributed [Long] value within the specified [longRange] (inclusive),
* drawn from this [random] number generator's sequence.
*/
fun nextLong(longRange: LongRange): Long

/**
* Returns a pseudorandom, uniformly distributed [Long] value between [min] (inclusive) and [max] (inclusive),
* drawn from this [random] number generator's sequence.
*/
fun nextLong(min: Long, max: Long): Long

/**
* Returns the next pseudorandom, uniformly distributed [Float] value between `0.0` and `1.0`
* from this [random] number generator's sequence.
Expand Down Expand Up @@ -242,4 +257,34 @@ interface IRandom {
* @param shuffled if `true` the [set] will be shuffled before extracting the subset
*/
fun <T> randomSubset(set: Set<T>, sizeRange: IntRange, shuffled: Boolean = false): Set<T>

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between Unix epoch (1970-01-01T00:00:00Z) (inclusive) and now (exclusive) using UTC zone offset
*/
fun randomPastDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between [min] (inclusive) and now (exclusive) using UTC zone offset
*/
fun randomPastDate(min: Instant): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between now (exclusive) and now + 50 years (inclusive) using UTC zone offset
*/
fun randomFutureDate(): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between now (exclusive) and [max] (inclusive) using UTC zone offset
*/
fun randomFutureDate(max: Instant): OffsetDateTime

/**
* Returns a pseudorandom, uniformly distributed [OffsetDateTime] value
* between [min] (inclusive) and [max] (inclusive) using the defined [zoneOffset]
*/
fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime
}
43 changes: 43 additions & 0 deletions core/src/main/kotlin/io/github/serpro69/kfaker/RandomService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package io.github.serpro69.kfaker
import com.ibm.icu.text.UnicodeSet
import com.ibm.icu.util.LocaleData
import com.ibm.icu.util.ULocale
import java.time.Duration
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*
import kotlin.experimental.and
import kotlin.experimental.or
Expand Down Expand Up @@ -73,6 +77,15 @@ class RandomService internal constructor(override val config: FakerConfig) : IRa
} else throw IllegalArgumentException("Bound bound must be greater than 0")
}

override fun nextLong(longRange: LongRange): Long {
val lowerBound = requireNotNull(longRange.minOrNull())
val upperBound = requireNotNull(longRange.maxOrNull())

return nextLong(lowerBound, upperBound)
}

override fun nextLong(min: Long, max: Long) = nextLong(max - min + 1) + min

override fun nextFloat() = random.nextFloat()

override fun nextDouble() = random.nextDouble()
Expand Down Expand Up @@ -212,6 +225,36 @@ class RandomService internal constructor(override val config: FakerConfig) : IRa
return randomSubset(set, nextInt(sizeRange), shuffled)
}

override fun randomPastDate(): OffsetDateTime {
return randomPastDate(Instant.ofEpochSecond(0))
}

override fun randomPastDate(min: Instant): OffsetDateTime {
val now = Instant.now()
require(min.isBefore(now))

return randomDate(min, now.minusMillis(1), ZoneOffset.UTC)
}

override fun randomFutureDate(): OffsetDateTime {
val maxInstant = Instant.now().plus(Duration.ofDays(50 * 365))
return randomFutureDate(maxInstant)
}

override fun randomFutureDate(max: Instant): OffsetDateTime {
val now = Instant.now()
require(max.isAfter(now))

return randomDate(now.plusMillis(1), max, ZoneOffset.UTC)
}

override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime {
val randomSeconds = nextLong(min.epochSecond, max.epochSecond)
val randomInstant = Instant.ofEpochSecond(randomSeconds)

return OffsetDateTime.ofInstant(randomInstant, zoneOffset)
}

private fun <T> Collection<T>.randomFromToIndices(s: Int): Pair<Int, Int> {
val fromIndex = if (s > 0) {
(0..size - s).random(random.asKotlinRandom())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum class YamlCategory : Category {
CRYPTO_COIN,
CULTURE_SERIES,
CURRENCY,
DATABASE,
DC_COMICS,
DEMOGRAPHIC,
DEPARTED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.NEXT_INT
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.NEXT_LETTER
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.NEXT_LONG
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.NEXT_UUID
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.RANDOM_DATE
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.RANDOM_STRING
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.RANDOM_SUBLIST
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.RANDOM_SUBSET
import io.github.serpro69.kfaker.provider.misc.RandomProvider.Key.RANDOM_VALUE
import io.github.serpro69.kfaker.provider.unique.LocalUniqueDataProvider
import io.github.serpro69.kfaker.provider.unique.UniqueProviderDelegate
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.*

/**
Expand Down Expand Up @@ -71,6 +75,10 @@ class RandomProvider internal constructor(

override fun nextLong(bound: Long): Long = resolveUnique(NEXT_LONG) { rs.nextLong(bound) }

override fun nextLong(longRange: LongRange): Long = resolveUnique(NEXT_LONG) { rs.nextLong(longRange) }

override fun nextLong(min: Long, max: Long): Long = resolveUnique(NEXT_LONG) { rs.nextLong(min, max) }

override fun nextFloat(): Float = resolveUnique(NEXT_FLOAT) { rs.nextFloat() }

override fun nextDouble(): Double = resolveUnique(NEXT_DOUBLE) { rs.nextDouble() }
Expand Down Expand Up @@ -189,6 +197,17 @@ class RandomProvider internal constructor(
return resolveUnique(RANDOM_SUBSET) { rs.randomSubset(set = set, sizeRange = sizeRange, shuffled = shuffled) }
}

override fun randomPastDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomPastDate() }

override fun randomPastDate(min: Instant): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomPastDate(min) }

override fun randomFutureDate(): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomFutureDate() }

override fun randomFutureDate(max: Instant): OffsetDateTime = resolveUnique(RANDOM_DATE) { rs.randomFutureDate(max) }

override fun randomDate(min: Instant, max: Instant, zoneOffset: ZoneOffset): OffsetDateTime =
resolveUnique(RANDOM_SUBSET) { rs.randomDate(min, max, zoneOffset) }

@PublishedApi
internal fun <T> resolveUnique(key: Key, f: () -> T): T = resolveUniqueValue(key.name, f)

Expand Down Expand Up @@ -255,6 +274,11 @@ class RandomProvider internal constructor(
* Key for [randomSubset] function
*/
RANDOM_SUBSET,

/**
* Key for [randomDate] function
*/
RANDOM_DATE,
;
}
}
11 changes: 11 additions & 0 deletions core/src/main/resources/locales/en/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# START database_provider_dict
en:
faker:
database:
column_name:
- id
- name
- email
- created_at
- updated_at
# END database_provider_dict
70 changes: 70 additions & 0 deletions core/src/main/resources/locales/en/mariadb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# START mariadb_provider_dict
en:
faker:
database:
mariadb:
data_type:
# Numeric
- TINYINT
- SMALLINT
- MEDIUMINT
- INT
- INTEGER
- BIGINT
- DECIMAL
- DEC
- NUMERIC
- FIXED
- FLOAT
- DOUBLE
- REAL

# Date and Time
- DATE
- TIME
- DATETIME
- TIMESTAMP

# Character Strings
- CHAR
- VARCHAR
- TINYTEXT
- TEXT
- MEDIUMTEXT
- LONGTEXT
- ENUM
- SET

# Binary
- BINARY
- VARBINARY
- TINYBLOB
- BLOB
- MEDIUMBLOB
- LONGBLOB

# JSON
- JSON

# Spatial
- GEOMETRY
- POINT
- LINESTRING
- POLYGON
- MULTIPOINT
- MULTILINESTRING
- MULTIPOLYGON
- GEOMETRYCOLLECTION
engine:
- InnoDB
- MyISAM
- Aria
- MEMORY
- CSV
- ARCHIVE
- BLACKHOLE
- MERGE
- FEDERATED
- TokuDB
- Spider
# END mariadb_provider_dict
44 changes: 44 additions & 0 deletions core/src/main/resources/locales/en/ms_sql_server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# START ms_sql_server_provider_dict
en:
faker:
database:
ms_sql_server:
data_type:
# Exact Numeric
- BIT
- TINYINT
- SMALLINT
- INT
- BIGINT
- DECIMAL

# Approximative Numeric
- FLOAT
- REAL
- DATE
- TIME

# Date and Time
- DATETIME
- DATETIME2
- SMALLDATETIME
- DATETIMEOFFSET

# Character Strings
- CHAR
- VARCHAR
- TEXT

# Binary
- BINARY
- VARBINARY
- IMAGE

# Other
- UNIQUEIDENTIFIER
- XML
- CURSOR
- SQL_VARIANT
- TABLE
- GEOMETRY
# END ms_sql_server_provider_dict
Loading

0 comments on commit 0522533

Please sign in to comment.