mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
add prepared_statements option to database
* use ?prepared_statements=true|false on connection string (default: true) * make inmutable state in database * make mutable state in connection * change Connection#build to use the current prepared_statements flag to build prepared or unprepared statements.
This commit is contained in:
parent
fe0ed55ef9
commit
9ef9d19d1d
6 changed files with 80 additions and 3 deletions
|
@ -97,4 +97,36 @@ describe DB::Database do
|
|||
DummyDriver::DummyConnection.connections.size.should eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe "prepared_statements connection option" do
|
||||
it "defaults to true" do
|
||||
with_dummy "dummy://localhost:1027" do |db|
|
||||
db.prepared_statements?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "can be set to false" do
|
||||
with_dummy "dummy://localhost:1027?prepared_statements=false" do |db|
|
||||
db.prepared_statements?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
it "is copied to connections and can be changed (false)" do
|
||||
with_dummy "dummy://localhost:1027?prepared_statements=false&initial_pool_size=1" do |db|
|
||||
connection = DummyDriver::DummyConnection.connections.first
|
||||
connection.prepared_statements?.should be_false
|
||||
connection.prepared_statements = true
|
||||
connection.prepared_statements?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "is copied to connections and can be changed (true)" do
|
||||
with_dummy "dummy://localhost:1027?prepared_statements=true&initial_pool_size=1" do |db|
|
||||
connection = DummyDriver::DummyConnection.connections.first
|
||||
connection.prepared_statements?.should be_true
|
||||
connection.prepared_statements = false
|
||||
connection.prepared_statements?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -97,4 +97,15 @@ describe DB do
|
|||
DB.open "foobar://baz"
|
||||
end
|
||||
end
|
||||
|
||||
it "should parse boolean query string params" do
|
||||
DB.fetch_bool(HTTP::Params.parse("foo=true"), "foo", false).should be_true
|
||||
DB.fetch_bool(HTTP::Params.parse("foo=True"), "foo", false).should be_true
|
||||
|
||||
DB.fetch_bool(HTTP::Params.parse("foo=false"), "foo", true).should be_false
|
||||
DB.fetch_bool(HTTP::Params.parse("foo=False"), "foo", true).should be_false
|
||||
|
||||
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", false).should be_false
|
||||
DB.fetch_bool(HTTP::Params.parse("bar=true"), "foo", true).should be_true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -17,6 +17,24 @@ describe DB::Statement do
|
|||
end
|
||||
end
|
||||
|
||||
describe "prepared_statements flag" do
|
||||
it "should build prepared statements if true" do
|
||||
with_dummy_connection do |cnn|
|
||||
cnn.prepared_statements = true
|
||||
stmt = cnn.query("the query").statement
|
||||
stmt.as(DummyDriver::DummyStatement).prepared?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
it "should build unprepared statements if false" do
|
||||
with_dummy_connection do |cnn|
|
||||
cnn.prepared_statements = false
|
||||
stmt = cnn.query("the query").statement
|
||||
stmt.as(DummyDriver::DummyStatement).prepared?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize positional params in query" do
|
||||
with_dummy_connection do |cnn|
|
||||
stmt = cnn.prepared("the query").as(DummyDriver::DummyStatement)
|
||||
|
|
|
@ -120,6 +120,15 @@ module DB
|
|||
private def self.build_database(uri : URI)
|
||||
Database.new(driver_class(uri.scheme).new, uri)
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
def self.fetch_bool(params : HTTP::Params, name, default : Bool)
|
||||
if value = params[name]?
|
||||
value.underscore == "true"
|
||||
else
|
||||
default
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "./db/pool"
|
||||
|
|
|
@ -24,15 +24,19 @@ module DB
|
|||
# :nodoc:
|
||||
getter database
|
||||
@statements_cache = StringKeyCache(Statement).new
|
||||
property? prepared_statements : Bool
|
||||
|
||||
def initialize(@database : Database)
|
||||
@prepared_statements = @database.prepared_statements?
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
def build(query) : Statement
|
||||
# TODO add flag for default statements kind.
|
||||
# configured in database overridable by connection
|
||||
if prepared_statements?
|
||||
fetch_or_build_prepared_statement(query)
|
||||
else
|
||||
build_unprepared_statement(query)
|
||||
end
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
|
|
|
@ -25,6 +25,8 @@ module DB
|
|||
# Returns the uri with the connection settings to the database
|
||||
getter uri
|
||||
|
||||
getter? prepared_statements : Bool
|
||||
|
||||
@pool : Pool(Connection)
|
||||
@setup_connection : Connection -> Nil
|
||||
@statements_cache = StringKeyCache(PoolStatement).new
|
||||
|
@ -32,6 +34,7 @@ module DB
|
|||
# :nodoc:
|
||||
def initialize(@driver : Driver, @uri : URI)
|
||||
params = HTTP::Params.parse(uri.query || "")
|
||||
@prepared_statements = DB.fetch_bool(params, "prepared_statements", true)
|
||||
pool_options = @driver.connection_pool_options(params)
|
||||
|
||||
@setup_connection = ->(conn : Connection) {}
|
||||
|
|
Loading…
Reference in a new issue