我在macOS 10.12.6上使用PostgreSQL版本10,并希望在查询中使用自定义plpgsql函数,HugSQL可以访问该函数。以下ansatz正常工作:
-- :name do-something! :! :1
CREATE OR REPLACE FUNCTION helper()
... (function body of helper)
LANGUAGE plpgsql;
INSERT INTO SomeTable (someColumn) VALUES (helper());
这是有效的,因为HugSQL允许我编写多行SQL语句,我可以包含helper()
的函数定义。
但是,我想知道这样做是否真的有效,因为现在每次运行查询do-something!
时我都会重新定义函数。我试图将函数定义放在输入文件的顶部,但它只会导致编译器异常。
问题:最好的方法是什么?
答案 0 :(得分:2)
我找到了解决方案。这是为了方便HugSQL的其他用户。
可以在设置表的迁移文件中定义该函数(为此,我使用migratus)。函数定义的范围与表的范围相同,因此如果迁移读取
function download(){
$export_type = $this->request->post['export_type'];
if($export_type=='p'){
$customer_id = $this->customer->getId();
$this->load->model('account/wishlist');
$this->load->model('catalog/product');
$noofproducts = $this->model_account_wishlist->getTotalWishlist();
$data = array();
ob_start();
$product_ids = $this->model_account_wishlist->getProductIdFromWishList();
$products = $this->model_account_wishlist->getProduct($product_ids);
$filename = "data_export_" . date("Y-m-d") . ".csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
header("Pragma: no-cache");
header("Expires: 0");
$result = $this->ExportCSVFile($products) ;
$xlsData = ob_get_contents();
$length = ob_get_length();
header('Content-Disposition: attachment; filename='.$filename);
$response = array(
'op' => 'ok',
'file' => "data:text/csv;base64,".base64_encode($xlsData),
'filename' => $filename
);
ob_get_clean();
ob_end_flush();
die(json_encode($response));
}
}
function ExportCSVFile($records) {
// create a file pointer connected to the output stream
$fh = fopen( 'php://output', 'w' );
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// output the column headings
fputcsv($fh, array_keys($row));
$heading = true;
}
// loop over the rows, outputting them
fputcsv($fh, array_values($row));
}
fclose($fh);
}
然后可以在上面发布的查询中使用函数<script type="text/javascript">
function getNotifications() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> <div id="export_import_loading"><img src="view/image/export-import/loading.gif" /><?php echo $text_loading_notifications; ?></div>');
setTimeout(
function(){
$.ajax({
type: 'GET',
url: 'index.php?route=account/wishlist/getNotifications&token=<?php echo $token; ?>',
dataType: 'json',
success: function(json) {
if (json['error']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['error']+' <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
} else if (json['message']) {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+json['message']);
} else {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_no_news; ?>');
}
},
failure: function(){
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
},
error: function() {
$('#export_import_notification').html('<i class="fa fa-info-circle"></i><button type="button" class="close" data-dismiss="alert">×</button> '+'<?php echo $error_notifications; ?> <span style="cursor:pointer;font-weight:bold;text-decoration:underline;float:right;" onclick="getNotifications();"><?php echo $text_retry; ?></span>');
}
});
},
500
);
}
,而无需在使用之前(重新)定义它:
CREATE OR REPLACE FUNCTION helper()
... (function body of helper)
LANGUAGE plpqsql;
CREATE TABLE IF NOT EXISTS SomeTable
(...row definitions);