1 /** 2 DDBC - D DataBase Connector - abstraction layer for RDBMS access, with interface similar to JDBC. 3 4 Source file ddbc/package.d 5 6 DDBC library attempts to provide implementation independent interface to different databases. API is similar to Java JDBC API. 7 http://docs.oracle.com/javase/7/docs/technotes/guides/jdbc/ 8 9 For using DDBC, import this file: 10 11 12 import ddbc; 13 14 15 16 Supported (built-in) RDBMS drivers: MySQL, PostgreSQL, SQLite 17 18 Configuration name Version constants Drivers included 19 -------------------------- ---------------------------------- --------------------------------- 20 full USE_MYSQL, USE_SQLITE, USE_PGSQL mysql, sqlite, postgresql, odbc 21 MySQL USE_MYSQL mysql 22 SQLite USE_SQLITE sqlite 23 PGSQL USE_PGSQL postgresql 24 ODBC USE_ODBC odbc 25 API (none) (no drivers, API only) 26 27 28 When using in DUB based project, add "ddbc" dependency to your project's dub.json: 29 30 "dependencies": { 31 "ddbc": "~>0.2.35" 32 } 33 34 Default configuration is "full". You can choose other configuration by specifying subConfiguration for ddbc, e.g.: 35 36 "subConfigurations": { 37 "ddbc": "SQLite" 38 } 39 40 41 If you want to support all DDBC configuration in your project, use configurations section: 42 43 "configurations": [ 44 { 45 "name": "default", 46 "subConfigurations": { 47 "ddbc": "full" 48 } 49 }, 50 { 51 "name": "MySQL", 52 "subConfigurations": { 53 "ddbc": "MySQL" 54 } 55 }, 56 { 57 "name": "SQLite", 58 "subConfigurations": { 59 "ddbc": "SQLite" 60 } 61 }, 62 { 63 "name": "PGSQL", 64 "subConfigurations": { 65 "ddbc": "PGSQL" 66 } 67 }, 68 { 69 "name": "API", 70 "subConfigurations": { 71 "ddbc": "API" 72 } 73 }, 74 ] 75 76 77 DDBC URLs 78 ========= 79 80 For creation of DDBC drivers or data sources, you can use DDBC URL. 81 82 Common form of DDBC URL: driver://host:port/dbname?param1=value1,param2=value2 83 84 As well, you can prefix url with "ddbc:" 85 ddbc:driver://host:port/dbname?param1=value1,param2=value2 86 87 Following helper function may be used to create URL 88 89 string makeDDBCUrl(string driverName, string host, int port, string dbName, string[string] params = null); 90 91 92 For PostgreSQL, use following form of URL: 93 94 postgresql://host:port/dbname 95 96 Optionally you can put user name, password, and ssl option as url parameters: 97 98 postgresql://host:port/dbname?user=username,password=userpassword,ssl=true 99 100 101 For MySQL, use following form of URL: 102 103 mysql://host:port/dbname 104 105 Optionally you can put user name and password as url parameters: 106 107 mysql://host:port/dbname?user=username,password=userpassword 108 109 110 For SQLite, use following form of URL: 111 112 sqlite:db_file_path_name 113 114 Sample urls: 115 116 string pgsqlurl = "postgresql://localhost:5432/ddbctestdb?user=ddbctest,password=ddbctestpass,ssl=true"; 117 string mysqlurl = "mysql://localhost:3306/ddbctestdb?user=ddbctest,password=ddbctestpass"; 118 string sqliteurl = "sqlite:testdb.sqlite"; 119 120 121 Drivers, connections, data sources and connection pools. 122 ======================================================= 123 124 125 Driver - factory interface for DB connections. This interface implements single method to create connections: 126 127 Connection connect(string url, string[string] params); 128 129 DataSource - factory interface for creating connections to specific DB instance, holds enough information to create connection using simple call of getConnection() 130 131 ConnectionPool - DataSource which implements pool of opened connections to avoid slow connection establishment. It keeps several connections opened in pool. 132 133 Connection - main object for dealing with DB. 134 135 136 Driver may be created using one of factory methods: 137 138 /// create driver by name, e.g. "mysql", "postgresql", "sqlite" 139 DriverFactory.createDriver(string driverName); 140 /// create driver by url, e.g. "mysql://host:port/db", "postgresql://host:port/db", "sqlite://" 141 DriverFactory.createDriverForURL(string url); 142 143 144 There are helper functions to create Connection, DataSource or ConnectionPool from URL and parameters. 145 146 /// Helper function to create DDBC connection, automatically selecting driver based on URL 147 Connection createConnection(string url, string[string]params = null); 148 149 /// Helper function to create simple DDBC DataSource, automatically selecting driver based on URL 150 DataSource createDataSource(string url, string[string]params = null); 151 152 /// Helper function to create connection pool data source, automatically selecting driver based on URL 153 DataSource createConnectionPool(string url, string[string]params = null, int maxPoolSize = 1, int timeToLive = 600, int waitTimeOut = 30); 154 155 156 If you are planning to create several connections, consider using DataSource or ConnectionPool. 157 158 For simple cases, it's enough to create connection directly. 159 160 Connection conn = createConnection("sqlite:testfile.sqlite"); 161 162 If you need to get / release connection multiple times, it makes sense to use ConnectionPool 163 164 DataSource ds = createConnectionPool("ddbc:postgresql://localhost:5432/ddbctestdb?user=ddbctest,password=ddbctestpass,ssl=true"); 165 // now we can take connection from pool when needed 166 auto conn = ds.getConnection(); 167 // and then release it back to pool when no more needed 168 conn.close(); 169 // if we call ds.getConnection() one more time, existing connection from pool will be used 170 171 172 Copyright: Copyright 2014 173 License: $(LINK www.boost.org/LICENSE_1_0.txt, Boost License 1.0). 174 Author: Vadim Lopatin 175 */ 176 module ddbc; 177 178 public import ddbc.core; 179 public import ddbc.common; 180 public import ddbc.pods; 181 182 version( USE_SQLITE ) 183 { 184 // register SQLite driver 185 private import ddbc.drivers.sqliteddbc; 186 } 187 version( USE_PGSQL ) 188 { 189 // register Postgres driver 190 private import ddbc.drivers.pgsqlddbc; 191 } 192 version(USE_MYSQL) 193 { 194 // register MySQL driver 195 private import ddbc.drivers.mysqlddbc; 196 } 197 version(USE_ODBC) 198 { 199 // register ODBC driver 200 private import ddbc.drivers.odbcddbc; 201 }