All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.client.PostgresServer Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2002-2013, 2016  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client;

import com.aoindustries.io.CompressedDataInputStream;
import com.aoindustries.io.CompressedDataOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
 * A PostgresServer corresponds to a unique PostgreSQL install
 * space on one server.  The server name must be unique per server.
 * PostgresDatabases and PostgresServerUsers are
 * unique per PostgresServer.
 *
 * @see  PostgresVersion
 * @see  PostgresDatabase
 * @see  PostgresServerUser
 *
 * @author  AO Industries, Inc.
 */
final public class PostgresServer extends CachedObjectIntegerKey {

	// 
	/**
	 * The directory that contains the PostgreSQL data files.
	 */
	public static final String DATA_BASE_DIR="/var/lib/pgsql";

	public enum ReservedWord {
		ABORT,
		ALL,
		ANALYSE,
		ANALYZE,
		AND,
		ANY,
		AS,
		ASC,
		BETWEEN,
		BINARY,
		BIT,
		BOTH,
		CASE,
		CAST,
		CHAR,
		CHARACTER,
		CHECK,
		CLUSTER,
		COALESCE,
		COLLATE,
		COLUMN,
		CONSTRAINT,
		COPY,
		CROSS,
		CURRENT_DATE,
		CURRENT_TIME,
		CURRENT_TIMESTAMP,
		CURRENT_USER,
		DEC,
		DECIMAL,
		DEFAULT,
		DEFERRABLE,
		DESC,
		DISTINCT,
		DO,
		ELSE,
		END,
		EXCEPT,
		EXISTS,
		EXPLAIN,
		EXTEND,
		EXTRACT,
		FALSE,
		FLOAT,
		FOR,
		FOREIGN,
		FROM,
		FULL,
		GLOBAL,
		GROUP,
		HAVING,
		ILIKE,
		IN,
		INITIALLY,
		INNER,
		INOUT,
		INTERSECT,
		INTO,
		IS,
		ISNULL,
		JOIN,
		LEADING,
		LEFT,
		LIKE,
		LIMIT,
		LISTEN,
		LOAD,
		LOCAL,
		LOCK,
		MOVE,
		NATURAL,
		NCHAR,
		NEW,
		NOT,
		NOTNULL,
		NULL,
		NULLIF,
		NUMERIC,
		OFF,
		OFFSET,
		OLD,
		ON,
		ONLY,
		OR,
		ORDER,
		OUT,
		OUTER,
		OVERLAPS,
		POSITION,
		PRECISION,
		PRIMARY,
		PUBLIC,
		REFERENCES,
		RESET,
		RIGHT,
		SELECT,
		SESSION_USER,
		SETOF,
		SHOW,
		SOME,
		SUBSTRING,
		TABLE,
		THEN,
		TO,
		TRAILING,
		TRANSACTION,
		TRIM,
		TRUE,
		UNION,
		UNIQUE,
		USER,
		USING,
		VACUUM,
		VARCHAR,
		VERBOSE,
		WHEN,
		WHERE;

		private static volatile Set reservedWords = null;

		/**
		 * Case-insensitive check for if the provided string is a reserved word.
		 */
		public static boolean isReservedWord(String value) {
			Set words = reservedWords;
			if(words==null) {
				ReservedWord[] values = values();
				words = new HashSet<>(values.length*4/3+1);
				for(ReservedWord word : values) words.add(word.name().toUpperCase(Locale.ROOT));
				reservedWords = words;
			}
			return words.contains(value.toUpperCase(Locale.ROOT));
		}
	}
	// 

	static final int
		COLUMN_PKEY=0,
		COLUMN_AO_SERVER=2,
		COLUMN_NET_BIND=5
	;
	static final String COLUMN_NAME_name = "name";
	static final String COLUMN_AO_SERVER_name = "ao_server";

	/**
	 * The maximum length of the name.
	 */
	public static final int MAX_SERVER_NAME_LENGTH=31;

	String name;
	int ao_server;
	private int version;
	private int max_connections;
	int net_bind;
	private int sort_mem;
	private int shared_buffers;
	private boolean fsync;

	public int addPostgresDatabase(
		String name,
		PostgresServerUser datdba,
		PostgresEncoding encoding,
		boolean enablePostgis
	) throws IOException, SQLException {
		return table.connector.getPostgresDatabases().addPostgresDatabase(
			name,
			this,
			datdba,
			encoding,
			enablePostgis
		);
	}

	public static void checkServerName(String name) throws IllegalArgumentException {
		// Must be a-z or 0-9 first, then a-z or 0-9 or . or _
		int len = name.length();
		if (len == 0 || len > MAX_SERVER_NAME_LENGTH) throw new IllegalArgumentException("PostgreSQL server name should not exceed "+MAX_SERVER_NAME_LENGTH+" characters.");

		// The first character must be [a-z] or [0-9]
		char ch = name.charAt(0);
		if ((ch < 'a' || ch > 'z') && (ch<'0' || ch>'9')) throw new IllegalArgumentException("PostgreSQL server names must start with [a-z] or [0-9]");
		// The rest may have additional characters
		for (int c = 1; c < len; c++) {
			ch = name.charAt(c);
			if (
				(ch<'a' || ch>'z')
				&& (ch<'0' || ch>'9')
				&& ch!='.'
				&& ch!='_'
			) throw new IllegalArgumentException("PostgreSQL server names may only contain [a-z], [0-9], period (.), and underscore (_)");
		}
	}

	@Override
	Object getColumnImpl(int i) {
		switch(i) {
			case COLUMN_PKEY: return pkey;
			case 1: return name;
			case COLUMN_AO_SERVER: return ao_server;
			case 3: return version;
			case 4: return max_connections;
			case COLUMN_NET_BIND: return net_bind;
			case 6: return sort_mem;
			case 7: return shared_buffers;
			case 8: return fsync;
			default: throw new IllegalArgumentException("Invalid index: "+i);
		}
	}

	public String getDataDirectory() {
		return DATA_BASE_DIR+'/'+name;
	}

	public String getName() {
		return name;
	}

	public PostgresVersion getPostgresVersion() throws SQLException, IOException {
		PostgresVersion obj=table.connector.getPostgresVersions().get(version);
		if(obj==null) throw new SQLException("Unable to find PostgresVersion: "+version);
		if(
			obj.getTechnologyVersion(table.connector).getOperatingSystemVersion(table.connector).getPkey()
			!= getAOServer().getServer().getOperatingSystemVersion().getPkey()
		) {
			throw new SQLException("resource/operating system version mismatch on PostgresServer: #"+pkey);
		}
		return obj;
	}

	public AOServer getAOServer() throws SQLException, IOException {
		AOServer ao=table.connector.getAoServers().get(ao_server);
		if(ao==null) throw new SQLException("Unable to find AOServer: "+ao_server);
		return ao;
	}

	public int getMaxConnections() {
		return max_connections;
	}

	public NetBind getNetBind() throws SQLException, IOException {
		NetBind nb=table.connector.getNetBinds().get(net_bind);
		if(nb==null) throw new SQLException("Unable to find NetBind: "+net_bind);
		return nb;
	}

	public PostgresDatabase getPostgresDatabase(String name) throws IOException, SQLException {
		return table.connector.getPostgresDatabases().getPostgresDatabase(name, this);
	}

	public List getPostgresDatabases() throws IOException, SQLException {
		return table.connector.getPostgresDatabases().getPostgresDatabases(this);
	}

	public PostgresServerUser getPostgresServerUser(String username) throws IOException, SQLException {
		return table.connector.getPostgresServerUsers().getPostgresServerUser(username, this);
	}

	public List getPostgresServerUsers() throws IOException, SQLException {
		return table.connector.getPostgresServerUsers().getPostgresServerUsers(this);
	}

	public List getPostgresUsers() throws SQLException, IOException {
		List psu=getPostgresServerUsers();
		int len=psu.size();
		List pu=new ArrayList<>(len);
		for(int c=0;c




© 2015 - 2025 Weber Informatics LLC | Privacy Policy