| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | package migrator | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Postgres struct { | 
					
						
							|  |  |  | 	BaseDialect | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewPostgresDialect() *Postgres { | 
					
						
							|  |  |  | 	d := Postgres{} | 
					
						
							|  |  |  | 	d.BaseDialect.dialect = &d | 
					
						
							|  |  |  | 	d.BaseDialect.driverName = POSTGRES | 
					
						
							|  |  |  | 	return &d | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 21:44:37 +08:00
										 |  |  | func (db *Postgres) SupportEngine() bool { | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | func (db *Postgres) Quote(name string) string { | 
					
						
							|  |  |  | 	return "\"" + name + "\"" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) QuoteStr() string { | 
					
						
							|  |  |  | 	return "\"" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-11 02:25:01 +08:00
										 |  |  | func (b *Postgres) LikeStr() string { | 
					
						
							|  |  |  | 	return "ILIKE" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | func (db *Postgres) AutoIncrStr() string { | 
					
						
							|  |  |  | 	return "" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 14:07:14 +08:00
										 |  |  | func (db *Postgres) BooleanStr(value bool) string { | 
					
						
							|  |  |  | 	return strconv.FormatBool(value) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-14 17:19:53 +08:00
										 |  |  | func (b *Postgres) Default(col *Column) string { | 
					
						
							|  |  |  | 	if col.Type == DB_Bool { | 
					
						
							|  |  |  | 		if col.Default == "0" { | 
					
						
							|  |  |  | 			return "FALSE" | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return "TRUE" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return col.Default | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | func (db *Postgres) SqlType(c *Column) string { | 
					
						
							|  |  |  | 	var res string | 
					
						
							|  |  |  | 	switch t := c.Type; t { | 
					
						
							|  |  |  | 	case DB_TinyInt: | 
					
						
							|  |  |  | 		res = DB_SmallInt | 
					
						
							|  |  |  | 		return res | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_MediumInt, DB_Int, DB_Integer: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		if c.IsAutoIncrement { | 
					
						
							|  |  |  | 			return DB_Serial | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return DB_Integer | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Serial, DB_BigSerial: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		c.IsAutoIncrement = true | 
					
						
							|  |  |  | 		c.Nullable = false | 
					
						
							|  |  |  | 		res = t | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Binary, DB_VarBinary: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		return DB_Bytea | 
					
						
							|  |  |  | 	case DB_DateTime: | 
					
						
							|  |  |  | 		res = DB_TimeStamp | 
					
						
							|  |  |  | 	case DB_TimeStampz: | 
					
						
							|  |  |  | 		return "timestamp with time zone" | 
					
						
							|  |  |  | 	case DB_Float: | 
					
						
							|  |  |  | 		res = DB_Real | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_TinyText, DB_MediumText, DB_LongText: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		res = DB_Text | 
					
						
							|  |  |  | 	case DB_NVarchar: | 
					
						
							|  |  |  | 		res = DB_Varchar | 
					
						
							|  |  |  | 	case DB_Uuid: | 
					
						
							|  |  |  | 		res = DB_Uuid | 
					
						
							| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | 	case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob: | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 		return DB_Bytea | 
					
						
							|  |  |  | 	case DB_Double: | 
					
						
							|  |  |  | 		return "DOUBLE PRECISION" | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		if c.IsAutoIncrement { | 
					
						
							|  |  |  | 			return DB_Serial | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		res = t | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var hasLen1 bool = (c.Length > 0) | 
					
						
							|  |  |  | 	var hasLen2 bool = (c.Length2 > 0) | 
					
						
							|  |  |  | 	if hasLen2 { | 
					
						
							|  |  |  | 		res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | 
					
						
							|  |  |  | 	} else if hasLen1 { | 
					
						
							|  |  |  | 		res += "(" + strconv.Itoa(c.Length) + ")" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) { | 
					
						
							|  |  |  | 	args := []interface{}{"grafana", tableName} | 
					
						
							| 
									
										
										
										
											2017-10-09 02:37:16 +08:00
										 |  |  | 	sql := "SELECT table_name FROM information_schema.tables WHERE table_schema=? and table_name=?" | 
					
						
							| 
									
										
										
										
											2015-01-20 16:20:44 +08:00
										 |  |  | 	return sql, args | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) DropIndexSql(tableName string, index *Index) string { | 
					
						
							|  |  |  | 	quote := db.Quote | 
					
						
							|  |  |  | 	idxName := index.XName(tableName) | 
					
						
							|  |  |  | 	return fmt.Sprintf("DROP INDEX %v", quote(idxName)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string { | 
					
						
							|  |  |  | 	var statements = []string{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, col := range columns { | 
					
						
							|  |  |  | 		statements = append(statements, "ALTER "+db.QuoteStr()+col.Name+db.QuoteStr()+" TYPE "+db.SqlType(col)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";" | 
					
						
							|  |  |  | } |