根据一些输入参数(前缀/后缀)重命名其输出参数的函数

时间:2016-08-09 16:13:12

标签: sql postgresql stored-procedures plpgsql

我希望有一个函数可以根据一些输入参数重命名它的输出参数。

我尝试了一些解决方案来自动执行此任务(主要集中在changing return type for function to polymorphic record variable),但这对我来说都不起作用。我会很感激任何吸烟。

这个问题让我发疯,因为我目前的设置让我写了大量不必要的(?)代码。 我使用这种函数来格式化视图中显示的日期,因此它应该是/ dynamic /.

我目前的设计是这样的:

CREATE OR REPLACE FUNCTION name_of(
    IN in_date date, 
    OUT mnth_date text,
    OUT qrt_date text
) AS
$func$
    BEGIN
        mnth_date =to_char(in_date ,'YYYY"M"MM');       
        qrt_date =to_char(in_date ,'YYYY"Q"Q'); 
    END
$func$ LANGUAGE plpgsql;

有什么结果:

select (name_of('2014-04-30')).*


________________________
|mnth_date  |qrt_date  |
------------------------
|2014M04    |2014Q2    |

我的目标设置,结果与此类似:

    select 
        A.*, B.*
     from
        name_of('2014-04-30','foo_prefix' ) A,
        name_of('2014-05-13','creation' ) B;

__________________________________________________________________________________________________
|foo_prefix_mnth_date   |foo_prefix_qrt_date    |creation_mnth_date     |creation_qrt_date      |
--------------------------------------------------------------------------------------------------
|2014M04                |2014Q2                 |2014M05                |2014Q2                 |

目前我大量使用别名,所以我的代码看起来类似于:

    select 
        A.mnth_date foo_prefix_mnth_date,
        A.qrt_date foo_prefix_qrt_date,
        B.mnth_date creation_mnth_date,
        B.qrt_date creation_qrt_date
     from
        name_of('2014-04-30') A,
        name_of('2014-05-13') B;

1 个答案:

答案 0 :(得分:0)

使用列aliases

#import "DetailTableViewController.h"
#import "Categories.h"
#import "MainTableViewController.h"

#define kUrl @"http://yoga.lifehealthinfo.com/api/yoga_list/all"

@interface DetailTableViewController ()

@end

@implementation DetailTableViewController
@synthesize categoryArray, currentCategory;
- (void)viewDidLoad {
    [super viewDidLoad];

 [self extractData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return categoryArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"maincell" forIndexPath:indexPath];
    Categories * categoryObject;
    categoryObject = [categoryArray objectAtIndex:indexPath.row];

    cell.textLabel.text = categoryObject.ycategoryName;

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Configure the cell...


    return cell;
}

-(void) getCategory:(id)yougaObject{

    currentCategory = yougaObject;

}

-(void) extractData{
    NSURL * curl = [NSURL URLWithString:kUrl];
    NSData * cdata = [NSData dataWithContentsOfURL:curl];
    NSDictionary *myDict = [NSJSONSerialization JSONObjectWithData:cdata options:kNilOptions error:nil];
    NSDictionary *cdataJson = [myDict objectForKey:@"data"];
    NSArray *allImagesJson = [cdataJson objectForKey:@"Images"];
    categoryArray = [[NSMutableArray alloc]init];

    for(int i = 0; i<allImagesJson.count; i++)
    {
        NSDictionary *aCategoryImages = [allImagesJson objectAtIndex:i];

            NSString * cId = [aCategoryImages objectForKey:@"id"];
            NSString * cTitle = [aCategoryImages objectForKey:@"tite"];
            NSString * cDescription = [aCategoryImages objectForKey:@"description"];
            NSString * cImage = [aCategoryImages objectForKey:@"image"];
            NSString * cDate = [aCategoryImages objectForKey:@"date_created"];
            NSString * ycName = [aCategoryImages objectForKey:@"category_name"];
            NSString * ycId = [aCategoryImages objectForKey:@"category_id"];

            [categoryArray addObject:[[Categories alloc]initWithCategoryId: cId andCategoryTitle:cTitle andCategoryDescription:cDescription andCategoryImage:cImage andCategoryDate:cDate andYcategoryId:ycId andYCategoryName:ycName]];

        }
 [self.tableView reloadData];

}

@end

关键字SELECT name_of('2014-04-30') AS foo_prefix_mnth_date, name_of('2014-05-13') AS creation_mnth_date, name_of('2014-06-20') AS access_mnth_date; 是可选的,因此这也有效:

AS

但建议使用它以提高可读性