@@ -10,9 +10,12 @@ use std::path::{Path, PathBuf};
1010use std:: sync:: Arc ;
1111
1212#[ cfg( feature = "sqlite" ) ]
13- use sqlx:: sqlite:: Sqlite ;
14- use sqlx:: { AnyPool , Any , Transaction , QueryBuilder } ;
15- use sqlx:: migrate:: MigrateDatabase ;
13+ use sqlx:: sqlite:: { SqliteConnectOptions , SqlitePool } ;
14+ #[ cfg( feature = "postgres" ) ]
15+ use sqlx:: postgres:: { PgConnectOptions , PgPool } ;
16+ #[ cfg( feature = "mysql" ) ]
17+ use sqlx:: mysql:: { MySql , MySqlPool } ;
18+ use sqlx:: { Transaction , QueryBuilder } ;
1619use nostr_database:: prelude:: * ;
1720use tokio:: sync:: Mutex ;
1821
@@ -66,75 +69,73 @@ impl NostrSqlBackend {
6669 }
6770}
6871
69- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
70- enum PoolKind {
72+ #[ derive( Debug , Clone ) ]
73+ enum Db {
7174 #[ cfg( feature = "sqlite" ) ]
72- Sqlite ,
75+ Sqlite ( SqlitePool ) ,
7376 #[ cfg( feature = "postgres" ) ]
74- Postgres ,
77+ Postgres ( PgPool ) ,
7578 #[ cfg( feature = "mysql" ) ]
76- MySql ,
79+ MySql ( MySqlPool ) ,
7780}
7881
7982/// Nostr SQL database
8083#[ derive( Clone ) ]
8184pub struct NostrSql {
82- pool : AnyPool ,
83- kind : PoolKind ,
85+ pool : Db ,
8486 fbb : Arc < Mutex < FlatBufferBuilder < ' static > > > ,
8587}
8688
8789impl fmt:: Debug for NostrSql {
8890 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
8991 f. debug_struct ( "NostrSql" )
9092 . field ( "pool" , & self . pool )
91- . field ( "kind" , & self . kind )
9293 . finish ( )
9394 }
9495}
9596
9697impl NostrSql {
9798 /// Connect to a SQL database
9899 pub async fn new ( backend : NostrSqlBackend ) -> Result < Self , Error > {
99- // Install drivers
100- sqlx:: any:: install_default_drivers ( ) ;
101-
102- let ( pool, kind) = match backend {
100+ let pool = match backend {
103101 #[ cfg( feature = "sqlite" ) ]
104102 NostrSqlBackend :: Sqlite { path} => {
105- let uri: Cow < str > = match path {
106- Some ( path) => Cow :: Owned ( format ! ( "sqlite://{}" , path. display( ) ) ) ,
107- None => Cow :: Borrowed ( "sqlite:memory:" ) ,
103+ let mut opts: SqliteConnectOptions = SqliteConnectOptions :: new ( ) . create_if_missing ( true ) ;
104+
105+ match path {
106+ Some ( path) => opts = opts. filename ( path) ,
107+ None => opts = opts. in_memory ( true ) ,
108108 } ;
109-
110- if !Sqlite :: database_exists ( & uri) . await ? {
111- Sqlite :: create_database ( & uri) . await ?;
112- }
113-
114- let pool: AnyPool = AnyPool :: connect ( & uri) . await ?;
109+
110+
111+ let pool: SqlitePool = SqlitePool :: connect_with ( opts) . await ?;
115112
116113 sqlx:: migrate!( "migrations/sqlite" ) . run ( & pool) . await ?;
117114
118- ( pool , PoolKind :: Sqlite )
115+ Db :: Sqlite ( pool )
119116 }
120117 #[ cfg( feature = "postgres" ) ]
121118 NostrSqlBackend :: Postgres { host, port, username, password, database } => {
122- let uri: String = match ( username, password) {
123- ( Some ( username) , Some ( password) ) => format ! ( "postgres://{username}:{password}@{host}:{port}/{database}" ) ,
124- _ => format ! ( "postgres://{host}:{port}/{database}" )
125- } ;
119+ let mut opts: PgConnectOptions = PgConnectOptions :: new_without_pgpass ( ) . host ( & host) . port ( port) . database ( & database) ;
120+
121+ if let Some ( username) = username {
122+ opts = opts. username ( & username) ;
123+ }
124+
125+ if let Some ( password) = password {
126+ opts = opts. password ( & password) ;
127+ }
126128
127- let pool: AnyPool = AnyPool :: connect ( & uri ) . await ?;
129+ let pool: PgPool = PgPool :: connect_with ( opts ) . await ?;
128130
129131 sqlx:: migrate!( "migrations/postgres" ) . run ( & pool) . await ?;
130132
131- ( pool , PoolKind :: Postgres )
133+ Db :: Postgres ( pool )
132134 }
133135 } ;
134136
135137 Ok ( Self {
136138 pool,
137- kind,
138139 fbb : Arc :: new ( Mutex :: new ( FlatBufferBuilder :: new ( ) ) ) ,
139140 } )
140141 }
0 commit comments