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

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

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2001-2012, 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 com.aoindustries.io.TerminalWriter;
import com.aoindustries.util.IntList;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @see  MySQLDBUser
 *
 * @author  AO Industries, Inc.
 */
final public class MySQLDBUserTable extends CachedTableIntegerKey {

	MySQLDBUserTable(AOServConnector connector) {
		super(connector, MySQLDBUser.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(MySQLDBUser.COLUMN_MYSQL_DATABASE_name+'.'+MySQLDatabase.COLUMN_NAME_name, ASCENDING),
		new OrderBy(MySQLDBUser.COLUMN_MYSQL_DATABASE_name+'.'+MySQLDatabase.COLUMN_MYSQL_SERVER_name+'.'+MySQLServer.COLUMN_AO_SERVER_name+'.'+AOServer.COLUMN_HOSTNAME_name, ASCENDING),
		new OrderBy(MySQLDBUser.COLUMN_MYSQL_DATABASE_name+'.'+MySQLDatabase.COLUMN_MYSQL_SERVER_name+'.'+MySQLServer.COLUMN_NAME_name, ASCENDING),
		new OrderBy(MySQLDBUser.COLUMN_MYSQL_SERVER_USER_name+'.'+MySQLServerUser.COLUMN_USERNAME_name, ASCENDING)
	};
	@Override
	OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	int addMySQLDBUser(
		final MySQLDatabase md,
		final MySQLServerUser msu,
		final boolean canSelect,
		final boolean canInsert,
		final boolean canUpdate,
		final boolean canDelete,
		final boolean canCreate,
		final boolean canDrop,
		final boolean canIndex,
		final boolean canAlter,
		final boolean canCreateTempTable,
		final boolean canLockTables,
		final boolean canCreateView,
		final boolean canShowView,
		final boolean canCreateRoutine,
		final boolean canAlterRoutine,
		final boolean canExecute,
		final boolean canEvent,
		final boolean canTrigger
	) throws IOException, SQLException {
		return connector.requestResult(
			true,
			new AOServConnector.ResultRequest() {
				int pkey;
				IntList invalidateList;

				@Override
				public void writeRequest(CompressedDataOutputStream out) throws IOException {
					out.writeCompressedInt(AOServProtocol.CommandID.ADD.ordinal());
					out.writeCompressedInt(SchemaTable.TableID.MYSQL_DB_USERS.ordinal());
					out.writeCompressedInt(md.pkey);
					out.writeCompressedInt(msu.pkey);
					out.writeBoolean(canSelect);
					out.writeBoolean(canInsert);
					out.writeBoolean(canUpdate);
					out.writeBoolean(canDelete);
					out.writeBoolean(canCreate);
					out.writeBoolean(canDrop);
					out.writeBoolean(canIndex);
					out.writeBoolean(canAlter);
					out.writeBoolean(canCreateTempTable);
					out.writeBoolean(canLockTables);
					out.writeBoolean(canCreateView);
					out.writeBoolean(canShowView);
					out.writeBoolean(canCreateRoutine);
					out.writeBoolean(canAlterRoutine);
					out.writeBoolean(canExecute);
					out.writeBoolean(canEvent);
					out.writeBoolean(canTrigger);
				}

				@Override
				public void readResponse(CompressedDataInputStream in) throws IOException, SQLException {
					int code=in.readByte();
					if(code==AOServProtocol.DONE) {
						pkey=in.readCompressedInt();
						invalidateList=AOServConnector.readInvalidateList(in);
					} else {
						AOServProtocol.checkResult(code, in);
						throw new IOException("Unexpected response code: "+code);
					}
				}

				@Override
				public Integer afterRelease() {
					connector.tablesUpdated(invalidateList);
					return pkey;
				}
			}
		);
	}

	@Override
	public MySQLDBUser get(int pkey) throws IOException, SQLException {
		return getUniqueRow(MySQLDBUser.COLUMN_PKEY, pkey);
	}

	MySQLDBUser getMySQLDBUser(MySQLDatabase db, MySQLServerUser msu) throws IOException, SQLException {
		int msuPKey=msu.pkey;

		// Use index first on database
		List cached=getMySQLDBUsers(db);
		int size=cached.size();
		for(int c=0;c getMySQLDBUsers(MySQLServer ms) throws IOException, SQLException {
		int msPKey=ms.pkey;

		List cached=getRows();
		int size=cached.size();
		List matches=new ArrayList<>(size);
		for(int c=0;c getMySQLDBUsers(MySQLServerUser msu) throws IOException, SQLException {
		return getIndexedRows(MySQLDBUser.COLUMN_MYSQL_SERVER_USER, msu.pkey);
	}

	List getMySQLDBUsers(MySQLDatabase md) throws IOException, SQLException {
		return getIndexedRows(MySQLDBUser.COLUMN_MYSQL_DATABASE, md.pkey);
	}

	List getMySQLServerUsers(MySQLDatabase md) throws IOException, SQLException {
		// Use index first
		List cached=getMySQLDBUsers(md);
		int len=cached.size();
		List array=new ArrayList<>(len);
		for(int c=0;c




© 2015 - 2024 Weber Informatics LLC | Privacy Policy