kotoba-handler 0.1.22

Unified Web Handler for Kotoba ecosystem - integrates server, CLI, and WASM execution
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Database Handler Module
//!
//! このモジュールは様々なデータベースとの統合を提供します。
//! PostgreSQL、MySQL、SQLite、Redisなどのデータベースをサポートします。

use crate::{HandlerError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// サポートされるデータベースタイプ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DatabaseType {
    PostgreSQL,
    MySQL,
    SQLite,
    Redis,
}

/// データベース設定
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
    pub db_type: DatabaseType,
    pub connection_string: String,
    pub max_connections: u32,
    pub min_connections: u32,
    pub connection_timeout: u64, // seconds
    pub command_timeout: u64,    // seconds
}

/// データベース接続
#[cfg_attr(feature = "sqlx", derive(Clone))]
pub struct DatabaseConnection {
    config: DatabaseConfig,
    #[cfg(feature = "sqlx")]
    pool: Option<sqlx::Pool<sqlx::Any>>,
    #[cfg(feature = "redis")]
    redis_client: Option<redis::Client>,
}

impl DatabaseConnection {
    /// 新しいデータベース接続を作成
    pub fn new(config: DatabaseConfig) -> Self {
        Self {
            config,
            #[cfg(feature = "sqlx")]
            pool: None,
            #[cfg(feature = "redis")]
            redis_client: None,
        }
    }
}

/// データベース操作結果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
    pub rows_affected: u64,
    pub last_insert_id: Option<i64>,
    pub columns: Vec<String>,
    pub rows: Vec<HashMap<String, serde_json::Value>>,
}

/// データベースクエリ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseQuery {
    pub sql: String,
    pub params: Vec<serde_json::Value>,
    pub query_type: QueryType,
}

/// クエリタイプ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryType {
    Select,
    Insert,
    Update,
    Delete,
    Create,
    Drop,
    Other,
}

/// データベース接続を初期化
#[cfg(feature = "sqlx")]
pub async fn init_connection(url: &str) -> Result<DatabaseConnection> {
    use sqlx::Connection;

    // URLからデータベースタイプを推測
    let db_type = if url.starts_with("postgresql://") {
        DatabaseType::PostgreSQL
    } else if url.starts_with("mysql://") {
        DatabaseType::MySQL
    } else if url.starts_with("sqlite://") {
        DatabaseType::SQLite
    } else if url.starts_with("redis://") {
        DatabaseType::Redis
    } else {
        return Err(HandlerError::Jsonnet(
            "Unsupported database URL format".to_string()
        ));
    };

    let config = DatabaseConfig {
        db_type,
        connection_string: url.to_string(),
        max_connections: 10,
        min_connections: 1,
        connection_timeout: 30,
        command_timeout: 60,
    };

    let mut connection = DatabaseConnection::new(config);

    match connection.config.db_type {
        DatabaseType::PostgreSQL | DatabaseType::MySQL | DatabaseType::SQLite => {
            let pool = sqlx::Pool::<sqlx::Any>::connect(&connection.config.connection_string)
                .await
                .map_err(|e| HandlerError::Jsonnet(format!("Database connection error: {}", e)))?;

            connection.pool = Some(pool);
        }
        DatabaseType::Redis => {
            let client = redis::Client::open(connection.config.connection_string.clone())
                .map_err(|e| HandlerError::Jsonnet(format!("Redis connection error: {}", e)))?;

            connection.redis_client = Some(client);
        }
    }

    Ok(connection)
}

/// SQLクエリを実行
#[cfg(feature = "sqlx")]
pub async fn execute_sql_query(
    connection: &DatabaseConnection,
    query: DatabaseQuery
) -> Result<QueryResult> {
    if let Some(pool) = &connection.pool {
        match query.query_type {
            QueryType::Select => {
                let rows = sqlx::query(&query.sql)
                    .fetch_all(pool)
                    .await
                    .map_err(|e| HandlerError::Jsonnet(format!("Query execution error: {}", e)))?;

                let mut result_rows = Vec::new();
                let mut columns = Vec::new();

                if let Some(first_row) = rows.first() {
                    columns = first_row.columns().iter().map(|c| c.name().to_string()).collect();
                }

                for row in rows {
                    let mut row_data = HashMap::new();
                    for (i, column) in columns.iter().enumerate() {
                        let value: serde_json::Value = match row.try_get::<Option<String>, _>(i) {
                            Ok(Some(s)) => serde_json::Value::String(s),
                            Ok(None) => serde_json::Value::Null,
                            Err(_) => match row.try_get::<Option<i32>, _>(i) {
                                Ok(Some(n)) => serde_json::Value::Number(n.into()),
                                Ok(None) => serde_json::Value::Null,
                                Err(_) => match row.try_get::<Option<bool>, _>(i) {
                                    Ok(Some(b)) => serde_json::Value::Bool(b),
                                    Ok(None) => serde_json::Value::Null,
                                    Err(_) => serde_json::Value::String("unknown_type".to_string()),
                                },
                            },
                        };
                        row_data.insert(column.clone(), value);
                    }
                    result_rows.push(row_data);
                }

                Ok(QueryResult {
                    rows_affected: result_rows.len() as u64,
                    last_insert_id: None,
                    columns,
                    rows: result_rows,
                })
            }
            QueryType::Insert | QueryType::Update | QueryType::Delete => {
                let result = sqlx::query(&query.sql)
                    .execute(pool)
                    .await
                    .map_err(|e| HandlerError::Jsonnet(format!("Query execution error: {}", e)))?;

                Ok(QueryResult {
                    rows_affected: result.rows_affected(),
                    last_insert_id: Some(result.last_insert_rowid() as i64),
                    columns: vec![],
                    rows: vec![],
                })
            }
            _ => {
                sqlx::query(&query.sql)
                    .execute(pool)
                    .await
                    .map_err(|e| HandlerError::Jsonnet(format!("Query execution error: {}", e)))?;

                Ok(QueryResult {
                    rows_affected: 0,
                    last_insert_id: None,
                    columns: vec![],
                    rows: vec![],
                })
            }
        }
    } else {
        Err(HandlerError::Jsonnet("Database pool not initialized".to_string()))
    }
}

/// Redis操作を実行
#[cfg(feature = "redis")]
pub async fn execute_redis_command(
    connection: &DatabaseConnection,
    command: &str,
    args: Vec<String>
) -> Result<serde_json::Value> {
    if let Some(client) = &connection.redis_client {
        let mut conn = client.get_async_connection().await
            .map_err(|e| HandlerError::Jsonnet(format!("Redis connection error: {}", e)))?;

        use redis::AsyncCommands;

        let result: redis::Value = match command.to_uppercase().as_str() {
            "GET" => {
                if let Some(key) = args.first() {
                    conn.get(key).await
                        .map_err(|e| HandlerError::Jsonnet(format!("Redis GET error: {}", e)))?
                } else {
                    return Err(HandlerError::Jsonnet("GET command requires a key".to_string()));
                }
            }
            "SET" => {
                if args.len() >= 2 {
                    conn.set(&args[0], &args[1]).await
                        .map_err(|e| HandlerError::Jsonnet(format!("Redis SET error: {}", e)))?
                } else {
                    return Err(HandlerError::Jsonnet("SET command requires key and value".to_string()));
                }
            }
            "DEL" => {
                if let Some(key) = args.first() {
                    conn.del(key).await
                        .map_err(|e| HandlerError::Jsonnet(format!("Redis DEL error: {}", e)))?
                } else {
                    return Err(HandlerError::Jsonnet("DEL command requires a key".to_string()));
                }
            }
            "EXISTS" => {
                if let Some(key) = args.first() {
                    conn.exists(key).await
                        .map_err(|e| HandlerError::Jsonnet(format!("Redis EXISTS error: {}", e)))?
                } else {
                    return Err(HandlerError::Jsonnet("EXISTS command requires a key".to_string()));
                }
            }
            _ => {
                return Err(HandlerError::Jsonnet(format!("Unsupported Redis command: {}", command)));
            }
        };

        // Redis ValueをJSONに変換
        let json_value = match result {
            redis::Value::Nil => serde_json::Value::Null,
            redis::Value::Int(i) => serde_json::Value::Number(i.into()),
            redis::Value::Data(bytes) => {
                String::from_utf8(bytes)
                    .map(|s| serde_json::Value::String(s))
                    .unwrap_or(serde_json::Value::String("binary_data".to_string()))
            }
            redis::Value::Bulk(values) => {
                let json_values: Vec<serde_json::Value> = values.into_iter()
                    .map(|v| match v {
                        redis::Value::Data(bytes) => {
                            String::from_utf8(bytes)
                                .map(|s| serde_json::Value::String(s))
                                .unwrap_or(serde_json::Value::String("binary_data".to_string()))
                        }
                        redis::Value::Int(i) => serde_json::Value::Number(i.into()),
                        _ => serde_json::Value::String("complex_type".to_string()),
                    })
                    .collect();
                serde_json::Value::Array(json_values)
            }
            _ => serde_json::Value::String("complex_result".to_string()),
        };

        Ok(json_value)
    } else {
        Err(HandlerError::Jsonnet("Redis client not initialized".to_string()))
    }
}

/// データベースマイグレーションを実行
#[cfg(feature = "sqlx")]
pub async fn run_migration(
    connection: &DatabaseConnection,
    migration_sql: &str
) -> Result<()> {
    if let Some(pool) = &connection.pool {
        sqlx::query(migration_sql)
            .execute(pool)
            .await
            .map_err(|e| HandlerError::Jsonnet(format!("Migration error: {}", e)))?;

        Ok(())
    } else {
        Err(HandlerError::Jsonnet("Database pool not initialized".to_string()))
    }
}

/// データベース接続をテスト
#[cfg(feature = "sqlx")]
pub async fn test_connection(connection: &DatabaseConnection) -> Result<bool> {
    if let Some(pool) = &connection.pool {
        let result = sqlx::query("SELECT 1 as test")
            .fetch_one(pool)
            .await;

        Ok(result.is_ok())
    } else if connection.redis_client.is_some() {
        #[cfg(feature = "redis")]
        {
            if let Some(client) = &connection.redis_client {
                let mut conn = client.get_async_connection().await
                    .map_err(|e| HandlerError::Jsonnet(format!("Redis test connection error: {}", e)))?;

                let _: () = redis::cmd("PING").query_async(&mut conn).await
                    .map_err(|e| HandlerError::Jsonnet(format!("Redis PING error: {}", e)))?;

                Ok(true)
            } else {
                Ok(false)
            }
        }
        #[cfg(not(feature = "redis"))]
        {
            Ok(false)
        }
    } else {
        Ok(false)
    }
}

/// 簡易クエリビルダー
pub struct QueryBuilder {
    table: String,
    columns: Vec<String>,
    conditions: Vec<String>,
    order_by: Option<String>,
    limit: Option<usize>,
    offset: Option<usize>,
}

impl QueryBuilder {
    /// 新しいクエリビルダーを作成
    pub fn new(table: &str) -> Self {
        Self {
            table: table.to_string(),
            columns: vec![],
            conditions: vec![],
            order_by: None,
            limit: None,
            offset: None,
        }
    }

    /// カラムを選択
    pub fn select(mut self, columns: Vec<&str>) -> Self {
        self.columns = columns.into_iter().map(|s| s.to_string()).collect();
        self
    }

    /// 条件を追加
    pub fn where_condition(mut self, condition: &str) -> Self {
        self.conditions.push(condition.to_string());
        self
    }

    /// ソート順を指定
    pub fn order_by(mut self, column: &str, direction: &str) -> Self {
        self.order_by = Some(format!("{} {}", column, direction));
        self
    }

    /// 制限数を指定
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    /// オフセットを指定
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }

    /// SELECTクエリを構築
    pub fn build_select(&self) -> String {
        let columns = if self.columns.is_empty() {
            "*".to_string()
        } else {
            self.columns.join(", ")
        };

        let mut query = format!("SELECT {} FROM {}", columns, self.table);

        if !self.conditions.is_empty() {
            query.push_str(&format!(" WHERE {}", self.conditions.join(" AND ")));
        }

        if let Some(order_by) = &self.order_by {
            query.push_str(&format!(" ORDER BY {}", order_by));
        }

        if let Some(limit) = self.limit {
            query.push_str(&format!(" LIMIT {}", limit));
        }

        if let Some(offset) = self.offset {
            query.push_str(&format!(" OFFSET {}", offset));
        }

        query
    }

    /// INSERTクエリを構築
    pub fn build_insert(&self, values: Vec<Vec<String>>) -> String {
        let columns_str = self.columns.join(", ");
        let placeholders: Vec<String> = (0..self.columns.len())
            .map(|_| "?".to_string())
            .collect();

        let value_placeholders: Vec<String> = values.iter()
            .map(|_| format!("({})", placeholders.join(", ")))
            .collect();

        format!(
            "INSERT INTO {} ({}) VALUES {}",
            self.table,
            columns_str,
            value_placeholders.join(", ")
        )
    }

    /// UPDATEクエリを構築
    pub fn build_update(&self, set_values: HashMap<&str, &str>) -> String {
        let set_clause: Vec<String> = set_values.iter()
            .map(|(col, val)| format!("{} = '{}'", col, val))
            .collect();

        let mut query = format!("UPDATE {} SET {}", self.table, set_clause.join(", "));

        if !self.conditions.is_empty() {
            query.push_str(&format!(" WHERE {}", self.conditions.join(" AND ")));
        }

        query
    }

    /// DELETEクエリを構築
    pub fn build_delete(&self) -> String {
        let mut query = format!("DELETE FROM {}", self.table);

        if !self.conditions.is_empty() {
            query.push_str(&format!(" WHERE {}", self.conditions.join(" AND ")));
        }

        query
    }
}

/// データベーストランザクション
#[cfg(feature = "sqlx")]
pub struct DatabaseTransaction<'a> {
    transaction: sqlx::Transaction<'a, sqlx::Any>,
}

#[cfg(feature = "sqlx")]
impl<'a> DatabaseTransaction<'a> {
    /// 新しいトランザクションを開始
    pub async fn begin(connection: &'a DatabaseConnection) -> Result<Self> {
        if let Some(pool) = &connection.pool {
            let transaction = pool.begin().await
                .map_err(|e| HandlerError::Jsonnet(format!("Transaction begin error: {}", e)))?;

            Ok(Self { transaction })
        } else {
            Err(HandlerError::Jsonnet("Database pool not initialized".to_string()))
        }
    }

    /// クエリを実行
    pub async fn execute(&mut self, query: &str) -> Result<QueryResult> {
        let result = sqlx::query(query)
            .execute(&mut *self.transaction)
            .await
            .map_err(|e| HandlerError::Jsonnet(format!("Transaction query error: {}", e)))?;

        Ok(QueryResult {
            rows_affected: result.rows_affected(),
            last_insert_id: Some(result.last_insert_rowid() as i64),
            columns: vec![],
            rows: vec![],
        })
    }

    /// トランザクションをコミット
    pub async fn commit(self) -> Result<()> {
        self.transaction.commit().await
            .map_err(|e| HandlerError::Jsonnet(format!("Transaction commit error: {}", e)))?;
        Ok(())
    }

    /// トランザクションをロールバック
    pub async fn rollback(self) -> Result<()> {
        self.transaction.rollback().await
            .map_err(|e| HandlerError::Jsonnet(format!("Transaction rollback error: {}", e)))?;
        Ok(())
    }
}

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

    #[test]
    fn test_query_builder_select() {
        let query = QueryBuilder::new("users")
            .select(vec!["id", "name", "email"])
            .where_condition("active = 1")
            .order_by("name", "ASC")
            .limit(10)
            .build_select();

        assert_eq!(query, "SELECT id, name, email FROM users WHERE active = 1 ORDER BY name ASC LIMIT 10");
    }

    #[test]
    fn test_query_builder_insert() {
        let query = QueryBuilder::new("users")
            .select(vec!["name", "email"])
            .build_insert(vec![
                vec!["Alice".to_string(), "alice@example.com".to_string()],
                vec!["Bob".to_string(), "bob@example.com".to_string()],
            ]);

        assert_eq!(query, "INSERT INTO users (name, email) VALUES (?, ?), (?, ?)");
    }

    #[test]
    fn test_query_builder_update() {
        let mut set_values = HashMap::new();
        set_values.insert("name", "Alice Updated");
        set_values.insert("email", "alice.updated@example.com");

        let query = QueryBuilder::new("users")
            .where_condition("id = 1")
            .build_update(set_values);

        assert_eq!(query, "UPDATE users SET name = 'Alice Updated', email = 'alice.updated@example.com' WHERE id = 1");
    }

    #[test]
    fn test_query_builder_delete() {
        let query = QueryBuilder::new("users")
            .where_condition("id = 1")
            .build_delete();

        assert_eq!(query, "DELETE FROM users WHERE id = 1");
    }

    #[test]
    fn test_database_config_creation() {
        let config = DatabaseConfig {
            db_type: DatabaseType::PostgreSQL,
            connection_string: "postgresql://user:pass@localhost/db".to_string(),
            max_connections: 10,
            min_connections: 1,
            connection_timeout: 30,
            command_timeout: 60,
        };

        assert_eq!(config.max_connections, 10);
        assert_eq!(config.connection_timeout, 30);
    }
}