1 module appbase.mysql.dao; 2 3 import database.mysql; 4 5 alias DataRow = string[string]; 6 alias DataRows = DataRow[]; 7 8 DataRow queryOneRow(bool Prepared = true, Params...)(Connection conn, string cmd, Params params) 9 { 10 DataRows rows = query!(Prepared)(conn, cmd, params); 11 12 if (rows.length == 0) 13 { 14 return null; 15 } 16 17 return rows[0]; 18 } 19 20 DataRows query(bool Prepared = true, Params...)(Connection conn, string cmd, Params params) 21 { 22 DataRows rows; 23 24 static if (Prepared) 25 conn.execute(cmd, params, (MySQLRow row) { 26 rows ~= row.toAA(); 27 }); 28 else 29 conn.executeNoPrepare(cmd, params, (MySQLRow row) { 30 rows ~= row.toAA(); 31 }); 32 33 return rows; 34 } 35 36 // Return affected, no lastInsertId. 37 // Get the lastInsertId: use conn.lastInsertId() please. 38 ulong exec(bool Prepared = true, Params...)(Connection conn, string cmd, Params params) 39 { 40 static if (Prepared) 41 { 42 conn.execute(cmd, params); 43 } 44 else 45 { 46 conn.executeNoPrepare(cmd, params); 47 } 48 49 return conn.affected(); 50 } 51 52 DataRows selectDataRows(DataRows rows, string key, string value) 53 { 54 int start = -1; 55 int end = -1; 56 57 int i = 0; 58 while (i < rows.length) 59 { 60 if (rows[i][key] == value) 61 { 62 start = i; 63 break; 64 } 65 66 i++; 67 } 68 69 if (start < 0) 70 { 71 return rows[0 .. 0]; 72 } 73 74 i = start + 1; 75 while (i < rows.length) 76 { 77 if (rows[i][key] != value) 78 { 79 end = i; 80 break; 81 } 82 83 i++; 84 } 85 86 if (end < 0) 87 { 88 end = cast(int)rows.length; 89 } 90 91 return rows[start .. end]; 92 } 93 94 DataRows selectDataRows(DataRows rows, int rowsInPage, int pageno) 95 { 96 if (rows.length == 0) return rows; 97 if ((pageno - 1) * rowsInPage > cast(int)rows.length - 1) return rows[0 .. 0]; 98 return rows[(pageno - 1) * rowsInPage .. (pageno * rowsInPage >= rows.length) ? rows.length : pageno * rowsInPage]; 99 }