Skip to content
n-tdi edited this page Feb 21, 2023 · 1 revision

CRUD operations on a row are arguably the most important, and sometimes most difficult parts of SQL! Fortunately for you, Postglam couldn't make the process any easier with the Row object.

Accessing a row

In order for a row object to be created, the row needs to already exist in the table. You can insert a row into a table, or verify that the row already exists with the Table#doesRowExist() method.

If we don't verify that the row is already apart of the table, Postglam will get angry and throw errors!!

Now let's go over how to represent a Row with Postglam. We're going to assume that we have a table with the primary key being a TEXT datatype.

Table table = ...

Row bob = new Row(table, "Bob");

Wonderful! We have our new Bob row, this row already exists inside our table so we are able to represent it with no issue.

Drop a row

Sadly, Bob quit Postglam and decided to become a Marine Biologist, so in order to get him out of our table, we need to use Postglam's drop() method!

Row bob = ...

bob.drop();

Bye bye bob!

Reading and Writing a row

What is being able to represent a row without being able to fetch data from it, and update said data! These next methods use Column(), if you aren't familiar with what a Column is in Postglam, review the Wiki here, but it is not mandatory.

Fetch

In our table, we have a column with each row's age. Now because Bob left our company, we want to get revenge and leak his age to the super secret dark web!

Table table = ...
Row bob = ...

int age = (int) bob.fetch(new Column(table, "age"));

sendToDarkWeb(age); // Note: this method does not actually exist..

the Fetch function returns an object for simplicity, but you can be sure that casting will have no side effects.

Update

Now in order to hide from the police who are investigating the leakers of Bob's age, we need to update our table to have an outdated value. This will get the cops off of our trail!

Thankfully, Postglam has a function for that as well.

Table table = ...
Row bob = ...
Column age = ...

bob.update(age, "5063");

Postglam will make sure that SQL knows what datatype to assign it to, all you need to do is give the value in string form.

Advanced

We've covered most of the a row's methods, but if you want to view the row's current information, Row#toString will output it all in JSON format.

The Javadoc contains method summary and other information.

Clone this wiki locally