1 16 19 package org.apache.xalan.lib.sql; 20 21 import java.math.BigDecimal ; 22 import java.sql.Connection ; 23 import java.sql.Date ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.SQLWarning ; 28 import java.sql.Statement ; 29 import java.sql.Time ; 30 import java.sql.Timestamp ; 31 import java.util.Enumeration ; 32 import java.util.Properties ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 36 import org.apache.xalan.extensions.ExpressionContext; 37 import org.apache.xml.dtm.DTM; 38 import org.apache.xml.dtm.DTMManager; 39 import org.apache.xml.dtm.ref.DTMManagerDefault; 40 import org.apache.xpath.XPathContext; 41 import org.apache.xpath.objects.XBooleanStatic; 42 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.NamedNodeMap ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 48 import javax.xml.transform.ErrorListener ; 49 import javax.xml.transform.TransformerException ; 50 51 71 public class XConnection 72 { 73 74 77 private static final boolean DEBUG = false; 78 79 83 private ConnectionPool m_ConnectionPool = null; 84 85 90 private Connection m_Connection = null; 91 92 104 private boolean m_DefaultPoolingEnabled = false; 105 106 107 114 private Vector m_OpenSQLDocuments = new Vector (); 115 116 117 122 private ConnectionPoolManager m_PoolMgr = new ConnectionPoolManager(); 123 124 128 private Vector m_ParameterList = new Vector (); 129 130 136 private Exception m_Error = null; 137 138 144 private SQLDocument m_LastSQLDocumentWithError = null; 145 146 151 private boolean m_FullErrors = false; 152 153 154 155 160 private SQLQueryParser m_QueryParser = new SQLQueryParser(); 161 162 164 private boolean m_IsDefaultPool = false; 165 166 172 private boolean m_IsStreamingEnabled = true; 173 174 177 private boolean m_InlineVariables = false; 178 179 185 private boolean m_IsMultipleResultsEnabled = false; 186 187 192 private boolean m_IsStatementCachingEnabled = false; 193 194 196 public XConnection( ) 197 { 198 } 199 200 208 public XConnection( ExpressionContext exprContext, String ConnPoolName ) 209 { 210 connect(exprContext, ConnPoolName); 211 } 212 213 218 public XConnection( ExpressionContext exprContext, String driver, String dbURL ) 219 { 220 connect(exprContext, driver, dbURL); 221 } 222 223 227 public XConnection( ExpressionContext exprContext, NodeList list ) 228 { 229 connect(exprContext, list); 230 } 231 232 239 public XConnection( ExpressionContext exprContext, String driver, String dbURL, String user, String password ) 240 { 241 connect(exprContext, driver, dbURL, user, password); 242 } 243 244 250 public XConnection( ExpressionContext exprContext, String driver, String dbURL, Element protocolElem ) 251 { 252 connect(exprContext, driver, dbURL, protocolElem); 253 } 254 255 256 262 public XBooleanStatic connect( ExpressionContext exprContext, String ConnPoolName ) 263 { 264 try 265 { 266 m_ConnectionPool = m_PoolMgr.getPool(ConnPoolName); 267 268 if (m_ConnectionPool == null) 269 throw new java.lang.IllegalArgumentException ("Invalid Pool Name"); 270 271 m_IsDefaultPool = false; 272 return new XBooleanStatic(true); 273 } 274 catch (Exception e) 275 { 276 setError(e, exprContext); 277 return new XBooleanStatic(false); 278 } 279 280 } 281 282 289 public XBooleanStatic connect( ExpressionContext exprContext, String driver, String dbURL ) 290 { 291 try 292 { 293 init(driver, dbURL, new Properties ()); 294 return new XBooleanStatic(true); 295 } 296 catch(SQLException e) 297 { 298 setError(e,exprContext); 299 return new XBooleanStatic(false); 300 } 301 catch (Exception e) 302 { 303 setError(e,exprContext); 304 return new XBooleanStatic(false); 305 } 306 } 307 308 313 public XBooleanStatic connect( ExpressionContext exprContext, Element protocolElem ) 314 { 315 try 316 { 317 initFromElement(protocolElem); 318 return new XBooleanStatic(true); 319 } 320 catch(SQLException e) 321 { 322 setError(e,exprContext); 323 return new XBooleanStatic(false); 324 } 325 catch (Exception e) 326 { 327 setError(e,exprContext); 328 return new XBooleanStatic(false); 329 } 330 } 331 332 337 public XBooleanStatic connect( ExpressionContext exprContext, NodeList list ) 338 { 339 try 340 { 341 initFromElement( (Element ) list.item(0) ); 342 return new XBooleanStatic(true); 343 } 344 catch(SQLException e) 345 { 346 setError(e, exprContext); 347 return new XBooleanStatic(false); 348 } 349 catch (Exception e) 350 { 351 setError(e,exprContext); 352 return new XBooleanStatic(false); 353 } 354 } 355 356 365 public XBooleanStatic connect( ExpressionContext exprContext, String driver, String dbURL, String user, String password ) 366 { 367 try 368 { 369 Properties prop = new Properties (); 370 prop.put("user", user); 371 prop.put("password", password); 372 373 init(driver, dbURL, prop); 374 375 return new XBooleanStatic(true); 376 } 377 catch(SQLException e) 378 { 379 setError(e,exprContext); 380 return new XBooleanStatic(false); 381 } 382 catch (Exception e) 383 { 384 setError(e,exprContext); 385 return new XBooleanStatic(false); 386 } 387 } 388 389 390 399 public XBooleanStatic connect( ExpressionContext exprContext, String driver, String dbURL, Element protocolElem ) 400 { 401 try 402 { 403 Properties prop = new Properties (); 404 405 NamedNodeMap atts = protocolElem.getAttributes(); 406 407 for (int i = 0; i < atts.getLength(); i++) 408 { 409 prop.put(atts.item(i).getNodeName(), atts.item(i).getNodeValue()); 410 } 411 412 init(driver, dbURL, prop); 413 414 return new XBooleanStatic(true); 415 } 416 catch(SQLException e) 417 { 418 setError(e,exprContext); 419 return new XBooleanStatic(false); 420 } 421 catch (Exception e) 422 { 423 setError(e, exprContext); 424 return new XBooleanStatic(false); 425 } 426 } 427 428 429 456 private void initFromElement( Element e )throws SQLException 457 { 458 459 Properties prop = new Properties (); 460 String driver = ""; 461 String dbURL = ""; 462 Node n = e.getFirstChild(); 463 464 if (null == n) return; 466 do 467 { 468 String nName = n.getNodeName(); 469 470 if (nName.equalsIgnoreCase("dbdriver")) 471 { 472 driver = ""; 473 Node n1 = n.getFirstChild(); 474 if (null != n1) 475 { 476 driver = n1.getNodeValue(); 477 } 478 } 479 480 if (nName.equalsIgnoreCase("dburl")) 481 { 482 dbURL = ""; 483 Node n1 = n.getFirstChild(); 484 if (null != n1) 485 { 486 dbURL = n1.getNodeValue(); 487 } 488 } 489 490 if (nName.equalsIgnoreCase("password")) 491 { 492 String s = ""; 493 Node n1 = n.getFirstChild(); 494 if (null != n1) 495 { 496 s = n1.getNodeValue(); 497 } 498 prop.put("password", s); 499 } 500 501 if (nName.equalsIgnoreCase("user")) 502 { 503 String s = ""; 504 Node n1 = n.getFirstChild(); 505 if (null != n1) 506 { 507 s = n1.getNodeValue(); 508 } 509 prop.put("user", s); 510 } 511 512 if (nName.equalsIgnoreCase("protocol")) 513 { 514 String Name = ""; 515 516 NamedNodeMap attrs = n.getAttributes(); 517 Node n1 = attrs.getNamedItem("name"); 518 if (null != n1) 519 { 520 String s = ""; 521 Name = n1.getNodeValue(); 522 523 Node n2 = n.getFirstChild(); 524 if (null != n2) s = n2.getNodeValue(); 525 526 prop.put(Name, s); 527 } 528 } 529 530 } while ( (n = n.getNextSibling()) != null); 531 532 init(driver, dbURL, prop); 533 } 534 535 536 537 547 private void init( String driver, String dbURL, Properties prop )throws SQLException 548 { 549 Connection con = null; 550 551 if (DEBUG) 552 System.out.println("XConnection, Connection Init"); 553 554 String user = prop.getProperty("user"); 555 if (user == null) user = ""; 556 557 String passwd = prop.getProperty("password"); 558 if (passwd == null) passwd = ""; 559 560 561 String poolName = driver + dbURL + user + passwd; 562 ConnectionPool cpool = m_PoolMgr.getPool(poolName); 563 564 if (cpool == null) 565 { 566 567 if (DEBUG) 568 { 569 System.out.println("XConnection, Creating Connection"); 570 System.out.println(" Driver :" + driver); 571 System.out.println(" URL :" + dbURL); 572 System.out.println(" user :" + user); 573 System.out.println(" passwd :" + passwd); 574 } 575 576 577 DefaultConnectionPool defpool = new DefaultConnectionPool(); 578 579 if ((DEBUG) && (defpool == null)) 580 System.out.println("Failed to Create a Default Connection Pool"); 581 582 defpool.setDriver(driver); 583 defpool.setURL(dbURL); 584 defpool.setProtocol(prop); 585 586 if (m_DefaultPoolingEnabled) defpool.setPoolEnabled(true); 589 590 m_PoolMgr.registerPool(poolName, defpool); 591 m_ConnectionPool = defpool; 592 } 593 else 594 { 595 m_ConnectionPool = cpool; 596 } 597 598 m_IsDefaultPool = true; 599 600 try 605 { 606 con = m_ConnectionPool.getConnection(); 607 } 608 catch(SQLException e) 609 { 610 if (con != null) 611 { 612 m_ConnectionPool.releaseConnectionOnError(con); 613 con = null; 614 } 615 throw e; 616 } 617 finally 618 { 619 if ( con != null ) m_ConnectionPool.releaseConnection(con); 620 } 621 } 622 623 627 public ConnectionPool getConnectionPool() 628 { 629 return m_ConnectionPool; 630 } 631 632 633 645 public DTM query( ExpressionContext exprContext, String queryString ) 646 { 647 SQLDocument doc = null; 648 649 try 650 { 651 if (DEBUG) System.out.println("pquery()"); 652 653 if ( null == m_ConnectionPool ) return null; 655 656 SQLQueryParser query = 657 m_QueryParser.parse 658 (this, queryString, SQLQueryParser.NO_INLINE_PARSER); 659 660 doc = SQLDocument.getNewDocument(exprContext); 661 doc.execute(this, query); 662 663 m_OpenSQLDocuments.addElement(doc); 665 } 666 catch (Exception e) 667 { 668 671 if (DEBUG) System.out.println("exception in query()"); 672 673 if (doc != null) 674 { 675 if (doc.hasErrors()) 676 { 677 setError(e, doc, doc.checkWarnings()); 678 } 679 680 doc.close(); 681 doc = null; 682 } 683 } 684 finally 685 { 686 if (DEBUG) System.out.println("leaving query()"); 687 } 688 689 return doc; 691 } 692 693 705 public DTM pquery( ExpressionContext exprContext, String queryString ) 706 { 707 return(pquery(exprContext, queryString, null)); 708 } 709 710 727 public DTM pquery( ExpressionContext exprContext, String queryString, String typeInfo) 728 { 729 SQLDocument doc = null; 730 731 try 732 { 733 if (DEBUG) System.out.println("pquery()"); 734 735 if ( null == m_ConnectionPool ) return null; 737 738 SQLQueryParser query = 739 m_QueryParser.parse 740 (this, queryString, SQLQueryParser.NO_OVERRIDE); 741 742 if ( !m_InlineVariables ) 745 { 746 addTypeToData(typeInfo); 747 query.setParameters(m_ParameterList); 748 } 749 750 doc = SQLDocument.getNewDocument(exprContext); 751 doc.execute(this, query); 752 753 m_OpenSQLDocuments.addElement(doc); 755 } 756 catch (Exception e) 757 { 758 761 if (DEBUG) System.out.println("exception in query()"); 762 763 if (doc != null) 764 { 765 if (doc.hasErrors()) 766 { 767 setError(e, doc, doc.checkWarnings()); 768 } 769 770 doc.close(); 771 doc = null; 772 } 773 } 774 finally 775 { 776 if (DEBUG) System.out.println("leaving query()"); 777 } 778 779 return doc; 781 } 782 783 private void addTypeToData(String typeInfo) 784 { 785 int indx; 786 787 if ( typeInfo != null && m_ParameterList != null ) 788 { 789 StringTokenizer plist = new StringTokenizer (typeInfo); 792 793 indx = 0; 798 while (plist.hasMoreTokens()) 799 { 800 String value = plist.nextToken(); 801 QueryParameter qp = (QueryParameter) m_ParameterList.elementAt(indx); 802 if ( null != qp ) 803 { 804 qp.setTypeName(value); 805 } 806 807 indx++; 808 } 809 } 810 } 811 812 817 public void addParameter( String value ) 818 { 819 addParameterWithType(value, null); 820 } 821 822 828 public void addParameterWithType( String value, String Type ) 829 { 830 m_ParameterList.addElement( new QueryParameter(value, Type) ); 831 } 832 833 834 840 public void addParameterFromElement( Element e ) 841 { 842 NamedNodeMap attrs = e.getAttributes(); 843 Node Type = attrs.getNamedItem("type"); 844 Node n1 = e.getFirstChild(); 845 if (null != n1) 846 { 847 String value = n1.getNodeValue(); 848 if (value == null) value = ""; 849 m_ParameterList.addElement( new QueryParameter(value, Type.getNodeValue()) ); 850 } 851 } 852 853 854 860 public void addParameterFromElement( NodeList nl ) 861 { 862 int count = nl.getLength(); 879 for (int x=0; x<count; x++) 880 { 881 addParameters( (Element ) nl.item(x)); 882 } 883 } 884 885 889 private void addParameters( Element elem ) 890 { 891 902 Node n = elem.getFirstChild(); 903 904 if (null == n) return; 905 906 do 907 { 908 if (n.getNodeType() == Node.ELEMENT_NODE) 909 { 910 NamedNodeMap attrs = n.getAttributes(); 911 Node Type = attrs.getNamedItem("type"); 912 String TypeStr; 913 914 if (Type == null) TypeStr = "string"; 915 else TypeStr = Type.getNodeValue(); 916 917 Node n1 = n.getFirstChild(); 918 if (null != n1) 919 { 920 String value = n1.getNodeValue(); 921 if (value == null) value = ""; 922 923 924 m_ParameterList.addElement( 925 new QueryParameter(value, TypeStr) ); 926 } 927 } 928 } while ( (n = n.getNextSibling()) != null); 929 } 930 931 934 public void clearParameters( ) 935 { 936 m_ParameterList.removeAllElements(); 937 } 938 939 951 public void enableDefaultConnectionPool( ) 952 { 953 954 if (DEBUG) 955 System.out.println("Enabling Default Connection Pool"); 956 957 m_DefaultPoolingEnabled = true; 958 959 if (m_ConnectionPool == null) return; 960 if (m_IsDefaultPool) return; 961 962 m_ConnectionPool.setPoolEnabled(true); 963 964 } 965 966 971 public void disableDefaultConnectionPool( ) 972 { 973 if (DEBUG) 974 System.out.println("Disabling Default Connection Pool"); 975 976 m_DefaultPoolingEnabled = false; 977 978 if (m_ConnectionPool == null) return; 979 if (!m_IsDefaultPool) return; 980 981 m_ConnectionPool.setPoolEnabled(false); 982 } 983 984 985 993 public void enableStreamingMode( ) 994 { 995 996 if (DEBUG) 997 System.out.println("Enabling Streaming Mode"); 998 999 m_IsStreamingEnabled = true; 1000 } 1001 1002 1010 public void disableStreamingMode( ) 1011 { 1012 1013 if (DEBUG) 1014 System.out.println("Disable Streaming Mode"); 1015 1016 m_IsStreamingEnabled = false; 1017 } 1018 1019 1024 public DTM getError( ) 1025 { 1026 if ( m_FullErrors ) 1027 { 1028 for ( int idx = 0 ; idx < m_OpenSQLDocuments.size() ; idx++ ) 1029 { 1030 SQLDocument doc = (SQLDocument)m_OpenSQLDocuments.elementAt(idx); 1031 SQLWarning warn = doc.checkWarnings(); 1032 if ( warn != null ) setError(null, doc, warn); 1033 } 1034 } 1035 1036 return(buildErrorDocument()); 1037 } 1038 1039 1044 public void close( )throws SQLException 1045 { 1046 if (DEBUG) 1047 System.out.println("Entering XConnection.close()"); 1048 1049 1054 while(m_OpenSQLDocuments.size() != 0) 1055 { 1056 SQLDocument d = (SQLDocument) m_OpenSQLDocuments.elementAt(0); 1057 try 1058 { 1059 d.close(); 1060 } 1061 catch (Exception se ) {} 1062 1063 m_OpenSQLDocuments.removeElementAt(0); 1064 } 1065 1066 if ( null != m_Connection ) 1067 { 1068 m_ConnectionPool.releaseConnection(m_Connection); 1069 m_Connection = null; 1070 } 1071 1072 if (DEBUG) 1073 System.out.println("Exiting XConnection.close"); 1074 } 1075 1076 1083 public void close( SQLDocument sqldoc )throws SQLException 1084 { 1085 if (DEBUG) 1086 System.out.println("Entering XConnection.close(" + sqldoc + ")"); 1087 1088 int size = m_OpenSQLDocuments.size(); 1089 1090 for(int x=0; x<size; x++) 1091 { 1092 SQLDocument d = (SQLDocument) m_OpenSQLDocuments.elementAt(x); 1093 if (d == sqldoc) 1094 { 1095 d.close(); 1096 m_OpenSQLDocuments.removeElementAt(x); 1097 } 1098 } 1099 } 1100 1101 1102 1107 private SQLErrorDocument buildErrorDocument() 1108 { 1109 SQLErrorDocument eDoc = null; 1110 1111 if ( m_LastSQLDocumentWithError != null) 1112 { 1113 1117 ExpressionContext ctx = m_LastSQLDocumentWithError.getExpressionContext(); 1118 SQLWarning warn = m_LastSQLDocumentWithError.checkWarnings(); 1119 1120 1121 try 1122 { 1123 DTMManager mgr = 1124 ((XPathContext.XPathExpressionContext)ctx).getDTMManager(); 1125 DTMManagerDefault mgrDefault = (DTMManagerDefault) mgr; 1126 int dtmIdent = mgrDefault.getFirstFreeDTMID(); 1127 1128 eDoc = new SQLErrorDocument( 1129 mgr, dtmIdent<<DTMManager.IDENT_DTM_NODE_BITS, 1130 m_Error, warn, m_FullErrors); 1131 1132 mgrDefault.addDTM(eDoc, dtmIdent); 1134 1135 m_Error = null; 1137 m_LastSQLDocumentWithError = null; 1138 } 1139 catch(Exception e) 1140 { 1141 eDoc = null; 1142 } 1143 } 1144 1145 return(eDoc); 1146 } 1147 1148 1149 1154 public void setError(Exception excp,ExpressionContext expr) 1155 { 1156 try 1157 { 1158 ErrorListener listen = expr.getErrorListener(); 1159 if ( listen != null && excp != null ) 1160 { 1161 listen.warning( 1162 new TransformerException (excp.toString(), 1163 expr.getXPathContext().getSAXLocator(), excp)); 1164 } 1165 } 1166 catch(Exception e) {} 1167 } 1168 1169 1175 public void setError(Exception excp, SQLDocument doc, SQLWarning warn) 1176 { 1177 1178 ExpressionContext cont = doc.getExpressionContext(); 1179 m_LastSQLDocumentWithError = doc; 1180 1181 try 1182 { 1183 ErrorListener listen = cont.getErrorListener(); 1184 if ( listen != null && excp != null ) 1185 listen.warning( 1186 new TransformerException (excp.toString(), 1187 cont.getXPathContext().getSAXLocator(), excp)); 1188 1189 if ( listen != null && warn != null ) 1190 { 1191 listen.warning(new TransformerException ( 1192 warn.toString(), cont.getXPathContext().getSAXLocator(), warn)); 1193 } 1194 1195 if ( excp != null ) m_Error = excp; 1197 1198 if ( warn != null ) 1199 { 1200 SQLWarning tw = 1203 new SQLWarning (warn.getMessage(), warn.getSQLState(), 1204 warn.getErrorCode()); 1205 SQLWarning nw = warn.getNextWarning(); 1206 while ( nw != null ) 1207 { 1208 tw.setNextWarning(new SQLWarning (nw.getMessage(), 1209 nw.getSQLState(), nw.getErrorCode())); 1210 1211 nw = nw.getNextWarning(); 1212 } 1213 1214 tw.setNextWarning( 1215 new SQLWarning (warn.getMessage(), warn.getSQLState(), 1216 warn.getErrorCode())); 1217 1218 1220 } 1221 } 1222 catch(Exception e) 1223 { 1224 } 1226 } 1227 1228 1234 public void setFeature(String feature, String setting) 1235 { 1236 boolean value = false; 1237 1238 if ( "true".equalsIgnoreCase(setting) ) value = true; 1239 1240 if ( "streaming".equalsIgnoreCase(feature) ) 1241 { 1242 m_IsStreamingEnabled = value; 1243 } 1244 else if ( "inline-variables".equalsIgnoreCase(feature) ) 1245 { 1246 m_InlineVariables = value; 1247 } 1248 else if ( "multiple-results".equalsIgnoreCase(feature) ) 1249 { 1250 m_IsMultipleResultsEnabled = value; 1251 } 1252 else if ( "cache-statements".equalsIgnoreCase(feature) ) 1253 { 1254 m_IsStatementCachingEnabled = value; 1255 } 1256 else if ( "default-pool-enabled".equalsIgnoreCase(feature) ) 1257 { 1258 m_DefaultPoolingEnabled = value; 1259 1260 if (m_ConnectionPool == null) return; 1261 if (m_IsDefaultPool) return; 1262 1263 m_ConnectionPool.setPoolEnabled(value); 1264 } 1265 else if ( "full-errors".equalsIgnoreCase(feature) ) 1266 { 1267 m_FullErrors = value; 1268 } 1269 } 1270 1271 1276 public String getFeature(String feature) 1277 { 1278 String value = null; 1279 1280 if ( "streaming".equalsIgnoreCase(feature) ) 1281 value = m_IsStreamingEnabled ? "true" : "false"; 1282 else if ( "inline-variables".equalsIgnoreCase(feature) ) 1283 value = m_InlineVariables ? "true" : "false"; 1284 else if ( "multiple-results".equalsIgnoreCase(feature) ) 1285 value = m_IsMultipleResultsEnabled ? "true" : "false"; 1286 else if ( "cache-statements".equalsIgnoreCase(feature) ) 1287 value = m_IsStatementCachingEnabled ? "true" : "false"; 1288 else if ( "default-pool-enabled".equalsIgnoreCase(feature) ) 1289 value = m_DefaultPoolingEnabled ? "true" : "false"; 1290 else if ( "full-errors".equalsIgnoreCase(feature) ) 1291 value = m_FullErrors ? "true" : "false"; 1292 1293 return(value); 1294 } 1295 1296 1297 1298 1301 protected void finalize( ) 1302 { 1303 if (DEBUG) System.out.println("In XConnection, finalize"); 1304 try 1305 { 1306 close(); 1307 } 1308 catch(Exception e) 1309 { 1310 } 1312 } 1313 1314} 1315 | Popular Tags |