R is Read

From Onix OS Wiki
Jump to: navigation, search

This is the method used to read data or to extract and assign data to an object.

Definition

def resultdef = (fetch(modeldef, {...keys and values}))

Data extraction can be "read()" anywhere, but the fetch () method is used here. We have to fetch "fetch()" instead of "read()" in the system because it is used to read the file. Here method connects to the model and draws data. To use this, you need to make a description. The definition is a definition that contains an array of data automatically. Results come in arrays or hash.

def resultdef = (query(modeldef, "YOUR QUERY....", params))

Under some circumstances, you may need to send plain text. If hash definitions are insufficient, you can also read the data with a plain query. To do this, use the "query()" function.

Example

def db = (database("sqlite3","sqlite_database_example.db"))

def payment = (model(db, "payments", {
	"user_id": "int",
	"money": "int",
	"currency": "text",
	"datetime": "DATETIME",
	"duedate": "DATETIME"
}))

def payment_list = (fetch(payment, {"id": 1})

inspect(payment_list)

# => ...data hash

In the example, we pulled a series of data from the "payments" model and synchronized it to an object. We also wrote the database and model in terms of being an example to you before.

def db = (database("sqlite3","sqlite_database_example.db"))

def user = (model(db, "users", {
	"id": "int"
	"name": "text",
	"surname": "text",
	"username": "text",
	"email": "text",
	"password": "text",
}))

def limited_users = (query(user, "SELECT * FROM users LIMIT ?", 100))

# => ...hash data...

Sometimes you may need to use very different queries when you are importing data. For example, we made a constraint on the user table and asked for only 100 data with SQL query. The results were returned to us as hash and array.