diff --git a/dialects/dialect.go b/dialects/dialect.go index 8e512c4f..892fd852 100644 --- a/dialects/dialect.go +++ b/dialects/dialect.go @@ -271,23 +271,28 @@ func regDrvsNDialects() bool { getDriver func() Driver getDialect func() Dialect }{ - "mssql": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, - "odbc": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access - "mysql": {"mysql", func() Driver { return &mysqlDriver{} }, func() Dialect { return &mysql{} }}, - "mymysql": {"mysql", func() Driver { return &mymysqlDriver{} }, func() Dialect { return &mysql{} }}, - "postgres": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &postgres{} }}, - "pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }}, - "sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }}, - "sqlite": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }}, - "oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }}, - "godror": {"oracle", func() Driver { return &godrorDriver{} }, func() Dialect { return &oracle{} }}, - "oracle": {"oracle", func() Driver { return &oracleDriver{} }, func() Dialect { return &oracle{} }}, + "mssql": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, + "odbc": {"mssql", func() Driver { return &odbcDriver{} }, func() Dialect { return &mssql{} }}, // !nashtsai! TODO change this when supporting MS Access + "mysql": {"mysql", func() Driver { return &mysqlDriver{} }, func() Dialect { return &mysql{} }}, + "mymysql": {"mysql", func() Driver { return &mymysqlDriver{} }, func() Dialect { return &mysql{} }}, + "postgres": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &postgres{} }}, + "opengauss": {"postgres", func() Driver { return &pqDriver{} }, func() Dialect { return &openGauss{} }}, + "pgx": {"postgres", func() Driver { return &pqDriverPgx{} }, func() Dialect { return &postgres{} }}, + "sqlite3": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }}, + "sqlite": {"sqlite3", func() Driver { return &sqlite3Driver{} }, func() Dialect { return &sqlite3{} }}, + "oci8": {"oracle", func() Driver { return &oci8Driver{} }, func() Dialect { return &oracle{} }}, + "godror": {"oracle", func() Driver { return &godrorDriver{} }, func() Dialect { return &oracle{} }}, + "oracle": {"oracle", func() Driver { return &oracleDriver{} }, func() Dialect { return &oracle{} }}, } for driverName, v := range providedDrvsNDialects { if driver := QueryDriver(driverName); driver == nil { RegisterDriver(driverName, v.getDriver()) - RegisterDialect(v.dbType, v.getDialect) + if driverName == "opengauss" { + RegisterDialect("opengauss", v.getDialect) + } else { + RegisterDialect(v.dbType, v.getDialect) + } } } return true diff --git a/dialects/driver.go b/dialects/driver.go index c63dbfa3..ec4adee1 100644 --- a/dialects/driver.go +++ b/dialects/driver.go @@ -68,7 +68,12 @@ func OpenDialect(driverName, connstr string) (Dialect, error) { return nil, err } - dialect := QueryDialect(uri.DBType) + var dialect Dialect + if driverName == "opengauss" { + dialect = QueryDialect("opengauss") + } else { + dialect = QueryDialect(uri.DBType) + } if dialect == nil { return nil, fmt.Errorf("unsupported dialect type: %v", uri.DBType) } diff --git a/dialects/opengauss.go b/dialects/opengauss.go new file mode 100644 index 00000000..a23e0368 --- /dev/null +++ b/dialects/opengauss.go @@ -0,0 +1,823 @@ +// Copyright 2015 The Xorm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dialects + +import ( + "context" + "errors" + "strings" + + "xorm.io/xorm/core" + "xorm.io/xorm/schemas" +) + +// from https://docs-opengauss.osinfra.cn/en/docs/latest/docs/SQLReference/keywords.html +var ( + openGaussReservedWords = map[string]bool{ + "ABORT": true, + "ABS": true, + "ABSOLUTE": true, + "ACCESS": true, + "ACCOUNT": true, + "ACTION": true, + "ADA": true, + "ADD": true, + "ADMIN": true, + "AFTER": true, + "AGGREGATE": true, + "ALGORITHM": true, + "ALIAS": true, + "ALL": true, + "ALLOCATE": true, + "ALSO": true, + "ALTER": true, + "ALWAYS": true, + "ANALYSE": true, + "ANALYZE": true, + "AND": true, + "ANY": true, + "APP": true, + "ARE": true, + "ARRAY": true, + "AS": true, + "ASC": true, + "ASENSITIVE": true, + "ASSERTION": true, + "ASSIGNMENT": true, + "ASYMMETRIC": true, + "AT": true, + "ATOMIC": true, + "ATTRIBUTE": true, + "AUTHID": true, + "AUTHORIZATION": true, + "AUTOEXTEND": true, + "AUTOMAPPED": true, + "AVG": true, + "BACKWARD": true, + "BARRIER": true, + "BEFORE": true, + "BEGIN": true, + "BEGIN_NON_ANOYBLOCK": true, + "BETWEEN": true, + "BIGINT": true, + "BINARY": true, + "BINARY_DOUBLE": true, + "BINARY_INTEGER": true, + "BIT": true, + "BITVAR": true, + "BIT_LENGTH": true, + "BLOB": true, + "BOOLEAN": true, + "BOTH": true, + "BUCKETS": true, + "BREADTH": true, + "BY": true, + "BYTEAWITHOUTODER": true, + "BYTEAWITHOUTORDERWITHEQUAL": true, + "C": true, + "CACHE": true, + "CALL": true, + "CALLED": true, + "CARDINALITY": true, + "CASCADE": true, + "CASCADED": true, + "CASE": true, + "CAST": true, + "CATALOG": true, + "CATALOG_NAME": true, + "CHAIN": true, + "CHAR": true, + "CHARACTER": true, + "CHARACTERISTICS": true, + "CHARACTER_LENGTH": true, + "CHARACTER_SET_CATALOG": true, + "CHARACTER_SET_NAME": true, + "CHARACTER_SET_SCHEMA": true, + "CHAR_LENGTH": true, + "CHECK": true, + "CHECKED": true, + "CHECKPOINT": true, + "CLASS": true, + "CLEAN": true, + "CLASS_ORIGIN": true, + "CLIENT": true, + "CLIENT_MASTER_KEY": true, + "CLIENT_MASTER_KEYS": true, + "CLOB": true, + "CLOSE": true, + "CLUSTER": true, + "COALESCE": true, + "COBOL": true, + "COLLATE": true, + "COLLATION": true, + "COLLATION_CATALOG": true, + "COLLATION_NAME": true, + "COLLATION_SCHEMA": true, + "COLUMN": true, + "COLUMN_ENCRYPTION_KEY": true, + "COLUMN_ENCRYPTION_KEYS": true, + "COLUMN_NAME": true, + "COMPACT": true, + "COMPATIBLE_ILLEGAL_CHARS": true, + "COMMAND_FUNCTION": true, + "COMPLETE": true, + "COMMAND_FUNCTION_CODE": true, + "COMMENT": true, + "COMMENTS": true, + "COMMIT": true, + "COMMITTED": true, + "COMPRESS": true, + "COMPLETION": true, + "CONCURRENTLY": true, + "CONDITION": true, + "CONDITION_NUMBER": true, + "CONFIGURATION": true, + "CONNECT": true, + "CONNECTION": true, + "CONNECTION_NAME": true, + "CONSTRAINT": true, + "CONSTRAINTS": true, + "CONSTRAINT_CATALOG": true, + "CONSTRAINT_NAME": true, + "CONSTRAINT_SCHEMA": true, + "CONSTRUCTOR": true, + "CONTAINS": true, + "CONTENT": true, + "CONTINUE": true, + "CONVERSION": true, + "CONVERT": true, + "COPY": true, + "CORRESPONDING": true, + "COST": true, + "COUNT": true, + "CREATE": true, + "CROSS": true, + "CSV": true, + "CUBE": true, + "CURRENT": true, + "CURRENT_CATALOG": true, + "CURRENT_DATE": true, + "CURRENT_PATH": true, + "CURRENT_ROLE": true, + "CURRENT_SCHEMA": true, + "CURRENT_TIME": true, + "CURRENT_TIMESTAMP": true, + "CURRENT_USER": true, + "CURSOR": true, + "CURSOR_NAME": true, + "CYCLE": true, + "DATA": true, + "DATABASE": true, + "DATAFILE": true, + "DATE_FORMAT": true, + "DATATYPE_CL": true, + "DATE": true, + "DELTAMERGE": true, + "DATETIME_INTERVAL_CODE": true, + "DATETIME_INTERVAL_PRECISION": true, + "DAY": true, + "DBCOMPATIBILITY": true, + "DEALLOCATE": true, + "DEC": true, + "DECIMAL": true, + "DECLARE": true, + "DECODE": true, + "DEFAULT": true, + "DEFAULTS": true, + "DEFERRABLE": true, + "DEFERRED": true, + "DEFINED": true, + "DEFINER": true, + "DELETE": true, + "DELIMITER": true, + "DELIMITERS": true, + "DELTA": true, + "DEPTH": true, + "DEREF": true, + "DESC": true, + "DESCRIBE": true, + "DESCRIPTOR": true, + "DESTROY": true, + "DESTRUCTOR": true, + "DETERMINISTIC": true, + "DIAGNOSTICS": true, + "DICTIONARY": true, + "DIRECT": true, + "DIRECTORY": true, + "DISABLE": true, + "DISCARD": true, + "DISCONNECT": true, + "DISPATCH": true, + "DISTINCT": true, + "DISTRIBUTE": true, + "DISTRIBUTION": true, + "DO": true, + "DOCUMENT": true, + "DOMAIN": true, + "DOUBLE": true, + "DROP": true, + "DUPLICATE": true, + "DYNAMIC": true, + "DYNAMIC_FUNCTION": true, + "DYNAMIC_FUNCTION_CODE": true, + "EACH": true, + "ELSE": true, + "ELASTIC": true, + "ENABLE": true, + "ENCODING": true, + "ENCRYPTED": true, + "ENCRYPTED_VALUE": true, + "ENCRYPTION": true, + "ENCRYPTION_TYPE": true, + "END": true, + "END-EXEC": true, + "ENFORCED": true, + "ENUM": true, + "EOL": true, + "ERRORS": true, + "EQUALS": true, + "ESCAPE": true, + "ESCAPING": true, + "EVERY": true, + "EXCEPT": true, + "EXCEPTION": true, + "EXCHANGE": true, + "EXCLUDE": true, + "EXCLUDED": true, + "EXCLUDING": true, + "EXCLUSIVE": true, + "EXEC": true, + "EXECUTE": true, + "EXISTING": true, + "EXISTS": true, + "EXPIRED_P": true, + "EXPLAIN": true, + "EXTENSION": true, + "EXTERNAL": true, + "EXTRACT": true, + "FALSE": true, + "FAMILY": true, + "FAST": true, + "FETCH": true, + "FENCED": true, + "FILEHEADER": true, + "FINAL": true, + "FIRST": true, + "FIXED": true, + "FILL_MISSING_FIELDS": true, + "FLOAT": true, + "FOLLOWING": true, + "FOR": true, + "FORCE": true, + "FOREIGN": true, + "FORMATTER": true, + "FORTRAN": true, + "FORWARD": true, + "FOUND": true, + "FREE": true, + "FREEZE": true, + "FROM": true, + "FULL": true, + "FUNCTION": true, + "FUNCTIONS": true, + "G": true, + "GENERAL": true, + "GENERATED": true, + "GET": true, + "GLOBAL": true, + "GO": true, + "GOTO": true, + "GRANT": true, + "GRANTED": true, + "GREATEST": true, + "GROUP": true, + "GROUPING": true, + "HANDLER": true, + "HAVING": true, + "HDFSDIRECTORY": true, + "HEADER": true, + "HIERARCHY": true, + "HOLD": true, + "HOST": true, + "HOUR": true, + "IDENTIFIED": true, + "IDENTITY": true, + "IF": true, + "IGNORE": true, + "IGNORE_EXTRA_DATA": true, + "ILIKE": true, + "IMMEDIATE": true, + "IMMUTABLE": true, + "IMPLEMENTATION": true, + "IMPLICIT": true, + "IN": true, + "INTERNAL": true, + "INCLUDING": true, + "INCREMENT": true, + "INDEX": true, + "INDEXES": true, + "INDICATOR": true, + "INFIX": true, + "INHERIT": true, + "INHERITS": true, + "INITIAL": true, + "INITIALIZE": true, + "INITIALLY": true, + "INITRANS": true, + "INLINE": true, + "INNER": true, + "INOUT": true, + "INPUT": true, + "INSENSITIVE": true, + "INSERT": true, + "INSTANCE": true, + "INSTANTIABLE": true, + "INSTEAD": true, + "INT": true, + "INTEGER": true, + "INTERSECT": true, + "INTERVAL": true, + "INTO": true, + "INVOKER": true, + "IS": true, + "ISNULL": true, + "ISOLATION": true, + "ITERATE": true, + "JOIN": true, + "K": true, + "KEY": true, + "KEY_PATH": true, + "KEY_MEMBER": true, + "KEY_STORE": true, + "KEY_TYPE": true, + "KILL": true, + "LABEL": true, + "LANGUAGE": true, + "LARGE": true, + "LAST": true, + "LATERAL": true, + "LC_COLLATE": true, + "LC_CTYPE": true, + "LEADING": true, + "LEAKPROOF": true, + "LEAST": true, + "LEFT": true, + "LENGTH": true, + "LESS": true, + "LEVEL": true, + "LIKE": true, + "LIMIT": true, + "LISTEN": true, + "LOAD": true, + "LOCAL": true, + "LOCALTIME": true, + "LOCALTIMESTAMP": true, + "LOCATION": true, + "LOCATOR": true, + "LOCK": true, + "LOG": true, + "LOGGING": true, + "LOOP": true, + "LOWER": true, + "MAP": true, + "MAPPING": true, + "MASTER": true, + "MATCH": true, + "MATCHED": true, + "MAX": true, + "MAXEXTENTS": true, + "MAXSIZE": true, + "MAXTRANS": true, + "MAXVALUE": true, + "MERGE": true, + "MESSAGE_LENGTH": true, + "MESSAGE_OCTET_LENGTH": true, + "MESSAGE_TEXT": true, + "METHOD": true, + "MIN": true, + "MINEXTENTS": true, + "MINUS": true, + "MINUTE": true, + "MINVALUE": true, + "MOD": true, + "MODE": true, + "MODIFIES": true, + "MODIFY": true, + "MODULE": true, + "MONTH": true, + "MORE": true, + "MOVE": true, + "MOVEMENT": true, + "MUMPS": true, + "NAME": true, + "NAMES": true, + "NATIONAL": true, + "NATURAL": true, + "NCHAR": true, + "NCLOB": true, + "NEW": true, + "NEXT": true, + "NO": true, + "NOCOMPRESS": true, + "NOCYCLE": true, + "NODE": true, + "NOLOGGING": true, + "NOMAXVALUE": true, + "NOMINVALUE": true, + "NONE": true, + "NOT": true, + "NOTHING": true, + "NOTIFY": true, + "NOTNULL": true, + "NOWAIT": true, + "NULL": true, + "NULLABLE": true, + "NULLIF": true, + "NULLS": true, + "NUMBER": true, + "NUMERIC": true, + "NUMSTR": true, + "NVARCHAR2": true, + "NVL": true, + "OBJECT": true, + "OCTET_LENGTH": true, + "OF": true, + "OFF": true, + "OFFSET": true, + "OIDS": true, + "OLD": true, + "ON": true, + "ONLY": true, + "OPEN": true, + "OPERATION": true, + "OPERATOR": true, + "OPTIMIZATION": true, + "OPTION": true, + "OPTIONS": true, + "OR": true, + "ORDER": true, + "ORDINALITY": true, + "OUT": true, + "OUTER": true, + "OUTPUT": true, + "OVER": true, + "OVERLAPS": true, + "OVERLAY": true, + "OVERRIDING": true, + "OWNED": true, + "OWNER": true, + "PACKAGE": true, + "PAD": true, + "PARAMETER": true, + "PARAMETERS": true, + "PARAMETER_MODE": true, + "PARAMETER_NAME": true, + "PARAMETER_ORDINAL_POSITION": true, + "PARAMETER_SPECIFIC_CATALOG": true, + "PARAMETER_SPECIFIC_NAME": true, + "PARAMETER_SPECIFIC_SCHEMA": true, + "PARSER": true, + "PARTIAL": true, + "PARTITION": true, + "PARTITIONS": true, + "PASCAL": true, + "PASSING": true, + "PASSWORD": true, + "PATH": true, + "PCTFREE": true, + "PER": true, + "PERM": true, + "PERCENT": true, + "PERFORMANCE": true, + "PLACING": true, + "PLAN": true, + "PLANS": true, + "PLI": true, + "POOL": true, + "POLICY": true, + "POSITION": true, + "POSTFIX": true, + "PRECEDING": true, + "PRECISION": true, + "PREFERRED": true, + "PREFIX": true, + "PREORDER": true, + "PREPARE": true, + "PREPARED": true, + "PRESERVE": true, + "PRIMARY": true, + "PRIOR": true, + "PRIVATE": true, + "PRIVILEGE": true, + "PRIVILEGES": true, + "PROCEDURAL": true, + "PROCEDURE": true, + "PROFILE": true, + "PUBLIC": true, + "QUERY": true, + "QUOTE": true, + "RANDOMIZED": true, + "RANGE": true, + "RAW": true, + "READ": true, + "READS": true, + "REAL": true, + "REASSIGN": true, + "REBUILD": true, + "RECHECK": true, + "RECURSIVE": true, + "REF": true, + "REFERENCES": true, + "REFERENCING": true, + "REINDEX": true, + "REJECT": true, + "RELATIVE": true, + "RELEASE": true, + "RELOPTIONS": true, + "REMOTE": true, + "RENAME": true, + "REPEATABLE": true, + "REPLACE": true, + "REPLICA": true, + "RESET": true, + "RESIZE": true, + "RESOURCE": true, + "RESTART": true, + "RESTRICT": true, + "RESULT": true, + "RETURN": true, + "RETURNED_LENGTH": true, + "RETURNED_OCTET_LENGTH": true, + "RETURNED_SQLSTATE": true, + "RETURNING": true, + "RETURNS": true, + "REUSE": true, + "REVOKE": true, + "RIGHT": true, + "ROLE": true, + "ROLLBACK": true, + "ROLLUP": true, + "ROUTINE": true, + "ROUTINE_CATALOG": true, + "ROUTINE_NAME": true, + "ROUTINE_SCHEMA": true, + "ROW": true, + "ROWS": true, + "ROW_COUNT": true, + "RULE": true, + "ROWNUM": true, + "SAVEPOINT": true, + "SCALE": true, + "SCHEMA": true, + "SCHEMA_NAME": true, + "SCOPE": true, + "SCROLL": true, + "SEARCH": true, + "SECOND": true, + "SECTION": true, + "SECURITY": true, + "SELECT": true, + "SELF": true, + "SENSITIVE": true, + "SEQUENCE": true, + "SEQUENCES": true, + "SERIALIZABLE": true, + "SERVER": true, + "SERVER_NAME": true, + "SESSION": true, + "SESSION_USER": true, + "SET": true, + "SETOF": true, + "SETS": true, + "SHARE": true, + "SHIPPABLE": true, + "SHOW": true, + "SHUTDOWN": true, + "SIMILAR": true, + "SIMPLE": true, + "SIZE": true, + "SMALLDATETIME_FORMAT": true, + "SMALLDATETIME": true, + "SMALLINT": true, + "SNAPSHOT": true, + "SOME": true, + "SOURCE": true, + "SPACE": true, + "SPECIFIC": true, + "SPECIFICTYPE": true, + "SPECIFIC_NAME": true, + "SPILL": true, + "SPLIT": true, + "SQL": true, + "SQLCODE": true, + "SQLERROR": true, + "SQLEXCEPTION": true, + "SQLSTATE": true, + "SQLWARNING": true, + "STABLE": true, + "STANDALONE": true, + "START": true, + "STATE": true, + "STATEMENT": true, + "STATEMENT_ID": true, + "STATIC": true, + "STATISTICS": true, + "STDIN": true, + "STDOUT": true, + "STORAGE": true, + "STORE": true, + "STRICT": true, + "STRIP": true, + "STRUCTURE": true, + "STYLE": true, + "SUBCLASS_ORIGIN": true, + "SUBLIST": true, + "SUBSTRING": true, + "SUM": true, + "SYMMETRIC": true, + "SYNONYM": true, + "SYS_REFCURSOR": true, + "SYSDATE": true, + "SYSID": true, + "SYSTEM": true, + "SYSTEM_USER": true, + "TABLE": true, + "TABLES": true, + "TABLE_NAME": true, + "TIME_FORMAT": true, + "TIMESTAMP_FORMAT": true, + "TEMP": true, + "TEMPLATE": true, + "TEMPORARY": true, + "TERMINATE": true, + "TEXT": true, + "THAN": true, + "THEN": true, + "TIME": true, + "TIMESTAMP": true, + "TIMESTAMPDIFF": true, + "TIMEZONE_HOUR": true, + "TIMEZONE_MINUTE": true, + "TINYINT": true, + "TO": true, + "TRAILING": true, + "TRANSACTION": true, + "TRANSACTIONS_COMMITTED": true, + "TRANSACTIONS_ROLLED_BACK": true, + "TRANSACTION_ACTIVE": true, + "TRANSFORM": true, + "TRANSFORMS": true, + "TRANSLATE": true, + "TRANSLATION": true, + "TREAT": true, + "TRIGGER": true, + "TRIGGER_CATALOG": true, + "TRIGGER_NAME": true, + "TRIGGER_SCHEMA": true, + "TRIM": true, + "TRUE": true, + "TRUNCATE": true, + "TRUSTED": true, + "TSFIELD": true, + "TSTAG": true, + "TSTIME": true, + "TYPE": true, + "TYPES": true, + "UESCAPE": true, + "UNBOUNDED": true, + "UNCOMMITTED": true, + "UNDER": true, + "UNENCRYPTED": true, + "UNION": true, + "UNIQUE": true, + "UNKNOWN": true, + "UNLIMITED": true, + "UNLISTEN": true, + "UNLOCK": true, + "UNLOGGED": true, + "UNNAMED": true, + "UNNEST": true, + "UNTIL": true, + "UNUSABLE": true, + "UPDATE": true, + "UPPER": true, + "USAGE": true, + "USER": true, + "USER_DEFINED_TYPE_CATALOG": true, + "USER_DEFINED_TYPE_NAME": true, + "USER_DEFINED_TYPE_SCHEMA": true, + "USING": true, + "VACUUM": true, + "VALID": true, + "VALIDATE": true, + "VALIDATION": true, + "VALIDATOR": true, + "VALUE": true, + "VALUES": true, + "VARCHAR": true, + "VARCHAR2": true, + "VARIABLE": true, + "VARIADIC": true, + "VARYING": true, + "VCGROUP": true, + "VERBOSE": true, + "VERSION": true, + "VERIFY": true, + "VIEW": true, + "VOLATILE": true, + "WHEN": true, + "WHENEVER": true, + "WHERE": true, + "WHITESPACE": true, + "WINDOW": true, + "WITH": true, + "WITHIN": true, + "WITHOUT": true, + "WORK": true, + "WORKLOAD": true, + "WRAPPER": true, + "WRITE": true, + "XML": true, + "XMLATTRIBUTES": true, + "XMLCONCAT": true, + "XMLELEMENT": true, + "XMLEXISTS": true, + "XMLFOREST": true, + "XMLPARSE": true, + "XMLPI": true, + "XMLROOT": true, + "XMLSERIALIZE": true, + "YEAR": true, + "YES": true, + "ZONE": true, + } +) + +type openGauss struct { + postgres +} + +// Version returns version of OpenGauss +// version like: xxx ... (GaussDB Kernel VXXXRXXXCXX build XXX) xxx ... +// version also like: xxx ... gaussdb (GaussDB Kernel 505.2.0 build e3976aad) xxx ... +func (db *openGauss) Version(ctx context.Context, queryer core.Queryer) (*schemas.Version, error) { + rows, err := queryer.QueryContext(ctx, "SELECT version()") + if err != nil { + return nil, err + } + defer rows.Close() + + if !rows.Next() { + if rows.Err() != nil { + return nil, rows.Err() + } + return nil, errors.New("unknown version") + } + + var version string + if err := rows.Scan(&version); err != nil { + return nil, err + } + + start := -1 + end := -1 + for i, c := range version { + if start == -1 && (string(c) == "(" || string(c) == "(") { + start = i + } + if string(c) == ")" || string(c) == ")" { + end = i + break + } + } + + if start == -1 || end < start { + return nil, errors.New("unknown version") + } + + words := strings.Split(version[start+1:end], " ") + + wordCnt := 0 + word := "" + for _, word = range words { + if word != "" { + wordCnt++ + if wordCnt == 3 { + break + } + } + } + + return &schemas.Version{ + Number: word, + Level: "", + Edition: "GaussDB", + }, nil +} + +func (db *openGauss) IsReserved(name string) bool { + _, ok := openGaussReservedWords[strings.ToUpper(name)] + return ok +}