I had seen somewhere the builder pattern used to create DML SQL statements. I was wondering, w开发者_如何学编程hich pattern(s) would be (more) appropriate for building SQL DDL statements.
I am thinking about simple program (DB tool, solely for self-education purposes), that would create (dynamically) simple SQL DDL statements. I am not sure which patterns should I consider.
The factory pattern allows me to decouple client code from concrete database provider classes library. I suppose it's a clear choice here (please correct me if I am wrong). The decorator pattern was my first choice for building sql statements, but after coding some examples and then reading this answer I am almost sure, I shouldn't be using decorator, as I am building objects and not decorating already created objects.
So.. which patterns should I consider and why are they good/better in this case?
Updated for clarification.
The "core" of your DB tool needs to represent the knowledge it possesses in a DB-independent form, and when the time comes a database-specific code takes over and translates that knowledge into DB-specific DDL.
You'll probably need some sort of dependency injection to accomplish that. The basic idea is this: the core of your application works with interfaces only and never knows anything beyond what is declared in these interfaces. At run-time, DB-specific objects implementing these interfaces are instantiated and "injected" into the core. Create than blindly "calls" them as it would any other set of objects implementing the same set of interfaces.
If another DB needs to be supported, juts make another set of classes that implement these interfaces and instantiate them in run-time.
Of course this is all just a theory. You'll find that there are many nuances between different database systems and I suspect it will be hard for you to represent all that in a completely generalized way...
精彩评论