The `db_sqlite.DbConn | db_mysql.DbConn` syntax creates a typeclass, which is a
kind of generics that resolves at **compile-time**.
Instead, use a variant object to create a type that stores either `DbConn`:
import db_sqlite, db_mysql
type
DbType = enum
dtSqlite
dtMysql
Db = object
case typ: DbType
of dtSqlite:
connSqlite: db_sqlite.DbConn
of dtMysql:
connMysql: db_mysql.DbConn
proc newDb(typ: DbType, connection, user, pass, name: string): Db =
case typ
of dtSqlite:
result = Db(typ: typ, connSqlite: db_sqlite.open(connection, user,
pass, name))
of dtMysql:
result = Db(typ: typ, connMysql: db_mysql.open(connection, user, pass,
name))
discard result.connMysql.setEncoding "utf8"
Run
Then check the `typ` field to know which procedure should you call:
proc close(db: var Db) =
case db.typ
of dtSqlite:
db.connSqlite.close()
db.connSqlite = nil
of dtMysql:
db.connMysql.close()
db.connMysql = nil
Run
[Playground link](https://play.nim-lang.org/#ix=1QM5) (note: playground doesn't
have mysql so this won't run)