—Silas
var results []Result
for rows.Next():
results = append(results, res)
should be pretty efficient. It's going to allocate larger arrays
rather quickly, and as a plus your memory usage grows as you are
fetching the rows, not instantly.
I quickly looked over the API for MySQL and I'm pretty sure that you
can do it with mysql_num_rows() it really matters which result format
you use.
For PostgreSQL, I'm not sure as I'm not familiar with it, but I think
PQntuples() might return what I'm are looking for.
With Sqlite3 I've already done it.
For Firebird, I couldn't find my answer quick enough.
>
> I actually ensured that my ODBC driver
> (https://bitbucket.org/miquella/mgodbc) implemented the Result interface in
> my rows struct. However, this wouldn't help as the driver's internal rows
> struct is wrapped by sql.Rows. It would have to be something exposed through
> sql.Rows before it could be effective.
>
> Perhaps Brad has some input on this?
I'm pretty sure that what I'm asking can be done at a DB level, if
said API doesn't support it at DB level then falling back to using
Patrick's method of appending is another option.
I think you'd need at least `2` or `3` different functions or a different options to tell it what to do and these options would need to be set before the db connection is started.In the case of MySQL alone:1. num_rows is for the number of matched rows in a select - fine2. affected_rows is for the number of ? rows in an updated
I put a question mark in the latter because it can return 1 of 2 different results that may cause all sorts of subtle bugs. Depending on settings tied to the db `connection` it can return either the number of matched rows, or the number of rows actually updated (MySQL doesn't update a row if it already had the same value(s)).You'd also need to set options before the query is executed because depending on what you're doing you don't want every query being counted as this can affect performance in odd ways.
func (self *UserDAO) GetUsers() (users []models.User){
selectStatement,err := self.db.Prepare("SELECT userid,firstname,lastname,dob,email FROM users");
if(err!=nil){
panic(err.Error());
}
defer selectStatement.Close();
rows,err := selectStatement.Query();
if(err != nil){
panic(err.Error());
}
defer rows.Close();
users = make([]models.User,0);
var id int;
var fname, lname, dob, email string;
for rows.Next(){
err := rows.Scan(&id,&fname,&lname,&dob,&email);
if(err!=nil){
panic(err.Error());
}
users = append(users,*models.NewUser(id,fname,lname,dob,email));
}
return
}