Example #1
0
func main() {
	flag.Parse()
	if *flagDatabase == "" {
		exitf("--database flag required")
	}

	db, err := mysql.DialTCP(*flagHost, *flagUser, *flagPassword, "")
	if err != nil {
		exitf("Error connecting to database: %v", err)
	}

	dbname := *flagDatabase
	exists := dbExists(db, dbname)
	if exists {
		if *flagIgnore {
			return
		}
		if !*flagWipe {
			exitf("Databases %q already exists, but --wipe not given. Stopping.", dbname)
		}
		do(db, "DROP DATABASE "+dbname)
	}
	do(db, "CREATE DATABASE "+dbname)
	do(db, "USE "+dbname)

	for _, tableSql := range mysqlindexer.SQLCreateTables() {
		do(db, tableSql)
	}
	do(db, fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, mysqlindexer.SchemaVersion()))
}
Example #2
0
// Get a free cached connection or allocate a new one.
func (mi *Indexer) getConnection() (client *mysql.Client, err os.Error) {
	mi.clientLock.Lock()
	if len(mi.cachedClients) > 0 {
		defer mi.clientLock.Unlock()
		client = mi.cachedClients[len(mi.cachedClients)-1]
		mi.cachedClients = mi.cachedClients[:len(mi.cachedClients)-1]
		// TODO: Outside the mutex, double check that the client is still good.
		return
	}
	mi.clientLock.Unlock()

	client, err = mysql.DialTCP(mi.Host, mi.User, mi.Password, mi.Database)
	return
}