xdl-database 0.1.1

Database connectivity module for XDL - supports PostgreSQL, MySQL, DuckDB, SQLite, ODBC, Redis, and more
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Apache Kafka driver - Streaming data platform
//!
//! Supports Kafka operations including:
//! - Producing messages to topics
//! - Consuming messages from topics
//! - Topic management
//!
//! Special query syntax:
//! - PRODUCE TO topic: message_content
//! - CONSUME FROM topic [LIMIT n] [OFFSET group_id]
//! - CREATE TOPIC topic
//! - LIST TOPICS
//! - DELETE TOPIC topic

use crate::{recordset::ColumnInfo, DatabaseError, DatabaseResult, Recordset};
use rdkafka::{
    admin::{AdminClient, AdminOptions, NewTopic, TopicReplication},
    client::DefaultClientContext,
    config::ClientConfig,
    consumer::{BaseConsumer, Consumer},
    message::{BorrowedMessage, Message},
    producer::{FutureProducer, FutureRecord},
    util::Timeout,
};
use serde_json::Value as JsonValue;
use std::time::Duration;

/// Kafka connection wrapper
#[derive(Debug)]
pub struct KafkaConnection {
    brokers: String,
    producer: Option<FutureProducer>,
    consumer: Option<BaseConsumer>,
    admin: Option<AdminClient<DefaultClientContext>>,
}

impl KafkaConnection {
    /// Connect to Kafka cluster
    ///
    /// Connection string format: kafka://broker1:9092,broker2:9092
    /// Or just: localhost:9092
    pub async fn connect(connection_string: &str) -> DatabaseResult<Self> {
        // Parse broker list from connection string
        let brokers = connection_string
            .trim_start_matches("kafka://")
            .trim_start_matches("kafka:")
            .to_string();

        // Create producer
        let producer: FutureProducer = ClientConfig::new()
            .set("bootstrap.servers", &brokers)
            .set("message.timeout.ms", "5000")
            .create()
            .map_err(|e| {
                DatabaseError::connection_error(format!("Failed to create Kafka producer: {}", e))
            })?;

        // Create consumer
        let consumer: BaseConsumer = ClientConfig::new()
            .set("bootstrap.servers", &brokers)
            .set("group.id", "xdl-database-consumer")
            .set("enable.auto.commit", "false")
            .set("auto.offset.reset", "earliest")
            .create()
            .map_err(|e| {
                DatabaseError::connection_error(format!("Failed to create Kafka consumer: {}", e))
            })?;

        // Create admin client
        let admin: AdminClient<DefaultClientContext> = ClientConfig::new()
            .set("bootstrap.servers", &brokers)
            .create()
            .map_err(|e| {
                DatabaseError::connection_error(format!(
                    "Failed to create Kafka admin client: {}",
                    e
                ))
            })?;

        Ok(Self {
            brokers,
            producer: Some(producer),
            consumer: Some(consumer),
            admin: Some(admin),
        })
    }

    /// Execute a Kafka "query" (special syntax)
    ///
    /// Supported commands:
    /// - PRODUCE TO topic: message
    /// - CONSUME FROM topic LIMIT n
    /// - CREATE TOPIC topic
    /// - LIST TOPICS
    /// - DELETE TOPIC topic
    pub async fn execute(&self, query: &str) -> DatabaseResult<Recordset> {
        let query = query.trim();
        let upper_query = query.to_uppercase();

        if upper_query.starts_with("PRODUCE TO") {
            self.handle_produce(query).await
        } else if upper_query.starts_with("CONSUME FROM") {
            self.handle_consume(query).await
        } else if upper_query.starts_with("LIST TOPICS") {
            self.handle_list_topics().await
        } else if upper_query.starts_with("CREATE TOPIC") {
            self.handle_create_topic(query).await
        } else if upper_query.starts_with("DELETE TOPIC") {
            self.handle_delete_topic(query).await
        } else {
            Err(DatabaseError::query_error(format!(
                "Unsupported Kafka command. Use: PRODUCE TO, CONSUME FROM, LIST TOPICS, CREATE TOPIC, or DELETE TOPIC"
            )))
        }
    }

    /// Handle PRODUCE TO topic: message
    async fn handle_produce(&self, query: &str) -> DatabaseResult<Recordset> {
        let producer = self.producer.as_ref().ok_or(DatabaseError::NotConnected)?;

        // Parse: PRODUCE TO topic: message
        let parts: Vec<&str> = query.splitn(2, ':').collect();
        if parts.len() != 2 {
            return Err(DatabaseError::query_error(
                "PRODUCE syntax: PRODUCE TO topic: message",
            ));
        }

        let topic_part = parts[0].trim();
        let message = parts[1].trim();

        // Extract topic name
        let topic = topic_part
            .trim_start_matches("PRODUCE TO")
            .trim_start_matches("produce to")
            .trim();

        // Send message
        let record = FutureRecord::to(topic).payload(message).key("xdl-key");

        let delivery_status = producer
            .send(record, Timeout::After(Duration::from_secs(5)))
            .await
            .map_err(|(err, _)| {
                DatabaseError::query_error(format!("Failed to produce message: {}", err))
            })?;

        // Return success info
        let columns = vec![
            ColumnInfo {
                name: "status".to_string(),
                data_type: "text".to_string(),
                ordinal: 0,
            },
            ColumnInfo {
                name: "partition".to_string(),
                data_type: "integer".to_string(),
                ordinal: 1,
            },
            ColumnInfo {
                name: "offset".to_string(),
                data_type: "integer".to_string(),
                ordinal: 2,
            },
        ];

        let rows = vec![vec![
            JsonValue::from("success"),
            JsonValue::from(delivery_status.0),
            JsonValue::from(delivery_status.1),
        ]];

        Ok(Recordset::new(columns, rows))
    }

    /// Handle CONSUME FROM topic [LIMIT n]
    async fn handle_consume(&self, query: &str) -> DatabaseResult<Recordset> {
        let consumer = self.consumer.as_ref().ok_or(DatabaseError::NotConnected)?;

        // Parse: CONSUME FROM topic [LIMIT n]
        let parts: Vec<&str> = query.split_whitespace().collect();
        if parts.len() < 3 {
            return Err(DatabaseError::query_error(
                "CONSUME syntax: CONSUME FROM topic [LIMIT n]",
            ));
        }

        let topic = parts[2];
        let limit = if parts.len() >= 5 && parts[3].to_uppercase() == "LIMIT" {
            parts[4].parse::<usize>().unwrap_or(10)
        } else {
            10
        };

        // Subscribe to topic
        consumer.subscribe(&[topic]).map_err(|e| {
            DatabaseError::query_error(format!("Failed to subscribe to topic: {}", e))
        })?;

        // Collect messages
        let mut messages = Vec::new();

        for _ in 0..limit {
            match consumer.poll(Timeout::After(Duration::from_millis(1000))) {
                Some(Ok(msg)) => {
                    messages.push(Self::message_to_row(&msg)?);
                }
                Some(Err(e)) => {
                    eprintln!("Kafka consumer error: {}", e);
                    break;
                }
                None => {
                    // No more messages within timeout
                    break;
                }
            }
        }

        // Define columns
        let columns = vec![
            ColumnInfo {
                name: "partition".to_string(),
                data_type: "integer".to_string(),
                ordinal: 0,
            },
            ColumnInfo {
                name: "offset".to_string(),
                data_type: "integer".to_string(),
                ordinal: 1,
            },
            ColumnInfo {
                name: "key".to_string(),
                data_type: "text".to_string(),
                ordinal: 2,
            },
            ColumnInfo {
                name: "payload".to_string(),
                data_type: "text".to_string(),
                ordinal: 3,
            },
            ColumnInfo {
                name: "timestamp".to_string(),
                data_type: "integer".to_string(),
                ordinal: 4,
            },
        ];

        Ok(Recordset::new(columns, messages))
    }

    /// Convert Kafka message to row data
    fn message_to_row(msg: &BorrowedMessage) -> DatabaseResult<Vec<JsonValue>> {
        let partition = JsonValue::from(msg.partition());
        let offset = JsonValue::from(msg.offset());

        let key = match msg.key() {
            Some(k) => JsonValue::from(String::from_utf8_lossy(k).to_string()),
            None => JsonValue::Null,
        };

        let payload = match msg.payload() {
            Some(p) => JsonValue::from(String::from_utf8_lossy(p).to_string()),
            None => JsonValue::Null,
        };

        let timestamp = match msg.timestamp().to_millis() {
            Some(ts) => JsonValue::from(ts),
            None => JsonValue::Null,
        };

        Ok(vec![partition, offset, key, payload, timestamp])
    }

    /// Handle LIST TOPICS
    async fn handle_list_topics(&self) -> DatabaseResult<Recordset> {
        let consumer = self.consumer.as_ref().ok_or(DatabaseError::NotConnected)?;

        let metadata = consumer
            .fetch_metadata(None, Timeout::After(Duration::from_secs(5)))
            .map_err(|e| DatabaseError::query_error(format!("Failed to fetch metadata: {}", e)))?;

        let columns = vec![
            ColumnInfo {
                name: "topic".to_string(),
                data_type: "text".to_string(),
                ordinal: 0,
            },
            ColumnInfo {
                name: "partitions".to_string(),
                data_type: "integer".to_string(),
                ordinal: 1,
            },
        ];

        let mut rows = Vec::new();
        for topic in metadata.topics() {
            rows.push(vec![
                JsonValue::from(topic.name()),
                JsonValue::from(topic.partitions().len()),
            ]);
        }

        Ok(Recordset::new(columns, rows))
    }

    /// Handle CREATE TOPIC topic_name
    async fn handle_create_topic(&self, query: &str) -> DatabaseResult<Recordset> {
        let admin = self.admin.as_ref().ok_or(DatabaseError::NotConnected)?;

        // Parse topic name
        let parts: Vec<&str> = query.split_whitespace().collect();
        if parts.len() < 3 {
            return Err(DatabaseError::query_error(
                "CREATE TOPIC syntax: CREATE TOPIC topic_name",
            ));
        }

        let topic_name = parts[2];

        // Create topic
        let new_topic = NewTopic::new(
            topic_name,
            1,                          // num_partitions
            TopicReplication::Fixed(1), // replication_factor
        );

        let results = admin
            .create_topics(&[new_topic], &AdminOptions::new())
            .await
            .map_err(|e| DatabaseError::query_error(format!("Failed to create topic: {}", e)))?;

        // Check result
        let status = if results.is_empty() {
            "success"
        } else {
            match &results[0] {
                Ok(_) => "success",
                Err(e) => {
                    return Err(DatabaseError::query_error(format!(
                        "Topic creation failed: {}",
                        e
                    )))
                }
            }
        };

        let columns = vec![
            ColumnInfo {
                name: "status".to_string(),
                data_type: "text".to_string(),
                ordinal: 0,
            },
            ColumnInfo {
                name: "topic".to_string(),
                data_type: "text".to_string(),
                ordinal: 1,
            },
        ];

        let rows = vec![vec![JsonValue::from(status), JsonValue::from(topic_name)]];

        Ok(Recordset::new(columns, rows))
    }

    /// Handle DELETE TOPIC topic_name
    async fn handle_delete_topic(&self, query: &str) -> DatabaseResult<Recordset> {
        let admin = self.admin.as_ref().ok_or(DatabaseError::NotConnected)?;

        // Parse topic name
        let parts: Vec<&str> = query.split_whitespace().collect();
        if parts.len() < 3 {
            return Err(DatabaseError::query_error(
                "DELETE TOPIC syntax: DELETE TOPIC topic_name",
            ));
        }

        let topic_name = parts[2];

        // Delete topic
        let results = admin
            .delete_topics(&[topic_name], &AdminOptions::new())
            .await
            .map_err(|e| DatabaseError::query_error(format!("Failed to delete topic: {}", e)))?;

        // Check result
        let status = if results.is_empty() {
            "success"
        } else {
            match &results[0] {
                Ok(_) => "success",
                Err(e) => {
                    return Err(DatabaseError::query_error(format!(
                        "Topic deletion failed: {}",
                        e
                    )))
                }
            }
        };

        let columns = vec![
            ColumnInfo {
                name: "status".to_string(),
                data_type: "text".to_string(),
                ordinal: 0,
            },
            ColumnInfo {
                name: "topic".to_string(),
                data_type: "text".to_string(),
                ordinal: 1,
            },
        ];

        let rows = vec![vec![JsonValue::from(status), JsonValue::from(topic_name)]];

        Ok(Recordset::new(columns, rows))
    }

    /// Execute a command (producer operations)
    pub async fn execute_command(&self, command: &str) -> DatabaseResult<u64> {
        // For Kafka, commands are the same as queries
        let result = self.execute(command).await?;
        Ok(result.row_count() as u64)
    }

    /// Close the connection
    pub async fn close(&mut self) -> DatabaseResult<()> {
        self.producer = None;
        self.consumer = None;
        self.admin = None;
        Ok(())
    }

    /// Check if connection is alive
    pub async fn is_connected(&self) -> bool {
        self.producer.is_some() && self.consumer.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_kafka_connection_string_parsing() {
        let conn_str = "kafka://localhost:9092";
        let brokers = conn_str.trim_start_matches("kafka://");
        assert_eq!(brokers, "localhost:9092");
    }

    #[test]
    fn test_query_parsing() {
        let query = "PRODUCE TO my-topic: Hello World";
        assert!(query.to_uppercase().starts_with("PRODUCE TO"));

        let query2 = "CONSUME FROM my-topic LIMIT 10";
        assert!(query2.to_uppercase().starts_with("CONSUME FROM"));
    }
}