-- |-- The names hasql prepares statements under, as the server sees them.---- The unit tests in @Hasql.ConnectionState.StatementCacheSpec@ pin the naming-- function itself. This pins the property that motivates it: the name is a-- pure function of the statement, so it is the same on every connection.-- PgBouncer's transaction pooling depends on that — it is how it recognises-- that a statement is already prepared on whichever server connection a-- transaction lands on — and a driver naming statements per connection keeps-- working fine against a bare Postgres, so nothing else here would notice the-- property regressing.module Integration.Sharing.Statement.PreparedStatementNamingSpec (spec) whereimport Data.Text qualified as Textimport Hasql.Connection qualified as Connectionimport Hasql.Decoders qualified as Decodersimport Hasql.Encoders qualified as Encodersimport Hasql.Session qualified as Sessionimport Hasql.Statement qualified as Statementimport Helpers.Dsls.Execution qualified as Executionimport Helpers.Scripts qualified as Scriptsimport Helpers.Statements qualified as Statementsimport Preludeimport Test.Hspecspec :: SpecWith Scripts.ScopeParamsspec = describe "Prepared statement naming" do it "gives the same statement the same name on different connections" \config -> do firstNames <- Scripts.onPreparableConnection config \connection -> do result <- Connection.use connection (Session.statement () selectFortyTwo) result `shouldBe` Right 42 selectNames connection secondNames <- Scripts.onPreparableConnection config \connection -> do result <- Connection.use connection (Session.statement () selectFortyTwo) result `shouldBe` Right 42 selectNames connection secondNames `shouldBe` firstNames it "gives different names to the same SQL under different parameter types" \config -> do Scripts.onPreparableConnection config \connection -> do int4Result <- Connection.use connection (Session.statement 1 selectInt4Param) int4Result `shouldBe` Right 1 textResult <- Connection.use connection (Session.statement "a" selectTextParam) textResult `shouldBe` Right "a" names <- selectNames connection -- Two server-side plans, because the parameter OIDs are part of the -- statement's identity. Collapsing them onto one name is a 42P05. length names `shouldBe` 2 it "names statements with an identifier-safe prefix" \config -> do Scripts.onPreparableConnection config \connection -> do _ <- Connection.use connection (Session.statement () selectFortyTwo) names <- selectNames connection -- Unquoted-legal and greppable in pg_prepared_statements and in a -- pooler's logs, and it cannot collide with a user's own PREPARE. all (Text.isPrefixOf "hasql_") names `shouldBe` TrueselectNames :: Connection.Connection -> IO [Text]selectNames connection = do result <- Connection.use connection $ Execution.sessionByParams Statements.SelectPreparedStatementNames case result of Right names -> pure names Left err -> fail ("Failed to read prepared statement names: " <> show err)selectFortyTwo :: Statement.Statement () Int32selectFortyTwo = Statement.preparable "select 42" mempty (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.int4)))selectInt4Param :: Statement.Statement Int32 Int32selectInt4Param = Statement.preparable "select $1" (Encoders.param (Encoders.nonNullable Encoders.int4)) (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.int4)))selectTextParam :: Statement.Statement Text TextselectTextParam = Statement.preparable "select $1" (Encoders.param (Encoders.nonNullable Encoders.text)) (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.text)))