22
33import jakarta .persistence .Column ;
44import org .slowcoders .hyperquery .core .*;
5+ import org .slowcoders .hyperquery .impl .jdbc .JdbcColumn ;
6+ import org .slowcoders .hyperquery .impl .jdbc .JdbcSchemaLoader ;
7+ import org .slowcoders .hyperquery .impl .jdbc .PGSchemaLoader ;
58
69import java .lang .reflect .Field ;
710import java .lang .reflect .Modifier ;
811import java .lang .reflect .ParameterizedType ;
912import java .lang .reflect .Type ;
1013import java .sql .Connection ;
11- import java .sql .DatabaseMetaData ;
12- import java .sql .ResultSet ;
1314import java .sql .SQLException ;
1415import java .util .ArrayList ;
1516import java .util .Collection ;
@@ -24,6 +25,8 @@ public class HSchema extends HModel {
2425 private Map <String , QAttribute > attributes ;
2526
2627 private static final HashMap <Class <?>, HSchema > relations = new HashMap <>();
28+ private ArrayList <JdbcColumn > jdbcColumns ;
29+ private ArrayList <String > pkColumnNames ;
2730
2831 private static class EmptyEntity implements QEntity <EmptyEntity > {}
2932 static {
@@ -46,7 +49,7 @@ protected HSchema(Class<? extends QRecord<?>> entityType, boolean isEntity) {
4649 public final Class <? extends QRecord <?>> getEntityType () { return entityType ; }
4750
4851 public QAttribute getAttribute (String property ) {
49- this .initialize ();
52+ // this.initialize(conn );
5053 return attributes .get (property );
5154 }
5255
@@ -67,9 +70,19 @@ public QLambda getLambda(String alias) {
6770// return relation;
6871// }
6972
70- public synchronized void initialize () {
73+ static JdbcSchemaLoader jdbcSchemaLoader = null ;
74+ public synchronized void initialize (Connection conn ) throws SQLException {
7175 if (joins != null ) return ;
7276
77+ if (jdbcSchemaLoader == null ) {
78+ jdbcSchemaLoader = new PGSchemaLoader (conn );
79+ }
80+ if (!this .tableName .isEmpty ()) {
81+ JdbcSchemaLoader .TablePath tablePath = jdbcSchemaLoader .makeTablePath (tableName );
82+ this .pkColumnNames = jdbcSchemaLoader .getPrimaryKeys (conn , tablePath );
83+ this .jdbcColumns = jdbcSchemaLoader .getColumns (conn , new HashMap <>(), tablePath , pkColumnNames );
84+ }
85+
7386 HashMap <String , QJoin > joins = new HashMap <>();
7487 HashMap <String , QLambda > lambdas = new HashMap <>();
7588 HashMap <String , QAttribute > attributes = new HashMap <>();
@@ -105,7 +118,7 @@ else if (QAttribute.class.isAssignableFrom(propertyType)) {
105118 this .attributes = attributes ;
106119 }
107120
108- public static HSchema loadSchema (Class <?> clazz , boolean isEntity , Connection dbConn ) {
121+ public static HSchema loadSchema (Class <?> clazz , boolean isEntity , JdbcConnector dbConn ) {
109122 synchronized (relations ) {
110123 HSchema schema = relations .get (clazz );
111124 if (schema != null ) return schema ;
@@ -142,7 +155,12 @@ public static HSchema loadSchema(Class<?> clazz, boolean isEntity, Connection db
142155 synchronized (relations ) {
143156 HSchema schema = relations .get (genericClass );
144157 if (schema == null ) {
145- schema = new HSchema ((Class <? extends QEntity <?>>) genericClass , isEntity );
158+ final HSchema newSchema = new HSchema ((Class <? extends QEntity <?>>) genericClass , isEntity );
159+ dbConn .execute (conn -> {
160+ newSchema .initialize (conn );
161+ return null ;
162+ });
163+ schema = newSchema ;
146164 relations .put (genericClass , schema );
147165 }
148166 return schema ;
@@ -152,7 +170,7 @@ public static HSchema loadSchema(Class<?> clazz, boolean isEntity, Connection db
152170
153171
154172 @ Override
155- protected HSchema loadSchema (Connection dbConn ) {
173+ protected HSchema loadSchema (JdbcConnector dbConn ) {
156174 return this ;
157175 }
158176
@@ -161,8 +179,8 @@ protected String getTableName() {
161179 return this .tableName ;
162180 }
163181
164- public QJoin getJoin (String join , Connection dbConn ) {
165- this .initialize ();
182+ public QJoin getJoin (String join , JdbcConnector dbConn ) {
183+ // this.initialize(conn );
166184 int next = join .indexOf ('@' , 1 );
167185 String nextJoin = null ;
168186 if (next > 0 ) {
@@ -174,7 +192,7 @@ public QJoin getJoin(String join, Connection dbConn) {
174192 return joins .get (join );
175193 }
176194
177- String getColumnExpr (Field f ) {
195+ public String getColumnExpr (Field f ) {
178196 String columnExpr = Helper .getColumnName (f );
179197 if (columnExpr != null ) return columnExpr ;
180198 if (this .attributes != null ) {
@@ -184,49 +202,49 @@ String getColumnExpr(Field f) {
184202 return null ;
185203 }
186204
187- public static class TablePath {
188- private final String catalog ;
189- private final String schema ;
190- private final String simpleName ;
191-
192- TablePath (String catalog , String schema , String simpleName ) {
193- this .catalog = (catalog );
194- this .schema = (schema );
195- this .simpleName = (simpleName );
196- }
197-
198-
199- public String getSimpleName () {
200- return simpleName ;
201- }
202-
203- public String getCatalog () {
204- return catalog ;
205- }
206-
207- public String getSchema () {
208- return schema ;
209- }
210- }
211- private ArrayList <String > getPrimaryKeys (Connection conn , TablePath tablePath ) throws SQLException {
212- DatabaseMetaData md = conn .getMetaData ();
213- ResultSet rs = md .getPrimaryKeys (tablePath .getCatalog (), tablePath .getSchema (), tablePath .getSimpleName ());
214- ArrayList <String > keys = new ArrayList <>();
215- int next_key_seq = 1 ;
216- while (rs .next ()) {
217- String key = rs .getString ("column_name" );
218- if (false ) {
219- // postgresql 에서만 동작.
220- int seq = rs .getInt ("key_seq" );
221- if (seq != next_key_seq ) {
222- throw new RuntimeException ("something wrong" );
223- }
224- next_key_seq ++;
225- }
226- keys .add (key );
227- }
228- return keys ;
229- }
205+ // public static class TablePath {
206+ // private final String catalog;
207+ // private final String schema;
208+ // private final String simpleName;
209+ //
210+ // TablePath(String catalog, String schema, String simpleName) {
211+ // this.catalog = (catalog);
212+ // this.schema = (schema);
213+ // this.simpleName = (simpleName);
214+ // }
215+ //
216+ //
217+ // public String getSimpleName() {
218+ // return simpleName;
219+ // }
220+ //
221+ // public String getCatalog() {
222+ // return catalog;
223+ // }
224+ //
225+ // public String getSchema() {
226+ // return schema;
227+ // }
228+ // }
229+ // private ArrayList<String> getPrimaryKeys(Connection conn, TablePath tablePath) throws SQLException {
230+ // DatabaseMetaData md = conn.getMetaData();
231+ // ResultSet rs = md.getPrimaryKeys(tablePath.getCatalog(), tablePath.getSchema(), tablePath.getSimpleName());
232+ // ArrayList<String> keys = new ArrayList<>();
233+ // int next_key_seq = 1;
234+ // while (rs.next()) {
235+ // String key = rs.getString("column_name");
236+ // if (false) {
237+ // // postgresql 에서만 동작.
238+ // int seq = rs.getInt("key_seq");
239+ // if (seq != next_key_seq) {
240+ // throw new RuntimeException("something wrong");
241+ // }
242+ // next_key_seq++;
243+ // }
244+ // keys.add(key);
245+ // }
246+ // return keys;
247+ // }
230248
231249 static class Helper implements QEntity <Helper > {
232250 static String getColumnName (Field f ) {
0 commit comments