Commit ac6deca5 authored by gwenn's avatar gwenn

Make it installable by default on Mac OS X

SQLite 3.7.13
SQLITE_OMIT_LOAD_EXTENSION
SQLITE_ENABLE_COLUMN_METADATA not defined
parent ebcb34c3
...@@ -18,8 +18,6 @@ int goSqlite3Config(int op, int mode); ...@@ -18,8 +18,6 @@ int goSqlite3Config(int op, int mode);
*/ */
import "C" import "C"
import "unsafe"
// ThreadingMode enumerates SQLite threading mode // ThreadingMode enumerates SQLite threading mode
// See ConfigThreadingMode // See ConfigThreadingMode
type ThreadingMode int32 type ThreadingMode int32
...@@ -119,32 +117,3 @@ func (c *Conn) queryOrSetEnableDbConfig(key, i C.int) (bool, error) { ...@@ -119,32 +117,3 @@ func (c *Conn) queryOrSetEnableDbConfig(key, i C.int) (bool, error) {
func (c *Conn) EnableExtendedResultCodes(b bool) error { func (c *Conn) EnableExtendedResultCodes(b bool) error {
return c.error(C.sqlite3_extended_result_codes(c.db, btocint(b)), "Conn.EnableExtendedResultCodes") return c.error(C.sqlite3_extended_result_codes(c.db, btocint(b)), "Conn.EnableExtendedResultCodes")
} }
// EnableLoadExtension enables or disables extension loading.
// (See http://sqlite.org/c3ref/enable_load_extension.html)
func (c *Conn) EnableLoadExtension(b bool) error {
rv := C.sqlite3_enable_load_extension(c.db, btocint(b))
if rv == C.SQLITE_OK {
return nil
}
return c.error(rv, "Conn.EnableLoadExtension")
}
// LoadExtension loads an extension
// (See http://sqlite.org/c3ref/load_extension.html)
func (c *Conn) LoadExtension(file string, proc ...string) error {
cfile := C.CString(file)
defer C.free(unsafe.Pointer(cfile))
var cproc *C.char
if len(proc) > 0 {
cproc = C.CString(proc[0])
defer C.free(unsafe.Pointer(cproc))
}
var errMsg *C.char
rv := C.sqlite3_load_extension(c.db, cfile, cproc, &errMsg)
if rv != C.SQLITE_OK {
defer C.sqlite3_free(unsafe.Pointer(errMsg))
return c.error(rv, C.GoString(errMsg))
}
return nil
}
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
// See SQLITE_OMIT_LOAD_EXTENSION (http://www.sqlite.org/compile.html)
package sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
// EnableLoadExtension enables or disables extension loading.
// (See http://sqlite.org/c3ref/enable_load_extension.html)
func (c *Conn) EnableLoadExtension(b bool) error {
rv := C.sqlite3_enable_load_extension(c.db, btocint(b))
if rv == C.SQLITE_OK {
return nil
}
return c.error(rv, "Conn.EnableLoadExtension")
}
// LoadExtension loads an extension
// (See http://sqlite.org/c3ref/load_extension.html)
func (c *Conn) LoadExtension(file string, proc ...string) error {
cfile := C.CString(file)
defer C.free(unsafe.Pointer(cfile))
var cproc *C.char
if len(proc) > 0 {
cproc = C.CString(proc[0])
defer C.free(unsafe.Pointer(cproc))
}
var errMsg *C.char
rv := C.sqlite3_load_extension(c.db, cfile, cproc, &errMsg)
if rv != C.SQLITE_OK {
defer C.sqlite3_free(unsafe.Pointer(errMsg))
return c.error(rv, C.GoString(errMsg))
}
return nil
}
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite_test
import (
"fmt"
"testing"
. "github.com/gwenn/gosqlite"
)
func init() {
err := EnableSharedCache(false)
if err != nil {
panic(fmt.Sprintf("couldn't disable shared cache: '%s'", err))
}
}
func TestEnableLoadExtension(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.EnableLoadExtension(false)
checkNoError(t, err, "EnableLoadExtension error: %s")
}
...@@ -25,10 +25,6 @@ func init() { ...@@ -25,10 +25,6 @@ func init() {
if err != nil { if err != nil {
panic(fmt.Sprintf("cannot activate uri handling: '%s'", err)) panic(fmt.Sprintf("cannot activate uri handling: '%s'", err))
} }
err = EnableSharedCache(false)
if err != nil {
panic(fmt.Sprintf("couldn't disable shared cache: '%s'", err))
}
} }
func TestEnableFKey(t *testing.T) { func TestEnableFKey(t *testing.T) {
...@@ -64,8 +60,6 @@ func TestEnableExtendedResultCodes(t *testing.T) { ...@@ -64,8 +60,6 @@ func TestEnableExtendedResultCodes(t *testing.T) {
func TestConnSettings(t *testing.T) { func TestConnSettings(t *testing.T) {
db := open(t) db := open(t)
defer checkClose(db, t) defer checkClose(db, t)
err := db.EnableLoadExtension(false) err := db.SetRecursiveTriggers("main", true)
checkNoError(t, err, "EnableLoadExtension error: %s")
err = db.SetRecursiveTriggers("main", true)
checkNoError(t, err, "SetRecursiveTriggers error: %s") checkNoError(t, err, "SetRecursiveTriggers error: %s")
} }
...@@ -13,7 +13,6 @@ import "C" ...@@ -13,7 +13,6 @@ import "C"
import ( import (
"fmt" "fmt"
"strings" "strings"
"unsafe"
) )
// Databases returns one couple (name, file) for each database attached to the current database connection. // Databases returns one couple (name, file) for each database attached to the current database connection.
...@@ -169,54 +168,6 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) { ...@@ -169,54 +168,6 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) {
return columns, nil return columns, nil
} }
// Column extracts metadata about a column of a table (doesn't work with view).
// Column.Cid and Column.DfltValue are left unspecified.
// (See http://sqlite.org/c3ref/table_column_metadata.html)
func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
var zDbName *C.char
if len(dbName) > 0 {
zDbName = C.CString(dbName)
defer C.free(unsafe.Pointer(zDbName))
}
zTableName := C.CString(tableName)
defer C.free(unsafe.Pointer(zTableName))
zColumnName := C.CString(columnName)
defer C.free(unsafe.Pointer(zColumnName))
var zDataType, zCollSeq *C.char
var notNull, primaryKey, autoinc C.int
rv := C.sqlite3_table_column_metadata(c.db, zDbName, zTableName, zColumnName, &zDataType, &zCollSeq,
&notNull, &primaryKey, &autoinc)
if rv != C.SQLITE_OK {
return nil, c.error(rv, fmt.Sprintf("Conn.Column(db: %q, tbl: %q, col: %q)", dbName, tableName, columnName))
}
return &Column{-1, columnName, C.GoString(zDataType), notNull == 1, "", int(primaryKey),
autoinc == 1, C.GoString(zCollSeq)}, nil
}
// ColumnDatabaseName returns the database
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnDatabaseName(index int) string {
return C.GoString(C.sqlite3_column_database_name(s.stmt, C.int(index)))
}
// ColumnTableName returns the original un-aliased table name
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnTableName(index int) string {
return C.GoString(C.sqlite3_column_table_name(s.stmt, C.int(index)))
}
// ColumnOriginName returns the original un-aliased table column name
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnOriginName(index int) string {
return C.GoString(C.sqlite3_column_origin_name(s.stmt, C.int(index)))
}
// ColumnDeclaredType returns the declared type of the table column of a particular result column in SELECT statement. // ColumnDeclaredType returns the declared type of the table column of a particular result column in SELECT statement.
// If the result column is an expression or subquery, then an empty string is returned. // If the result column is an expression or subquery, then an empty string is returned.
// The left-most column is column 0. // The left-most column is column 0.
......
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
// See SQLITE_ENABLE_COLUMN_METADATA (http://www.sqlite.org/compile.html)
package sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
// Column extracts metadata about a column of a table (doesn't work with view).
// Column.Cid and Column.DfltValue are left unspecified.
// (See http://sqlite.org/c3ref/table_column_metadata.html)
func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
var zDbName *C.char
if len(dbName) > 0 {
zDbName = C.CString(dbName)
defer C.free(unsafe.Pointer(zDbName))
}
zTableName := C.CString(tableName)
defer C.free(unsafe.Pointer(zTableName))
zColumnName := C.CString(columnName)
defer C.free(unsafe.Pointer(zColumnName))
var zDataType, zCollSeq *C.char
var notNull, primaryKey, autoinc C.int
rv := C.sqlite3_table_column_metadata(c.db, zDbName, zTableName, zColumnName, &zDataType, &zCollSeq,
&notNull, &primaryKey, &autoinc)
if rv != C.SQLITE_OK {
return nil, c.error(rv, fmt.Sprintf("Conn.Column(db: %q, tbl: %q, col: %q)", dbName, tableName, columnName))
}
return &Column{-1, columnName, C.GoString(zDataType), notNull == 1, "", int(primaryKey),
autoinc == 1, C.GoString(zCollSeq)}, nil
}
// ColumnDatabaseName returns the database
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnDatabaseName(index int) string {
return C.GoString(C.sqlite3_column_database_name(s.stmt, C.int(index)))
}
// ColumnTableName returns the original un-aliased table name
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnTableName(index int) string {
return C.GoString(C.sqlite3_column_table_name(s.stmt, C.int(index)))
}
// ColumnOriginName returns the original un-aliased table column name
// that is the origin of a particular result column in SELECT statement.
// The left-most column is column 0.
// (See http://www.sqlite.org/c3ref/column_database_name.html)
func (s *Stmt) ColumnOriginName(index int) string {
return C.GoString(C.sqlite3_column_origin_name(s.stmt, C.int(index)))
}
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite_test
import (
"testing"
"github.com/bmizerany/assert"
. "github.com/gwenn/gosqlite"
)
func TestColumn(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
column, err := db.Column("", "test", "id")
checkNoError(t, err, "error getting column metadata: %s")
assert.Equal(t, "id", column.Name, "column name")
assert.Equal(t, 1, column.Pk, "primary key index")
assert.T(t, !column.Autoinc, "expecting autoinc flag to be false")
column, err = db.Column("main", "test", "id")
checkNoError(t, err, "error getting column metadata: %s")
column, err = db.Column("", "test", "bim")
assert.T(t, err != nil, "expected error")
assert.T(t, err.Error() != "")
//println(err.Error())
}
func TestColumnMetadata(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("SELECT name AS table_name FROM sqlite_master")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "main", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "sqlite_master", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "name", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "text", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnView(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT * FROM test")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT a_string AS str FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "main", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "test", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "a_string", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "TEXT", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnExpr(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT date('now') as tic")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT tic FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, None, affinity, "affinity")
}
...@@ -123,26 +123,6 @@ func TestColumns(t *testing.T) { ...@@ -123,26 +123,6 @@ func TestColumns(t *testing.T) {
//println(err.Error()) //println(err.Error())
} }
func TestColumn(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
column, err := db.Column("", "test", "id")
checkNoError(t, err, "error getting column metadata: %s")
assert.Equal(t, "id", column.Name, "column name")
assert.Equal(t, 1, column.Pk, "primary key index")
assert.T(t, !column.Autoinc, "expecting autoinc flag to be false")
column, err = db.Column("main", "test", "id")
checkNoError(t, err, "error getting column metadata: %s")
column, err = db.Column("", "test", "bim")
assert.T(t, err != nil, "expected error")
assert.T(t, err.Error() != "")
//println(err.Error())
}
func TestForeignKeys(t *testing.T) { func TestForeignKeys(t *testing.T) {
db := open(t) db := open(t)
defer checkClose(db, t) defer checkClose(db, t)
...@@ -210,70 +190,6 @@ func TestTableIndexes(t *testing.T) { ...@@ -210,70 +190,6 @@ func TestTableIndexes(t *testing.T) {
//println(err.Error()) //println(err.Error())
} }
func TestColumnMetadata(t *testing.T) {
db := open(t)
defer checkClose(db, t)
s, err := db.Prepare("SELECT name AS table_name FROM sqlite_master")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "main", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "sqlite_master", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "name", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "text", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnView(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT * FROM test")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT a_string AS str FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "main", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "test", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "a_string", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "TEXT", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, Textual, affinity, "affinity")
}
func TestColumnMetadataOnExpr(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.FastExec("CREATE VIEW vtest AS SELECT date('now') as tic")
checkNoError(t, err, "error creating view: %s")
s, err := db.Prepare("SELECT tic FROM vtest")
check(err)
defer checkFinalize(s, t)
databaseName := s.ColumnDatabaseName(0)
assert.Equal(t, "", databaseName, "database name")
tableName := s.ColumnTableName(0)
assert.Equal(t, "", tableName, "table name")
originName := s.ColumnOriginName(0)
assert.Equal(t, "", originName, "origin name")
declType := s.ColumnDeclaredType(0)
assert.Equal(t, "", declType, "declared type")
affinity := s.ColumnTypeAffinity(0)
assert.Equal(t, None, affinity, "affinity")
}
func TestColumnTypeAffinity(t *testing.T) { func TestColumnTypeAffinity(t *testing.T) {
db := open(t) db := open(t)
defer checkClose(db, t) defer checkClose(db, t)
......
...@@ -10,6 +10,12 @@ package sqlite ...@@ -10,6 +10,12 @@ package sqlite
#include <sqlite3.h> #include <sqlite3.h>
#include <stdlib.h> #include <stdlib.h>
#if SQLITE_VERSION_NUMBER < 3007015
const char *sqlite3_errstr(int rc) {
return "";
}
#endif
*/ */
import "C" import "C"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment