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;
007:
008: import static org.openrdf.sail.rdbms.algebra.base.SqlExprSupport.unsupported;
009:
010: import java.sql.Connection;
011: import java.sql.SQLException;
012:
013: import org.openrdf.query.algebra.Regex;
014: import org.openrdf.query.algebra.ValueConstant;
015: import org.openrdf.query.algebra.ValueExpr;
016: import org.openrdf.sail.rdbms.RdbmsConnectionFactory;
017: import org.openrdf.sail.rdbms.algebra.factories.BooleanExprFactory;
018: import org.openrdf.sail.rdbms.evaluation.QueryBuilderFactory;
019: import org.openrdf.sail.rdbms.evaluation.SqlExprBuilder;
020: import org.openrdf.sail.rdbms.evaluation.SqlRegexBuilder;
021: import org.openrdf.sail.rdbms.exceptions.UnsupportedRdbmsOperatorException;
022: import org.openrdf.sail.rdbms.optimizers.SelectQueryOptimizerFactory;
023: import org.openrdf.sail.rdbms.schema.NamespacesTable;
024: import org.openrdf.sail.rdbms.schema.RdbmsTable;
025: import org.openrdf.sail.rdbms.schema.TableFactory;
026: import org.openrdf.sail.rdbms.schema.ValueTable;
027: import org.openrdf.sail.rdbms.schema.ValueTableFactory;
028:
029: public class MySqlConnectionFactory extends RdbmsConnectionFactory {
030:
031: private static final String FEILD_COLLATE = " CHARACTER SET utf8 COLLATE utf8_bin";
032:
033: @Override
034: protected TableFactory createTableFactory() {
035: return new TableFactory() {
036: @Override
037: protected RdbmsTable newTable(String name) {
038: return new MySqlTable(name);
039: }
040: };
041: }
042:
043: @Override
044: protected ValueTableFactory createValueTableFactory() {
045: return new ValueTableFactory(createTableFactory()) {
046:
047: @Override
048: protected ValueTable newValueTable() {
049: return new MySqlValueTable();
050: }
051:
052: @Override
053: public NamespacesTable createNamespacesTable(Connection conn) {
054: return new NamespacesTable(
055: createTable(conn, NAMESPACES)) {
056:
057: @Override
058: protected void createTable() throws SQLException {
059: StringBuilder sb = new StringBuilder();
060: sb.append(" prefix VARCHAR(127)");
061: sb.append(FEILD_COLLATE);
062: sb.append(",\n namespace TEXT ");
063: sb.append(FEILD_COLLATE);
064: sb.append(" NOT NULL\n");
065: createTable(sb);
066: }
067: };
068: }
069: };
070: }
071:
072: @Override
073: protected SelectQueryOptimizerFactory createSelectQueryOptimizerFactory() {
074: return new SelectQueryOptimizerFactory() {
075:
076: @Override
077: protected BooleanExprFactory createBooleanExprFactory() {
078: return new BooleanExprFactory() {
079:
080: @Override
081: public void meet(Regex node)
082: throws UnsupportedRdbmsOperatorException {
083: ValueExpr flagsArg = node.getFlagsArg();
084: if (flagsArg == null) {
085: super .meet(node);
086: } else if (flagsArg instanceof ValueConstant) {
087: ValueConstant flags = (ValueConstant) flagsArg;
088: if (flags.getValue().stringValue().equals(
089: "i")) {
090: super .meet(node);
091: } else {
092: throw unsupported(node);
093: }
094: } else {
095: throw unsupported(node);
096: }
097: }
098: };
099: }
100: };
101: }
102:
103: @Override
104: protected QueryBuilderFactory createQueryBuilderFactory() {
105: return new QueryBuilderFactory() {
106:
107: @Override
108: public SqlRegexBuilder createSqlRegexBuilder(
109: SqlExprBuilder where) {
110: return new SqlRegexBuilder(where, this ) {
111:
112: @Override
113: protected void appendRegExp(SqlExprBuilder where) {
114: appendValue(where);
115: if (!this .flags().getParameters().isEmpty()) {
116: where.append(" COLLATE utf8_general_ci");
117: }
118: where.append(" REGEXP ");
119: appendPattern(where);
120: }
121: };
122: }
123: };
124: }
125: }
|