FEATURE: defer creation of prepared connection

FEATURE: defer creation of prepared connection

No need to spin up a cache for a feature not being used.

Also some implementation eg: Discourse override raw_connection initializing early can lead to failures

diff --git a/lib/mini_sql/mysql/connection.rb b/lib/mini_sql/mysql/connection.rb
index 3916b86..15375bf 100644
--- a/lib/mini_sql/mysql/connection.rb
+++ b/lib/mini_sql/mysql/connection.rb
@@ -9,12 +9,14 @@ module MiniSql
         @raw_connection = raw_connection
         @param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self)
         @deserializer_cache = (args && args[:deserializer_cache]) || DeserializerCache.new
-
-        @prepared = PreparedConnection.new(self, @deserializer_cache)
       end
 
       def prepared(condition = true)
-        condition ? @prepared : self
+        if condition
+          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+        else
+          self
+        end
       end
 
       def query_single(sql, *params)
diff --git a/lib/mini_sql/postgres/connection.rb b/lib/mini_sql/postgres/connection.rb
index 9df9e40..5f4443f 100644
--- a/lib/mini_sql/postgres/connection.rb
+++ b/lib/mini_sql/postgres/connection.rb
@@ -40,8 +40,6 @@ module MiniSql
         @deserializer_cache = (args && args[:deserializer_cache]) || self.class.default_deserializer_cache
         @param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self)
         @type_map = args && args[:type_map]
-
-        @prepared = PreparedConnection.new(self, @deserializer_cache)
       end
 
       def type_map
@@ -49,7 +47,11 @@ module MiniSql
       end
 
       def prepared(condition = true)
-        condition ? @prepared : self
+        if condition
+          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+        else
+          self
+        end
       end
 
       # Returns a flat array containing all results.
diff --git a/lib/mini_sql/sqlite/connection.rb b/lib/mini_sql/sqlite/connection.rb
index 5834dee..3b9c28e 100644
--- a/lib/mini_sql/sqlite/connection.rb
+++ b/lib/mini_sql/sqlite/connection.rb
@@ -9,12 +9,14 @@ module MiniSql
         @raw_connection = raw_connection
         @param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self)
         @deserializer_cache = (args && args[:deserializer_cache]) || DeserializerCache.new
-
-        @prepared = PreparedConnection.new(self, @deserializer_cache)
       end
 
       def prepared(condition = true)
-        condition ? @prepared : self
+        if condition
+          @prepared ||= PreparedConnection.new(self, @deserializer_cache)
+        else
+          self
+        end
       end
 
       def query_single(sql, *params)

GitHub sha: 657be30c