Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Dec 6, 2019
1 parent 5b6816a commit ff4f541
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
10 changes: 8 additions & 2 deletions diesel/src/mysql/connection/stmt/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,20 @@ impl<'a> Row<Mysql> for MysqlRow<'a> {
self.binds.len()
}

fn column_name(&self) -> &str {
fn column_name(&self) -> Option<&str> {
let metadata = self.stmt.metadata().expect("Failed to get metadata");
let field = if self.col_idx == 0 {
metadata.fields()[0]
} else {
metadata.fields()[self.col_idx - 1]
};
unsafe { CStr::from_ptr(field.name).to_str().expect("It's utf8") }
unsafe {
Some(CStr::from_ptr(field.name).to_str().expect(
"Diesel assumes that your mysql database uses the \
utf8mb4 encoding. That's not the case if you hit \
this error message.",
))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions diesel/src/pg/connection/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl<'a> Row<Pg> for PgRow<'a> {
self.db_result.column_count()
}

fn column_name(&self) -> &str {
self.db_result.column_name(self.col_idx)
fn column_name(&self) -> Option<&str> {
Some(self.db_result.column_name(self.col_idx))
}
}

Expand Down
2 changes: 1 addition & 1 deletion diesel/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait Row<DB: Backend> {
fn column_count(&self) -> usize;

/// Name of the current column
fn column_name(&self) -> &str;
fn column_name(&self) -> Option<&str>;
}

/// Represents a row of a SQL query, where the values are accessed by name
Expand Down
4 changes: 2 additions & 2 deletions diesel/src/sqlite/connection/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<'a> Row<Sqlite> for FunctionRow<'a> {
self.args.len()
}

fn column_name(&self) -> &str {
unimplemented!()
fn column_name(&self) -> Option<&str> {
None
}
}
14 changes: 10 additions & 4 deletions diesel/src/sqlite/connection/sqlite_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,22 @@ impl Row<Sqlite> for SqliteRow {
})
}

fn column_name(&self) -> &str {
fn column_name(&self) -> Option<&str> {
unsafe {
let ptr = if self.next_col_index == 0 {
ffi::sqlite3_column_name(self.stmt.as_ptr(), 0)
} else {
ffi::sqlite3_column_name(self.stmt.as_ptr(), self.next_col_index - 1)
};
std::ffi::CStr::from_ptr(ptr)
.to_str()
.expect("Sqlite3 doc's say it's UTF8")
Some(
std::ffi::CStr::from_ptr(ptr)
.to_str()
.expect(
"The Sqlite documentation states that this is UTF8. \
If you see this error message something has gone \
horribliy wrong. Please open an issue at the \
diesel repository."),
)
}
}

Expand Down

0 comments on commit ff4f541

Please sign in to comment.