Skip to content

Commit

Permalink
[persistence]添加PostgreSQL数据库Dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
datageartech committed Apr 2, 2020
1 parent a0a286a commit 26ee794
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2018 datagear.tech. All Rights Reserved.
*/

package org.datagear.connection.support;

import org.datagear.connection.PrefixURLSensor;
import org.datagear.connection.URLSensor;

/**
* PostgreSQL {@linkplain URLSensor}。
*
* @author [email protected]
*
*/
public class PostgresqlURLSensor extends PrefixURLSensor
{
public static final String JDBC_PREFIX = "jdbc:postgresql";

public PostgresqlURLSensor()
{
super(JDBC_PREFIX);
}

@Override
public void setPrefix(String prefix)
{
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.datagear.persistence.Order;
import org.datagear.persistence.support.dialect.MysqlDialectBuilder;
import org.datagear.persistence.support.dialect.OracleDialectBuilder;
import org.datagear.persistence.support.dialect.PostgresqlDialectBuilder;
import org.datagear.persistence.support.dialect.SqlServerDialectBuilder;
import org.datagear.util.Sql;

Expand Down Expand Up @@ -57,6 +58,7 @@ public DefaultDialectSource(DBMetaResolver dbMetaResolver)
this.dialectBuilders = new ArrayList<>();

this.dialectBuilders.add(new MysqlDialectBuilder());
this.dialectBuilders.add(new PostgresqlDialectBuilder());
this.dialectBuilders.add(new OracleDialectBuilder());
this.dialectBuilders.add(new SqlServerDialectBuilder());
}
Expand Down Expand Up @@ -150,7 +152,7 @@ protected Dialect getDetectiveDialect(Connection cn) throws DialectException
{
dialect = dialectBuilder.build(cn);
}
catch(Throwable e)
catch (Throwable e)
{
dialect = null;
}
Expand All @@ -163,7 +165,7 @@ protected Dialect getDetectiveDialect(Connection cn) throws DialectException
if (testDialectToPagingSql(cn, databaseMetaData, testInfo, dialect))
combinedDialectBuilder.setToPagingQuerySqlDialectBuilder(dialectBuilder);
}
catch(Throwable e)
catch (Throwable e)
{
}
}
Expand Down Expand Up @@ -244,15 +246,10 @@ protected TestInfo buildTestInfo(Connection cn, DatabaseMetaData databaseMetaDat
protected boolean testDialectToPagingSql(Connection cn, DatabaseMetaData databaseMetaData, TestInfo testInfo,
Dialect dialect) throws Exception
{
String identifierQuote = databaseMetaData.getIdentifierQuoteString();
Sql query = Sql.valueOf().sql("SELECT ").sql(dialect.quote(testInfo.getOrderColumnName())).sql(" FROM ")
.sql(dialect.quote(testInfo.getTableName()));

String tableQuote = identifierQuote + testInfo.getTableName() + identifierQuote;
String columnName = identifierQuote + testInfo.getOrderColumnName() + identifierQuote;

Sql query = Sql.valueOf();
query.sql("SELECT ").sql(columnName).sql(" FROM ").sql(tableQuote);

Order[] orders = Order.asArray(Order.valueOf(columnName, Order.ASC));
Order[] orders = Order.asArray(Order.valueOf(testInfo.getOrderColumnName(), Order.ASC));

Sql pagingQuerySql = dialect.toPagingQuerySql(query, orders, 1, 5);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 datagear.tech. All Rights Reserved.
*/

package org.datagear.persistence.support.dialect;

import org.datagear.persistence.Order;
import org.datagear.persistence.support.AbstractDialect;
import org.datagear.util.Sql;

/**
* PostgreSQL方言。
*
* @author [email protected]
*
*/
public class PostgresqlDialect extends AbstractDialect
{
public PostgresqlDialect()
{
super();
}

public PostgresqlDialect(String identifierQuote)
{
super(identifierQuote);
}

@Override
public boolean supportsPagingSql()
{
return true;
}

@Override
public Sql toPagingQuerySql(Sql query, Order[] orders, long startRow, int count)
{
Sql sql = Sql.valueOf();

Sql orderSql = toOrderSql(orders);

if (isEmptySql(orderSql))
{
sql.sql(query);
}
else
{
sql.sql("SELECT * FROM (");
sql.sql(query);
sql.sql(") T ");
sql.sql(" ORDER BY ");
sql.sql(orderSql);
}

sql.sql(" LIMIT " + count + " OFFSET " + (startRow - 1));

return sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2018 datagear.tech. All Rights Reserved.
*/

/**
*
*/
package org.datagear.persistence.support.dialect;

import java.sql.Connection;

import org.datagear.connection.URLSensor;
import org.datagear.connection.support.PostgresqlURLSensor;
import org.datagear.persistence.Dialect;
import org.datagear.persistence.DialectBuilder;
import org.datagear.persistence.support.AbstractURLSensedDialectBuilder;

/**
* PostgreSQL的{@linkplain DialectBuilder}。
*
* @author [email protected]
*
*/
public class PostgresqlDialectBuilder extends AbstractURLSensedDialectBuilder
{
public PostgresqlDialectBuilder()
{
super(new PostgresqlURLSensor());
}

@Override
public void setUrlSensor(URLSensor urlSensor)
{
throw new UnsupportedOperationException();
}

@Override
public Dialect build(Connection cn)
{
return new PostgresqlDialect(getIdentifierQuote(cn));
}
}

0 comments on commit 26ee794

Please sign in to comment.