1616public abstract class Model {
1717
1818 protected Long id ;
19- protected final String tableName ;
19+ private String escapedTableName ;
2020 private final String modelName ;
2121 private final HashMap <String , String > fieldMap ;
2222 private String [] keywords ;
@@ -36,6 +36,9 @@ public abstract class Model {
3636 private static ConcurrentHashMap <String , Field []>
3737 fields = new ConcurrentHashMap <String , Field []>();
3838
39+ private static ConcurrentHashMap <String , String []>
40+ tables = new ConcurrentHashMap <String , String []>();
41+
3942
4043 //**************************************************************************
4144 //** Constructor
@@ -60,11 +63,31 @@ protected Model(String tableName){
6063 * You do not need to include the "id" field.
6164 */
6265 protected Model (String tableName , HashMap <String , String > fieldMap ){
66+
67+ //Set modelName
68+ Class c = this .getClass ();
69+ String className = c .getName ();
70+ modelName = c .getSimpleName ();
71+
72+
73+ //Set keywords
6374 synchronized (reservedKeywords ){
64- keywords = reservedKeywords .get (this .getClass ().getName ());
75+ keywords = reservedKeywords .get (className );
76+ }
77+
78+
79+ //Set tableInfo
80+ synchronized (tables ){
81+ String [] tableInfo = tables .get (className );
82+ if (tableInfo ==null ){
83+ tableInfo = getTableInfo (tableName );
84+ tables .put (className , tableInfo );
85+ }
86+ escapedTableName = tableInfo [0 ];
6587 }
66- this .tableName = escape (tableName );
67- this .modelName = this .getClass ().getSimpleName ();
88+
89+
90+ //Set fieldMap
6891 this .fieldMap = fieldMap ;
6992 }
7093
@@ -145,7 +168,7 @@ protected final void init(long id, String...fieldNames) throws SQLException {
145168 }
146169 if (addID ) sql .append (", id" );
147170 sql .append (" from " );
148- sql .append (tableName );
171+ sql .append (escapedTableName );
149172 sql .append (" where id=" );
150173
151174
@@ -378,7 +401,7 @@ else if (packageName.startsWith("javaxt.json") ||
378401 Connection conn = getConnection (this .getClass ());
379402
380403 StringBuilder sql = new StringBuilder ();
381- sql .append ("INSERT INTO " + tableName + " (" );
404+ sql .append ("INSERT INTO " + escapedTableName + " (" );
382405 it = fields .keySet ().iterator ();
383406 while (it .hasNext ()){
384407 java .lang .reflect .Field f = it .next ();
@@ -460,7 +483,7 @@ else if (packageName.startsWith("javaxt.geospatial.geometry") ||
460483 if (driver ==null ) driver = new Driver ("" ,"" ,"" );
461484
462485 Recordset rs = new Recordset ();
463- rs .open ("select * from " + tableName + " where id=" + id , conn , false );
486+ rs .open ("select * from " + escapedTableName + " where id=" + id , conn , false );
464487 if (rs .EOF ){
465488 rs .addNew ();
466489 rs .setValue ("id" , id );
@@ -508,7 +531,7 @@ public void delete() throws SQLException {
508531 Connection conn = null ;
509532 try {
510533 conn = getConnection (this .getClass ());
511- conn .execute ("delete from " + tableName + " where id=" + id );
534+ conn .execute ("delete from " + escapedTableName + " where id=" + id );
512535 conn .close ();
513536 }
514537 catch (SQLException e ){
@@ -723,7 +746,7 @@ private static String getSQL(Class c, Object...args){
723746
724747 //Get tableName
725748 String tableName = null ;
726- try { tableName = ((Model ) c .newInstance ()).tableName ; }
749+ try { tableName = ((Model ) c .newInstance ()).escapedTableName ; }
727750 catch (Exception e ){}
728751
729752 StringBuilder str = new StringBuilder ("select " );
@@ -838,9 +861,9 @@ public static void init(Class c, ConnectionPool connectionPool) throws SQLExcept
838861
839862
840863 //Generate list of fields
841- String tableName = ((Model ) c .newInstance ()). tableName ;
864+ Model model = ((Model ) c .newInstance ());
842865 Recordset rs = new Recordset ();
843- rs .open ("select * from " + tableName + " where id is null" , conn );
866+ rs .open ("select * from " + model . escapedTableName + " where id is null" , conn );
844867 synchronized (fields ){
845868 fields .put (className , rs .getFields ());
846869 fields .notifyAll ();
@@ -879,7 +902,61 @@ public static void init(javaxt.io.Jar jar, ConnectionPool connectionPool) throws
879902 /** Returns the name of the table backing a given Model
880903 */
881904 public static String getTableName (Model model ){
882- return model .tableName ;
905+ return model .escapedTableName ;
906+ }
907+
908+
909+ //**************************************************************************
910+ //** setSchemaName
911+ //**************************************************************************
912+ /** Provides an option to override the default schema used by a model
913+ */
914+ public static void setSchemaName (String schemaName , Class c ){
915+ if (!javaxt .sql .Model .class .isAssignableFrom (c )) return ;
916+ String className = c .getName ();
917+ try {
918+
919+ //Instantiate model to initialize table info
920+ Model model = ((Model ) c .newInstance ());
921+
922+
923+ //Update table info
924+ synchronized (tables ){
925+ String [] tableInfo = tables .get (className );
926+ String tableName = tableInfo [1 ];
927+ if (schemaName !=null ) tableName = schemaName + "." + tableName ;
928+ tableInfo = model .getTableInfo (tableName );
929+ tables .put (className , tableInfo );
930+ }
931+ }
932+ catch (Exception e ){
933+ Exception ex = new Exception ("Failed to update schema for Model: " + className );
934+ ex .setStackTrace (e .getStackTrace ());
935+ throw new RuntimeException (ex );
936+ }
937+ }
938+
939+
940+ //**************************************************************************
941+ //** getTableInfo
942+ //**************************************************************************
943+ /** Used to parse a given string and extract table info (e.g. schema name,
944+ * table name, etc).
945+ */
946+ private String [] getTableInfo (String tableName ){
947+ String schemaName ;
948+ String escapedTableName ;
949+ int idx = tableName .indexOf ("." );
950+ if (idx >-1 ){
951+ schemaName = tableName .substring (0 , idx );
952+ tableName = tableName .substring (idx +1 );
953+ escapedTableName = escape (schemaName ) + "." + tableName ;
954+ }
955+ else {
956+ schemaName = null ;
957+ escapedTableName = escape (tableName );
958+ }
959+ return new String []{escapedTableName , tableName , schemaName };
883960 }
884961
885962
0 commit comments