expression.setStatement( "select count(*) from pg_class where relname = '"+ sequenceName.toLowerCase() +"' and relkind = 'S'" );
int dotIndex = sequenceName.indexOf("."); if ( dotIndex == -1 ) { expression.setStatement( "select count(*) from pg_class where relname = '"+ sequenceName.toLowerCase() +"' and relkind = 'S'" ); } else { String schemaName = sequenceName.substring(0, dotIndex); String sequenceNameOnly = sequenceName.toLowerCase().substring(dotIndex + 1); expression.setStatement("select count(c.*) from pg_catalog.pg_class c, pg_catalog.pg_namespace n where c.relnamespace=n.oid AND c.relkind = 'S' AND c.relname='"+sequenceNameOnly+"' AND n.nspname='"+schemaName+"'"); }
    public NSArray newPrimaryKeys (int count, EOEntity entity, JDBCChannel channel) {        if( isPrimaryKeyGenerationNotSupported( entity ) )            return null;        NSMutableArray results = new NSMutableArray(count);        String sequenceName = sequenceNameForEntity( entity );        PostgresqlExpression expression = new PostgresqlExpression(entity);        for(int tries = 0; tries < 2; tries ++) {        	try {        		expression.setStatement("SELECT SETVAL( '"+ sequenceName +"', NEXTVAL('"+ sequenceName +"') + "+ (count-1) +" )"); //, false        		channel.evaluateExpression( expression );        		NSDictionary row = channel.fetchRow();        		channel.cancelFetch();        		long maxValue = ((Number) row.objectForKey("SETVAL")).longValue();        		EOAttribute attribute =  (EOAttribute) entity.primaryKeyAttributes().lastObject();        		String attrName = attribute.name();        		boolean isIntType = "i".equals(attribute.valueType());        		for( int i = 0; i < count; i++ ) {        			long newPK = maxValue - count + 1 + i;        			Number value;        			if(isIntType) {         				value = new Integer((int) newPK);        			} else {        				value = new Long(newPK);        			}        			results.addObject(new NSDictionary( value, attrName));        		}        		return results;        	} catch(JDBCAdaptorException ex) {        		expression.setStatement( "select count(*) from pg_class where relname = '"+ sequenceName.toLowerCase() +"' and relkind = 'S'" );        		channel.evaluateExpression( expression );        		NSDictionary row = channel.fetchRow();        		channel.cancelFetch();        		if( new Long( 0 ).equals( row.objectForKey( "COUNT" ) ) ) {        			EOSynchronizationFactory f = createSynchronizationFactory();        			NSArray statements = f.primaryKeySupportStatementsForEntityGroup( new NSArray( entity ) );        			int stmCount = statements.count();        			for( int i = 0; i < stmCount; i++ ) {        				channel.evaluateExpression( (EOSQLExpression) statements.objectAtIndex(i) );        			}                    		} else {        			throw new IllegalStateException("Caught exception, but sequence did already exist: " + ex);        		}        	}        }		throw new IllegalStateException("Could not create or read sequence " + sequenceName);    }