Beispiel #1
0
func GetPosts(n int) (results []interface{}) {
	db := OpenConn()
	stmt, err := db.Prepare(`SELECT * from post order by created DESC LIMIT $1`)
	util.HandleErr(err)

	var atts = make([]interface{}, 0)
	atts = append(atts, strconv.Itoa(n))
	rows, err := stmt.Query(atts...)
	util.HandleErr(err)

	defer db.Close()
	//fields, err := rows.Columns()
	util.HandleErr(err)

	results = make([]interface{}, 0)

	var id, userId int
	var title string
	var content string
	var published bool
	var created, modified time.Time

	for rows.Next() {
		err = rows.Scan(&id, &title, &content, &userId, &published, &created, &modified)
		util.HandleErr(err)
		var p = Post{id, title, content, userId, published, created, modified}
		results = append(results, p)
	}
	return results
}
Beispiel #2
0
func TestEmptyDB(db *sql.DB) {
	q := `select relname from pg_class where relname = 'post' and relkind='r'`
	var initialized int = 0
	rows, err := db.Query(q)
	if err != nil {
		fmt.Println("DB Query panic")
		panic(fmt.Sprintf("%s", err))
	}
	for rows.Next() {
		var relname string
		err = rows.Scan(&relname)
		if len(relname) > 0 {
			initialized++
		}
	}
	err = rows.Err()
	util.HandleErr(err)
	rows.Close()

	if initialized == 0 {
		q = `DROP SEQUENCE IF EXISTS post_id_seq CASCADE;
	            DROP TABLE IF EXISTS post CASCADE;
	            CREATE SEQUENCE post_id_seq;
	            CREATE TABLE post(id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('post_id_seq'),
	                title VARCHAR(32), content TEXT, user_id INTEGER,
	                published BOOLEAN, created TIMESTAMP, modified TIMESTAMP)`
		_, err = db.Exec(q)
		util.HandleErr(err)
	}
	defer db.Close()
}
Beispiel #3
0
func InsertIntoDB(atts []interface{}) {
	//db := OpenConn()
	db, err := sql.Open("postgres", dbParams)
	if err != nil {
		fmt.Println("Connection panic")
		panic(fmt.Sprintf("%s", err))
	}
	db.Begin()
	stmt, err := db.Prepare(`INSERT INTO POST (title,content,user_id,published,created,modified)
							 values ($1,$2,$3,$4,$5,$6)`)
	util.HandleErr(err)

	_, err = stmt.Exec(atts...)
	util.HandleErr(err)
	defer db.Close()
}
Beispiel #4
0
func TestEmptyDB() bool {
	db, err := sql.Open("postgres", dbParams)
	if err != nil {
		fmt.Println("Connection panic")
		panic(fmt.Sprintf("%s", err))
	}
	db.Begin()
	q := `select relname from pg_class where relname = 'post' and relkind='r'`
	var initialized bool = false
	rows, err := db.Query(q)
	if err != nil {
		fmt.Println("DB Query panic")
		panic(fmt.Sprintf("%s", err))
	}
	for rows.Next() {
		var relname string
		err = rows.Scan(&relname)
		if len(relname) > 0 {
			initialized = true
			return initialized
		}
	}
	err = rows.Err()
	util.HandleErr(err)
	rows.Close()

	if initialized == false {
		fmt.Println("\033[32;1m %s\033[0m", "Initializing empty DB")
		q = `DROP TABLE IF EXISTS post CASCADE;
	       CREATE TABLE post(id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('post_id_seq'),
	                title VARCHAR(32), content TEXT, user_id INTEGER,
	                published BOOLEAN, created TIMESTAMP, modified TIMESTAMP)`
		_, err = db.Exec(q)
		util.HandleErr(err)
	}
	defer db.Close()
	return initialized
}
Beispiel #5
0
func GetPosts(n int) {
	db := OpenConn()
	rows, err := db.Query(`SELECT * FROM POST LIMIT 10`)
	util.HandleErr(err)
	defer db.Close()
	rows.Next()
	cols, _ := rows.Columns()
	out := make([]interface{}, len(cols))
	dest := make([]interface{}, len(cols))
	for i, _ := range dest {
		dest[i] = &out[i]
	}
	err = rows.Scan(dest...)

	fmt.Sprintf("%s", dest)
	fmt.Println(rows)
	//fmt.Println((*valuePtrs[0].(*interface{})).(string))
	//return rows
}