Skip to content

Commit de570ff

Browse files
committed
Implement NamedRow change for sqlite
Also guard against `sqlite3_column_name` returning a null pointer (documentation mentions that can happen in edge cases)
1 parent 0b2e8c5 commit de570ff

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

diesel/src/sqlite/connection/sqlite_value.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,11 @@ impl Row<Sqlite> for SqliteRow {
123123
}
124124

125125
fn column_name(&self) -> Option<&str> {
126-
unsafe {
127-
let ptr = ffi::sqlite3_column_name(self.stmt.as_ptr(), self.next_col_index);
128-
Some(std::ffi::CStr::from_ptr(ptr).to_str().expect(
129-
"The Sqlite documentation states that this is UTF8. \
130-
If you see this error message something has gone \
131-
horribliy wrong. Please open an issue at the \
132-
diesel repository.",
133-
))
134-
}
126+
column_name(self.stmt, self.next_col_index)
135127
}
136128

137129
fn column_count(&self) -> usize {
138-
unsafe { ffi::sqlite3_column_count(self.stmt.as_ptr()) as usize }
130+
column_count(self.stmt) as usize
139131
}
140132
}
141133

@@ -155,4 +147,30 @@ impl<'a> NamedRow<Sqlite> for SqliteNamedRow<'a> {
155147
SqliteValue::new(ptr)
156148
}
157149
}
150+
151+
fn field_names(&self) -> Vec<&str> {
152+
(0..column_count(self.stmt))
153+
.filter_map(|c| column_name(self.stmt, c))
154+
.collect()
155+
}
156+
}
157+
158+
fn column_name<'a>(stmt: NonNull<ffi::sqlite3_stmt>, field_number: i32) -> Option<&'a str> {
159+
unsafe {
160+
let ptr = ffi::sqlite3_column_name(stmt.as_ptr(), field_number);
161+
if ptr.is_null() {
162+
None
163+
} else {
164+
Some(std::ffi::CStr::from_ptr(ptr).to_str().expect(
165+
"The Sqlite documentation states that this is UTF8. \
166+
If you see this error message something has gone \
167+
horribliy wrong. Please open an issue at the \
168+
diesel repository.",
169+
))
170+
}
171+
}
172+
}
173+
174+
fn column_count(stmt: NonNull<ffi::sqlite3_stmt>) -> i32 {
175+
unsafe { ffi::sqlite3_column_count(stmt.as_ptr()) }
158176
}

0 commit comments

Comments
 (0)