Advertisement
bls

std.sql Level 2

bls
Oct 13th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.71 KB | None | 0 0
  1. int main(string[] args)
  2. {
  3.     IDatabaseFactory factory;
  4.    
  5.     if(args[0] == "MySQL")
  6.         factory = new MySQLFactory();
  7.     else
  8.         factory = new PostreSQLFactory();
  9.  
  10.     updatePricelist(factory);
  11.     return 0;
  12. }
  13.  
  14. public interface IDatabase {
  15.     //common database stuff
  16.     alias open connect;
  17.     public void open(string host, string db, string user, string pw);
  18.     public void open(string host, string db, string user, string pw, const string[string] params);
  19.    
  20.     //alias close disconnect;
  21.     //public void close();
  22.     // exec(); prepare() etc...
  23. }
  24.  
  25. public interface IDatabaseFactory {
  26.  
  27.    public  IDatabase createDatabaseInstance();
  28. }
  29.  
  30. final class PostgreSQL : IDatabase {
  31.     // common
  32.     public void open(string host,string db,string user,string pw,
  33.         const string[string] params) {
  34.          
  35.     }
  36.     public void open(string host,string db,string user,string pw) {
  37.     }
  38.    //PostgreSQL specific
  39.    public void funkyPGstuff() {}
  40. }
  41.  
  42. final class PostreSQLFactory : IDatabaseFactory {
  43.    
  44.    public IDatabase createDatabaseInstance() {
  45.  
  46.       return new PostgreSQL();
  47.  
  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