Skip to content

Commit

Permalink
Parse date
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetector1374 committed Aug 6, 2024
1 parent 08a54fe commit d6ff223
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/innodb/table/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum FieldType {

Text(usize, InnoDBCharset), // CHAR type with non-latin charset also uses this apparently
Char(usize, InnoDBCharset),

Date,
}
impl FieldType {
// Returns how many bytes does the "length" metadata takes up
Expand All @@ -27,7 +29,7 @@ impl FieldType {
| FieldType::Int(_)
| FieldType::Int6(_)
| FieldType::BigInt(_) => false,
FieldType::Enum(_) => false,
FieldType::Enum(_) | FieldType::Date => false,
FieldType::Char(_, _) => false,
FieldType::Text(_, _) => true,
}
Expand All @@ -42,6 +44,7 @@ impl FieldType {
FieldType::Int6(_) => 6,
FieldType::BigInt(_) => 8,
FieldType::Enum(_) => 2,
FieldType::Date => 3,
FieldType::Text(len, charset) => (*len as u64) * charset.max_len(),
FieldType::Char(len, charset) => (*len as u64) * charset.max_len(),
}
Expand Down Expand Up @@ -133,6 +136,16 @@ impl Field {
(FieldValue::String(str), length as usize)
}
},
FieldType::Date => {
if let FieldValue::SignedInt(date_num) = self.parse_int(buf, 3, true) {
let day = date_num & 0x1F;
let month = (date_num >> 5) & 0xF;
let year = date_num >> 9;
(FieldValue::String(format!("{:04}-{:02}-{:02}", year, month, day)), 3)
} else {
panic!("Can't parse int");
}
},
FieldType::Enum(ref values) => {
let len = if values.len() <= u8::MAX as usize {
1
Expand All @@ -141,7 +154,10 @@ impl Field {
};

if let FieldValue::UnsignedInt(num) = self.parse_int(buf, len, false) {
assert!((num as usize) < values.len(), "Enum Value is larger than expected?");
assert!(
(num as usize) < values.len(),
"Enum Value is larger than expected?"
);
(FieldValue::String(values[num as usize].clone()), len)
} else {
panic!("Unexpected Enum Parsing Failure");
Expand Down
1 change: 1 addition & 0 deletions src/innodb/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl TableDefinition {
DataType::Enum(values) => {
FieldType::Enum(values.clone())
},
DataType::Date => FieldType::Date,
_ => unimplemented!("mapping of {:?}", column.data_type),
};

Expand Down

0 comments on commit d6ff223

Please sign in to comment.