Skip to content
n-tdi edited this page Feb 21, 2023 · 3 revisions

A table represents a Postgresql table! You're able to create tables and insert rows into them. If you're interested in modifying or accessing a single row in a table, see Row. If you don't know how to use a database object, you should see Database.

Accessing and Creating a table

By default, if a table does not exist within the Database that we made earlier, it will automatically create one with the given information.

Here is an example of how to create a basic table with Postglam.

Main.java

public class Main {
    public static void main(String[] args) {
        Database database = ...
        database.connect();

        String tableName = "mytable"; // Table name
        Map.Entry<String, Datatypes> primaryKey = Map.entry("id", Datatypes.UUID); // Id column of the table

        LinkedHashMap<String, Datatypes> keys = new LinkedHashMap<>(); // Hashmap of the rest of the columns
        keys.put("name", Datatypes.TEXT);
        keys.put("level", Datatypes.INTEGER);
        keys.put("money", Datatypes.INTEGER);

        Table table = new Table(tableName, primaryKey, keys);
    }
}

Wonderful! We've just created our first table. The code is so simple that it should be self explanatory, but I'll explain it anyway. new Table() takes a string for the name of the table, and then a primary key value. This is the value that will be used for finding rows in the table and such. Lastly, we can put as many other columns and their respective datatypes in the table.

If the table already exists in the Postgresql database, it is important that the column labels and datatypes are exactly correct.

Inserting rows into a table

What is a table without a row or two? Let's go over how to add rows and how Postglam makes your life easier.

Main.java

public class Main {
    public static void main(String[] args) {
        Database database = ...
        database.connect();

        String tableName = "mytable"; // Table name
        Map.Entry<String, Datatypes> primaryKey = Map.entry("id", Datatypes.UUID); // Id column of the table

        LinkedHashMap<String, Datatypes> keys = new LinkedHashMap<>(); // Hashmap of the rest of the columns
        keys.put("name", Datatypes.TEXT);
        keys.put("level", Datatypes.INTEGER);
        keys.put("money", Datatypes.INTEGER);

        Table table = new Table(tableName, primaryKey, keys);

        table.insert(UUID.randomUUID().toString(), "Ted", "1", "200"); // Inserting a row into the table
    }
}

As we can see, Postglam just needs the values for each column. It is important that they are all in the correct order!

"But how does this improve my experience? Strings suck!" - some developer probably

Well that's simple. Postglam uses the columns and keys that you gave it when you first created the table, and assigned each value that was passed into the insert() method and translated it into SQL! This means adding single quotes where single quotes are due and other fun things.

Deleting values from a Table.

Let's supposed we have a table with a bunch of data in it, but there was a bug in some other code that caused a few players to get way to high level! Postglam makes deleting and dropping rows easy-as-pie.

For deleting one specific row, look at the Row wiki

If we wanted to just get a clean and fresh table, we could just call

Table table = ...

table.deleteAllRows(); // Delete all the rows from a table

This is what we need in some cases, but in our pervious statement, we wanted to only delete rows that had a level of 100

Table table = ...

table.deleteAllRowsRowsWhere(new Column(table, "level"), "100"); // Delete all the rows where level = 100

Now this example gets into the use of Columns a little bit, but thats besides the point. Postglam makes it super easy to delete all these rows with a level of 100!

Deleting the table itself

I'm so sick of my data, I just wish all my users information would be lost.

If this is you, then you should look at the Table's #drop() method! Table#drop(); just drops the table out of the database!

Altering

Being able to alter a table is super important. In order to access the Alter subclass, call Table#alter() and you'll be able to use all the methods to their fullest glory.

Postglam helps you out here again, with any change you make to the table, all the data and variables are automatically updated!

Renaming a column

Let's say you had a column called "coins" but you really wanted to rename that to "balance", super simple!

Table table = ...

table.alter().renameColumn(new Column(table, "coins"), "balance");

If you saved the column somewhere instead of making a new one, you should promptly regenerate it as it's name will have changed, causing unexpected side affects.

Renaming a table

Table table = ...

table.alter().rename("ourTable");

It doesn't get much easier!

Adding a column

Now there are two ways you add a new column to a Table. Postglam makes both of them super easy as well.

Let's look at how to add a new column with no default value.

Table table = ...

table.alter().addColumn("Rank", DataTypes.TEXT);

Easy, but every row is going to be NULL by default. If we wanted to add a default value that every row would have before we set it to anything else, we can do it like this!

Table table = ...

table.alter().addColumn("Rank", DataTypes.TEXT, "King");

Beautiful! We've added a default value, so when we add the Rank Column, by default, every row will have the value King in it.

Dropping a column

Table table = ...

table.alter().dropColumn(new Column(table, "level"));

If you need any help understanding what in the hell a Column is. Review the Column wiki!

Altering a table with Postglam has never been easier.

Advanced

Advanced information about method summary can be found in the Javadocs.

Clone this wiki locally