|
| 1 | +import std.database.util; |
| 2 | +import std.stdio; |
| 3 | +import std.database.oracle; |
| 4 | +import std.experimental.logger; |
| 5 | +import std.datetime; |
| 6 | +import std.variant; |
| 7 | + |
| 8 | +struct Resource(T) {} |
| 9 | +struct Policy {} |
| 10 | + |
| 11 | +auto create(alias T)() { |
| 12 | + T!Policy r; |
| 13 | + return r; |
| 14 | +} |
| 15 | + |
| 16 | +auto r = create!Resource; |
| 17 | + |
| 18 | +struct TestOutputRange { |
| 19 | + void put(int i) { |
| 20 | + log("I: ", i); |
| 21 | + } |
| 22 | + void put(string s) { |
| 23 | + log("S: ", s); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +unittest { |
| 29 | + import std.database.testsuite; |
| 30 | + //alias DB = Database!DefaultPolicy; |
| 31 | + //testAll!DB("oracle"); |
| 32 | + |
| 33 | + // these are all heading to test suite |
| 34 | + |
| 35 | + if (true) { |
| 36 | + auto db = createDatabase; |
| 37 | + auto con1 = db.connection("oracle").autoCommit(false); |
| 38 | + auto con2 = db.connection("oracle").autoCommit(false); |
| 39 | + |
| 40 | + scope(failure) con1.rollback; |
| 41 | + scope(failure) con2.rollback; |
| 42 | + |
| 43 | + //con1.begin.query("insert into account(id,amount) values(1,100)"); |
| 44 | + //con2.begin.query("insert into account(id,amount) values(2,-100)"); |
| 45 | + |
| 46 | + con1.commit; |
| 47 | + con2.commit; |
| 48 | + } |
| 49 | + |
| 50 | + if (false) { |
| 51 | + auto db = createDatabase("oracle"); |
| 52 | + auto con = db.connection; |
| 53 | + auto stmt = con.statement("select * from score"); |
| 54 | + auto row = stmt.query.rows.front(); |
| 55 | + //stmt.query.rows.writeRows; |
| 56 | + |
| 57 | + TestOutputRange r; |
| 58 | + row.into(r); |
| 59 | + } |
| 60 | + |
| 61 | + if (false) { |
| 62 | + auto db = createDatabase("oracle"); |
| 63 | + auto con = db.connection; |
| 64 | + auto stmt = con.statement("select * from score"); |
| 65 | + auto rows = stmt.query.rows; |
| 66 | + //stmt.query.rows.writeRows; |
| 67 | + |
| 68 | + foreach (row; rows) { |
| 69 | + string name; |
| 70 | + int score; |
| 71 | + row.into(name,score); |
| 72 | + writeln("name: ", name, ", score: ", score); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (false) { // classic example 2 |
| 77 | + auto db = createDatabase("oracle"); |
| 78 | + auto con = db.connection; |
| 79 | + auto stmt = con.statement("select * from t"); |
| 80 | + auto rows = stmt.query.rows; |
| 81 | + |
| 82 | + foreach (row; rows) { |
| 83 | + foreach (column; rows.columns) { |
| 84 | + auto field = row[column]; |
| 85 | + writeln("name: ", column.name, ", value: ", field); |
| 86 | + } |
| 87 | + writeln; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (false) { // classic example |
| 92 | + auto db = createDatabase("oracle"); |
| 93 | + auto con = db.connection; |
| 94 | + auto stmt = con.statement("select * from t"); |
| 95 | + auto rows = stmt.query.rows; |
| 96 | + |
| 97 | + foreach (row; rows) { |
| 98 | + for(int c = 0; c != row.width; ++c) { |
| 99 | + auto field = row[c]; |
| 100 | + write(field," "); |
| 101 | + } |
| 102 | + writeln; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + if (false) { |
| 108 | + auto db = createDatabase; |
| 109 | + //auto db = Database.create(); |
| 110 | + //auto db = DB.create; |
| 111 | + |
| 112 | + auto con = db.connection("oracle"); |
| 113 | + con.query("drop table t"); |
| 114 | + con.query("create table t (a varchar(10), b integer, c integer, d date)"); |
| 115 | + con.query("insert into t values('one', 2, 3, TO_DATE('2015/02/03', 'yyyy/mm/dd'))"); |
| 116 | + auto row = con.query("select * from t").rows.front; |
| 117 | + assert(row[0].option!string == "one"); |
| 118 | + assert(row[1].option!int == 2); |
| 119 | + } |
| 120 | + |
| 121 | + if (false) { |
| 122 | + auto db = createDatabase; |
| 123 | + auto con = db.connection("oracle"); |
| 124 | + con.query("drop table t"); |
| 125 | + con.query("create table t (a varchar(10), b integer, c integer, d date)"); |
| 126 | + con.query("insert into t values('one', 2, 3, TO_DATE('2015/02/03', 'yyyy/mm/dd'))"); |
| 127 | + auto row = con.query("select * from t").rows.front; |
| 128 | + assert(row[0].get!string == "one"); |
| 129 | + assert(row[1].get!int == 2); |
| 130 | + } |
| 131 | + |
| 132 | + if (false) { |
| 133 | + // into |
| 134 | + auto db = createDatabase; |
| 135 | + auto con = db.connection("oracle"); |
| 136 | + con.query("drop table t"); |
| 137 | + con.query("create table t (a varchar(10), b integer, c integer, d date)"); |
| 138 | + con.query("insert into t values('one', 2, 3, TO_DATE('2015/02/03', 'yyyy/mm/dd'))"); |
| 139 | + string a; |
| 140 | + int b; |
| 141 | + Variant c; |
| 142 | + Date d; |
| 143 | + con.query("select * from t").into(a,b,c,d); |
| 144 | + assert(a == "one"); |
| 145 | + assert(b == 2); |
| 146 | + |
| 147 | + auto v = Variant(234); |
| 148 | + assert(v == 234); |
| 149 | + |
| 150 | + assert(c == "3"); // fix variant to support string |
| 151 | + assert(d == Date(2015,2,3)); |
| 152 | + } |
| 153 | + |
| 154 | + if (false) { |
| 155 | + alias DB = Database!DefaultPolicy; |
| 156 | + polyTest!DB("oracle"); |
| 157 | + |
| 158 | + if (false) { |
| 159 | + auto db = createDatabase; |
| 160 | + auto con = db.connection("oracle"); |
| 161 | + con.query("select * from score").writeRows; |
| 162 | + } |
| 163 | + |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void polyTest(DB) (string source) { |
| 168 | + import std.database.poly; |
| 169 | + import std.experimental.logger; |
| 170 | + import std.database.front: Feature; |
| 171 | + |
| 172 | + registerDatabase!DB("oracle"); |
| 173 | + auto polyDB = createDatabase; |
| 174 | + |
| 175 | + auto db = polyDB.database("oracle"); |
| 176 | + |
| 177 | + log("ConnectionPool: ", db.hasFeature(Feature.ConnectionPool)); |
| 178 | + log("OutputArrayBinding: ", db.hasFeature(Feature.OutputArrayBinding)); |
| 179 | + |
| 180 | + auto con = db.connection("oracle"); |
| 181 | + auto stmt = con.statement("select * from score"); |
| 182 | + |
| 183 | + stmt.query("joe",20); |
| 184 | + stmt.query(); |
| 185 | + |
| 186 | +} |
| 187 | + |
0 commit comments