Объявление собственных функций в SQLite3 (замена пробелов на табуляции)

This commit is contained in:
Ivan Nikitin 2014-05-20 20:54:16 +04:00
parent 3970f394b0
commit 1d8cb29dda
2 changed files with 109 additions and 109 deletions

View File

@ -4,49 +4,49 @@ using SQLite;
namespace Touchin.SQLiteExtensions
{
public static partial class SQLite3Extensions
{
public static void CreateDistanceFunction (this SQLiteConnection connection)
{
connection.CreateFunction ("DISTANCE", 4, DistanceBody);
}
private static double Distance (double latitude1, double longitude1, double latitude2, double longitude2)
{
const double earthRadius = 6378.1;
double lat1 = latitude1 * Math.PI / 180;
double lon1 = longitude1 * Math.PI / 180;
double lat2 = latitude2 * 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));
}
public static partial class SQLite3Extensions
{
public static void CreateDistanceFunction (this SQLiteConnection connection)
{
connection.CreateFunction ("DISTANCE", 4, DistanceBody);
}
private static double Distance (double latitude1, double longitude1, double latitude2, double longitude2)
{
const double earthRadius = 6378.1;
double lat1 = latitude1 * Math.PI / 180;
double lon1 = longitude1 * Math.PI / 180;
double lat2 = latitude2 * 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));
}
#if __IOS__
// https://bugzilla.novell.com/show_bug.cgi?id=576775
[MonoTouch.MonoPInvokeCallback (typeof (Action<IntPtr, int, IntPtr>))]
// https://bugzilla.novell.com/show_bug.cgi?id=576775
[MonoTouch.MonoPInvokeCallback (typeof (Action<IntPtr, int, IntPtr>))]
#endif
private static void DistanceBody (IntPtr ctx, int argc, IntPtr argv)
{
IntPtr[] args = ExtractArgs (argc, argv);
if (ValueType (args [0]) != SQLite3.ColType.Float ||
ValueType (args [1]) != SQLite3.ColType.Float ||
ValueType (args [2]) != SQLite3.ColType.Float ||
ValueType (args [3]) != SQLite3.ColType.Float)
{
ResultNull (ctx);
}
else
{
double latitude1 = ValueDouble (args [0]);
double longitude1 = ValueDouble (args [1]);
double latitude2 = ValueDouble (args [2]);
double longitude2 = ValueDouble (args [3]);
double result = Distance (latitude1, longitude1, latitude2, longitude2);
ResultDouble (ctx, result);
}
}
}
private static void DistanceBody (IntPtr ctx, int argc, IntPtr argv)
{
IntPtr[] args = ExtractArgs (argc, argv);
if (ValueType (args [0]) != SQLite3.ColType.Float ||
ValueType (args [1]) != SQLite3.ColType.Float ||
ValueType (args [2]) != SQLite3.ColType.Float ||
ValueType (args [3]) != SQLite3.ColType.Float)
{
ResultNull (ctx);
}
else
{
double latitude1 = ValueDouble (args [0]);
double longitude1 = ValueDouble (args [1]);
double latitude2 = ValueDouble (args [2]);
double longitude2 = ValueDouble (args [3]);
double result = Distance (latitude1, longitude1, latitude2, longitude2);
ResultDouble (ctx, result);
}
}
}
}

View File

@ -4,79 +4,79 @@ using SQLite;
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)
{
{
var result = CreateFunction (connection.Handle, functionName, paramCount, TextEncoding.UTF8, IntPtr.Zero, functionBody, null, null);
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New (result, "Can not create function");
}
}
if (result != SQLite3.Result.OK)
{
throw SQLiteException.New (result, "Can not create function");
}
}
private static IntPtr [] ExtractArgs (int argc, IntPtr argv)
{
IntPtr [] args = new IntPtr [argc];
Marshal.Copy (argv, args, 0, argc);
return args;
}
private static IntPtr [] ExtractArgs (int argc, IntPtr argv)
{
IntPtr [] args = new IntPtr [argc];
Marshal.Copy (argv, args, 0, argc);
return args;
}
// SQLITE_API int sqlite3_create_function(
// sqlite3 *db,
// const char *zFunctionName,
// int nArg,
// int eTextRep,
// void *pApp,
// void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
// void (*xStep)(sqlite3_context*,int,sqlite3_value**),
// void (*xFinal)(sqlite3_context*)
// );
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_create_function")]
private static extern SQLite3.Result CreateFunction (
IntPtr db,
string zFunctionName,
int nArg,
TextEncoding eTextRep,
IntPtr pApp,
Action<IntPtr, int, IntPtr> xFunc,
Action<IntPtr, int, IntPtr> xStep,
Action<IntPtr, int, IntPtr> xFinal
);
// SQLITE_API int sqlite3_create_function(
// sqlite3 *db,
// const char *zFunctionName,
// int nArg,
// int eTextRep,
// void *pApp,
// void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
// void (*xStep)(sqlite3_context*,int,sqlite3_value**),
// void (*xFinal)(sqlite3_context*)
// );
[DllImport("sqlite3", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_create_function")]
private static extern SQLite3.Result CreateFunction (
IntPtr db,
string zFunctionName,
int nArg,
TextEncoding eTextRep,
IntPtr pApp,
Action<IntPtr, int, IntPtr> xFunc,
Action<IntPtr, int, IntPtr> xStep,
Action<IntPtr, int, IntPtr> xFinal
);
#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);
#region sqlite3_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 void sqlite3_result_null(sqlite3_context*);
[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);
// 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
// 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 */
}
}
#endregion
#region sqlite3_context
// SQLITE_API void sqlite3_result_null(sqlite3_context*);
[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
// 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 */
}
}
}