DEV: reduce coupling of internal implementation

DEV: reduce coupling of internal implementation

Previously deserializer cache needed to be passed explicitly to prepared connections

This avoids needing to pass it around, this make overriding behaviors somewhat cleaner

deserializer_cache used to be hidden on some connections, it is now globally available

diff --git a/CHANGELOG b/CHANGELOG
index 0f7a36a..88d48d5 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+2021-03-22 - 1.1.3
+
+- DEV: reduce coupling of internal interfaces and allow or cleaner override of prepared connections
+
 2021-03-22 - 1.1.2
 
 - FEATURE: improve compatability with clients overriding raw_connection
diff --git a/lib/mini_sql/mysql/connection.rb b/lib/mini_sql/mysql/connection.rb
index 15375bf..e6b8331 100644
--- a/lib/mini_sql/mysql/connection.rb
+++ b/lib/mini_sql/mysql/connection.rb
@@ -13,7 +13,7 @@ module MiniSql
 
       def prepared(condition = true)
         if condition
-          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+          @prepared ||= PreparedConnection.new(self)
         else
           self
         end
@@ -39,12 +39,12 @@ module MiniSql
 
       def query(sql, *params)
         result = run(sql, :array, params)
-        @deserializer_cache.materialize(result)
+        deserializer_cache.materialize(result)
       end
 
       def query_decorator(decorator, sql, *params)
         result = run(sql, :array, params)
-        @deserializer_cache.materialize(result, decorator)
+        deserializer_cache.materialize(result, decorator)
       end
 
       def escape_string(str)
diff --git a/lib/mini_sql/mysql/prepared_connection.rb b/lib/mini_sql/mysql/prepared_connection.rb
index 3d0e2af..0bd1e64 100644
--- a/lib/mini_sql/mysql/prepared_connection.rb
+++ b/lib/mini_sql/mysql/prepared_connection.rb
@@ -6,10 +6,9 @@ module MiniSql
 
       attr_reader :unprepared
 
-      def initialize(unprepared_connection, deserializer_cache)
+      def initialize(unprepared_connection)
         @unprepared         = unprepared_connection
         @raw_connection     = unprepared_connection.raw_connection
-        @deserializer_cache = deserializer_cache
         @param_encoder      = unprepared_connection.param_encoder
 
         @prepared_cache     = PreparedCache.new(@raw_connection)
@@ -24,6 +23,10 @@ module MiniSql
         condition ? self : @unprepared
       end
 
+      def deserializer_cache
+        @unprepared.deserializer_cache
+      end
+
       private def run(sql, as, params)
         prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
         statement = @prepared_cache.prepare_statement(prepared_sql)
diff --git a/lib/mini_sql/postgres/connection.rb b/lib/mini_sql/postgres/connection.rb
index 5f4443f..121aaba 100644
--- a/lib/mini_sql/postgres/connection.rb
+++ b/lib/mini_sql/postgres/connection.rb
@@ -3,7 +3,7 @@
 module MiniSql
   module Postgres
     class Connection < MiniSql::Connection
-      attr_reader :raw_connection, :param_encoder
+      attr_reader :raw_connection, :param_encoder, :deserializer_cache
 
       def self.default_deserializer_cache
         @deserializer_cache ||= DeserializerCache.new
@@ -48,7 +48,7 @@ module MiniSql
 
       def prepared(condition = true)
         if condition
-          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+          @prepared ||= PreparedConnection.new(self)
         else
           self
         end
@@ -98,7 +98,7 @@ module MiniSql
       def query(sql, *params)
         result = run(sql, params)
         result.type_map = type_map
-        @deserializer_cache.materialize(result)
+        deserializer_cache.materialize(result)
       ensure
         result.clear if result
       end
@@ -121,7 +121,7 @@ module MiniSql
           if result.ntuples == 0
             # skip, this happens at the end when we get totals
           else
-            materializer ||= @deserializer_cache.materializer(result)
+            materializer ||= deserializer_cache.materializer(result)
             result.type_map = type_map
             i = 0
             # technically we should only get 1 row here
@@ -172,7 +172,7 @@ module MiniSql
       def query_decorator(decorator, sql, *params)
         result = run(sql, params)
         result.type_map = type_map
-        @deserializer_cache.materialize(result, decorator)
+        deserializer_cache.materialize(result, decorator)
       ensure
         result.clear if result
       end
diff --git a/lib/mini_sql/postgres/prepared_connection.rb b/lib/mini_sql/postgres/prepared_connection.rb
index dca00e0..b69029d 100644
--- a/lib/mini_sql/postgres/prepared_connection.rb
+++ b/lib/mini_sql/postgres/prepared_connection.rb
@@ -6,10 +6,9 @@ module MiniSql
 
       attr_reader :unprepared
 
-      def initialize(unprepared_connection, deserializer_cache)
+      def initialize(unprepared_connection)
         @unprepared         = unprepared_connection
         @raw_connection     = unprepared_connection.raw_connection
-        @deserializer_cache = deserializer_cache
         @type_map           = unprepared_connection.type_map
         @param_encoder      = unprepared_connection.param_encoder
 
@@ -25,6 +24,10 @@ module MiniSql
         condition ? self : @unprepared
       end
 
+      def deserializer_cache
+        @unprepared.deserializer_cache
+      end
+
       private def run(sql, params)
         prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
         prepare_statement_key = @prepared_cache.prepare_statement(prepared_sql)
diff --git a/lib/mini_sql/sqlite/connection.rb b/lib/mini_sql/sqlite/connection.rb
index 3b9c28e..dc66c4f 100644
--- a/lib/mini_sql/sqlite/connection.rb
+++ b/lib/mini_sql/sqlite/connection.rb
@@ -13,7 +13,7 @@ module MiniSql
 
       def prepared(condition = true)
         if condition
-          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+          @prepared ||= PreparedConnection.new(self)
         else
           self
         end
diff --git a/lib/mini_sql/sqlite/prepared_connection.rb b/lib/mini_sql/sqlite/prepared_connection.rb
index 67bbf3f..45ba8d5 100644
--- a/lib/mini_sql/sqlite/prepared_connection.rb
+++ b/lib/mini_sql/sqlite/prepared_connection.rb
@@ -6,10 +6,9 @@ module MiniSql
 
       attr_reader :unprepared
 
-      def initialize(unprepared_connection, deserializer_cache)
+      def initialize(unprepared_connection)
         @unprepared         = unprepared_connection
         @raw_connection     = unprepared_connection.raw_connection
-        @deserializer_cache = deserializer_cache
         @param_encoder      = unprepared_connection.param_encoder
 
         @prepared_cache     = PreparedCache.new(@raw_connection)
@@ -24,6 +23,10 @@ module MiniSql
         condition ? self : @unprepared
       end
 
+      def deserializer_cache
+        @unprepared.deserializer_cache
+      end
+
       private def run(sql, params)
         prepared_sql, binds, _bind_names = @param_binder.bind(sql, params)
         statement = @prepared_cache.prepare_statement(prepared_sql)
diff --git a/lib/mini_sql/version.rb b/lib/mini_sql/version.rb
index 7e83d64..de4c7ca 100644
--- a/lib/mini_sql/version.rb
+++ b/lib/mini_sql/version.rb
@@ -1,4 +1,4 @@
 # frozen_string_literal: true
 module MiniSql
-  VERSION = "1.1.2"
+  VERSION = "1.1.3"
 end

GitHub sha: 5ed92252