本文整理匯總了Golang中github.com/coopernurse/gorp.DbMap.CreateTablesIfNotExists方法的典型用法代碼示例。如果您正苦於以下問題:Golang DbMap.CreateTablesIfNotExists方法的具體用法?Golang DbMap.CreateTablesIfNotExists怎麽用?Golang DbMap.CreateTablesIfNotExists使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coopernurse/gorp.DbMap
的用法示例。
在下文中一共展示了DbMap.CreateTablesIfNotExists方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewDbMap
func NewDbMap() *gorp.DbMap {
dsn, _ := globalCfg.ReadString("dsn", "root:[email protected]/tyrant")
dbType, _ := globalCfg.ReadString("db", "mysql")
if dbType != "mysql" && dbType != "sqlite3" {
log.Fatal("db must be mysql or sqlite3")
}
db, err := sql.Open(dbType, dsn)
if err != nil {
log.Fatal(err)
}
var dbmap *gorp.DbMap
if dbType == "mysql" {
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{}}
} else {
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
}
tbl := dbmap.AddTableWithName(Job{}, "jobs").SetKeys(true, "Id")
tbl.ColMap("name").SetMaxSize(512).SetUnique(true)
tbl.ColMap("executor").SetMaxSize(4096)
tbl.ColMap("executor_flags").SetMaxSize(4096)
tbl.ColMap("uris").SetMaxSize(2048)
tbl = dbmap.AddTableWithName(Task{}, "tasks").SetKeys(true, "Id")
tbl.ColMap("id").SetMaxSize(128).SetUnique(true)
err = dbmap.CreateTablesIfNotExists()
if err != nil {
log.Fatal(err)
}
return dbmap
}
示例2: Serve
// Serve starts the RPC server on listener. Serve blocks.
func Serve(listener net.Listener, dbmap *gorp.DbMap) error {
dbmap.AddTable(UniVar{}).SetKeys(false, "Name")
err := dbmap.CreateTablesIfNotExists()
if err != nil {
return err
}
server := &Elvishd{dbmap}
rpc.Register(server)
rpc.Accept(listener)
return nil
}
示例3: HostInit
// Called first when a host connects.
// NOTE that this may be called multiple times as a host may drop and reconnect.
func HostInit(playerId int, gameId string, gs GameService, ws *websocket.Conn, wsReadChan chan Message, db *gorp.DbMap) error {
log.Printf("Host initing")
// since host is always the first to connect, setup tables if they don't already exist
db.AddTableWithName(TicTacToe_Board{}, "tictactoe_board").SetKeys(true, "Id")
db.AddTableWithName(TicTacToe_Turn{}, "tictactoe_turn").SetKeys(true, "Id")
err := db.CreateTablesIfNotExists()
if err != nil {
log.Printf("Unable to create TTT tables: %#v", err)
return err
}
log.Printf("Tables created")
// get the game so we know what state we should be in
game, _, err := gs.GetGame(db, gameId, playerId)
if err != nil {
log.Printf("Host failed to get game: %#v", err)
return err
}
log.Printf("got game")
if game.State == "lobby" {
log.Printf("Game is still in lobby")
// update the lobby based on players that are currently connected
pids := gs.GetConnectedPlayers(gameId)
// angular wants an array of objects, it can't handle an array of ints
players := []Message{}
for _, pid := range pids {
players = append(players, Message{"id": pid})
}
ws.WriteJSON(Message{
"type": "players",
"players": players,
})
ws.WriteJSON(Message{
"type": "state",
"state": game.State,
})
} else {
log.Printf("Host rejoining game in progress")
// get the game board so we can send an update
board, err := getBoard(gameId, db)
if err != nil {
log.Printf("Could not get board, this might not be an error: %#v", err)
return err
}
log.Printf("Getting nicer board: %#v", board)
niceBoard, err := board.getBoard()
if err != nil {
log.Printf("Can't init with board: %#v", err)
return err
}
ws.WriteJSON(Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
}
return nil
}
示例4: getParamString
)
var InitDB func() = func() {
dbConfig := new(DbConfig)
config := dbConfig.getConnectionString()
if db, err := sql.Open(config.Driver, config.ConnectionString); err != nil {
revel.ERROR.Fatal(err)
} else {
Dbm = &gorp.DbMap{
Db: db,
Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
}
// Defines the table for use by GORP
// This is a function we will create soon.
// defineObjectTable(Dbm)
if err := Dbm.CreateTablesIfNotExists(); err != nil {
revel.ERROR.Fatal(err)
}
}
// Set up tables here - a bit janky but it looks like you need to
// define tables manually
//func defineObjectTable(dbm *gorp.DbMap){
// // set "id" as primary key and autoincrement
// t := dbm.AddTable(models.Object{}).SetKeys(true, "id")
// // e.g. VARCHAR(25)
// t.ColMap("oid").SetMaxSize(65)
//}
func getParamString(param string, defaultValue string) string {
p, found := revel.Config.String(param)
示例5: InitTables
func InitTables(dbmap *gorp.DbMap) error {
dbmap.AddTableWithName(LogLine{}, "log_lines").SetKeys(true, "Id")
return dbmap.CreateTablesIfNotExists()
}