Early implementation of Tiberius as Diesel backend #4341
Replies: 3 comments 7 replies
-
These are great news 🎉 Thanks for working on this and thanks for making this work public ❤️
That's currently not possible, as we did not have any need for that yet. In the back of my mind is the idea to have some sort of extension functionality for this, similar to what cargo does. Maybe by just allowing to register extension command line tools via
The table! {
mySchema.myTable {
id -> Integer
}
} and it will just work as expected. |
Beta Was this translation helpful? Give feedback.
-
I've made some more progress. I've started stealing tests from diesel_tests one by one and making them compile. There are still many bugs left and unimplemented features. I'm having extra difficulties with the traits FromSql and ToSql. At first I used the raw value-struct (ColumnData) from Tiberius as the type for my backend and the RawBytesBindCollector. This did not work for dates though. Turns out all parameters got bound as VARBINARY when using this approach and SQL Server couldn't convert this to the DATETIME2-type, so I made a wrapper struct for the types and this solved the issue. Instead I got a new bug with nullable values. If you take a look at create table slime (
external_id NVARCHAR(100) NOT NULL PRIMARY KEY,
name NVARCHAR(MAX) NULL
);
INSERT INTO slime (external_id) VALUES ('id-34'); When I load that row from that table in Diesel, I don't get None for the name column, but rather Some(""). Why is the from_sql fn used for values that are null from the database? Any guidance on how to resolve this would be appreciated! 😄 |
Beta Was this translation helpful? Give feedback.
-
T-SQL is a bit weird when it comes to OFFSET/LIMIT-related things. -- LimitOffsetClause<LimitClause<L>, NoOffsetClause>
DECLARE @P1 INT = 5;
SELECT
TOP(@P1) -- limiting is done here
FROM mytable;
-- instead of here
-- LimitOffsetClause<LimitClause<L>, OffsetClause<O>
SELECT * FROM myTable
ORDER BY 1 -- this is more like postgres, but order, offset and limit are all required now.
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY So, limiting the result set is possible without any offset or ordering. I'm not quite sure how to best implement this in Diesel. I currently implement my own SelectStatement syntax just like the Firebird implementation to allow moving the "TOP()" to just after the DISTINCT clause. fn walk_ast<'b>(
&'b self,
mut out: diesel::query_builder::AstPass<'_, 'b, Mssql>,
) -> diesel::QueryResult<()> {
out.push_sql("SELECT ");
self.distinct.walk_ast(out.reborrow())?;
self.limit_offset.walk_ast(out.reborrow())?; // This is after WHERE I think in postgres/mysql/sqlite
self.select.walk_ast(out.reborrow())?;
self.from.walk_ast(out.reborrow())?;
self.where_clause.walk_ast(out.reborrow())?;
self.group_by.walk_ast(out.reborrow())?;
self.having.walk_ast(out.reborrow())?;
self.order.walk_ast(out.reborrow())?;
Ok(())
} But this ordering does not work with the FETCH/NEXT syntax in TSQL when OFFSET is needed. I'm not sure if I can check whether it's just a LIMIT-clause and no OFFSET and in that case use TOP otherwise if OFFSET/LIMIT/ORDER are present in that case use FETCH/NEXT. I had another idea of using FETCH/NEXT in SelectStatements and not using TOP() at all. To allow for just limiting I'd instead find a way to extend the QueryDSL with a custom function so you could do something like: users.select((name, hair_color))
.top(5) -- This looks more like TSQL!
.load(connection)
.unwrap(); But SelectStatement isn't open for extending, so I guess I'd have to reimplement the entire SELECT thing and do something like the Pagination-example in the Extending Diesel-guide. I realize this is a confusing question, but I'm a bit confused as how to go about this in a wise way. I'd prefer making as few custom implementations as possible so it doesn't become too hard to maintain, but I also think it's a good idea to make the querybuilder go nicely with tsql. |
Beta Was this translation helpful? Give feedback.
-
I've started working on Tiberius/SQL Server as a backend for Diesel in this repository.
The current status is that I can select, insert and join from existing tables with simple data types. I discover more things that I need to implement as I go about adding more tests.
If anyone's interested in taking a look and giving some feedback this early on that'd be welcome! 😊
Does anyone know if it's possible to plug a custom backend into the CLI in any way? I'd really like to inspect an existing database to generate schema-files.
Is it possible to handle using several schemas in SQL Server somehow? That is, a table [mySchema].[myTable], rather than using the default schema.
Beta Was this translation helpful? Give feedback.
All reactions