Skip to content

Selector class

Bob edited this page Jun 27, 2018 · 8 revisions

A selector is a class which builds a query to get instances of a class.

super class

Selector is a sub class of the Object class

Methods

Any method on a selector which modifies the query will return the selector, so they can be chained.

constructor( targetClass )

Creates a new instance of a selector for instances of the targetClass, prefered alias is

TargetClass:select()

where(...)

Adds a where clause to the selector, the ammount of arguments are variable, if two variables are passed then they are compared using the "=" comparator. Otherwise the second parameter would be the comparator Examples:

selector:where("x",5)

is the same as

selector:where("x","=",5)

But the second syntax also allows for

selector:where("x",">",4)

orWhere(...)

Same as where() but will use an OR statement in the query instead.

orderBy(...)

Adds an order by statement to the query, the first parameter is the key, and second should be ASC or DESC

limit(limit)

Limits the ammount of instances in the result to the limit parameter

generateQuery()

Internal function to generate the mysql query

get(callback)

Returns the instances of the class to the callback in a table

first(callback)

Returns a single instance of the class to the callback

handleFirst(callback,data)

Internal function to handle the result of a first call, do not call manually.

handleGet(callback,data)

Internal function to handle the result of a get call, do not call manually

Example:

Player:select():where("name",getPlayerName(playerElement)):get(callback)

The resulting query of the example would be:

SELECT * FROM `player` WHERE `name` = ?

The resulting rows will then be used to create instances of the class the select() method was called on, in this example the Player class. And these would be returned to the callback function as table of the instances.