|
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 | | - * |
28 | | - * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the |
29 | | - * database. Its responsibility is to transfer data between the two and also to isolate them from |
30 | | - * each other. With Data Mapper the in-memory objects needn't know even that there's a database |
31 | | - * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The |
32 | | - * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , |
33 | | - * Data Mapper itself is even unknown to the domain layer. |
34 | | - * <p> |
35 | | - * The below example demonstrates basic CRUD operations: select, add, update, and delete. |
36 | | - * |
37 | | - */ |
38 | | -public final class App { |
39 | | - |
40 | | - private static Logger log = Logger.getLogger(App.class); |
41 | | - |
42 | | - |
43 | | - private static final String DB_TYPE_ORACLE = "Oracle"; |
44 | | - private static final String DB_TYPE_MYSQL = "MySQL"; |
45 | | - |
46 | | - |
47 | | - /** |
48 | | - * Program entry point. |
49 | | - * |
50 | | - * @param args command line args. |
51 | | - */ |
52 | | - public static final void main(final String... args) { |
53 | | - |
54 | | - if (log.isInfoEnabled() & args.length > 0) { |
55 | | - log.debug("App.main(), db type: " + args[0]); |
56 | | - } |
57 | | - |
58 | | - StudentDataMapper mapper = null; |
59 | | - |
60 | | - /* Check the desired db type from runtime arguments */ |
61 | | - if (args.length == 0) { |
62 | | - |
63 | | - /* Create default data mapper for mysql */ |
64 | | - mapper = new StudentMySQLDataMapper(); |
65 | | - |
66 | | - } else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { |
67 | | - |
68 | | - /* Create new data mapper for mysql */ |
69 | | - mapper = new StudentMySQLDataMapper(); |
70 | | - |
71 | | - } else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { |
72 | | - |
73 | | - /* Create new data mapper for oracle */ |
74 | | - mapper = new StudentMySQLDataMapper(); |
75 | | - } else { |
76 | | - |
77 | | - /* Don't couple any Data Mapper to java.sql.SQLException */ |
78 | | - throw new DataMapperException("Following data source(" + args[0] + ") is not supported"); |
79 | | - } |
80 | | - |
81 | | - /* Create new student */ |
82 | | - Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A'); |
83 | | - |
84 | | - /* Add student in respectibe db */ |
85 | | - mapper.insert(student); |
86 | | - |
87 | | - /* Find this student */ |
88 | | - final Optional<Student> studentToBeFound = mapper.find(student.getGuId()); |
89 | | - |
90 | | - if (log.isDebugEnabled()) { |
91 | | - log.debug("App.main(), db find returned : " + studentToBeFound); |
92 | | - } |
93 | | - |
94 | | - /* Update existing student object */ |
95 | | - student = new Student(student.getGuId(), 1, "AdamUpdated", 'A'); |
96 | | - |
97 | | - /* Update student in respectibe db */ |
98 | | - mapper.update(student); |
99 | | - |
100 | | - /* Delete student in db */ |
101 | | - mapper.delete(student); |
102 | | - } |
103 | | - |
104 | | - private App() {} |
105 | | -} |
| 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 | + |
| 23 | +import org.apache.log4j.Logger; |
| 24 | + |
| 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: Create, Read, 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 (args.length == 0) { |
| 61 | + |
| 62 | + /* Create default data mapper for mysql */ |
| 63 | + mapper = new StudentMySQLDataMapper(); |
| 64 | + |
| 65 | + } else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { |
| 66 | + |
| 67 | + /* Create new data mapper for mysql */ |
| 68 | + mapper = new StudentMySQLDataMapper(); |
| 69 | + |
| 70 | + } else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { |
| 71 | + |
| 72 | + /* Create new data mapper for oracle */ |
| 73 | + mapper = new StudentMySQLDataMapper(); |
| 74 | + } else { |
| 75 | + |
| 76 | + /* Don't couple any Data Mapper to java.sql.SQLException */ |
| 77 | + throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported"); |
| 78 | + } |
| 79 | + |
| 80 | + /* Create new student */ |
| 81 | + Student student = new Student(1, "Adam", 'A'); |
| 82 | + |
| 83 | + /* Add student in respectibe db */ |
| 84 | + mapper.insert(student); |
| 85 | + |
| 86 | + /* Find this student */ |
| 87 | + final Optional<Student> studentToBeFound = mapper.find(student.getStudentId()); |
| 88 | + |
| 89 | + if (log.isDebugEnabled()) { |
| 90 | + log.debug("App.main(), db find returned : " + studentToBeFound); |
| 91 | + } |
| 92 | + |
| 93 | + /* Update existing student object */ |
| 94 | + student = new Student(student.getStudentId(), "AdamUpdated", 'A'); |
| 95 | + |
| 96 | + /* Update student in respectibe db */ |
| 97 | + mapper.update(student); |
| 98 | + |
| 99 | + /* Delete student in db */ |
| 100 | + mapper.delete(student); |
| 101 | + } |
| 102 | + |
| 103 | + private App() {} |
| 104 | +} |
0 commit comments