1 /*
2  * Copyright 2015-2018 HuntLabs.cn
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 module hunt.sql.util.PGUtils;
17 
18 import hunt.sql.util.FnvHash;
19 import hunt.sql.util.Utils;
20 import hunt.collection;
21 import std.uni;
22 import std.string;
23 
24 public class PGUtils {
25 
26     // public static XAConnection createXAConnection(Connection physicalConn) throws SQLException {
27     //     return new PGXAConnection((BaseConnection) physicalConn);
28     // }
29 
30     // public static List!(string) showTables(Connection conn) throws SQLException {
31     //     List!(string) tables = new ArrayList!(string)();
32 
33     //     Statement stmt = null;
34     //     ResultSet rs = null;
35     //     try {
36     //         stmt = conn.createStatement();
37     //         rs = stmt.executeQuery("SELECT tablename FROM pg_catalog.pg_tables where schemaname not in ('pg_catalog', 'information_schema', 'sys')");
38     //         while (rs.next()) {
39     //             string tableName = rs.getString(1);
40     //             tables.add(tableName);
41     //         }
42     //     } finally {
43     //         DBType.close(rs);
44     //         DBType.close(stmt);
45     //     }
46 
47     //     return tables;
48     // }
49 
50     private static Set!(string) keywords;
51     public static bool isKeyword(string name) {
52         if (name is null) {
53             return false;
54         }
55 
56         string name_lower = toLower(name);
57 
58         Set!(string) words = keywords;
59 
60         if (words is null) {
61             words = new HashSet!(string)();
62             Utils.loadFromFile("entity/sql/resource/postgresql/keywords", words);
63             keywords = words;
64         }
65 
66         return words.contains(name_lower);
67     }
68 
69     private  __gshared long[] pseudoColumnHashCodes;
70     
71     shared static this(){
72         long[] array = [
73                 FnvHash.Constants.CURRENT_TIMESTAMP
74         ];
75         // Arrays.sort(array);
76         pseudoColumnHashCodes = array;
77     }
78 
79     public static bool isPseudoColumn(long hash) {
80         return search(pseudoColumnHashCodes, hash) >= 0;
81     }
82 }