001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.mysql.alt;
007:
008: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.unsupported;
009:
010: import java.sql.Connection;
011: import java.sql.PreparedStatement;
012: import java.sql.SQLException;
013:
014: import org.openrdf.query.algebra.Regex;
015: import org.openrdf.query.algebra.ValueConstant;
016: import org.openrdf.query.algebra.ValueExpr;
017: import org.openrdf.sail.rdbms.RdbmsConnectionFactory;
018: import org.openrdf.sail.rdbms.algebra.factories.BooleanExprFactory;
019: import org.openrdf.sail.rdbms.evaluation.QueryBuilderFactory;
020: import org.openrdf.sail.rdbms.evaluation.SqlExprBuilder;
021: import org.openrdf.sail.rdbms.evaluation.SqlRegexBuilder;
022: import org.openrdf.sail.rdbms.exceptions.UnsupportedRdbmsOperatorException;
023: import org.openrdf.sail.rdbms.managers.TransTableManager;
024: import org.openrdf.sail.rdbms.optimizers.SelectQueryOptimizerFactory;
025: import org.openrdf.sail.rdbms.schema.NamespacesTable;
026: import org.openrdf.sail.rdbms.schema.RdbmsTable;
027: import org.openrdf.sail.rdbms.schema.TableFactory;
028: import org.openrdf.sail.rdbms.schema.TransactionTable;
029: import org.openrdf.sail.rdbms.schema.ValueTable;
030: import org.openrdf.sail.rdbms.schema.ValueTableFactory;
031:
032: public class MySqlConnectionFactory extends RdbmsConnectionFactory {
033:
034: private static final String FEILD_COLLATE = " CHARACTER SET utf8 COLLATE utf8_bin";
035:
036: @Override
037: protected TableFactory createTableFactory() {
038: return new TableFactory() {
039: @Override
040: protected RdbmsTable newTable(String name) {
041: return new MySqlTable(name);
042: }
043: };
044: }
045:
046: @Override
047: protected ValueTableFactory createValueTableFactory() {
048: return new ValueTableFactory(createTableFactory()) {
049:
050: @Override
051: protected ValueTable newValueTable() {
052: return new MySqlValueTable();
053: }
054:
055: @Override
056: public NamespacesTable createNamespacesTable(Connection conn) {
057: return new NamespacesTable(
058: createTable(conn, NAMESPACES)) {
059:
060: @Override
061: protected void createTable() throws SQLException {
062: StringBuilder sb = new StringBuilder();
063: sb.append(" prefix VARCHAR(127)");
064: sb.append(FEILD_COLLATE);
065: sb.append(",\n namespace TEXT ");
066: sb.append(FEILD_COLLATE);
067: sb.append(" NOT NULL\n");
068: createTable(sb);
069: }
070: };
071: }
072: };
073: }
074:
075: @Override
076: protected SelectQueryOptimizerFactory createSelectQueryOptimizerFactory() {
077: return new SelectQueryOptimizerFactory() {
078:
079: @Override
080: protected BooleanExprFactory createBooleanExprFactory() {
081: return new BooleanExprFactory() {
082:
083: @Override
084: public void meet(Regex node)
085: throws UnsupportedRdbmsOperatorException {
086: ValueExpr flagsArg = node.getFlagsArg();
087: if (flagsArg == null) {
088: super .meet(node);
089: } else if (flagsArg instanceof ValueConstant) {
090: ValueConstant flags = (ValueConstant) flagsArg;
091: if (flags.getValue().stringValue().equals(
092: "i")) {
093: super .meet(node);
094: } else {
095: throw unsupported(node);
096: }
097: } else {
098: throw unsupported(node);
099: }
100: }
101: };
102: }
103: };
104: }
105:
106: @Override
107: protected QueryBuilderFactory createQueryBuilderFactory() {
108: return new QueryBuilderFactory() {
109:
110: @Override
111: public SqlRegexBuilder createSqlRegexBuilder(
112: SqlExprBuilder where) {
113: return new SqlRegexBuilder(where, this ) {
114:
115: @Override
116: protected void appendRegExp(SqlExprBuilder where) {
117: appendValue(where);
118: if (!this .flags().getParameters().isEmpty()) {
119: where.append(" COLLATE utf8_general_ci");
120: }
121: where.append(" REGEXP ");
122: appendPattern(where);
123: }
124: };
125: }
126: };
127: }
128:
129: @Override
130: protected TransTableManager createTransTableManager() {
131: return new TransTableManager() {
132:
133: @Override
134: protected TransactionTable createTransactionTable() {
135: return new TransactionTable() {
136: private boolean predColumnPresent;
137:
138: @Override
139: protected String buildInsert(String tableName,
140: boolean predColumnPresent)
141: throws SQLException {
142: this .predColumnPresent = predColumnPresent;
143: return BatchInsertStatement.prepareSql(
144: tableName, getColumns());
145: }
146:
147: @Override
148: protected PreparedStatement prepareInsert(String sql)
149: throws SQLException {
150: String name = getTripleTable().getName();
151: PreparedStatement stmt = super
152: .prepareInsert(sql);
153: return new BatchInsertStatement(stmt, name,
154: getColumns());
155: }
156:
157: private String[] getColumns() {
158: if (predColumnPresent)
159: return new String[] { "ctx", "subj",
160: "pred", "obj" };
161: return new String[] { "ctx", "subj", "obj" };
162: }
163: };
164: }
165: };
166: }
167: }
|