Browse Source

Added feature for SELECT ... FOR UPDATE

xormplus 10 years ago
parent
commit
0da16ae0de
1 changed files with 29 additions and 18 deletions
  1. 29 18
      dialect.go

+ 29 - 18
dialect.go

@@ -63,6 +63,8 @@ type Dialect interface {
 
 	ModifyColumnSql(tableName string, col *Column) string
 
+	ForUpdateSql(query string) string
+
 	//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
 	//MustDropTable(tableName string) error
 
@@ -229,28 +231,33 @@ func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset stri
 		tableName = table.Name
 	}
 
-	sql += b.dialect.Quote(tableName) + " ("
-
-	pkList := table.PrimaryKeys
+	sql += b.dialect.Quote(tableName)
+	sql += " ("
+
+	if len(table.ColumnsSeq()) > 0 {
+		pkList := table.PrimaryKeys
+
+		for _, colName := range table.ColumnsSeq() {
+			col := table.GetColumn(colName)
+			if col.IsPrimaryKey && len(pkList) == 1 {
+				sql += col.String(b.dialect)
+			} else {
+				sql += col.StringNoPk(b.dialect)
+			}
+			sql = strings.TrimSpace(sql)
+			sql += ", "
+		}
 
-	for _, colName := range table.ColumnsSeq() {
-		col := table.GetColumn(colName)
-		if col.IsPrimaryKey && len(pkList) == 1 {
-			sql += col.String(b.dialect)
-		} else {
-			sql += col.StringNoPk(b.dialect)
+		if len(pkList) > 1 {
+			sql += "PRIMARY KEY ( "
+			sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
+			sql += " ), "
 		}
-		sql = strings.TrimSpace(sql)
-		sql += ", "
-	}
 
-	if len(pkList) > 1 {
-		sql += "PRIMARY KEY ( "
-		sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
-		sql += " ), "
+		sql = sql[:len(sql)-2]
 	}
+	sql += ")"
 
-	sql = sql[:len(sql)-2] + ")"
 	if b.dialect.SupportEngine() && storeEngine != "" {
 		sql += " ENGINE=" + storeEngine
 	}
@@ -262,10 +269,14 @@ func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset stri
 			sql += " DEFAULT CHARSET " + charset
 		}
 	}
-	sql += ";"
+
 	return sql
 }
 
+func (b *Base) ForUpdateSql(query string) string {
+	return query + " FOR UPDATE"
+}
+
 var (
 	dialects = map[DbType]func() Dialect{}
 )