|
| 1 | +/** |
| 2 | + * The MIT License Copyright (c) 2014 Ilkka Seppälä |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 5 | + * associated documentation files (the "Software"), to deal in the Software without restriction, |
| 6 | + * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 7 | + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 11 | + * substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 14 | + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 15 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 16 | + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | + */ |
| 19 | +package com.iluwatar.datamapper; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import org.apache.log4j.Logger; |
| 25 | + |
| 26 | +/** |
| 27 | + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the |
| 28 | + * database. Its responsibility is to transfer data between the two and also to isolate them from |
| 29 | + * each other. With Data Mapper the in-memory objects needn't know even that there's a database |
| 30 | + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The |
| 31 | + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , |
| 32 | + * Data Mapper itself is even unknown to the domain layer. |
| 33 | + * <p> |
| 34 | + * The below example demonstrates basic CRUD operations: select, add, update, and delete. |
| 35 | + * |
| 36 | + */ |
| 37 | +public final class App { |
| 38 | + |
| 39 | + private static Logger log = Logger.getLogger(App.class); |
| 40 | + |
| 41 | + |
| 42 | + private static final String DB_TYPE_ORACLE = "Oracle"; |
| 43 | + private static final String DB_TYPE_MYSQL = "MySQL"; |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Program entry point. |
| 48 | + * |
| 49 | + * @param args command line args. |
| 50 | + */ |
| 51 | + public static final void main(final String... args) { |
| 52 | + |
| 53 | + if (log.isInfoEnabled() & args.length > 0) { |
| 54 | + log.debug("App.main(), db type: " + args[0]); |
| 55 | + } |
| 56 | + |
| 57 | + StudentDataMapper mapper = null; |
| 58 | + |
| 59 | + /* Check the desired db type from runtime arguments */ |
| 60 | + if (DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { |
| 61 | + |
| 62 | + /* Create new data mapper for mysql */ |
| 63 | + mapper = new StudentMySQLDataMapper(); |
| 64 | + |
| 65 | + } else if (DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { |
| 66 | + |
| 67 | + /* Create new data mapper for oracle */ |
| 68 | + mapper = new StudentMySQLDataMapper(); |
| 69 | + } else { |
| 70 | + |
| 71 | + /* Don't couple any Data Mapper to java.sql.SQLException */ |
| 72 | + throw new DataMapperException("Following data source(" + args[0] + ") is not supported"); |
| 73 | + } |
| 74 | + |
| 75 | + /* Create new student */ |
| 76 | + Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A'); |
| 77 | + |
| 78 | + /* Add student in respectibe db */ |
| 79 | + mapper.insert(student); |
| 80 | + |
| 81 | + /* Find this student */ |
| 82 | + final Optional<Student> studentToBeFound = mapper.find(student.getGuId()); |
| 83 | + |
| 84 | + if (log.isDebugEnabled()) { |
| 85 | + log.debug("App.main(), db find returned : " + studentToBeFound); |
| 86 | + } |
| 87 | + |
| 88 | + /* Update existing student object */ |
| 89 | + student = new Student(student.getGuId(), 1, "AdamUpdated", 'A'); |
| 90 | + |
| 91 | + /* Update student in respectibe db */ |
| 92 | + mapper.update(student); |
| 93 | + |
| 94 | + /* Delete student in db */ |
| 95 | + mapper.delete(student); |
| 96 | + } |
| 97 | + |
| 98 | + private App() {} |
| 99 | +} |
0 commit comments