1 7 8 package com.hp.hpl.jena.db.impl; 9 10 import java.sql.PreparedStatement ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 14 import com.hp.hpl.jena.db.*; 15 16 17 22 public class Driver_MySQL extends DriverRDB { 23 24 25 26 29 public Driver_MySQL( ){ 30 super(); 31 32 String myPackageName = this.getClass().getPackage().getName(); 33 34 DATABASE_TYPE = "MySQL"; 35 DRIVER_NAME = "com.mysql.jdbc.Driver"; 36 37 ID_SQL_TYPE = "INTEGER"; 38 URI_COMPRESS = false; 39 INDEX_KEY_LENGTH_MAX = INDEX_KEY_LENGTH = 250; 40 LONG_OBJECT_LENGTH_MAX = LONG_OBJECT_LENGTH = 250; 41 TABLE_NAME_LENGTH_MAX = 64; 42 IS_XACT_DB = true; 43 PRE_ALLOCATE_ID = false; 44 SKIP_DUPLICATE_CHECK = false; 45 SQL_FILE = "etc/mysql.sql"; 46 DB_NAMES_TO_UPPER = false; 47 setTableNames(TABLE_NAME_PREFIX); 48 QUOTE_CHAR = '\''; 49 50 51 m_psetClassName = myPackageName + ".PSet_TripleStore_RDB"; 52 m_psetReifierClassName = myPackageName + ".PSet_ReifStore_RDB"; 53 54 m_lsetClassName = myPackageName + ".SpecializedGraph_TripleStore_RDB"; 55 m_lsetReifierClassName = myPackageName + ".SpecializedGraphReifier_RDB"; 56 } 57 58 61 public void setConnection( IDBConnection dbcon ) { 62 m_dbcon = dbcon; 63 64 try { 65 m_sql = new SQLCache(SQL_FILE, null, dbcon, ID_SQL_TYPE); 68 } catch (Exception e) { 69 e.printStackTrace( System.err ); 70 logger.error("Unable to set connection for Driver:", e); 71 } 72 } 73 74 78 public int graphIdAlloc ( String graphName ) { 79 DBIDInt result = null; 80 int dbid = 0; 81 try { 82 PreparedStatement ps = m_sql.getPreparedSQLStatement("insertGraph",GRAPH_TABLE); 83 ps.setString(1,graphName); 84 ps.executeUpdate(); 85 dbid = getInsertID(GRAPH_TABLE); 86 } catch (SQLException e) { 87 throw new RDFRDBException("Failed to get last inserted ID: " + e); 88 } 89 return dbid; 90 } 91 92 96 public void graphIdDealloc ( int graphId ) { 97 DBIDInt result = null; 98 try { 99 PreparedStatement ps = m_sql.getPreparedSQLStatement("deleteGraph",GRAPH_TABLE); 100 ps.setInt(1,graphId); 101 ps.executeUpdate(); 102 } catch (SQLException e) { 103 throw new RDFRDBException("Failed to delete graph ID: " + e); 104 } 105 return; 106 } 107 108 public int getInsertID ( String tableName ) { 109 DBIDInt result = null; 110 try { 111 PreparedStatement ps = m_sql.getPreparedSQLStatement("getInsertID"); 112 ResultSet rs = ps.executeQuery(); 113 if (rs.next()) { 114 result = wrapDBID(rs.getObject(1)); 115 } else 116 throw new RDFRDBException("No last insert ID"); 117 } catch (SQLException e) { 118 throw new RDFRDBException("Failed to get last inserted ID: " + e); 119 } 120 return result.getIntID(); 121 } 122 123 124 134 protected void getTblParams ( String [] param ) { 135 String objColType; 136 String tblImpl; 137 String spoKeyLen; 138 String headKeyLen; 139 String headColType; 140 141 spoKeyLen = Integer.toString(LONG_OBJECT_LENGTH); 142 headKeyLen = Integer.toString(INDEX_KEY_LENGTH); 143 144 if ( INDEX_KEY_LENGTH > 250 ) 145 throw new RDFRDBException("Key length specified (" + INDEX_KEY_LENGTH + 146 ") exceeds MySQL maximum key length of 250."); 147 tblImpl = IS_XACT_DB ? "INNODB" : "MyISAM"; 148 if ( IS_XACT_DB ) { 149 if ( LONG_OBJECT_LENGTH > 250 ) 150 throw new RDFRDBException("Long object length specified (" + LONG_OBJECT_LENGTH + 151 ") exceeds MySQL maximum VARCHAR length of 250."); 152 153 objColType = "VARCHAR(" + LONG_OBJECT_LENGTH + ") BINARY"; 154 STRINGS_TRIMMED = true; 155 EOS = ":"; 156 } else { 157 objColType = LONG_OBJECT_LENGTH <= 250 ? 158 "TINYBLOB" : "MEDIUMBLOB"; 159 STRINGS_TRIMMED = false; 160 EOS = ""; 161 } 162 if ( IS_XACT_DB ) { 163 if ( INDEX_KEY_LENGTH > 250 ) 164 throw new RDFRDBException("Index key length specified (" + INDEX_KEY_LENGTH + 165 ") exceeds MySQL maximum VARCHAR length of 250."); 166 167 headColType = "VARCHAR(" + INDEX_KEY_LENGTH + ") BINARY"; 168 } else { 169 headColType = INDEX_KEY_LENGTH <= 250 ? 170 "TINYBLOB" : "MEDIUMBLOB"; 171 } 172 173 param[0] = objColType; 174 param[1] = tblImpl; 175 param[2] = spoKeyLen; 176 param[3] = headColType; 177 param[4] = headKeyLen; 178 param[5] = TABLE_NAME_PREFIX; 179 } 180 181 182 183 187 protected String [] getDbInitTablesParams() { 188 String [] res = new String [6]; 189 190 getTblParams (res); 191 if ( IS_XACT_DB ) { 192 STRINGS_TRIMMED = true; 193 EOS = ":"; 194 } else { 195 STRINGS_TRIMMED = false; 196 EOS = ""; 197 } 198 EOS_LEN = EOS.length(); 199 200 return res; 201 } 202 203 211 212 protected String [] getCreateTableParams( int graphId, boolean isReif ) { 213 String [] parms = new String [6]; 214 String [] res = new String [4]; 215 216 getTblParams (parms); 217 int tblCnt = getTableCount(graphId); 218 res[0] = genTableName(graphId,tblCnt,isReif); 219 res[1] = parms[0]; 220 res[2] = parms[1]; 221 res[3] = parms[2]; 222 return res; 223 } 224 225 public String genSQLStringMatchLHS_IC(String var) { 226 return "cast(" + var + " as char)"; 227 } 228 229 } 230 231 257 | Popular Tags |