H1 2015 - db access support in Phobos

Vadim Lopatin via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 2 23:44:21 PST 2015


On Monday, 2 February 2015 at 18:00:28 UTC, AndyC wrote:
> On Monday, 2 February 2015 at 04:00:31 UTC, Vadim Lopatin wrote:
>> I would like to propose Java way for implementation of DB 
>> access (JDBC - Java DataBase Connectors).
>>
>> Standard library must contain only
>> * set of interfaces for uniform DB access
>>    * Connection - can create Statement or PreparedStatement, 
>> control transactions
>>    * Statement - can run update or query
>>    * PreparedStatement - same as Statement, but with parameters
>>    * ResultSet - DB query result row access
>>    * DataSource - can create connections, w/o parameters
>>    * Driver - interface which implements connection factory 
>> for particular DB types
>>    * Some useful classes like ConnectionPool
>> * factory method to create connection by URL and parameter set
>> * some method to register DB driver for factory - e.g. when 
>> called from driver's module __gshared static this()
>>
>> Drivers may be placed in separate packages in standard library 
>> or in some DUB packages.
>>
>> Look at https://github.com/buggins/ddbc
>> Currently it contains drivers for sqlite, MySQL and PostgreSQL.
>>
>>
>> If Andrey/Walter wish, I can prepare pool request based on 
>> code from ddbc.
>>
>> Best regards,
>>     Vadim
>
>
> -1 on copy Java.  It seems over complicated.
>
> We need a base Database Class, then derive PostgresDatabase 
> from that.  No need for Driver class.  PostgresDatabase class 
> contains all the knowledge to connect to the actual database so 
> what's left for Driver?
>
> If we have Database class, why do we need Connection or 
> DataSource?  They don't offer anything Database shouldn't 
> already have.
>
> Database (and children), ResultSet and Statement are all that's 
> needed.  Statement can have a prepare() function.  No need to 
> make an entire PreparedStatement class, seems overkill.
>
> I pray we never ever have factories.  I like verbs, they make 
> sense.
>
> -Andy

Did you ever write complex DB applications?

Connection is important part for DB access.
Statements are always being executed on particular connection. 
E.g. sequence temporary table operations, transaction management, 
and other operations require to work on the same physical 
connection.
Connection is a resource. It should be managed. It's mandatory to 
close physical connection when no more used. But reopening of 
physical connection to DB is high cost operation. Therefore 
usually connections are being reused using Connection Pools.
Often application needs several connections to do operations in 
parallel.

Some kind of factory is needed anyway, to allow application be 
independent from particular DB kind (e.g. to allow configuring DB 
type and parameters in some configuration file).

Driver is just an abstraction which can be used for creation of 
DB connections (you just call it Database).

PreparedStatement is important thing - it's not only ability to 
specify query parameters, but it is being compiled (prepared) on 
server side and can be reused and be executed several times with 
higher performance than usual query. Prepared statements are 
resources - and should have an ability to be closed when no 
longer needed.

ResultSets are probably redundant and may be embedded into 
statements, but in this case it will not be possible to deal with 
multiple result sets from same query.





More information about the Digitalmars-d mailing list