Esempio n. 1
0
func Get(conn *sqlite.Conn, kind interface{}, id string) interface{} {
	result := reflect.NewValue(kind)
	tableType := reflect.Typeof(kind).(*reflect.StructType)
	numColumns := tableType.NumField()
	schemaArray := make([]string, numColumns)
	things := make([]interface{}, numColumns)
	_ = unsafe.Unreflect

	for i := 0; i < numColumns; i++ {
		name := tableType.Field(i).Name
		schemaArray[i] = strings.ToLower(name)
		field := result.(*reflect.StructValue).Field(i)
		k := field.Interface()
		things[i] = k
		fmt.Printf("%v.", k)
	}
	str := "SELECT " + strings.Join(schemaArray, ", ") + " FROM " + tableType.Name() + " WHERE id = ?"

	stmt, _ := conn.Prepare(str)
	HandleError(stmt.Exec(id))

	if stmt.Next() {
		HandleError(stmt.Scan(things...))
	}
	return result
}
Esempio n. 2
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. 3
0
func GetForum(conn *sqlite.Conn, id string) Forum {
	stmt, _ := conn.Prepare("SELECT id, name, desc FROM main.forum WHERE id = ?;")
	var f Forum
	HandleError(stmt.Exec(id))
	if stmt.Next() {
		HandleError(stmt.Scan(&f.Id, &f.Name, &f.Desc))
	}
	return f
}
Esempio n. 4
0
func TablesExist(conn *sqlite.Conn) bool {
	stmt, _ := conn.Prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table';")

	r := 0
	for stmt.Next() {
		stmt.Scan(&r)
	}

	return r != 0
}
Esempio n. 5
0
/** This function performs the db lookup. */
func doLookup(c *sqlite.Conn, key string) (url string) {
	stmt, err := c.Prepare("select url from redirects where key = ?")
	if err != nil {
		panic(err.String())
	}
	defer stmt.Finalize()
	stmt.Exec(key)
	stmt.Next()
	stmt.Scan(&url)
	return
}
Esempio n. 6
0
func TableExists(conn *sqlite.Conn, tableName string) bool {
	stmt, err := conn.Prepare(
		"select 1 from sqlite_master where type = ? and tbl_name = ?")
	defer stmt.Finalize()
	if err != nil {
		log.Println("sql error: " + err.String())
		return false
	}

	stmt.Exec("table", tableName)
	return stmt.Next()
}
Esempio n. 7
0
func CreateTable(conn *sqlite.Conn, tableName string, columns ...string) {
	if len(columns) == 0 {
		return
	}

	parameter_list := strings.Join(columns, ", ")
	query := fmt.Sprintf("create table %s (%s)", tableName, parameter_list)
	stmt, err := conn.Prepare(query)
	defer stmt.Finalize()

	if err != nil {
		log.Fatalf("Cannot create table %s; %s (%s)\n", tableName, err, query)
	}

	stmt.Exec()
	stmt.Next()
}
Esempio n. 8
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))
}