Example #1
0
		/*
		 * Constructs the LargeObject API.
		 *
		 * <p><b>Important Notice</b>
		 * <br>This method should only be called by org.postgresql.Connection
		 *
		 * <p>There should only be one LargeObjectManager per Connection. The
		 * org.postgresql.Connection class keeps track of the various extension API's
		 * and it's advised you use those to gain access, and not going direct.
		 */

		public LargeObjectManager(NpgsqlConnection conn)
		{
			// We need Fastpath to do anything
			// Now get the function oid's for the api
			//
			// This is an example of Fastpath.addFunctions();
			//
			//String sql;
			StringBuilder sql = null;
			try
			{
				sql = new StringBuilder();
				if (conn.PostgreSqlVersion > new Version(7, 3, 0))
				{
					sql.Append("SELECT p.proname,p.oid ");
					sql.Append(" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n ");
					sql.Append(" WHERE p.pronamespace=n.oid AND n.nspname='pg_catalog' AND (");
				}
				else
				{
					sql.Append("SELECT proname,oid FROM pg_proc WHERE ");
				}
				sql.Append(" proname = 'lo_open'");
				sql.Append(" or proname = 'lo_close'");
				sql.Append(" or proname = 'lo_creat'");
				sql.Append(" or proname = 'lo_unlink'");
				sql.Append(" or proname = 'lo_lseek'");
				sql.Append(" or proname = 'lo_tell'");
				sql.Append(" or proname = 'loread'");
				sql.Append(" or proname = 'lowrite'");

				if (conn.PostgreSqlVersion > new Version(7, 3, 0))
				{
					sql.Append(")");
				}

				using (IDbCommand cmd = new NpgsqlCommand(sql.ToString()))
				{
					cmd.Connection = conn;

					this.fp = new Fastpath(conn, conn.Connector.Stream);

					using (IDataReader res = cmd.ExecuteReader())
					{
						if (res == null)
						{
							throw new NpgsqlException("postgresql.lo.init");
						}


						fp.AddFunctions(res);
					}
				}
			}
			finally
			{
				sql = null;
			}
		}
Example #2
0
		///<summary>
		///
		/// This method is reponsible to derive the command parameter list with values obtained from function definition.
		/// It clears the Parameters collection of command. Also, if there is any parameter type which is not supported by Npgsql, an InvalidOperationException will be thrown.
		/// Parameters name will be parameter1, parameter2, ...
		/// For while, only parameter name and NpgsqlDbType are obtained.
		///</summary>
		/// <param name="command">NpgsqlCommand whose function parameters will be obtained.</param>
		public static void DeriveParameters(NpgsqlCommand command)
		{
			// Updated after 0.99.3 to support the optional existence of a name qualifying schema and case insensitivity when the schema ror procedure name do not contain a quote.
			// This fixed an incompatibility with NpgsqlCommand.CheckFunctionReturn(String ReturnType)
			String query = null;
			string procedureName = null;
			string schemaName = null;
			string[] fullName = command.CommandText.Split('.');
			if (fullName.Length > 1 && fullName[0].Length > 0)
			{
				query =
					"select proargnames, proargtypes from pg_proc p left join pg_namespace n on p.pronamespace = n.oid where proname=:proname and n.nspname=:nspname";
				schemaName = (fullName[0].IndexOf("\"") != -1) ? fullName[0] : fullName[0].ToLower();
				procedureName = (fullName[1].IndexOf("\"") != -1) ? fullName[1] : fullName[1].ToLower();
			}
			else
			{
				query = "select proargnames, proargtypes from pg_proc where proname = :proname";
				procedureName = (fullName[0].IndexOf("\"") != -1) ? fullName[0] : fullName[0].ToLower();
			}

			using (NpgsqlCommand c = new NpgsqlCommand(query, command.Connection))
			{
				c.Parameters.Add(new NpgsqlParameter("proname", NpgsqlDbType.Text));
				c.Parameters[0].Value = procedureName.Replace("\"", "").Trim();
				if (fullName.Length > 1 && !String.IsNullOrEmpty(schemaName))
				{
					NpgsqlParameter prm = c.Parameters.Add(new NpgsqlParameter("nspname", NpgsqlDbType.Text));
					prm.Value = schemaName.Replace("\"", "").Trim();
				}

				String[] names = null;
				String[] types = null;

				using (NpgsqlDataReader rdr = c.ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult))
				{
					if (rdr.Read())
					{
						if (!rdr.IsDBNull(0))
							names = rdr.GetValue(0) as String[];
						if (!rdr.IsDBNull(1))
							types = rdr.GetString(1).Split();
					}
				}

				if (types == null)
				{
					throw new InvalidOperationException(
						String.Format(resman.GetString("Exception_InvalidFunctionName"), command.CommandText));
				}

				command.Parameters.Clear();
				for (Int32 i = 0; i < types.Length; i++)
				{
					// skip parameter if type string is empty
					// empty parameter lists can cause this
					if (!string.IsNullOrEmpty(types[i]))
					{
						NpgsqlBackendTypeInfo typeInfo = null;
						if (!c.Connector.OidToNameMapping.TryGetValue(int.Parse(types[i]), out typeInfo))
						{
							command.Parameters.Clear();
							throw new InvalidOperationException(String.Format("Invalid parameter type: {0}", types[i]));
						}
						if (names != null && i < names.Length)
							command.Parameters.Add(new NpgsqlParameter(":" + names[i], typeInfo.NpgsqlDbType));
						else
							command.Parameters.Add(new NpgsqlParameter("parameter" + (i + 1).ToString(), typeInfo.NpgsqlDbType));
					}
				}
			}
		}