// Close a query that was created with Prepare() // // This is called automaticly when the database is closed. func (query *Query) Close() { query.lock.Lock() defer query.lock.Unlock() if query.opened { C.c_dbxml_query_free(query.query) delete(query.db.queries, query.id) query.db = nil query.opened = false } }
// Prepare an XPATH query. // // The query can be run multiple times, and a running query can be cancelled by query.Cancel() func (db *Db) Prepare(query string) (*Query, error) { q := &Query{} db.lock.Lock() defer db.lock.Unlock() if !db.opened { return q, errclosed } cs := C.CString(query) defer C.free(unsafe.Pointer(cs)) q.query = C.c_dbxml_prepare_query(db.db, cs) if C.c_dbxml_get_prepared_error(q.query) != 0 { defer C.c_dbxml_query_free(q.query) return q, errors.New(C.GoString(C.c_dbxml_get_prepared_errstring(q.query))) } // No finalizer: query will be closed when database gets closed q.opened = true q.db = db q.id = db.counter db.counter++ db.queries[q.id] = q return q, nil }