Ejemplo n.º 1
0
		/// <summary>
		/// Retrieve a statement from the cache. If the statement is not present it will be generated
		/// and added to the cache.
		/// </summary>
		/// <param name="type">The business object with which the statement is associated</param>
		/// <param name="tableName">The table used in the statement</param>
		/// <param name="stmtType">The type of the SQL statement</param>
		/// <returns>An SqlStatement instance</returns>
		public SqlStatement GetStatement( Type type, string tableName, StatementType stmtType )
		{
			Initialize(); // ensure thread local variables have been initialized
			SqlStatement stmt;
			Hashtable stmts = (Hashtable) stmtByType[ type ];
			// check if an SqlStatement has been cached for the given type and StatementType
			bool isCached = stmts != null && stmts.ContainsKey( stmtType );
			if( isCached )
			{
				stmt = (SqlStatement) stmts[ stmtType ];
				// the npgsql library for postgres does not allow us to update the connection 
				// property when a transaction is active
				if( stmt.Command.Connection == null || providerName != "PostgreSQL" )
				{
					// make sure statement broker reference is updated before returning it
					stmt.SessionBroker = Broker;
					return stmt;
				}
			}
			// otherwise create and return fresh object
			PersistenceBroker broker = new PersistenceBroker( this );
			SqlBuilder sb = new SqlBuilder( broker, stmtType, type );
			// set the table name specified in the key (if null the default will be used instead)
			sb.SetTable( tableName );
			// get the statement using primary key fields as constraints
			stmt = sb.GetStatement( false );
			// dont cache statements for objects that map to multiple tables
			ObjectMap map = ObjectFactory.GetMap( broker, type );
			if( ! (map.IsDynamicTable || isCached) )
			{
				// TODO Prepare only works with a connection :-(
				// stmt.Prepare();
				CacheStatement( type, stmtType, stmt );
			}
			return stmt;
		}