ApacheCalcite實(shí)現(xiàn)方言轉(zhuǎn)換的代碼

定義

創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站推廣、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為成都OPP膠袋等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。

Calcite能夠通過解析Sql為SqlNode,再將SqlNode轉(zhuǎn)化為特定數(shù)據(jù)庫的方言的形式實(shí)現(xiàn)Sql的統(tǒng)一。

實(shí)現(xiàn)

在Calcite中實(shí)現(xiàn)方言轉(zhuǎn)換的主要類是SqlDialect基類,其具體的變量含義如下:

public class SqlDialect {BUILT_IN_OPERATORS_LIST: 支持的內(nèi)置定義函數(shù)或者運(yùn)算符(例如:abs and..)// 列 表的標(biāo)識(shí)符String identifierQuoteString: 標(biāo)識(shí)符的開始符號(hào)String identifierEndQuoteString: 標(biāo)識(shí)符的結(jié)束符號(hào)String identifierEscapedQuote: (暫時(shí)沒有弄明白這個(gè)在做什么,像是字符串中的轉(zhuǎn)義符?)// 常量的標(biāo)識(shí)符String literalQuoteString: 常量的開始符號(hào)String literalEndQuoteString: 常量的結(jié)束符號(hào)String literalEscapedQuote:(暫時(shí)沒有弄明白這個(gè)在做什么,像是字符串中的轉(zhuǎn)義符?)DatabaseProduct databaseProduct: 所屬的數(shù)據(jù)庫產(chǎn)品NullCollation nullCollation: 在進(jìn)行排序查詢式,空值的返回順序RelDataTypeSystem dataTypeSystem: 數(shù)據(jù)類型// 和解析相關(guān)Casing unquotedCasing: 大小寫轉(zhuǎn)換Casing quotedCasing: 大小寫轉(zhuǎn)換boolean caseSensitive: 是否大小寫敏感(列名 表明 函數(shù)名等)}// 方法區(qū)(不同的數(shù)據(jù)源根據(jù)細(xì)節(jié)的不同實(shí)現(xiàn)自定義的復(fù)寫方法)allowsAsconfigureParserconfigureParsercontainsNonAsciicreatedefaultNullDirectionemptyContextemulateJoinTypeForCrossJoinemulateNullDirectionemulateNullDirectionWithIsNullgetCalendarPolicygetCastSpecgetConformancegetDatabaseProductgetNullCollationgetProductgetQuotedCasinggetQuotinggetSingleRowTableNamegetTypeSystemgetUnquotedCasinghasImplicitTableAliasidentifierNeedsQuoteisCaseSensitivequoteIdentifierquoteIdentifierquoteIdentifierquoteStringLiteralquoteStringLiteralquoteStringLiteralUnicodequoteTimestampLiteralrequiresAliasForFromItemsrewriteSingleValueExprsupportsAggregateFunctionsupportsAliasedValuessupportsCharSetsupportsDataTypesupportsFunctionsupportsGroupByWithCubesupportsGroupByWithRollupsupportsImplicitTypeCoercionsupportsNestedAggregationssupportsOffsetFetchsupportsWindowFunctionsunparseCallunparseDateTimeLiteralunparseFetchUsingAnsiunparseFetchUsingLimitunparseLimitunparseOffsetunparseOffsetFetchunparseSqlDatetimeArithmeticunparseSqlIntervalLiteralunparseSqlIntervalQualifierunparseTopNunquoteStringLiteral

使用方式Demo

/** Returns SqlNode for type in "cast(column as type)", which might be * different between databases by type name, precision etc. * * <p>If this method returns null, the cast will be omitted. In the default * implementation, this is the case for the NULL type, and therefore * {@code CAST(NULL AS <nulltype>)} is rendered as {@code NULL}. */ public SqlNode getCastSpec(RelDataType type) 這個(gè)方法就可以根據(jù)具體的數(shù)據(jù)源的數(shù)據(jù)類型進(jìn)行轉(zhuǎn)換,例如: @Override public SqlNode getCastSpec(RelDataType type) { switch (type.getSqlTypeName()) { case VARCHAR: // MySQL doesn't have a VARCHAR type, only CHAR. int vcMaxPrecision = this.getTypeSystem().getMaxPrecision(SqlTypeName.CHAR); int precision = type.getPrecision(); if (vcMaxPrecision > 0 && precision > vcMaxPrecision) { precision = vcMaxPrecision; } return new SqlDataTypeSpec( new SqlBasicTypeNameSpec(SqlTypeName.CHAR, precision, SqlParserPos.ZERO), SqlParserPos.ZERO); } return super.getCastSpec(type); } 就可以經(jīng)Sql中的Cast語句Cast為特定的類型: final String query = "select cast(\"product_id\" as varchar(50)), \"product_id\" " + "from \"product\" "; final String expected = "SELECT CAST(`product_id` AS CHAR(50)), `product_id`\n" + "FROM `foodmart`.`product`";// 解析過的SqlNodesqlNode.toSqlString(CalciteSqlDialect.DEFAULT).getSql();

到此這篇關(guān)于Apache Calcite 實(shí)現(xiàn)方言轉(zhuǎn)換的代碼的文章就介紹到這了,更多相關(guān)Apache Calcite方言轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)題名稱:ApacheCalcite實(shí)現(xiàn)方言轉(zhuǎn)換的代碼
網(wǎng)站URL:http://bm7419.com/article32/cdeisc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)自適應(yīng)網(wǎng)站、建站公司品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化