From 1d8cb29dda5eee7ea6220063aa8854eba4aa8501 Mon Sep 17 00:00:00 2001 From: Ivan Nikitin Date: Tue, 20 May 2014 20:54:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=8A=D1=8F=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BE=D0=B1=D1=81=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D1=85=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20SQLite3=20(=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B1=D0=B5=D0=BB=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=82=D0=B0=D0=B1=D1=83=D0=BB=D1=8F=D1=86=D0=B8?= =?UTF-8?q?=D0=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Shared/SQLite3Extensions.Distance.cs | 84 +++++------ SQLiteExtensions/Shared/SQLite3Extensions.cs | 134 +++++++++--------- 2 files changed, 109 insertions(+), 109 deletions(-) diff --git a/SQLiteExtensions/Shared/SQLite3Extensions.Distance.cs b/SQLiteExtensions/Shared/SQLite3Extensions.Distance.cs index 796830c..82c5ae4 100644 --- a/SQLiteExtensions/Shared/SQLite3Extensions.Distance.cs +++ b/SQLiteExtensions/Shared/SQLite3Extensions.Distance.cs @@ -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))] + // https://bugzilla.novell.com/show_bug.cgi?id=576775 + [MonoTouch.MonoPInvokeCallback (typeof (Action))] #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); + } + } + } } \ No newline at end of file diff --git a/SQLiteExtensions/Shared/SQLite3Extensions.cs b/SQLiteExtensions/Shared/SQLite3Extensions.cs index 9241489..90ba181 100644 --- a/SQLiteExtensions/Shared/SQLite3Extensions.cs +++ b/SQLiteExtensions/Shared/SQLite3Extensions.cs @@ -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 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 xFunc, - Action xStep, - Action 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 xFunc, + Action xStep, + Action 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 */ + } + } } \ No newline at end of file