Объявление собственных функций в SQLite3 (замена пробелов на табуляции)
This commit is contained in:
parent
3970f394b0
commit
1d8cb29dda
|
|
@ -4,49 +4,49 @@ using SQLite;
|
||||||
|
|
||||||
namespace Touchin.SQLiteExtensions
|
namespace Touchin.SQLiteExtensions
|
||||||
{
|
{
|
||||||
public static partial class SQLite3Extensions
|
public static partial class SQLite3Extensions
|
||||||
{
|
{
|
||||||
public static void CreateDistanceFunction (this SQLiteConnection connection)
|
public static void CreateDistanceFunction (this SQLiteConnection connection)
|
||||||
{
|
{
|
||||||
connection.CreateFunction ("DISTANCE", 4, DistanceBody);
|
connection.CreateFunction ("DISTANCE", 4, DistanceBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static double Distance (double latitude1, double longitude1, double latitude2, double longitude2)
|
private static double Distance (double latitude1, double longitude1, double latitude2, double longitude2)
|
||||||
{
|
{
|
||||||
const double earthRadius = 6378.1;
|
const double earthRadius = 6378.1;
|
||||||
|
|
||||||
double lat1 = latitude1 * Math.PI / 180;
|
double lat1 = latitude1 * Math.PI / 180;
|
||||||
double lon1 = longitude1 * Math.PI / 180;
|
double lon1 = longitude1 * Math.PI / 180;
|
||||||
double lat2 = latitude2 * Math.PI / 180;
|
double lat2 = latitude2 * Math.PI / 180;
|
||||||
double lon2 = longitude2 * Math.PI / 180;
|
double lon2 = longitude2 * Math.PI / 180;
|
||||||
|
|
||||||
return earthRadius * Math.Acos (Math.Sin (lat1) * Math.Sin (lat2) + Math.Cos (lat1) * Math.Cos (lat2) * Math.Cos (lon2 - lon1));
|
return earthRadius * Math.Acos (Math.Sin (lat1) * Math.Sin (lat2) + Math.Cos (lat1) * Math.Cos (lat2) * Math.Cos (lon2 - lon1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if __IOS__
|
#if __IOS__
|
||||||
// https://bugzilla.novell.com/show_bug.cgi?id=576775
|
// https://bugzilla.novell.com/show_bug.cgi?id=576775
|
||||||
[MonoTouch.MonoPInvokeCallback (typeof (Action<IntPtr, int, IntPtr>))]
|
[MonoTouch.MonoPInvokeCallback (typeof (Action<IntPtr, int, IntPtr>))]
|
||||||
#endif
|
#endif
|
||||||
private static void DistanceBody (IntPtr ctx, int argc, IntPtr argv)
|
private static void DistanceBody (IntPtr ctx, int argc, IntPtr argv)
|
||||||
{
|
{
|
||||||
IntPtr[] args = ExtractArgs (argc, argv);
|
IntPtr[] args = ExtractArgs (argc, argv);
|
||||||
|
|
||||||
if (ValueType (args [0]) != SQLite3.ColType.Float ||
|
if (ValueType (args [0]) != SQLite3.ColType.Float ||
|
||||||
ValueType (args [1]) != SQLite3.ColType.Float ||
|
ValueType (args [1]) != SQLite3.ColType.Float ||
|
||||||
ValueType (args [2]) != SQLite3.ColType.Float ||
|
ValueType (args [2]) != SQLite3.ColType.Float ||
|
||||||
ValueType (args [3]) != SQLite3.ColType.Float)
|
ValueType (args [3]) != SQLite3.ColType.Float)
|
||||||
{
|
{
|
||||||
ResultNull (ctx);
|
ResultNull (ctx);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
double latitude1 = ValueDouble (args [0]);
|
double latitude1 = ValueDouble (args [0]);
|
||||||
double longitude1 = ValueDouble (args [1]);
|
double longitude1 = ValueDouble (args [1]);
|
||||||
double latitude2 = ValueDouble (args [2]);
|
double latitude2 = ValueDouble (args [2]);
|
||||||
double longitude2 = ValueDouble (args [3]);
|
double longitude2 = ValueDouble (args [3]);
|
||||||
double result = Distance (latitude1, longitude1, latitude2, longitude2);
|
double result = Distance (latitude1, longitude1, latitude2, longitude2);
|
||||||
ResultDouble (ctx, result);
|
ResultDouble (ctx, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -4,79 +4,79 @@ using SQLite;
|
||||||
|
|
||||||
namespace Touchin.SQLiteExtensions
|
namespace Touchin.SQLiteExtensions
|
||||||
{
|
{
|
||||||
public static partial class SQLite3Extensions
|
public static partial class SQLite3Extensions
|
||||||
{
|
{
|
||||||
private static void CreateFunction (this SQLiteConnection connection, string functionName, int paramCount, Action<IntPtr, int, IntPtr> functionBody)
|
private static void CreateFunction (this SQLiteConnection connection, string functionName, int paramCount, Action<IntPtr, int, IntPtr> functionBody)
|
||||||
{
|
{
|
||||||
var result = CreateFunction (connection.Handle, functionName, paramCount, TextEncoding.UTF8, IntPtr.Zero, functionBody, null, null);
|
var result = CreateFunction (connection.Handle, functionName, paramCount, TextEncoding.UTF8, IntPtr.Zero, functionBody, null, null);
|
||||||
if (result != SQLite3.Result.OK)
|
if (result != SQLite3.Result.OK)
|
||||||
{
|
{
|
||||||
throw SQLiteException.New (result, "Can not create function");
|
throw SQLiteException.New (result, "Can not create function");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IntPtr [] ExtractArgs (int argc, IntPtr argv)
|
private static IntPtr [] ExtractArgs (int argc, IntPtr argv)
|
||||||
{
|
{
|
||||||
IntPtr [] args = new IntPtr [argc];
|
IntPtr [] args = new IntPtr [argc];
|
||||||
Marshal.Copy (argv, args, 0, argc);
|
Marshal.Copy (argv, args, 0, argc);
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
// SQLITE_API int sqlite3_create_function(
|
// SQLITE_API int sqlite3_create_function(
|
||||||
// sqlite3 *db,
|
// sqlite3 *db,
|
||||||
// const char *zFunctionName,
|
// const char *zFunctionName,
|
||||||
// int nArg,
|
// int nArg,
|
||||||
// int eTextRep,
|
// int eTextRep,
|
||||||
// void *pApp,
|
// void *pApp,
|
||||||
// void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
// void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
||||||
// void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
// void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
||||||
// void (*xFinal)(sqlite3_context*)
|
// void (*xFinal)(sqlite3_context*)
|
||||||
// );
|
// );
|
||||||
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_create_function")]
|
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_create_function")]
|
||||||
private static extern SQLite3.Result CreateFunction (
|
private static extern SQLite3.Result CreateFunction (
|
||||||
IntPtr db,
|
IntPtr db,
|
||||||
string zFunctionName,
|
string zFunctionName,
|
||||||
int nArg,
|
int nArg,
|
||||||
TextEncoding eTextRep,
|
TextEncoding eTextRep,
|
||||||
IntPtr pApp,
|
IntPtr pApp,
|
||||||
Action<IntPtr, int, IntPtr> xFunc,
|
Action<IntPtr, int, IntPtr> xFunc,
|
||||||
Action<IntPtr, int, IntPtr> xStep,
|
Action<IntPtr, int, IntPtr> xStep,
|
||||||
Action<IntPtr, int, IntPtr> xFinal
|
Action<IntPtr, int, IntPtr> xFinal
|
||||||
);
|
);
|
||||||
|
|
||||||
#region sqlite3_value
|
#region sqlite3_value
|
||||||
|
|
||||||
// SQLITE_API int sqlite3_value_type(sqlite3_value*);
|
|
||||||
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_value_type")]
|
|
||||||
private static extern SQLite3.ColType ValueType (IntPtr value);
|
|
||||||
|
|
||||||
// SQLITE_API double sqlite3_value_double(sqlite3_value*);
|
|
||||||
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_value_double")]
|
|
||||||
private static extern double ValueDouble (IntPtr value);
|
|
||||||
|
|
||||||
#endregion
|
// SQLITE_API int sqlite3_value_type(sqlite3_value*);
|
||||||
|
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_value_type")]
|
||||||
|
private static extern SQLite3.ColType ValueType (IntPtr value);
|
||||||
|
|
||||||
#region sqlite3_context
|
// SQLITE_API double sqlite3_value_double(sqlite3_value*);
|
||||||
|
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_value_double")]
|
||||||
// SQLITE_API void sqlite3_result_null(sqlite3_context*);
|
private static extern double ValueDouble (IntPtr value);
|
||||||
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_result_null")]
|
|
||||||
private static extern void ResultNull (IntPtr ctx);
|
|
||||||
|
|
||||||
// SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
|
|
||||||
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_result_double")]
|
|
||||||
private static extern void ResultDouble (IntPtr ctx, double value);
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// These constant define integer codes that represent the various text encodings supported by SQLite.
|
#region sqlite3_context
|
||||||
private enum TextEncoding
|
|
||||||
{
|
// SQLITE_API void sqlite3_result_null(sqlite3_context*);
|
||||||
UTF8 = 1,
|
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_result_null")]
|
||||||
UTF16LE = 2,
|
private static extern void ResultNull (IntPtr ctx);
|
||||||
UTF16BE = 3,
|
|
||||||
UTF16 = 4, /* Use native byte order */
|
// SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
|
||||||
Any = 5, /* sqlite3_create_function only */
|
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_result_double")]
|
||||||
UTF16Aligned = 8, /* sqlite3_create_collation only */
|
private static extern void ResultDouble (IntPtr ctx, double value);
|
||||||
}
|
|
||||||
}
|
#endregion
|
||||||
|
|
||||||
|
// These constant define integer codes that represent the various text encodings supported by SQLite.
|
||||||
|
private enum TextEncoding
|
||||||
|
{
|
||||||
|
UTF8 = 1,
|
||||||
|
UTF16LE = 2,
|
||||||
|
UTF16BE = 3,
|
||||||
|
UTF16 = 4, /* Use native byte order */
|
||||||
|
Any = 5, /* sqlite3_create_function only */
|
||||||
|
UTF16Aligned = 8, /* sqlite3_create_collation only */
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue