|
| 1 | +use serde_json::Value as JsonValue; |
| 2 | +use sqlx::sqlite::SqliteValueRef; |
| 3 | +use sqlx::{TypeInfo, Value, ValueRef}; |
| 4 | +use time::PrimitiveDateTime; |
| 5 | + |
| 6 | +use crate::Error; |
| 7 | + |
| 8 | +/// Convert a SQLite value to a JSON value. |
| 9 | +/// |
| 10 | +/// This function handles the type conversion from SQLite's native types |
| 11 | +/// to JSON-compatible representations. |
| 12 | +pub fn to_json(value: SqliteValueRef) -> Result<JsonValue, Error> { |
| 13 | + if value.is_null() { |
| 14 | + return Ok(JsonValue::Null); |
| 15 | + } |
| 16 | + |
| 17 | + let column_type = value.type_info(); |
| 18 | + |
| 19 | + // Handle types based on SQLite's type affinity |
| 20 | + let result = match column_type.name() { |
| 21 | + "TEXT" => { |
| 22 | + if let Ok(v) = value.to_owned().try_decode::<String>() { |
| 23 | + JsonValue::String(v) |
| 24 | + } else { |
| 25 | + JsonValue::Null |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + "REAL" => { |
| 30 | + if let Ok(v) = value.to_owned().try_decode::<f64>() { |
| 31 | + JsonValue::from(v) |
| 32 | + } else { |
| 33 | + JsonValue::Null |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + "INTEGER" | "NUMERIC" => { |
| 38 | + if let Ok(v) = value.to_owned().try_decode::<i64>() { |
| 39 | + JsonValue::Number(v.into()) |
| 40 | + } else { |
| 41 | + JsonValue::Null |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + "BOOLEAN" => { |
| 46 | + if let Ok(v) = value.to_owned().try_decode::<bool>() { |
| 47 | + JsonValue::Bool(v) |
| 48 | + } else { |
| 49 | + JsonValue::Null |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + "DATE" => { |
| 54 | + // SQLite stores dates as TEXT in ISO 8601 format |
| 55 | + if let Ok(v) = value.to_owned().try_decode::<String>() { |
| 56 | + JsonValue::String(v) |
| 57 | + } else { |
| 58 | + JsonValue::Null |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + "TIME" => { |
| 63 | + // SQLite stores time as TEXT in HH:MM:SS format |
| 64 | + if let Ok(v) = value.to_owned().try_decode::<String>() { |
| 65 | + JsonValue::String(v) |
| 66 | + } else { |
| 67 | + JsonValue::Null |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + "DATETIME" => { |
| 72 | + // Try to decode as PrimitiveDateTime |
| 73 | + if let Ok(dt) = value.to_owned().try_decode::<PrimitiveDateTime>() { |
| 74 | + JsonValue::String(dt.to_string()) |
| 75 | + } else if let Ok(v) = value.to_owned().try_decode::<String>() { |
| 76 | + // Fall back to string representation |
| 77 | + JsonValue::String(v) |
| 78 | + } else { |
| 79 | + JsonValue::Null |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + "BLOB" => { |
| 84 | + if let Ok(blob) = value.to_owned().try_decode::<Vec<u8>>() { |
| 85 | + // Encode binary data as base64 for JSON serialization |
| 86 | + JsonValue::String(base64_encode(&blob)) |
| 87 | + } else { |
| 88 | + JsonValue::Null |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + "NULL" => JsonValue::Null, |
| 93 | + |
| 94 | + _ => { |
| 95 | + // For unknown types, try to decode as text |
| 96 | + if let Ok(text) = value.to_owned().try_decode::<String>() { |
| 97 | + JsonValue::String(text) |
| 98 | + } else { |
| 99 | + return Err(Error::UnsupportedDatatype(format!( |
| 100 | + "Unknown SQLite type: {}", |
| 101 | + column_type.name() |
| 102 | + ))); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + Ok(result) |
| 108 | +} |
| 109 | + |
| 110 | +/// Base64 encode binary data for JSON serialization. |
| 111 | +/// |
| 112 | +/// SQLite BLOB columns are encoded as base64 strings when serialized to JSON, |
| 113 | +/// as JSON does not have a native binary type. |
| 114 | +fn base64_encode(data: &[u8]) -> String { |
| 115 | + use std::io::Write; |
| 116 | + |
| 117 | + let mut buf = Vec::new(); |
| 118 | + { |
| 119 | + let mut encoder = base64::write::EncoderWriter::new(&mut buf, &base64::engine::general_purpose::STANDARD); |
| 120 | + encoder.write_all(data).unwrap(); |
| 121 | + } |
| 122 | + String::from_utf8(buf).unwrap() |
| 123 | +} |
| 124 | + |
| 125 | +#[cfg(test)] |
| 126 | +mod tests { |
| 127 | + use super::*; |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_base64_encode() { |
| 131 | + assert_eq!(base64_encode(b"hello"), "aGVsbG8="); |
| 132 | + assert_eq!(base64_encode(&[1, 2, 3, 4, 5]), "AQIDBAU="); |
| 133 | + assert_eq!(base64_encode(&[]), ""); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_base64_encode_binary() { |
| 138 | + // Test with binary data including null bytes |
| 139 | + assert_eq!(base64_encode(&[0, 0, 0]), "AAAA"); |
| 140 | + assert_eq!(base64_encode(&[255, 255, 255]), "////"); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn test_base64_encode_large() { |
| 145 | + // Test with larger binary data |
| 146 | + let data: Vec<u8> = (0..255).collect(); |
| 147 | + let encoded = base64_encode(&data); |
| 148 | + assert!(!encoded.is_empty()); |
| 149 | + // Verify it's valid base64 (only contains valid chars) |
| 150 | + assert!(encoded.chars().all(|c| c.is_alphanumeric() || c == '+' || c == '/' || c == '=')); |
| 151 | + } |
| 152 | +} |
0 commit comments