Ejemplo 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
}
Ejemplo n.º 2
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
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
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
}
Ejemplo n.º 5
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()
}
Ejemplo n.º 6
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()
}