github.com/team-ide/go-dialect@v1.9.20/dialect/back/dialect_postgresql.go (about)

     1  package back
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func NewPostgresqlDialect() *PostgresqlDialect {
     8  
     9  	res := &PostgresqlDialect{
    10  		DefaultDialect: NewDefaultDialect(PostgresqlType),
    11  	}
    12  	res.init()
    13  	return res
    14  }
    15  
    16  type PostgresqlDialect struct {
    17  	*DefaultDialect
    18  }
    19  
    20  func (this_ *PostgresqlDialect) init() {
    21  	/** 数值类型 **/
    22  
    23  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIT", TypeFormat: "NUMBER($l, $d)", HasLength: false, IsNumber: true})
    24  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    25  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SMALLINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    26  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    27  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    28  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INTEGER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    29  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIGINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    30  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMBER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    31  
    32  	/** 小数 **/
    33  
    34  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "FLOAT", TypeFormat: "NUMBER($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true})
    35  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DOUBLE", TypeFormat: "NUMBER($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true})
    36  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DEC", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    37  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DECIMAL", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    38  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMBER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    39  
    40  	/** 日期/时间类型 **/
    41  
    42  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "YEAR", TypeFormat: "DATE", IsDateTime: true})
    43  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIME", TypeFormat: "DATE", IsDateTime: true})
    44  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATE", TypeFormat: "DATE", IsDateTime: true})
    45  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATETIME", TypeFormat: "DATE", IsDateTime: true})
    46  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIMESTAMP", TypeFormat: "TIMESTAMP", IsDateTime: true})
    47  
    48  	/** 字符串类型 **/
    49  
    50  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "CHAR", TypeFormat: "CHAR($l)", HasLength: true, IsString: true})
    51  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "VARCHAR", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    52  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "VARCHAR2", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    53  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYTEXT", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    54  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TEXT", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    55  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMTEXT", TypeFormat: "CLOB", HasLength: true, IsString: true})
    56  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGTEXT", TypeFormat: "CLOB", HasLength: true, IsString: true})
    57  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "CLOB", TypeFormat: "CLOB", HasLength: true, IsString: true})
    58  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "ENUM", TypeFormat: "CHAR($l)", HasLength: true, IsString: true})
    59  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    60  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    61  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    62  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    63  
    64  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SET", TypeFormat: "SET($l)", HasLength: true, IsString: true})
    65  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "XMLTYPE", TypeFormat: "XMLTYPE($l)", HasLength: true, IsString: true})
    66  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "RAW", TypeFormat: "RAW($l)", HasLength: true, IsString: true})
    67  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NVARCHAR2", TypeFormat: "NVARCHAR2($l)", HasLength: true, IsString: true})
    68  
    69  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMERIC", TypeFormat: "NUMERIC($l)", HasLength: true, IsString: true})
    70  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "OID", TypeFormat: "OID($l)", HasLength: true, IsString: true})
    71  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NAME", TypeFormat: "NAME($l)", HasLength: true, IsString: true})
    72  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BOOL", TypeFormat: "BOOL($l)", HasLength: true, IsString: true})
    73  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT1", TypeFormat: "INT1($l)", HasLength: true, IsString: true})
    74  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT2", TypeFormat: "INT2($l)", HasLength: true, IsString: true})
    75  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT4", TypeFormat: "INT4($l)", HasLength: true, IsString: true})
    76  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT8", TypeFormat: "INT8($l)", HasLength: true, IsString: true})
    77  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SYS_LSN", TypeFormat: "SYS_LSN($l)", HasLength: true, IsString: true})
    78  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "REGCLASS", TypeFormat: "REGCLASS($l)", HasLength: true, IsString: true})
    79  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIMESTAMPTZ", TypeFormat: "TIMESTAMPTZ($l)", HasLength: true, IsString: true})
    80  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_TEXT", TypeFormat: "_TEXT", HasLength: true, IsString: true})
    81  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "JSON", TypeFormat: "JSON", HasLength: true, IsString: true})
    82  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SYS_NODE_TREE", TypeFormat: "SYS_NODE_TREE", HasLength: true, IsString: true})
    83  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "character_data", TypeFormat: "character_data", HasLength: true, IsString: true})
    84  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "yes_or_no", TypeFormat: "yes_or_no", HasLength: true, IsString: true})
    85  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "cardinal_number", TypeFormat: "cardinal_number", HasLength: true, IsString: true})
    86  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INTERVAL", TypeFormat: "INTERVAL($l)", HasLength: true, IsString: true})
    87  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "REGPROC", TypeFormat: "REGPROC($l)", HasLength: true, IsString: true})
    88  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_ACLITEM", TypeFormat: "_ACLITEM", HasLength: true, IsString: true})
    89  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "FLOAT4", TypeFormat: "FLOAT4($l)", HasLength: true, IsString: true})
    90  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "FLOAT8", TypeFormat: "FLOAT8($l)", HasLength: true, IsString: true})
    91  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "XID", TypeFormat: "XID($l)", HasLength: true, IsString: true})
    92  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TDEKEY", TypeFormat: "TDEKEY($l)", HasLength: true, IsString: true})
    93  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_INT2", TypeFormat: "_INT2", HasLength: true, IsString: true})
    94  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_INT4", TypeFormat: "_INT4", HasLength: true, IsString: true})
    95  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_OID", TypeFormat: "_OID", HasLength: true, IsString: true})
    96  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT2VECTOR", TypeFormat: "INT2VECTOR", HasLength: true, IsString: true})
    97  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "OIDVECTOR", TypeFormat: "OIDVECTOR", HasLength: true, IsString: true})
    98  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BYTEA", TypeFormat: "BYTEA", HasLength: true, IsString: true})
    99  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_CHAR", TypeFormat: "_CHAR", HasLength: true, IsString: true})
   100  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_FLOAT4", TypeFormat: "_FLOAT4", HasLength: true, IsString: true})
   101  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "_FLOAT8", TypeFormat: "_FLOAT8", HasLength: true, IsString: true})
   102  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "ANYARRAY", TypeFormat: "ANYARRAY", HasLength: true, IsString: true})
   103  
   104  	// Postgresql
   105  
   106  	this_.AddFuncTypeInfo(&FuncTypeInfo{Name: "md5", Format: "md5"})
   107  }
   108  
   109  func (this_ *PostgresqlDialect) OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error) {
   110  	if data == nil {
   111  		return
   112  	}
   113  	owner = &OwnerModel{}
   114  	if data["nspname"] != nil {
   115  		owner.Name = data["nspname"].(string)
   116  	}
   117  	return
   118  }
   119  func (this_ *PostgresqlDialect) OwnersSelectSql() (sql string, err error) {
   120  	sql = `select * from pg_catalog.pg_namespace ORDER BY nspname`
   121  	return
   122  }
   123  func (this_ *PostgresqlDialect) OwnerSelectSql(ownerName string) (sql string, err error) {
   124  	sql = `select * from pg_catalog.pg_namespace `
   125  	sql += `WHERE nspname ='` + ownerName + `' `
   126  	return
   127  }
   128  
   129  func (this_ *PostgresqlDialect) TableModel(data map[string]interface{}) (table *TableModel, err error) {
   130  	if data == nil {
   131  		return
   132  	}
   133  	table = &TableModel{}
   134  	if data["tablename"] != nil {
   135  		table.Name = data["tablename"].(string)
   136  	}
   137  	return
   138  }
   139  func (this_ *PostgresqlDialect) TablesSelectSql(ownerName string) (sql string, err error) {
   140  	sql = `SELECT * FROM pg_catalog.pg_tables   `
   141  	if ownerName != "" {
   142  		sql += `WHERE schemaname ='` + ownerName + `' `
   143  	}
   144  	sql += `ORDER BY tablename`
   145  	return
   146  }
   147  func (this_ *PostgresqlDialect) TableSelectSql(ownerName string, tableName string) (sql string, err error) {
   148  	sql = `SELECT * FROM pg_catalog.pg_tables `
   149  	sql += `WHERE 1=1 `
   150  	if ownerName != "" {
   151  		sql += `AND schemaname='` + ownerName + `' `
   152  	}
   153  	sql += `AND tablename='` + tableName + `' `
   154  	sql += `ORDER BY tablename`
   155  	return
   156  }
   157  
   158  func (this_ *PostgresqlDialect) ColumnModel(data map[string]interface{}) (column *ColumnModel, err error) {
   159  	if data == nil {
   160  		return
   161  	}
   162  	column = &ColumnModel{}
   163  	if data["COLUMN_NAME"] != nil {
   164  		column.Name = data["COLUMN_NAME"].(string)
   165  	}
   166  	if data["COMMENTS"] != nil {
   167  		column.Comment = data["COMMENTS"].(string)
   168  	}
   169  	if data["DATA_DEFAULT"] != nil {
   170  		column.Default = GetStringValue(data["DATA_DEFAULT"])
   171  	}
   172  	if data["TABLE_NAME"] != nil {
   173  		column.TableName = data["TABLE_NAME"].(string)
   174  	}
   175  	if data["CHARACTER_SET_NAME"] != nil {
   176  		column.CharacterSetName = data["CHARACTER_SET_NAME"].(string)
   177  	}
   178  
   179  	if GetStringValue(data["NULLABLE"]) == "N" {
   180  		column.NotNull = true
   181  	}
   182  	var columnTypeInfo *ColumnTypeInfo
   183  	if data["DATA_TYPE"] != nil {
   184  		dataType := data["DATA_TYPE"].(string)
   185  		if strings.Contains(dataType, "(") {
   186  			dataType = dataType[:strings.Index(dataType, "(")]
   187  		}
   188  		columnTypeInfo, err = this_.GetColumnTypeInfo(dataType)
   189  		if err != nil {
   190  			return
   191  		}
   192  		column.Type = columnTypeInfo.Name
   193  
   194  		//bs, _ := json.Marshal(data)
   195  		//println("data:", string(bs))
   196  		dataLength := GetStringValue(data["DATA_LENGTH"])
   197  		if dataLength != "" && dataLength != "0" {
   198  			column.Length, err = StringToInt(dataLength)
   199  			if err != nil {
   200  				return
   201  			}
   202  		}
   203  		dataPrecision := GetStringValue(data["DATA_PRECISION"])
   204  		if dataPrecision != "" && dataPrecision != "0" {
   205  			column.Length, err = StringToInt(dataPrecision)
   206  			if err != nil {
   207  				return
   208  			}
   209  		}
   210  		dataScale := GetStringValue(data["DATA_SCALE"])
   211  		if dataScale != "" && dataScale != "0" {
   212  			column.Decimal, err = StringToInt(dataScale)
   213  			if err != nil {
   214  				return
   215  			}
   216  		}
   217  	}
   218  	return
   219  }
   220  func (this_ *PostgresqlDialect) ColumnsSelectSql(ownerName string, tableName string) (sql string, err error) {
   221  	sql = `SELECT t.*,tc.COMMENTS from all_tab_columns t `
   222  	sql += "LEFT JOIN all_col_comments tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME)"
   223  	sql += `WHERE 1=1 `
   224  	if ownerName != "" {
   225  		sql += `AND t.OWNER='` + ownerName + `' `
   226  	}
   227  	sql += `AND t.TABLE_NAME='` + tableName + `' `
   228  	return
   229  }
   230  func (this_ *PostgresqlDialect) ColumnUpdateSql(ownerName string, tableName string, oldColumn *ColumnModel, newColumn *ColumnModel) (sqlList []string, err error) {
   231  
   232  	return
   233  }
   234  
   235  func (this_ *PostgresqlDialect) PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error) {
   236  	if data == nil {
   237  		return
   238  	}
   239  	primaryKey = &PrimaryKeyModel{}
   240  	if data["COLUMN_NAME"] != nil {
   241  		primaryKey.ColumnName = data["COLUMN_NAME"].(string)
   242  	}
   243  	if data["TABLE_NAME"] != nil {
   244  		primaryKey.TableName = data["TABLE_NAME"].(string)
   245  	}
   246  	return
   247  }
   248  func (this_ *PostgresqlDialect) PrimaryKeysSelectSql(ownerName string, tableName string) (sql string, err error) {
   249  	sql = `SELECT cu.* FROM all_cons_columns cu, all_constraints au `
   250  	sql += `WHERE cu.constraint_name = au.constraint_name and au.constraint_type = 'P' `
   251  	if ownerName != "" {
   252  		sql += `AND au.OWNER='` + ownerName + `' `
   253  	}
   254  	sql += `AND au.TABLE_NAME='` + tableName + `' `
   255  	return
   256  }
   257  
   258  func (this_ *PostgresqlDialect) IndexModel(data map[string]interface{}) (index *IndexModel, err error) {
   259  	if data == nil {
   260  		return
   261  	}
   262  	index = &IndexModel{}
   263  	if data["INDEX_NAME"] != nil {
   264  		index.Name = data["INDEX_NAME"].(string)
   265  	}
   266  	if data["COLUMN_NAME"] != nil {
   267  		index.ColumnName = data["COLUMN_NAME"].(string)
   268  	}
   269  	if data["INDEX_COMMENT"] != nil {
   270  		index.Comment = data["INDEX_COMMENT"].(string)
   271  	}
   272  	if GetStringValue(data["UNIQUENESS"]) == "UNIQUE" {
   273  		index.Type = "unique"
   274  	}
   275  	if data["TABLE_NAME"] != nil {
   276  		index.TableName = data["TABLE_NAME"].(string)
   277  	}
   278  	return
   279  }
   280  func (this_ *PostgresqlDialect) IndexesSelectSql(ownerName string, tableName string) (sql string, err error) {
   281  	sql = `SELECT t.*,i.index_type,i.UNIQUENESS FROM all_ind_columns t,all_indexes i  `
   282  	sql += `WHERE t.index_name = i.index_name `
   283  	if ownerName != "" {
   284  		sql += `AND t.TABLE_OWNER='` + ownerName + `' `
   285  	}
   286  	sql += `AND t.TABLE_NAME='` + tableName + `' `
   287  	sql += `AND t.COLUMN_NAME NOT IN( `
   288  	sql += `SELECT cu.COLUMN_NAME FROM all_cons_columns cu, all_constraints au `
   289  	sql += `WHERE cu.constraint_name = au.constraint_name and au.constraint_type = 'P' `
   290  	if ownerName != "" {
   291  		sql += `AND au.OWNER='` + ownerName + `' `
   292  	}
   293  	sql += `AND au.TABLE_NAME='` + tableName + `' `
   294  
   295  	sql += ") "
   296  	return
   297  }