Exemplo n.º 1
0
// Wrap this on the sqlStatement type to avoid exporting it
func (self *sqlStatement) BindParams(params ...interface{}) sql.Error {
	var rc int

	stmtCount := self.sqlBindParameterCount()
	paramCount := len(params)

	if paramCount != stmtCount {
		return sql.InvalidBindType
	}

	if paramCount < 1 {
		return nil
	}

	for pos, param := range params {
		switch param.(type) {
		case nil:
			rc = self.sqlBindNull(pos)
		case int:
			rc = self.sqlBindInt(pos, param.(int))
		case int64:
			rc = self.sqlBindInt64(pos, param.(int64))
		case string:
			rc = self.sqlBindText(pos, param.(string))
		case float32:
			rc = self.sqlBindFloat32(pos, param.(float32))
		case float64:
			rc = self.sqlBindFloat64(pos, param.(float64))
		case bool:
			// http://www.sqlite.org/datatype3.html
			// SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
			var b int
			if param.(bool) {
				b = 1
			} else {
				b = 0
			}
			rc = self.sqlBindInt(pos, b)
		default:
			return sql.NewError("Parameter " + string(pos+1) + "is an unrecognized type")
		}

		if rc != StatusOk {
			return sql.NewError("Could not bind parameter " + string(pos+1))
		}
	}
	return nil
}
Exemplo n.º 2
0
func (self *Connection) Close() sql.Error {
	rc := self.handle.sqlClose()
	if rc != StatusOk {
		return sql.NewError(self.handle.sqlErrorMessage())
	}
	return nil
}
Exemplo n.º 3
0
func (self *Connection) Prepare(query string) (sql.Statement, sql.Error) {
	sqlStmt, rc := self.handle.sqlPrepare(query)
	if rc != StatusOk {
		return nil, sql.NewError(self.handle.sqlErrorMessage())
	}

	stmt := new(Statement)
	stmt.handle = sqlStmt
	stmt.connection = self
	return stmt, nil
}
Exemplo n.º 4
0
// Scan the current row of results by column index.
func (self *ResultSet) Scan(refs ...interface{}) sql.Error {
	// TODO: is this the best way?
	columnCount := self.statement.handle.sqlColumnCount()

	if len(refs) > columnCount {
		return sql.NewError("Trying to scan more columns than exist!")
	}

	for i, val := range refs {
		err := self.getColumn(i, val)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 5
0
// connect string:
// 'postgresql://user:pwd@host:port/dbname?opt1=value
// for addition options see: http://www.postgresql.org/docs/8.1/static/libpq.html#LIBPQ-CONNECT
func (self *Driver) Connect(dsn *sql.DSN) (sql.Connection, sql.Error) {
	ds, err := parseDataSource(dsn)
	if err != nil {
		return nil, sql.NewError(err.String())
	}

	fmt.Println(ds)

	sqlConn := pqConnect(ds)
	if sqlConn == nil {
		//TODO: error checking
		return nil, sql.Busy
	}

	return nil, nil
}
Exemplo n.º 6
0
func (self *ResultSet) getColumn(pos int, ptr interface{}) sql.Error {
	switch v := ptr.(type) {
	case nil:
		// Do nothing
		break
	case *int:
		*v = self.statement.handle.sqlColumnInt(pos)
	case *int64:
		*v = self.statement.handle.sqlColumnInt64(pos)
	case *float32:
		*v = self.statement.handle.sqlColumnFloat32(pos)
	case *float64:
		*v = self.statement.handle.sqlColumnFloat64(pos)
	case *string:
		*v = self.statement.handle.sqlColumnText(pos)
	default:
		return sql.NewError("Attempting to scan an unrecognized type")
	}
	return nil
}