iPhone ve T-SQL ile oynaşmalar

Published 21.09.2010 by dogan

Hafta sonu bir iphone projesi için yakınımdaki kayıtlı işyerlerini sorgulayabilmem gerekiyordu. Ben de oturup MS SQL Server için sadece T-SQL kullanarak bir user-defined function yazdım :) Tüm bu işlemleri veritabanının içinden yapabilmek çok eğlenceli :) Tam bir ego tatmini oldu :)

  

 

CREATE FUNCTION dbo.GetNearStores (
 @Latitude FLOAT,
    @Longitude FLOAT,
    @MaximumDistance FLOAT
)
RETURNS
 @ResultTable table (
  [StoreLocationId] [uniqueidentifier],
        [StoreLocationName] [nvarchar] (50),
  [StoreLocationLatitude] [FLOAT],
        [StoreLocationLongitude] [FLOAT],
  [StoreId] [uniqueidentifier],
        [StoreName] [nvarchar] (1024),
  [StoreShortDescription] [nvarchar] (1024),
        [StoreDescription] [nvarchar] (1024),
  [Distance] [FLOAT]
 )
AS
BEGIN
    DECLARE @StoreLocationId UNIQUEIDENTIFIER
    DECLARE @StoreLocationName NVARCHAR(50)
    DECLARE @StoreLocationLatitude FLOAT
    DECLARE @StoreLocationLongitude FLOAT
    DECLARE @StoreId UNIQUEIDENTIFIER
    DECLARE @StoreName NVARCHAR(1024)
    DECLARE @StoreShortDescription NVARCHAR(1024)
    DECLARE @StoreDescription NVARCHAR(1024)

 DECLARE cur CURSOR
    FOR
    SELECT
    StoreLocationId,
    StoreLocationName,
        StoreLocationLatitude,
        StoreLocationLogitude,
        StoreId,
        StoreName,
        StoreShortDescription,
        StoreDescription
    FROM VWStoreLocations
   
    OPEN cur
   
   
    FETCH NEXT FROM cur
    INTO @StoreLocationId,
      @StoreLocationName,
         @StoreLocationLatitude,
         @StoreLocationLongitude,
         @StoreId,
         @StoreName,
         @StoreShortDescription,
         @StoreDescription

 WHILE @@FETCH_STATUS = 0
    BEGIN
          DECLARE @latitudeA FLOAT
          DECLARE @longitudeA FLOAT
          DECLARE @latitudeB FLOAT
          DECLARE @longitudeB FLOAT
         
          SELECT @latitudeA = @latitude
          SELECT @longitudeA = @longitude
          SELECT @latitudeB = @StoreLocationLatitude;
          SELECT @longitudeB = @StoreLocationLongitude;
         
          DECLARE @pi80 FLOAT
          SELECT @pi80 = PI() / 180
         
          DECLARE @lat1 FLOAT
          SELECT @lat1 = @latitudeA * @pi80

          DECLARE @lng1 FLOAT
          SELECT @lng1 = @longitudeA * @pi80
         
          DECLARE @lat2 FLOAT
          SELECT @lat2 = @latitudeB * @pi80
         
          DECLARE @lng2 FLOAT
          SELECT @lng2 = @longitudeB * @pi80
         
          DECLARE @r FLOAT
          SELECT @r = 6372.797
         
          DECLARE @dlat FLOAT
          SELECT @dlat = @lat2 - @lat1
         
          DECLARE @dlng FLOAT
          SELECT @dlng = @lng2 - @lng1
         
          DECLARE @a FLOAT
          SELECT @a = SIN(@dlat / 2) * SIN(@dlat / 2) + COS(@lat1) * COS(@lat2) * SIN(@dlng / 2) * SIN(@dlng / 2)

          DECLARE @c FLOAT
          SELECT @c = 2 * ATN2(SQRT(@a), SQRT(1 - @a))

          DECLARE @distance FLOAT
          SELECT @distance = @r * @c
         
          IF (@distance < @MaximumDistance)
          BEGIN
     INSERT INTO @ResultTable
             (StoreLocationId,
                 StoreLocationName,
                 StoreLocationLatitude,
                 StoreLocationLongitude,
                 StoreId,
                 StoreName,
                 StoreShortDescription,
                 StoreDescription,
                 Distance)
           VALUES
             (@StoreLocationId,
                 @StoreLocationName,
                 @StoreLocationLatitude,
                 @StoreLocationLongitude,
                 @StoreId,
                 @StoreName,
                 @StoreShortDescription,
                 @StoreDescription,
                 @distance)
          END
         
          FETCH NEXT FROM cur
          INTO @StoreLocationId,
               @StoreLocationName,
               @StoreLocationLatitude,
               @StoreLocationLongitude,
               @StoreId,
               @StoreName,
               @StoreShortDescription,
               @StoreDescription
    END

 CLOSE cur
    DEALLOCATE cur

  RETURN;
END

 

Sunucudan sql cümlesini çekmek için de asenkron güvenli bir yöntem kullanıyorum, 10 saniye timeout süreli bir asenkron bağlantı açıp sorgulamayı GET parametreleriyle yapıyorum ama POST işlemi de veri güvenliği ve diğer bazı sebepler için tabiki de kullanılabilir.

 

 

NSString *myRequestString = @"";

NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];

NSString *url_ = [NSString stringWithFormat:@"%@?op=getstorelocations&dist=%d&lat=%.6f&lon=%.6f", url, distance, lat, lon];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url_]];

[request setHTTPMethod: @"GET"];

[request setHTTPBody: myRequestData];

[request setTimeoutInterval:10.0];

if ([[NSURLConnection alloc] initWithRequest:request delegate:self])

{

    receivedData = [[NSMutableData data] retain];

}

 

 

Sevgiler.

 

 


Comments (1) -

Hermes Handbags
Hermes Handbags
18.08.2011 13:19:12 #


http://www.louisvuitton-outlet.me           Louis Vuitton Outlet
http://www.burberry4uk.com                  Burberry Scarves
http://www.chanel-bags.biz                  Chanel Sunglasses
http://www.coach-factorystores.com          Coach Outlet
http://www.store-coachoutlet.org            Coach Purses
http://www.bagsstyle.net                   Cheap Handbags
http://www.louis-vuitton.me                 Louis Vuitton Bags
http://www.gucci4bags.org                   Gucci Sunglasses
http://www.coachfactoryoutlet-online.org    Coach Outlet Store
http://www.coach4.net                       Coach Outlet
http://www.coach-factoryoutletstore.org     Coach Outlet Online
http://www.chanels.me                       Chanel[/url]
http://www.coach-shopping.org               Coach Outlet Sale
http://www.prada.bz                         Prada Handbags
http://www.chanel-purses.org                Chanel Handbags
http://www.louisvuitton-shop.net            Louis Vuitton Outlet Online
http://www.coachpurse-outlet.org            Coach Outlet Store
http://www.coach-purses.org                 Coach Sale
http://www.coach-outlet.net                 Coach Outlet Online
http://www.gucci-outlet.me                  Gucci Outlet
http://www.coachpurse-outlet.net            Coach Online Outlet
http://www.coach-onlinestore.org            Coach Factory
http://www.coachoutlet-onlinestores.net     Coach Outlet Store
http://www.coachfactoryoutlet-store.org     Coach Outlet Store Online
http://www.louisvuitton4uk.com              Louis Vuitton
http://www.outlet-coachpurses.com           Coach Bags
http://www.louisvuitton-handbags.me         Louis Vuitton Handbags
http://www.coachs.me                        Coach Factory
http://www.chanel-bags.me                   Chanel Handbags
http://www.burberry.tc                      Burberry Scarf
http://www.hermes-outlet.me                 Hermes
http://www.coach-factoryonline.com          Coach Outlet Stores
http://www.bag-words.com                    Designer Handbags
http://www.louis-vuitton.ca                 Louis Vuitton Outlet Online
http://www.gucci-online.net                 Gucci Shoes
http://www.hermes4uk.com                    Hermes Birkin

Add comment

  Country flag


  • Comment
  • Preview
Loading