KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > PostgresStatement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.quercus.env.LongValue;
34 import com.caucho.quercus.env.UnsetValue;
35 import com.caucho.quercus.env.Value;
36 import com.caucho.util.L10N;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43
44
45 /**
46  * Postgres statement class. Since Postgres has no object oriented API,
47  * this is essentially a JdbcStatementResource.
48  */

49 public class PostgresStatement extends JdbcStatementResource {
50   private static final Logger JavaDoc log = Logger.getLogger(PostgresStatement.class.getName());
51   private static final L10N L = new L10N(PostgresStatement.class);
52
53   // Map JDBC ?,?,? to any unsorted or duplicated params.
54
// Ex: INSERT INTO test VALUES($2, $1) is mapped as [0]->2, [1]->1
55
// INSERT INTO test VALUES($1, $1) is mapped as [0]->1, [1]->1
56
private ArrayList JavaDoc<LongValue> _preparedMapping = new ArrayList JavaDoc<LongValue>();
57
58   /**
59    * Constructor for PostgresStatement
60    *
61    * @param conn a Postgres connection
62    */

63   PostgresStatement(Postgres conn)
64   {
65     super(conn);
66   }
67
68   /**
69    * Executes a prepared Postgres Query.
70    *
71    * @param env the PHP executing environment
72    * @return true on success or false on failure
73    */

74   public boolean execute(Env env)
75   {
76     try {
77
78       int size = _preparedMapping.size();
79
80       int matches = 0;
81
82       for (int i = 0; i < size; i++) {
83         LongValue param = _preparedMapping.get(i);
84
85         Value paramV = getParam(param.toInt()-1);
86
87         if (paramV.equals(UnsetValue.UNSET)) {
88           env.warning(L.l("Not all parameters are bound"));
89           return false;
90         }
91
92         Object JavaDoc object = paramV.toJavaObject();
93
94         setObject(i+1, object);
95       }
96
97       return executeStatement();
98
99     } catch (Exception JavaDoc e) {
100       env.warning(L.l(e.toString()));
101       log.log(Level.FINE, e.toString(), e);
102       return false;
103     }
104   }
105
106   /**
107    * Prepares this statement with the given query.
108    *
109    * @param query SQL query
110    * @return true on success or false on failure
111    */

112   public boolean prepare(String JavaDoc query)
113   {
114     try {
115
116       _preparedMapping.clear();
117
118       // Map any unsorted or duplicated params.
119
// Ex: INSERT INTO test VALUES($2, $1) or
120
// INSERT INTO test VALUES($1, $1)
121
Pattern JavaDoc pattern = Pattern.compile("\\$([0-9]+)");
122       Matcher JavaDoc matcher = pattern.matcher(query);
123       while (matcher.find()) {
124         int phpParam;
125         try {
126           phpParam = Integer.parseInt(matcher.group(1));
127         } catch(Exception JavaDoc ex) {
128           _preparedMapping.clear();
129           return false;
130         }
131         _preparedMapping.add(LongValue.create(phpParam));
132       }
133
134       // Make the PHP query a JDBC like query
135
// replacing ($1 -> ?) with question marks.
136
// XXX: replace this with Matcher.appendReplacement
137
// above when StringBuilder is supported.
138
query = query.replaceAll("\\$[0-9]+", "?");
139
140       // Prepare the JDBC query
141
return super.prepare(query);
142
143     } catch (Exception JavaDoc e) {
144       log.log(Level.FINE, e.toString(), e);
145       return false;
146     }
147   }
148
149   protected int getPreparedMappingSize()
150   {
151     return _preparedMapping.size();
152   }
153 }
154
Popular Tags