http://blog.csdn.net/cdnight/article/details/18082255

这里是一份经过再三调试测试而成功的postgres数据库单表crud存储过程,请注意,对于多结果的返回方式,请查看 getPageByCondition的书写方式,用的是refcursor,返回一个cursor,同时可以返回其他out,inout参数,但是 refcursor必须在事务中调用,所以java端的调用过程需要注意,好吧,我同时放出一份dal样板,大家可以直接copy来用。

  1 /******************************************************************
  2 * 表名:test3
  3 * Made by 码农下的天桥
  4 ******************************************************************/
  5 --use MYDB;--你可以指定自己的数据库
  6 /******************************************************************
  7 ****************************各种常用查询***************************
  8 ******************************************************************/
  9 ------------------------------------
 10 --用途:复杂形式的查询语句,用于查询分页数据。
 11 --这个是泛用型的,假如你要根据用户输入去查询,那么最好不要用这个了,
 12 --以免出现sql注入。
 13 --参数说明:
 14 ---_offset int 需要取的记录的开始位置
 15 ---_limit int 需要获取记录的总条数,针对分页而言,就是分页的pagesize。
 16 ---_columns varchar(800) 需要获取的字段
 17 ---_where varchar(800) 需要过滤的条件譬如: where id<10 可以带where,不过建议不要带。
 18 ---_orderby varchar(800) 需要进行排序的提交,譬如:order by id
 19 ---_totalCount int 返回总共记录条数。
 20 ---_totalPages int 返回总共页数。
 21 ------------------------------------
 22 create or replace function test3_getListByCondition(
 23 INOUT pageindex INT,
 24 INOUT pagesize INT,
 25 IN _columns VARCHAR(800),
 26 IN _where VARCHAR(800),
 27 IN _orderby VARCHAR(800),
 28 out _totalCount INT,
 29 out _totalPages INT)
 30 returns SETOF record
 31 AS
 32 $$
 33 DECLARE condition_columns VARCHAR(800);
 34 DECLARE condition_where varchar(800);
 35 DECLARE condition_orderby VARCHAR(800);
 36 DECLARE _dymatic_sql VARCHAR(1600);
 37 DECLARE _beginNO INT;
 38 DECLARE _dynamic_getCount varchar(1600);
 39 DECLARE _theOffset INT;
 40 DECLARE _tmpInt1 INT;
 41 BEGIN
 42 condition_where:=ltrim(rtrim(COALESCE(_where,'')));
 43 condition_orderby:=ltrim(rtrim(COALESCE(_orderby,'order by t3id')));
 44 condition_columns:=ltrim(rtrim(COALESCE(_columns,'*')));
 45 --分析传过来的参数,构造动态sql语句。
 46 IF "character_length"(condition_where)>0 THEN
 47 IF strpos(condition_where, 'where ')!=1 THEN
 48 condition_where:='where ' || condition_where;
 49 END IF;
 50 END IF;
 51 --order by 语句构造
 52 IF "character_length"(condition_orderby)>0 THEN
 53 IF strpos(condition_orderby, 'order ')!=1 THEN
 54 condition_orderby:='order by '||condition_orderby;
 55 END IF;
 56 END IF;
 57 
 58 --判断pageindex是否合法及pagesize是否合法
 59 IF pageindex<1 THEN
 60 pageindex:=1;
 61 END IF;
 62 IF pagesize<1 THEN
 63 pagesize:=20;
 64 END IF;
 65 
 66 _dynamic_getCount:='select count(*) from test3 '||condition_where|| ' ' ;
 67 EXECUTE _dynamic_getCount INTO _totalCount;
 68 
 69 IF _totalCount<1 THEN
 70 pageindex:=1;
 71 RETURN;
 72 END IF;
 73 --计算总共页数
 74 _tmpInt1:=_totalCount%pagesize;
 75 IF _tmpInt1=0 THEN
 76 _totalPages:=_totalCount / pagesize;
 77 ELSE
 78 _totalPages:=(_totalCount-_tmpInt1)/pagesize+1;
 79 END IF;
 80 
 81 IF _totalPages < pageindex then
 82 pageindex:=_totalPages;
 83 END IF;
 84 
 85 _theOffset:=(pageindex-1) * pagesize+1;
 86 
 87 _dymatic_sql:='select '||condition_columns||' from test3 '||condition_where||' '||condition_orderby||' limit '||pagesize||' '|| ' offset '||_theOffset||' ';
 88 --raise info '动态构造语句为:%',_dymatic_sql;
 89 return query EXECUTE  _dymatic_sql;
 90 END;
 91 $$ language plpgsql VOLATILE;
 92 
 93 
 94 
 95 ------------------------------------
 96 --用途:复杂形式的查询语句,用于查询多条记录数据。
 97 --这个是泛用型的,假如你要根据用户输入去查询,那么最好不要用这个了,
 98 --以免出现sql注入。
 99 --参数说明:
100 ---_offset int 需要取的记录的开始位置
101 ---_limit int 需要获取记录的总条数,针对分页而言,就是分页的pagesize。
102 ---_columns varchar(800) 需要获取的字段
103 ---_where varchar(800) 需要过滤的条件譬如: where id<10 可以带where,不过建议不要带。
104 ---_orderby varchar(800) 需要进行排序的提交,譬如:order by id
105 ---_totalCount int 返回总共记录条数。
106 ------------------------------------
107 create or replace function test3_getPageByCondition(
108 INOUT pageindex INT,
109 INOUT pagesize INT,
110 IN _columns VARCHAR(800),
111 IN _where VARCHAR(800),
112 IN _orderby VARCHAR(800),
113 out _totalCount INT,
114 out _totalPages INT,
115 out _refcursor refcursor
116 )
117 returns SETOF record
118 AS
119 $$
120 DECLARE condition_columns VARCHAR(800);
121 DECLARE condition_where varchar(800);
122 DECLARE condition_orderby VARCHAR(800);
123 DECLARE _dymatic_sql VARCHAR(1600);
124 DECLARE _beginNO INT;
125 DECLARE _dynamic_getCount varchar(1600);
126 DECLARE _theOffset INT;
127 DECLARE _tmpInt1 INT;
128 BEGIN
129 condition_where:=ltrim(rtrim(COALESCE(_where,'')));
130 condition_orderby:=ltrim(rtrim(COALESCE(_orderby,'order by t3id')));
131 condition_columns:=ltrim(rtrim(COALESCE(_columns,'*')));
132 --分析传过来的参数,构造动态sql语句。
133 IF "character_length"(condition_where)>0 THEN
134 IF strpos(condition_where, 'where ')!=1 THEN
135 condition_where:='where ' || condition_where;
136 END IF;
137 END IF;
138 --order by 语句构造
139 IF "character_length"(condition_orderby)>0 THEN
140 IF strpos(condition_orderby, 'order ')!=1 THEN
141 condition_orderby:='order by '||condition_orderby;
142 END IF;
143 END IF;
144 
145 --判断pageindex是否合法及pagesize是否合法
146 IF pageindex<1 THEN
147 pageindex:=1;
148 END IF;
149 IF pagesize<1 THEN
150 pagesize:=20;
151 END IF;
152 
153 _dynamic_getCount:='select count(*) from test3 '||condition_where|| ' ' ;
154 EXECUTE _dynamic_getCount INTO _totalCount;
155 
156 IF _totalCount<1 THEN
157 pageindex:=1;
158 RETURN;
159 END IF;
160 --计算总共页数
161 _tmpInt1:=_totalCount%pagesize;
162 IF _tmpInt1=0 THEN
163 _totalPages:=_totalCount / pagesize;
164 ELSE
165 _totalPages:=(_totalCount-_tmpInt1)/pagesize+1;
166 END IF;
167 
168 IF _totalPages < pageindex then
169 pageindex:=_totalPages;
170 END IF;
171 
172 _theOffset:=(pageindex-1) * pagesize+1;
173 
174 _dymatic_sql:='select '||condition_columns||' from test3 '||condition_where||' '||condition_orderby||' limit '||pagesize||' '|| ' offset '||_theOffset||' ';
175 --raise info '动态构造语句为:%',_dymatic_sql;
176 open _refcursor for EXECUTE  _dymatic_sql;
177 RETURN NEXT;
178 END;
179 $$ language plpgsql VOLATILE;
180 
181 ------------------------------------
182 --用途:获取其中一条记录
183 ------------------------------------
184 create or replace function test3_getRecord(in _id integer)
185 returns SETOF test3
186 AS
187 $$
188 BEGIN
189 return query select * from test3 where t3id=_id LIMIT 1 OFFSET 0;
190 END;
191 $$ LANGUAGE plpgsql VOLATILE;
192 
193 
194 ------------------------------------
195 --用途:复杂形式的查询语句,用于查询前面第几条记录,这个就相当好了
196 --这个是泛用型的,假如你要根据用户输入去查询,那么最好不要用这个了,
197 --以免出现sql注入。
198 --参数说明:
199 ---_topN int 需要取的topN条记录。
200 ---_columns varchar(800) 需要获取的字段
201 ---_where varchar(800) 需要过滤的条件譬如: where id<10 可以带where,不过建议不要带。
202 ---_orderby varchar(800) 需要进行排序的提交,譬如:order by id
203 ------------------------------------
204 create or replace function test3_getTopNbyCondition(IN _topN int,IN _columns VARCHAR(800),IN _where VARCHAR(800),IN _orderby VARCHAR(800))
205 returns SETOF test3
206 AS
207 $$
208 DECLARE condition_columns VARCHAR(800);
209 DECLARE condition_where varchar(800);
210 DECLARE condition_orderby VARCHAR(800);
211 DECLARE _dymatic_sql VARCHAR(1600);
212 BEGIN
213 condition_where:=ltrim(rtrim(COALESCE(_where,'')));
214 condition_orderby:=ltrim(rtrim(COALESCE(_orderby,'order by t3id')));
215 condition_columns:=ltrim(rtrim(COALESCE(_columns,'*')));
216 
217 --分析传过来的参数,构造动态sql语句。
218 IF "character_length"(condition_where)>0 THEN
219 IF strpos(condition_where, 'where ')!=1 THEN
220 condition_where:='where ' || condition_where;
221 END IF;
222 END IF;
223 --order by 语句构造
224 IF "character_length"(condition_orderby)>0 THEN
225 IF strpos(condition_orderby, 'order ')!=1 THEN
226 condition_orderby:='order by '||condition_orderby;
227 END IF;
228 END IF;
229 _dymatic_sql:='select '||condition_columns||' from test2 '||condition_where||' '||condition_orderby||' limit '||CAST(_topN as VARCHAR)|| ' offset 0 ';
230 --raise info '动态构造语句为:%',_dymatic_sql;
231 return query EXECUTE  _dymatic_sql;
232 END;
233 $$ language plpgsql VOLATILE;
234 
235 
236 /******************************************************************
237 *****************************记录删除******************************
238 ******************************************************************/
239 ------------------------------------
240 --用途:删除多条记录
241 ------------------------------------
242 create or replace function test3_DeleteList(in ids VARCHAR(800),out status boolean,out msg VARCHAR(200))
243 returns record
244 AS
245 $$
246 DECLARE _arr_ids int[];
247 DECLARE _str_ids "text";
248 DECLARE _str_sql VARCHAR(1600);
249 DECLARE _effects int;
250 BEGIN
251 
252 IF "character_length"(ids)<1 THEN
253 status:=false;
254 msg:='没有指定需要删除的数据!';
255 return;
256 end if;
257 _arr_ids:=tools_str2intarray(ids, ',');
258 _str_ids:=tools_stringify(_arr_ids,',');
259 --pkey为主键,自增的整数, <@ 表示判断pkey是不是在数组里面。是不是很方便?
260 /*动态构造执行*/
261 --_str_sql:='DELETE FROM test3 where t3id in ('||_str_ids||') ;';
262 --EXECUTE _str_sql;
263 /*直接执行*/
264 delete from test3 where t3id =ANY( _arr_ids);
265 GET DIAGNOSTICS _effects = ROW_COUNT;
266 IF _effects>0 THEN
267 status:=true;
268 msg:='成功删除'||_effects||'条记录!';
269 ELSE
270 status:=false;
271 msg:='没有删除任何记录!';
272 end if;
273 
274 END
275 $$ LANGUAGE plpgsql VOLATILE;
276 
277 
278 /******************************************************************
279 ****************************添加及编辑*****************************
280 ******************************************************************/
281 
282 ------------------------------------
283 --用途:增加一条记录
284 ------------------------------------
285 
286 create or replace function test3_Insert(
287                             in __t3name  varchar(400) ,
288                             in __t_birthday  date ,
289                             in __myage  smallint ,
290                             in __isadmin  boolean ,
291                             in __myintro  text ,
292                             in __price  float ,
293     out __t3id integer,
294 out _status boolean,
295 out _msg varchar(200))
296 returns record AS $$
297 BEGIN
298                                                     
299 Insert into test3
300 (
301 "t3name","t_birthday","myage","isadmin","myintro","price"
302 )
303 values(
304 __t3name,__t_birthday,__myage,__isadmin,__myintro,__price
305 );
306 /*判断添加记录是否成功。*/
307 if FOUND  then
308 _status:=true;
309 _msg:='成功添加记录.';
310 __t3id:=currval(pg_get_serial_sequence('test3', 't3id'));
311 else
312 _status:=false;
313 _msg:='无法添加记录!';
314 end if;
315 end;
316 $$ LANGUAGE plpgsql VOLATILE;
317 
318 ------------------------------------
319 --用途:修改一条记录
320 ------------------------------------
321 create or replace function test3_Update(
322                             in __t3name  varchar(400) ,
323                             in __t_birthday  date ,
324                             in __myage  smallint ,
325                             in __isadmin  boolean ,
326                             in __myintro  text ,
327                             in __price  float ,
328     in __t3id integer,
329 out _status boolean,
330 out _msg varchar(200))
331 returns record AS $$
332 BEGIN
333                                                     
334 
335 update test3 set
336 "t3name"=__t3name,"t_birthday"=__t_birthday,"myage"=__myage,"isadmin"=__isadmin,"myintro"=__myintro,"price"=__price where t3id=__t3id;
337 /*判断保存记录是否成功。*/
338 if FOUND then
339 _status:=true;
340 _msg:='成功保存记录.';
341 else
342 _status:=false;
343 _msg:='无法保存记录!';
344 end if;
345 end;
346 $$ LANGUAGE plpgsql  VOLATILE;
PostgreSQL

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2021-11-08
  • 2021-06-29
猜你喜欢
  • 2022-02-14
  • 2021-11-24
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案