Something about database

Why doesn't the rust have a database lib like log ,it can provide A set of standard trait to other lib.
if did ,it can be esay to use

Well, if you would want to use it, you can try writing it yourself. But waving aside a fact that this would be a herculean task, consider that logging and databases are quite different things and comparing them is risky.

Logging can be boiled down to "emit event with some metadata into a sink that knows how to process them". Abstracting that concept with a trait and building an ecosystem around it is quite simple and once done, can be considered a solved problem (and then tracing cam along :grin:).

But the land of databases if much more diverse. You have SQL and NonSQL. You have subtle (or sometimes more than subtle) differences in extensions to SQL. And NonSQL can be so vastly different, that I don't know how to even compare them.

There are some crates that try to take a subset of databases and abstract over them. For example sqlx supports PostgreSQL, MySQL, MariaDB, and SQLite. It defines traits for database drivers, so you could add your own implementation if you wish. It even has generic driver, that selects concrete database driver at runtime.

And lastly consider if this would be even practical in the real world. If you want to communicate with some existing database, then you can use just some library that allows communication with that specific database. And if you are creating your own database as well, then just using SQLite and if you want something bigger PostgreSQL, will work 95% of a time.

1 Like

thx a lot. i understand what you mean , and a driver Abstracting all db is Impossible , nosql especially, there is an official interface in java , Because it's official , the db supplier will actively
be adapting to this driver . dveloper can implement many framework of db by this interface . this
framework improves work efficiency although it cant cover all db , in fact it adapting some popular db .its enough . when db changed, the code is will be rewritten again . that was a nightmare.

It does: GitHub - tokio-rs/rdbc: Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers
but there was no interest in developing it further. Most people using DB use one directly via native driver, there is little point in being limited to lowest common denominator, and then you have all sorts of custom nosql or key-value things that's hard to come up with any shared functionality.

3 Likes

There are valid use-cases for supporting more than one database in your application at once. For example because you customers request that.

Diesel provides a #[derive(MultiConnection)] proc macro to cover this case. This marco lets you describe which connection types you exactly want to support and provides a connection implementation based on that enum. The generated connection implementation will automatically restrict the supported SQL to that subset that's supported by your database selection.

From a technical point of view this might also work with a no-sql database, as long as you bring your own type implementing of diesels connection trait for such a database.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.