2024-03-15 23:08:47 +00:00
|
|
|
import std/strutils, regex, stew/results
|
2023-06-22 09:27:40 +00:00
|
|
|
|
|
|
|
proc validateDbUrl*(dbUrl: string): Result[string, string] =
|
|
|
|
## dbUrl mimics SQLAlchemy Database URL schema
|
|
|
|
## See: https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls
|
2024-05-16 20:29:11 +00:00
|
|
|
let regex = re2"^\w+:\/\/.+:.+@[\w*-.]+:[0-9]+\/[\w*-.]+$"
|
2023-06-22 09:27:40 +00:00
|
|
|
let dbUrl = dbUrl.strip()
|
2023-09-26 11:59:54 +00:00
|
|
|
if "sqlite" in dbUrl or dbUrl == "" or dbUrl == "none" or dbUrl.match(regex):
|
2023-06-22 09:27:40 +00:00
|
|
|
return ok(dbUrl)
|
|
|
|
else:
|
|
|
|
return err("invalid 'db url' option format: " & dbUrl)
|
|
|
|
|
|
|
|
proc getDbEngine*(dbUrl: string): Result[string, string] =
|
|
|
|
let dbUrlParts = dbUrl.split("://", 1)
|
|
|
|
|
|
|
|
if dbUrlParts.len != 2:
|
|
|
|
return err("Incorrect dbUrl : " & dbUrl)
|
|
|
|
|
|
|
|
let engine = dbUrlParts[0]
|
|
|
|
return ok(engine)
|
|
|
|
|
|
|
|
proc getDbPath*(dbUrl: string): Result[string, string] =
|
|
|
|
let dbUrlParts = dbUrl.split("://", 1)
|
|
|
|
|
|
|
|
if dbUrlParts.len != 2:
|
|
|
|
return err("Incorrect dbUrl : " & dbUrl)
|
|
|
|
|
|
|
|
let path = dbUrlParts[1]
|
|
|
|
return ok(path)
|