Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 Clarion Media, LLC
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.clarionmedia.infinitum.orm.sqlite;
 
 

This class contains utility methods for generating SQL strings for a SQLite database.

Author(s):
Tyler Treat
Version:
1.0 02/15/12
Since:
1.0
 
 public class SqliteUtils {
 
 	
 	
Generates a "where clause" java.lang.String used for updating or deleting the given persistent java.lang.Object in the database. Where conditions are indicated by primary key java.lang.reflect.Field's. Note that the actual String "where" is not included with the resulting output.

For example, passing an object of class Foobar which has a primary key foo with a value of 42 will result in the where clause foo = 42.

Parameters:
model the model to generate the where clause for
Returns:
where clause String for specified Object
Throws:
com.clarionmedia.infinitum.exception.InfinitumRuntimeException if there is an error generating the SQL
 
 	public String getWhereClause(Object modelSqliteMapper mapper)
 		if (AbstractProxy.isAopProxy(model)) {
 			model = AbstractProxy.getProxy(model).getTarget();
 		}
 		StringBuilder whereClause = new StringBuilder();
 		pk.setAccessible(true);
 		SqliteDataType t = mapper.getSqliteDataType(pk);
 		Serializable pkVal = null;
 		try {
 			pkVal = (Serializable.getFieldValue(modelpk);
 		} catch (ClassCastException e) {
 			throw new ModelConfigurationException("Invalid primary key specified for type '" + model.getClass().getName() + "'.");
 		}
 		if (t == .)
 			whereClause.append("'").append(pkVal).append("'");
 		else
 			whereClause.append(pkVal);
 		return whereClause.toString();
 	}

Generates a "where clause" java.lang.String used for the given persistent java.lang.Class in the database with the given primary key. Where conditions are indicated by primary key java.lang.reflect.Field's. Note that the actual String "where" is not included with the resulting output.

For example, passing a Class Foobar which has a primary key foo with a value of 42 will result in the where clause foo = 42.

Parameters:
c the Class of the model
id the primary key for the model
Returns:
where clause String for specified Class and primary key
Throws:
java.lang.IllegalArgumentException if there is a mismatch between the Class's primary key type and the type of the given primary key
			throw new IllegalArgumentException(String.format("Invalid primary key value of type '%s' for '%s'."id.getClass().getSimpleName(), c.getName()));
		if (t == .)
			sb.append("'").append(id).append("'");
		else
			sb.append(id);
		return sb.toString();
	}
New to GrepCode? Check out our FAQ X