コード例 #1
0
ファイル: function.go プロジェクト: pkf/gosqlite
// CreateScalarFunction creates or redefines SQL scalar functions.
// TODO Make possible to specify the preferred encoding
// (See http://sqlite.org/c3ref/create_function.html)
func (c *Conn) CreateScalarFunction(functionName string, nArg int, pApp interface{}, f ScalarFunction, d DestroyFunctionData) error {
	fname := C.CString(functionName)
	defer C.free(unsafe.Pointer(fname))
	if f == nil {
		if len(c.udfs) > 0 {
			delete(c.udfs, functionName)
		}
		return c.error(C.sqlite3_create_function_v2(c.db, fname, C.int(nArg), C.SQLITE_UTF8, nil, nil, nil, nil, nil),
			fmt.Sprintf("<Conn.CreateScalarFunction(%q)", functionName))
	}
	// To make sure it is not gced, keep a reference in the connection.
	udf := &sqliteFunction{f, nil, nil, d, pApp, make(map[*ScalarContext]bool), nil}
	if len(c.udfs) == 0 {
		c.udfs = make(map[string]*sqliteFunction)
	}
	c.udfs[functionName] = udf // FIXME same function name with different args is not supported
	return c.error(C.goSqlite3CreateScalarFunction(c.db, fname, C.int(nArg), C.SQLITE_UTF8, unsafe.Pointer(udf)),
		fmt.Sprintf("Conn.CreateScalarFunction(%q)", functionName))
}
コード例 #2
0
ファイル: function.go プロジェクト: gwenn/gosqlite
// CreateAggregateFunction creates or redefines SQL aggregate functions.
// Cannot be used with Go >= 1.6 and cgocheck enabled.
// TODO Make possible to specify the preferred encoding
// (See http://sqlite.org/c3ref/create_function.html)
func (c *Conn) CreateAggregateFunction(functionName string, nArg int32, pApp interface{},
	step StepFunction, final FinalFunction, d DestroyDataFunction) error {
	fname := C.CString(functionName)
	defer C.free(unsafe.Pointer(fname))
	if step == nil {
		if len(c.udfs) > 0 {
			delete(c.udfs, functionName)
		}
		return c.error(C.sqlite3_create_function_v2(c.db, fname, C.int(nArg), C.SQLITE_UTF8, nil, nil, nil, nil, nil),
			fmt.Sprintf("<Conn.CreateAggregateFunction(%q)", functionName))
	}
	// To make sure it is not gced, keep a reference in the connection.
	udf := &sqliteFunction{nil, step, final, d, pApp, nil, make(map[*AggregateContext]struct{})}
	if len(c.udfs) == 0 {
		c.udfs = make(map[string]*sqliteFunction)
	}
	c.udfs[functionName] = udf // FIXME same function name with different args is not supported
	return c.error(C.goSqlite3CreateAggregateFunction(c.db, fname, C.int(nArg), C.SQLITE_UTF8, unsafe.Pointer(udf)),
		fmt.Sprintf("Conn.CreateAggregateFunction(%q)", functionName))
}