Advertisement
bls

std.sql level2 ?

bls
Oct 13th, 2011
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.72 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. int main(string[] args)
  4. {
  5.     IDatabaseFactory factory;
  6.    
  7.     if(args[0] == "MySQL")
  8.         factory = new MySQLFactory();
  9.     else
  10.         factory = new PostreSQLFactory();
  11.  
  12.     updatePricelist(factory);
  13.     return 0;
  14. }
  15.  
  16. public interface IDatabase {
  17.     //common database stuff
  18.     alias open connect;
  19.     public void open(string host, string db, string user, string pw);
  20.     public void open(string host, string db, string user, string pw, const string[string] params);
  21.    
  22.     //alias close disconnect;
  23.     //public void close();
  24.     // exec(); prepare() etc...
  25. }
  26.  
  27. public interface IDatabaseFactory {
  28.  
  29.    public  IDatabase createDatabaseInstance();
  30. }
  31.  
  32. final class PostgreSQL : IDatabase {
  33.     // common
  34.     public void open(string host,string db,string user,string pw,
  35.         const string[string] params) {
  36.          
  37.     }
  38.     public void open(string host,string db,string user,string pw) {
  39.     }
  40.     //PostgreSQL specific
  41.     public void funkyPGstuff() {}
  42. }
  43.  
  44. final class PostreSQLFactory : IDatabaseFactory {
  45.    
  46.     public IDatabase createDatabaseInstance() {
  47.         return new PostgreSQL();
  48.    }
  49. }
  50.  
  51. final class MySQL : IDatabase {    
  52.     // common
  53.     public void open(string host,string db,string user,string pw,
  54.         const string[string] params) {
  55.          
  56.     }
  57.     public void open(string host,string db,string user,string pw) {
  58.     }
  59.    //MySQL specific
  60.     public void funkyMySQLstuff() {}
  61. }
  62.  
  63. final class MySQLFactory : IDatabaseFactory {
  64.     public IDatabase createDatabaseInstance() {
  65.         return new MySQL();
  66.    }
  67. }
  68.  
  69. // Update PriceList without knowing the database
  70. void updatePricelist(IDatabaseFactory factory) {
  71.     IDatabase db = factory.createDatabaseInstance();
  72.     db.connect("localhost", "test", "bls", "secret");
  73.        
  74.     //db.exec("UPDATE ...");
  75.    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement