BrightSide Workbench Full Report + Source Code
org.turro.elephant.db.ElementDB Class Reference
Collaboration diagram for org.turro.elephant.db.ElementDB:

Public Member Functions

 ElementDB (IElement element)
 
Connection getConnection () throws SQLException
 
boolean positionFirst (ResultSet rs, int first) throws SQLException
 
boolean iterate (ResultSet rs, int count, int current) throws SQLException
 
int findLast (ResultSet rs) throws SQLException
 
ResultSet query (String sql) throws SQLException
 
ResultSet query (String sql, Collection params) throws SQLException
 
ResultSet query (String sql, Object[] params) throws SQLException
 
int execute (String sql) throws SQLException
 
int execute (String sql, Collection params) throws SQLException
 
int execute (String sql, Object[] params) throws SQLException
 
void execute (String commands[]) throws SQLException
 
void close (ResultSet rs) throws SQLException
 
void close () throws SQLException
 
void renderNavigator (PrintWriter out, String param)
 
java.util.Date toDate (java.sql.Timestamp date)
 
java.sql.Timestamp toTimestamp (java.util.Date date)
 
java.util.Date toDate (java.sql.Date date)
 
java.sql.Date toSqlDate (java.util.Date date)
 

Protected Attributes

IElement element
 
Connection conn = null
 
int lastFirst
 

Detailed Description

Author
Lluis TurrĂ³ Cutiller lluis.nosp@m.@tur.nosp@m.ro.or.nosp@m.g

Definition at line 37 of file ElementDB.java.

Constructor & Destructor Documentation

◆ ElementDB()

org.turro.elephant.db.ElementDB.ElementDB ( IElement  element)

Creates a new instance of ElementDB

Parameters
element

Definition at line 46 of file ElementDB.java.

46  {
47  this.element = element;
48  }

Member Function Documentation

◆ close() [1/2]

void org.turro.elephant.db.ElementDB.close ( ) throws SQLException

Definition at line 174 of file ElementDB.java.

174  {
175  if(conn != null && !conn.isClosed())
176  conn.close();
177  }

◆ close() [2/2]

void org.turro.elephant.db.ElementDB.close ( ResultSet  rs) throws SQLException

Definition at line 170 of file ElementDB.java.

170  {
171  rs.close();
172  }

◆ execute() [1/4]

void org.turro.elephant.db.ElementDB.execute ( String  commands[]) throws SQLException

Definition at line 164 of file ElementDB.java.

164  {
165  for (String command : commands) {
166  execute(command);
167  }
168  }
Here is the call graph for this function:

◆ execute() [2/4]

int org.turro.elephant.db.ElementDB.execute ( String  sql) throws SQLException

Definition at line 138 of file ElementDB.java.

138  {
139  Connection con = getConnection();
140  return con.createStatement().executeUpdate(sql);
141  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute() [3/4]

int org.turro.elephant.db.ElementDB.execute ( String  sql,
Collection  params 
) throws SQLException

Definition at line 143 of file ElementDB.java.

143  {
144  return execute(sql, params.toArray());
145  }
Here is the call graph for this function:

◆ execute() [4/4]

int org.turro.elephant.db.ElementDB.execute ( String  sql,
Object[]  params 
) throws SQLException

Definition at line 147 of file ElementDB.java.

147  {
148  if(params == null) {
149  return execute(sql);
150  }
151  Connection con = getConnection();
152  PreparedStatement ps = con.prepareStatement(sql);
153  for(int i = 0; i < params.length; i++) {
154  if(params[i] != null) {
155  ps.setObject(i + 1, params[i]);
156  }
157  else {
158  ps.setNull(i + 1, java.sql.Types.VARCHAR);
159  }
160  }
161  return ps.executeUpdate();
162  }
Here is the call graph for this function:

◆ findLast()

int org.turro.elephant.db.ElementDB.findLast ( ResultSet  rs) throws SQLException

Definition at line 104 of file ElementDB.java.

104  {
105  totalCount = lastCurrent;
106  while(rs.next()) {
107  totalCount++;
108  }
109  return totalCount;
110  }

◆ getConnection()

Connection org.turro.elephant.db.ElementDB.getConnection ( ) throws SQLException

Definition at line 50 of file ElementDB.java.

50  {
51  if(conn == null || conn.isClosed()) {
52  String driverClass = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-driver", null),
53  url = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-url", null),
54  user = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-user", null),
55  password = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-password", null);
56  if(driverClass != null && url != null) {
57  // TODO: optimize
58  url = url.replaceAll("\\#idel", element.getId());
59  url = url.replaceAll("\\#ctx",
60  ElephantContext.getRealPath(
62  ));
63  url = url.replaceAll("\\#root", ElephantContext.getRealPath("/"));
64  url = url.replaceAll("\\\\","/");
65  try {
66  Class.forName(driverClass);
67  if(user != null && user.length() > 0 && password != null) {
68  conn = DriverManager.getConnection(url, user, password);
69  }
70  else {
71  conn = DriverManager.getConnection(url);
72  }
73  return conn;
74  } catch (ClassNotFoundException | SQLException ex) {
75  Logger.getLogger(ElementDB.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
76  return null;
77  }
78  }
79  return null;
80  }
81  return conn;
82  }
ElementDB(IElement element)
Definition: ElementDB.java:46
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate()

boolean org.turro.elephant.db.ElementDB.iterate ( ResultSet  rs,
int  count,
int  current 
) throws SQLException

Definition at line 95 of file ElementDB.java.

95  {
96  lastCount = count;
97  lastCurrent = current;
98  if(current - lastFirst < count) {
99  return rs.next();
100  }
101  return false;
102  }

◆ positionFirst()

boolean org.turro.elephant.db.ElementDB.positionFirst ( ResultSet  rs,
int  first 
) throws SQLException

Definition at line 84 of file ElementDB.java.

84  {
85  lastFirst = first;
86  boolean hasNext = true;
87  int current = 0;
88  while((current < first) && hasNext) {
89  current++;
90  hasNext = rs.next();
91  }
92  return hasNext;
93  }

◆ query() [1/3]

ResultSet org.turro.elephant.db.ElementDB.query ( String  sql) throws SQLException

Definition at line 112 of file ElementDB.java.

112  {
113  Connection con = getConnection();
114  return con.createStatement().executeQuery(sql);
115  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ query() [2/3]

ResultSet org.turro.elephant.db.ElementDB.query ( String  sql,
Collection  params 
) throws SQLException

Definition at line 117 of file ElementDB.java.

117  {
118  return query(sql, params.toArray());
119  }
ResultSet query(String sql)
Definition: ElementDB.java:112
Here is the call graph for this function:

◆ query() [3/3]

ResultSet org.turro.elephant.db.ElementDB.query ( String  sql,
Object[]  params 
) throws SQLException

Definition at line 121 of file ElementDB.java.

121  {
122  if(params == null) {
123  return query(sql);
124  }
125  Connection con = getConnection();
126  PreparedStatement ps = con.prepareStatement(sql);
127  for(int i = 0; i < params.length; i++) {
128  if(params[i] != null) {
129  ps.setObject(i + 1, params[i]);
130  }
131  else {
132  ps.setNull(i + 1, java.sql.Types.VARCHAR);
133  }
134  }
135  return ps.executeQuery();
136  }
Here is the call graph for this function:

◆ renderNavigator()

void org.turro.elephant.db.ElementDB.renderNavigator ( PrintWriter  out,
String  param 
)

Definition at line 179 of file ElementDB.java.

179  {
180  if(totalCount <= lastCount) return;
181  out.print("<div class='dbnav'>");
182  out.print("<ul>");
183  if(lastFirst > 0) {
184  out.print("<li>");
185  out.print("<a href='?" + param + "=" + (lastFirst - lastCount) + "'>");
186  out.print("&lt;&lt;");
187  out.print("</a>");
188  out.print("</li>");
189  }
190  int iter = (int)Math.ceil(((double)totalCount) / ((double)lastCount));
191  for(int i = 0; i < iter; i++) {
192  if(lastFirst >= i * lastCount && lastFirst < (i+1) * lastCount) {
193  out.print("<li class='active'>");
194  }
195  else {
196  out.print("<li>");
197  }
198  out.print("<a href='?" + param + "=" + (i * lastCount) + "'>");
199  out.print(i + 1);
200  out.print("</a>");
201  out.print("</li>");
202  }
203  if(lastFirst + lastCount < totalCount) {
204  out.print("<li>");
205  out.print("<a href='?" + param + "=" + (lastFirst + lastCount) + "'>");
206  out.print("&gt;&gt;");
207  out.print("</a>");
208  out.print("</li>");
209  }
210  out.print("</ul>");
211  out.print("</div>");
212  }

◆ toDate() [1/2]

java.util.Date org.turro.elephant.db.ElementDB.toDate ( java.sql.Date  date)

Definition at line 224 of file ElementDB.java.

224  {
225  if(date == null) return null;
226  return new java.util.Date(date.getTime());
227  }

◆ toDate() [2/2]

java.util.Date org.turro.elephant.db.ElementDB.toDate ( java.sql.Timestamp  date)

Definition at line 214 of file ElementDB.java.

214  {
215  if(date == null) return null;
216  return new java.util.Date(date.getTime());
217  }

◆ toSqlDate()

java.sql.Date org.turro.elephant.db.ElementDB.toSqlDate ( java.util.Date  date)

Definition at line 229 of file ElementDB.java.

229  {
230  if(date == null) return null;
231  return new java.sql.Date(date.getTime());
232  }

◆ toTimestamp()

java.sql.Timestamp org.turro.elephant.db.ElementDB.toTimestamp ( java.util.Date  date)

Definition at line 219 of file ElementDB.java.

219  {
220  if(date == null) return null;
221  return new java.sql.Timestamp(date.getTime());
222  }

Member Data Documentation

◆ conn

Connection org.turro.elephant.db.ElementDB.conn = null
protected

Definition at line 39 of file ElementDB.java.

◆ element

IElement org.turro.elephant.db.ElementDB.element
protected

Definition at line 38 of file ElementDB.java.

◆ lastFirst

int org.turro.elephant.db.ElementDB.lastFirst
protected

Definition at line 40 of file ElementDB.java.


The documentation for this class was generated from the following file: