com.aoindustries.aoserv.client.mysql.Table_Name Maven / Gradle / Ivy
/*
* aoserv-client - Java client for the AOServ Platform.
* Copyright (C) 2010-2013, 2016, 2017, 2018, 2019 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.mysql;
import static com.aoindustries.aoserv.client.mysql.ApplicationResources.accessor;
import com.aoindustries.dto.DtoFactory;
import com.aoindustries.validation.InvalidResult;
import com.aoindustries.validation.ValidResult;
import com.aoindustries.validation.ValidationException;
import com.aoindustries.validation.ValidationResult;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectInputValidation;
import java.io.Serializable;
/**
* Represents a name that may be used for a MySQL table. Table names must:
*
* - Be non-null
* - Be non-empty
* - Be between 1 and 64 characters
* - Must start with
[a-z,A-Z,_]
* - The rest of the characters may contain [a-z], [A-Z], [0-9], underscore (_), hyphen (-), or dollar ($)
*
*
* @author AO Industries, Inc.
*/
// TODO: PostgreSQL type will be "Table.Name" - move to inner class of Table to match, once there is a "Table" class.
final public class Table_Name implements
Comparable,
Serializable,
ObjectInputValidation,
DtoFactory
{
private static final long serialVersionUID = -4427431696460618301L;
/**
* The longest name allowed for a MySQL table name.
*/
public static final int MAX_LENGTH = 64;
/**
* Validates a MySQL table name.
*/
// TODO: Add other characters allowed in Database.Name, such as space
public static ValidationResult validate(String name) {
if(name==null) return new InvalidResult(accessor, "Table.Name.validate.isNull");
int len = name.length();
if(len==0) return new InvalidResult(accessor, "Table.Name.validate.isEmpty");
if(len > MAX_LENGTH) return new InvalidResult(accessor, "Table.Name.validate.tooLong", MAX_LENGTH, len);
// The first character must be [a-z], [A-Z], [0-9], or _
char ch = name.charAt(0);
if(
(ch < 'a' || ch > 'z')
&& (ch < 'A' || ch > 'Z')
&& (ch < '0' || ch > '9')
&& ch != '_'
) return new InvalidResult(accessor, "Table.Name.validate.badFirstCharacter");
// The rest may have additional characters
for (int c = 1; c < len; c++) {
ch = name.charAt(c);
if (
(ch<'a' || ch>'z')
&& (ch<'A' || ch>'Z')
&& (ch<'0' || ch>'9')
&& ch != '_'
&& ch != '-'
&& ch != '$'
) return new InvalidResult(accessor, "Table.Name.validate.illegalCharacter");
}
return ValidResult.getInstance();
}
/**
* @param name when {@code null}, returns {@code null}
*/
public static Table_Name valueOf(String name) throws ValidationException {
if(name == null) return null;
return new Table_Name(name);
}
final private String name;
private Table_Name(String name) throws ValidationException {
this.name = name;
validate();
}
private void validate() throws ValidationException {
ValidationResult result = validate(name);
if(!result.isValid()) throw new ValidationException(result);
}
/**
* Perform same validation as constructor on readObject.
*/
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
validateObject();
}
@Override
public void validateObject() throws InvalidObjectException {
try {
validate();
} catch(ValidationException err) {
InvalidObjectException newErr = new InvalidObjectException(err.getMessage());
newErr.initCause(err);
throw newErr;
}
}
@Override
public boolean equals(Object O) {
return
O!=null
&& O instanceof Table_Name
&& name.equals(((Table_Name)O).name)
;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public int compareTo(Table_Name other) {
return this==other ? 0 : name.compareTo(other.name);
}
@Override
public String toString() {
return name;
}
@Override
public com.aoindustries.aoserv.client.dto.MySQLTableName getDto() {
return new com.aoindustries.aoserv.client.dto.MySQLTableName(name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy