akita 0.7.0

Akita - Mini orm for rust.
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
use crate::database_err;
use crate::driver::DriverType;
use crate::errors::AkitaError;
use akita_core::SqlSecurityConfig;
use std::collections::HashMap;
use std::fmt::Write;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use url::Url;

#[derive(Debug, Clone)]
pub struct XmlSqlLoaderConfig {
    pub auto_reload: bool,
    pub parameter_detection: bool,
    pub sql_formatting: bool,
}

impl Default for XmlSqlLoaderConfig {
    fn default() -> Self {
        Self {
            auto_reload: false,
            parameter_detection: true,
            sql_formatting: true,
        }
    }
}

/// Parsed connection information cached
#[derive(Clone, Debug, Default)]
struct ParsedConnectionInfo {
    pub platform: DriverType,
    pub hostname: Option<String>,
    pub port: Option<u16>,
    pub database: Option<String>,
    pub username: Option<String>,
    pub password: Option<String>,
    pub extra_params: HashMap<String, String>,
}

/// The main configuration structure
#[derive(Clone, Debug)]
pub struct AkitaConfig {
    // Connection pool configuration
    connection_timeout: Duration,
    idle_timeout: Option<Duration>,
    max_lifetime: Option<Duration>,
    min_idle: Option<u32>,
    max_size: u32,
    test_on_check_out: bool,

    // SQL Related Configuration
    pub platform: DriverType,
    pub sql_security: Option<SqlSecurityConfig>,
    pub xml_sql_loader: Option<XmlSqlLoaderConfig>,

    // Connection information (externally configurable)
    url: Option<String>,
    hostname: Option<String>,
    port: Option<u16>,
    database: Option<String>,
    username: Option<String>,
    password: Option<String>,
    extra_params: HashMap<String, String>,

    // Internal state
    parsed_cache: Arc<RwLock<Option<(ParsedConnectionInfo, u64)>>>,
}

impl Default for AkitaConfig {
    fn default() -> Self {
        Self {
            connection_timeout: Duration::from_secs(30),
            idle_timeout: Some(Duration::from_secs(600)),
            max_lifetime: Some(Duration::from_secs(1800)),
            min_idle: Some(1),
            max_size: 10,
            test_on_check_out: true,
            platform: DriverType::MySQL,
            sql_security: None,
            xml_sql_loader: None,
            url: None,
            hostname: None,
            port: None,
            database: None,
            username: None,
            password: None,
            extra_params: HashMap::new(),
            parsed_cache: Arc::new(RwLock::new(None)),
        }
    }
}

impl AkitaConfig {
    pub fn new() -> Self {
        Self::default()
    }

    // Setting methods
    pub fn url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self.invalidate_cache();
        self
    }

    pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
        self.hostname = Some(hostname.into());
        self.invalidate_cache();
        self
    }

    pub fn max_size(mut self, max_size: u32) -> Self {
        self.max_size = max_size;
        self
    }

    pub fn min_idle(mut self, min_idle: u32) -> Self {
        self.min_idle = Some(min_idle);
        self
    }

    pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
        self.connection_timeout = connection_timeout;
        self
    }

    pub fn idle_timeout(mut self, idle_timeout: Duration) -> Self {
        self.idle_timeout = Some(idle_timeout);
        self
    }

    pub fn port(mut self, port: u16) -> Self {
        self.port = Some(port);
        self.invalidate_cache();
        self
    }

    pub fn database(mut self, database: impl Into<String>) -> Self {
        self.database = Some(database.into());
        self.invalidate_cache();
        self
    }

    pub fn username(mut self, username: impl Into<String>) -> Self {
        self.username = Some(username.into());
        self.invalidate_cache();
        self
    }

    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.password = Some(password.into());
        self.invalidate_cache();
        self
    }

    pub fn platform(mut self, platform: DriverType) -> Self {
        self.platform = platform;
        self
    }

    pub fn param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra_params.insert(key.into(), value.into());
        self.invalidate_cache();
        self
    }

    // Get method (resolve if needed)
    pub fn get_connection_timeout(&self) -> Duration {
        self.connection_timeout
    }

    pub fn get_idle_timeout(&self) -> Duration {
        self.idle_timeout
            .unwrap_or_else(|| Duration::from_secs(600))
    }

    pub fn get_max_lifetime(&self) -> Duration {
        self.max_lifetime
            .unwrap_or_else(|| Duration::from_secs(1800))
    }

    pub fn get_min_idle(&self) -> u32 {
        self.min_idle.unwrap_or(1)
    }

    pub fn get_max_size(&self) -> u32 {
        self.max_size
    }

    pub fn get_test_on_check_out(&self) -> bool {
        self.test_on_check_out
    }

    pub fn get_url(&self) -> Option<&str> {
        self.url.as_deref()
    }

    pub fn sql_security(&self) -> Option<&SqlSecurityConfig> {
        self.sql_security.as_ref()
    }

    pub fn xml_sql_loader(&self) -> Option<&XmlSqlLoaderConfig> {
        self.xml_sql_loader.as_ref()
    }

    pub fn has_url(&self) -> bool {
        self.url.is_some()
    }

    pub fn get_platform(&self) -> Result<DriverType, AkitaError> {
        self.ensure_parsed().map(|info| info.platform.clone())
    }

    pub fn get_hostname(&self) -> Result<Option<String>, AkitaError> {
        let info = self.ensure_parsed()?;
        Ok(info.hostname)
    }

    pub fn get_port(&self) -> Result<Option<u16>, AkitaError> {
        let info = self.ensure_parsed()?;
        Ok(info.port)
    }

    pub fn get_database(&self) -> Result<Option<String>, AkitaError> {
        let info = self.ensure_parsed()?;
        Ok(info.database)
    }

    pub fn get_username(&self) -> Result<Option<String>, AkitaError> {
        let info = self.ensure_parsed()?;
        Ok(info.username)
    }

    pub fn get_password(&self) -> Result<Option<String>, AkitaError> {
        let info = self.ensure_parsed()?;
        Ok(info.password)
    }

    pub fn get_params(&self) -> Result<HashMap<String, String>, AkitaError> {
        self.ensure_parsed().map(|info| info.extra_params.clone())
    }

    pub fn get_connection_string(&self) -> Result<String, AkitaError> {
        self.build_connection_string()
    }

    // Internal methods
    pub fn invalidate_cache(&mut self) {
        let mut cache = self.parsed_cache.write().unwrap();
        *cache = None;
    }

    fn ensure_parsed(&self) -> Result<ParsedConnectionInfo, AkitaError> {
        // Calculate the hash of the current configuration
        let current_hash = self.config_hash();

        {
            let cache = self.parsed_cache.read().unwrap();
            if let Some((info, cached_hash)) = cache.as_ref() {
                if current_hash == *cached_hash {
                    return Ok(info.clone());
                }
            }
        }

        // Need to parse
        let info = self.parse()?;
        let mut cache = self.parsed_cache.write().unwrap();
        *cache = Some((info.clone(), current_hash));
        Ok(info)
    }

    fn config_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();

        // Hash all relevant fields
        self.url.hash(&mut hasher);
        self.hostname.hash(&mut hasher);
        self.port.hash(&mut hasher);
        self.database.hash(&mut hasher);
        self.username.hash(&mut hasher);
        self.password.hash(&mut hasher);

        // EXTRA_PARAMS ARE SORTED TO ENSURE CONSISTENCY
        let mut params: Vec<_> = self.extra_params.iter().collect();
        params.sort_by_key(|(k, _)| *k);
        for (k, v) in params {
            k.hash(&mut hasher);
            v.hash(&mut hasher);
        }

        // If both URL and individual parameters are set, we need to combine them
        self.connection_timeout.as_secs().hash(&mut hasher);

        hasher.finish()
    }

    fn parse(&self) -> Result<ParsedConnectionInfo, AkitaError> {
        let mut info = ParsedConnectionInfo::default();

        // Urls are treated first, but separate parameters are allowed to override values in urls
        if let Some(url) = &self.url {
            self.parse_url(url, &mut info)?;
        }

        // Individually set parameters override the resolved URL values
        self.apply_individual_parameters(&mut info);

        // If the URL does not specify a platform, try to infer it from the parameters
        if info.platform == DriverType::Unsupported {
            info.platform = self.detect_platform_from_params();
        }

        Ok(info)
    }

    fn apply_individual_parameters(&self, info: &mut ParsedConnectionInfo) {
        // Separately set parameters override the URL resolved values
        if let Some(hostname) = &self.hostname {
            info.hostname = Some(hostname.clone());
        }

        if self.port.is_some() {
            info.port = self.port;
        }

        if let Some(database) = &self.database {
            info.database = Some(database.clone());
        }

        if let Some(username) = &self.username {
            info.username = Some(username.clone());
        }

        if let Some(password) = &self.password {
            info.password = Some(password.clone());
        }

        // Combining additional parameters (individually set higher priority)
        for (key, value) in &self.extra_params {
            info.extra_params.insert(key.clone(), value.clone());
        }
    }

    fn parse_url(&self, url: &str, info: &mut ParsedConnectionInfo) -> Result<(), AkitaError> {
        let url = url.trim();

        if url.is_empty() {
            return Ok(());
        }

        // Detecting protocol type
        if let Some(url_without_jdbc) = url.strip_prefix("jdbc:") {
            self.parse_jdbc_url(url_without_jdbc, info)
        } else if url.starts_with("mysql://") {
            info.platform = DriverType::MySQL;
            self.parse_standard_url(url, info)
        } else if url.starts_with("postgresql://") || url.starts_with("postgres://") {
            info.platform = DriverType::Postgres;
            self.parse_standard_url(url, info)
        } else if url.starts_with("oracle://") {
            info.platform = DriverType::Oracle;
            self.parse_standard_url(url, info)
        } else if url.starts_with("sqlserver://") || url.starts_with("mssql://") {
            info.platform = DriverType::Mssql;
            self.parse_standard_url(url, info)
        } else if url.contains(';') && (url.contains("Server=") || url.contains("Database=")) {
            // ADO.NET format
            info.platform = DriverType::Mssql;
            self.parse_adonet_url(url, info)
        } else if url.ends_with(".db")
            || url.ends_with(".sqlite")
            || url.contains(".sqlite")
            || url == ":memory:"
        {
            // SQLite file
            info.platform = DriverType::Sqlite;
            info.database = Some(url.to_string());
            Ok(())
        } else if url.contains('@') && !url.contains("://") {
            // Oracle Easy Connect
            info.platform = DriverType::Oracle;
            self.parse_oracle_easy_connect(url, info)
        } else {
            // Attempt to resolve to a standard URL or file path
            self.try_parse_as_generic(url, info)
        }
    }

    fn try_parse_as_generic(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        // Try to resolve as a standard URL
        if let Ok(parsed) = Url::parse(url) {
            // Guess the platform based on the protocol
            info.platform = match parsed.scheme() {
                "mysql" => DriverType::MySQL,
                "postgresql" | "postgres" => DriverType::Postgres,
                "oracle" => DriverType::Oracle,
                "sqlserver" | "mssql" => DriverType::Mssql,
                _ => DriverType::Unsupported,
            };

            self.parse_standard_url(url, info)
        } else {
            // If not a valid URL, try as a file path
            if url.contains('/') || url.contains('\\') || url.contains('.') {
                info.platform = DriverType::Sqlite;
                info.database = Some(url.to_string());
                Ok(())
            } else {
                Err(database_err!(format!(
                    "Unresolvable connection string: {}",
                    url
                )))
            }
        }
    }

    fn parse_jdbc_url(&self, url: &str, info: &mut ParsedConnectionInfo) -> Result<(), AkitaError> {
        if let Some(url_without_mysql) = url.strip_prefix("mysql:") {
            info.platform = DriverType::MySQL;
            self.parse_standard_url(url_without_mysql, info)
        } else if let Some(url_without_pg) = url.strip_prefix("postgresql:") {
            info.platform = DriverType::Postgres;
            self.parse_standard_url(url_without_pg, info)
        } else if let Some(url_without_oracle) = url.strip_prefix("oracle:") {
            info.platform = DriverType::Oracle;
            self.parse_oracle_jdbc_url(url_without_oracle, info)
        } else if let Some(url_without_sqlserver) = url
            .strip_prefix("sqlserver:")
            .or_else(|| url.strip_prefix("microsoft:sqlserver:"))
        {
            info.platform = DriverType::Mssql;
            self.parse_sqlserver_jdbc_url(url_without_sqlserver, info)
        } else if let Some(url_without_sqlite) = url.strip_prefix("sqlite:") {
            info.platform = DriverType::Sqlite;
            info.database = Some(url_without_sqlite.to_string());
            Ok(())
        } else {
            Err(database_err!(format!(
                "Unsupported JDBC drivers: jdbc:{}",
                url
            )))
        }
    }

    fn parse_standard_url(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        let parsed =
            Url::parse(url).map_err(|e| database_err!(format!("Failed URL resolution: {}", e)))?;

        // Only set values that are not specified in separate parameters
        if info.hostname.is_none() {
            if let Some(host) = parsed.host_str() {
                info.hostname = Some(host.to_string());
            }
        }

        if info.port.is_none() {
            info.port = parsed.port();
        }

        // Resolving the database name
        if info.database.is_none() {
            let path = parsed.path();
            if !path.is_empty() && path != "/" {
                let db_name = path.trim_start_matches('/');
                if !db_name.is_empty() {
                    info.database = Some(db_name.to_string());
                }
            }
        }

        // Username and password (only use the URL if a separate parameter is not set)
        if info.username.is_none() {
            let username = parsed.username();
            if !username.is_empty() {
                info.username = Some(username.to_string());
            }
        }

        if info.password.is_none() {
            if let Some(password) = parsed.password() {
                info.password = Some(password.to_string());
            }
        }

        // Query parameters (merged, URL parameters have lower priority)
        for (key, value) in parsed.query_pairs() {
            if !info.extra_params.contains_key(&key.to_string()) {
                info.extra_params
                    .insert(key.into_owned(), value.into_owned());
            }
        }

        Ok(())
    }

    fn parse_oracle_jdbc_url(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        let url = if let Some(url_without_thin) = url.strip_prefix("thin:@") {
            url_without_thin
        } else if let Some(url_without_oci) = url.strip_prefix("oci:@") {
            url_without_oci
        } else if let Some(url_without_at) = url.strip_prefix('@') {
            url_without_at
        } else {
            url
        };

        // parsing host:port/service or host:port:service
        if url.contains('/') {
            let (host_port, service) = url
                .split_once('/')
                .ok_or_else(|| database_err!("Invalid Oracle URL format".to_string()))?;

            info.database = Some(service.to_string());
            self.parse_host_port(host_port, info)
        } else if url.contains(':') {
            let parts: Vec<&str> = url.split(':').collect();
            match parts.len() {
                2 => {
                    // host:port
                    info.hostname = Some(parts[0].to_string());
                    info.port = parts[1].parse().ok();
                    Ok(())
                }
                3 => {
                    // host:port:service
                    info.hostname = Some(parts[0].to_string());
                    info.port = parts[1].parse().ok();
                    info.database = Some(parts[2].to_string());
                    Ok(())
                }
                _ => Err(database_err!("Invalid Oracle connection string".to_string())),
            }
        } else {
            // Hostname only
            info.hostname = Some(url.to_string());
            Ok(())
        }
    }

    fn parse_sqlserver_jdbc_url(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        if let Some(rest) = url.strip_prefix("//") {
            let (host_port, params) = rest.split_once(';').unwrap_or((rest, ""));

            // Resolve the host and port
            self.parse_host_port(host_port, info)?;

            // Parsing parameters
            for param in params.split(';') {
                if let Some((key, value)) = param.split_once('=') {
                    let key_lower = key.to_lowercase();
                    match key_lower.as_str() {
                        "databasename" | "databasename" | "database" => {
                            info.database = Some(value.to_string());
                        }
                        "user" | "username" => {
                            info.username = Some(value.to_string());
                        }
                        "password" => {
                            info.password = Some(value.to_string());
                        }
                        _ => {
                            info.extra_params.insert(key_lower, value.to_string());
                        }
                    }
                }
            }
        }

        Ok(())
    }

    fn parse_host_port(
        &self,
        host_port: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        if let Some((host, port)) = host_port.split_once(':') {
            info.hostname = Some(host.to_string());
            let port = port
                .parse::<u16>()
                .map_err(|_| database_err!(format!("Invalid port number: {}", port)))?;
            info.port = Some(port);
        } else {
            info.hostname = Some(host_port.to_string());
        }
        Ok(())
    }

    fn parse_native_url(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        if url.starts_with("mysql://") {
            info.platform = DriverType::MySQL;
        } else if url.starts_with("postgresql://") || url.starts_with("postgres://") {
            info.platform = DriverType::Postgres;
        } else if url.starts_with("oracle://") {
            info.platform = DriverType::Oracle;
        } else if url.starts_with("sqlserver://") || url.starts_with("mssql://") {
            info.platform = DriverType::Mssql;
        } else {
            return Err(database_err!(format!("Unsupported protocol: {}", url)));
        }

        self.parse_standard_url(url, info)
    }

    fn parse_adonet_url(
        &self,
        conn_str: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        info.platform = DriverType::Mssql;

        for part in conn_str.split(';') {
            let part = part.trim();
            if part.is_empty() {
                continue;
            }

            let kv: Vec<&str> = part.splitn(2, '=').collect();
            if kv.len() == 2 {
                let key = kv[0].trim().to_lowercase();
                let value = kv[1].trim();

                match key.as_str() {
                    "server" | "data source" => {
                        let parts: Vec<&str> = value.split(',').collect();
                        info.hostname = Some(parts[0].to_string());
                        if parts.len() > 1 {
                            info.port = parts[1].parse().ok();
                        }
                    }
                    "database" | "initial catalog" => {
                        info.database = Some(value.to_string());
                    }
                    "user id" | "uid" => {
                        info.username = Some(value.to_string());
                    }
                    "password" | "pwd" => {
                        info.password = Some(value.to_string());
                    }
                    "port" => {
                        info.port = value.parse().ok();
                    }
                    _ => {
                        info.extra_params.insert(key, value.to_string());
                    }
                }
            }
        }

        Ok(())
    }

    fn parse_oracle_easy_connect(
        &self,
        url: &str,
        info: &mut ParsedConnectionInfo,
    ) -> Result<(), AkitaError> {
        info.platform = DriverType::Oracle;

        let parts: Vec<&str> = url.split('@').collect();
        if parts.len() != 2 {
            return Err(database_err!("Invalid Oracle connection string".to_string()));
        }

        // Resolve the username and password
        let cred_part = parts[0];
        if let Some(slash_pos) = cred_part.find('/') {
            info.username = Some(cred_part[..slash_pos].to_string());
            info.password = Some(cred_part[slash_pos + 1..].to_string());
        } else {
            info.username = Some(cred_part.to_string());
        }

        // Parsing connection information
        let connect_part = parts[1];
        if connect_part.contains('/') {
            let connect_parts: Vec<&str> = connect_part.splitn(2, '/').collect();
            let host_port = connect_parts[0];
            info.database = Some(connect_parts[1].to_string());

            let host_port_parts: Vec<&str> = host_port.split(':').collect();
            if !host_port_parts.is_empty() {
                info.hostname = Some(host_port_parts[0].to_string());
                if host_port_parts.len() > 1 {
                    info.port = host_port_parts[1].parse().ok();
                }
            }
        } else {
            let host_port_parts: Vec<&str> = connect_part.split(':').collect();
            if !host_port_parts.is_empty() {
                info.hostname = Some(host_port_parts[0].to_string());
                if host_port_parts.len() > 1 {
                    info.port = host_port_parts[1].parse().ok();
                }
            }
        }

        Ok(())
    }

    fn detect_platform_from_params(&self) -> DriverType {
        // Guess the platform based on the database name or other parameters
        if let Some(db) = self.database.as_ref() {
            if db.ends_with(".db") || db.ends_with(".sqlite") || db == ":memory:" {
                return DriverType::Sqlite;
            }
        }
        self.platform.clone()
    }

    fn build_connection_string(&self) -> Result<String, AkitaError> {
        let info = self.ensure_parsed()?;

        match info.platform {
            DriverType::MySQL => self.build_mysql_url(&info),
            DriverType::Postgres => self.build_postgres_url(&info),
            DriverType::Oracle => self.build_oracle_url(&info),
            DriverType::Mssql => self.build_sqlserver_url(&info),
            DriverType::Sqlite => Ok(info.database.unwrap_or_else(|| ":memory:".to_string())),
            DriverType::Unsupported => Err(database_err!("Unknown database type".to_string())),
        }
    }

    fn build_mysql_url(&self, info: &ParsedConnectionInfo) -> Result<String, AkitaError> {
        let mut url = String::from("mysql://");

        match (&info.username, &info.password) {
            (Some(u), Some(p)) => {
                write!(&mut url, "{}:{}@", u, p).map_err(|err| database_err!(err.to_string()))?
            }
            (Some(u), None) => {
                write!(&mut url, "{}@", u).map_err(|err| database_err!(err.to_string()))?
            }
            _ => {}
        }

        if let Some(host) = &info.hostname {
            if let Some(port) = info.port {
                write!(&mut url, "{}:{}", host, port)
                    .map_err(|err| database_err!(err.to_string()))?;
            } else {
                url.push_str(host);
            }
        }

        if let Some(db) = &info.database {
            write!(&mut url, "/{}", db).map_err(|err| database_err!(err.to_string()))?;
        }

        if !info.extra_params.is_empty() {
            url.push('?');
            let params: String = info
                .extra_params
                .iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect::<Vec<_>>()
                .join("&");
            url.push_str(&params);
        }

        Ok(url)
    }

    fn build_postgres_url(&self, info: &ParsedConnectionInfo) -> Result<String, AkitaError> {
        let mut url = String::from("postgresql://");

        // The username and password section
        match (&info.username, &info.password) {
            (Some(u), Some(p)) => {
                write!(&mut url, "{}:{}@", u, p).map_err(|err| database_err!(err.to_string()))?
            }
            (Some(u), None) => {
                write!(&mut url, "{}@", u).map_err(|err| database_err!(err.to_string()))?
            }
            _ => {}
        }

        // Host port section
        if let Some(host) = &info.hostname {
            if let Some(port) = info.port {
                write!(&mut url, "{}:{}", host, port)
                    .map_err(|err| database_err!(err.to_string()))?;
            } else {
                url.push_str(host);
            }
        }

        // Database part
        if let Some(db) = &info.database {
            write!(&mut url, "/{}", db).map_err(|err| database_err!(err.to_string()))?;
        }

        // Parameters part
        if !info.extra_params.is_empty() {
            url.push('?');
            for (i, (k, v)) in info.extra_params.iter().enumerate() {
                if i > 0 {
                    url.push('&');
                }
                write!(&mut url, "{}={}", k, v).map_err(|err| database_err!(err.to_string()))?;
            }
        }

        Ok(url)
    }

    fn build_oracle_url(&self, info: &ParsedConnectionInfo) -> Result<String, AkitaError> {
        let mut url = String::new();

        if let (Some(username), Some(password)) = (&info.username, &info.password) {
            url.push_str(&format!("{}/{}", username, password));
        } else if let Some(username) = &info.username {
            url.push_str(username);
        }

        if info.hostname.is_some() {
            url.push('@');
            if let Some(host) = &info.hostname {
                url.push_str(host);
                if let Some(port) = info.port {
                    url.push_str(&format!(":{}", port));
                }
            }

            if let Some(service) = &info.database {
                url.push_str(&format!("/{}", service));
            }
        }

        Ok(url)
    }

    fn build_sqlserver_url(&self, info: &ParsedConnectionInfo) -> Result<String, AkitaError> {
        let mut parts = Vec::new();

        if let Some(host) = &info.hostname {
            let server_str = if let Some(port) = info.port {
                format!("{},{}", host, port)
            } else {
                host.clone()
            };
            parts.push(format!("Server={}", server_str));
        }

        if let Some(db) = &info.database {
            parts.push(format!("Database={}", db));
        }

        if let Some(username) = &info.username {
            parts.push(format!("User Id={}", username));
        }

        if let Some(password) = &info.password {
            parts.push(format!("Password={}", password));
        }

        for (key, value) in &info.extra_params {
            parts.push(format!("{}={}", key, value));
        }

        Ok(parts.join(";"))
    }
}