Esempio n. 1
0
func SetupForum(conn *sqlite.Conn) {
	for i := 0; i < len(ForumsTables); i++ {
		createTable(conn, "", ForumsTables[i])
	}
	for i := 0; i < len(UsersTables); i++ {
		createTable(conn, "users", UsersTables[i])
	}
	HandleError(conn.Exec("INSERT INTO main.forum (name, desc) VALUES ('Main Forum', 'This is the main forum.');"))
}
Esempio n. 2
0
func createTable(conn *sqlite.Conn, dbName string, schemaDefinition interface{}) {
	schemaString := ""
	forumType := reflect.Typeof(schemaDefinition).(*reflect.StructType)
	numColumns := forumType.NumField()
	schemaArray := make([]string, numColumns)
	for i := 0; i < numColumns; i++ {
		schemaArray[i] = strings.ToLower(forumType.Field(i).Name) +
			" " + strings.ToUpper(forumType.Field(i).Tag)
	}
	schemaString = strings.Join(schemaArray, ", ")

	if dbName == "" {
		dbName = "main"
	}

	dbName = dbName + "." + forumType.Name()

	schemaString = "CREATE TABLE " + strings.ToLower(dbName) +
		" (" + schemaString + ");"

	HandleError(conn.Exec(schemaString))
}