You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add hook for sending pragmas before opening connection #29186
SQLite and SQLCipher have pragma's that need to be set before "normal" queries are executed.
However when the password is set in the connection string, the "normal" query SELECT COUNT(*) FROM sqlite_master; (SqliteConnectionInternal.cs:120) is executed, preventing execution of these special pragma.
varbuilder=newSqliteConnectionStringBuilder{DataSource=Path.GetTempFileName(),Cache=SqliteCacheMode.Private,Mode=SqliteOpenMode.ReadWriteCreate,Pooling=false,Password=Guid.NewGuid().ToString()};for(inti=0;i<2;i++){usingvarconn=newSqliteConnection(builder.ConnectionString);conn.Open();// Fails 2nd time with Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 26: 'file is not a database'.'using(varcommand=conn.CreateCommand()){command.CommandText="PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA256;";command.ExecuteNonQuery();}using(varcommand=conn.CreateCommand()){command.CommandText="PRAGMA journal_mode = WAL";command.ExecuteNonQuery();}}
A few pragma's need to be set before querying sqlite_master, for example:
journal_mode
kdf_iter
cipher_kdf_algorithm
cipher_page_size
It would be nicer if I can somehow define PRAGMAs in a SQLiteConnection before opening the connection. This would also work better with connection pooling because I don't have to manually execute the PRAGMA every time when opening a connection.
changed the title [-]Setting password in combination with other pragma fails[/-][+]Add hook for sending pragmas before opening connection[/+]on Sep 27, 2022
@rgroenewoudt For now, a workaround is to not set the password in the connection string. In the future we may add a hook that would allow pragams to be set before the connection is opened.
@ajcvickers: Is there some way to detect if an pooled SQLiteConnection is a new connection or an existing one from the pool?
I would need to only execute all pragma's on the new connections. Otherwise I would run into the same performance issue as #28774 as resending pragma key is very slow.
I worked around this by turning off pooling, and using a custom pool that opens the connections and runs initial pragma commands. Still, an official solution would be better.
Activity
roji commentedon Sep 23, 2022
Could be a good candidate for a Sqlite DbDataSource feature (i.e. pragmas to set on all created connections at the outset)
[-]Setting password in combination with other pragma fails[/-][+]Add hook for sending pragmas before opening connection[/+]ajcvickers commentedon Sep 27, 2022
@rgroenewoudt For now, a workaround is to not set the password in the connection string. In the future we may add a hook that would allow pragams to be set before the connection is opened.
rgroenewoudt commentedon Sep 28, 2022
@ajcvickers: Is there some way to detect if an pooled SQLiteConnection is a new connection or an existing one from the pool?
I would need to only execute all pragma's on the new connections. Otherwise I would run into the same performance issue as #28774 as resending pragma key is very slow.
chylex commentedon Jul 19, 2023
I worked around this by turning off pooling, and using a custom pool that opens the connections and runs initial pragma commands. Still, an official solution would be better.