@@ -38,6 +38,8 @@ struct Foo {
3838- ` [fkey: 'parent_id'] ` sets foreign key for an field which holds an array
3939
4040## Usage
41+ > [ !NOTE]
42+ > For using the Function Call API for ` orm ` , please check [ ` Function Call API ` ] ( #function-call-api ) .
4143
4244Here are a couple example structs showing most of the features outlined above.
4345
@@ -246,3 +248,106 @@ fn main() {
246248 }!
247249}
248250```
251+
252+ ## Function Call API
253+ You can utilize the ` Function Call API ` to work with ` ORM ` . It provides the
254+ capability to dynamically construct SQL statements. The Function Call API
255+ supports common operations such as ` Create Table ` /` Drop Table ` /` Insert ` /` Delete ` /` Update ` /` Select ` ,
256+ and offers convenient yet powerful features for constructing ` WHERE ` clauses,
257+ ` SET ` clauses, ` SELECT ` clauses, and more.
258+
259+ A complete example is available [ here] ( https://github.com/vlang/v/blob/master/vlib/orm/orm_func_test.v ) .
260+
261+ Below, we illustrate its usage through several examples.
262+
263+ 1. Define your struct with the same method definitions as before:
264+
265+ ``` v ignore
266+ @[table: 'sys_users']
267+ struct User {
268+ id int @[primary;serial]
269+ name string
270+ age int
271+ role string
272+ status int
273+ salary int
274+ title string
275+ score int
276+ created_at ?time.Time @[sql_type: 'TIMESTAMP']
277+ }
278+ ```
279+
280+ 2. Create a database connection:
281+
282+ ``` v ignore
283+ mut db := sqlite.connect(':memory:')!
284+ defer { db.close() or {} }
285+ ```
286+
287+ 3 . Create a ` QueryBuilder ` (which also completes struct mapping):
288+
289+ ``` v ignore
290+ mut qb := orm.new_query[User](db)
291+ ```
292+
293+ 4 . Create a database table:
294+
295+ ``` v ignore
296+ qb.create()!
297+ ```
298+
299+ 5 . Insert multiple records into the table:
300+
301+ ``` v ignore
302+ qb.insert_many(users)!
303+ ```
304+
305+ 6 . Delete records (note: ` delete() ` must follow ` where() ` ):
306+
307+ ``` v ignore
308+ qb.where('name = ?','John')!.delete()!
309+ ```
310+
311+ 7 . Query records (you can specify fields of interest via ` select ` ):
312+
313+ ``` v ignore
314+ // Returns []User with only 'name' populated; other fields are zero values.
315+ only_names := qb.select('name')!.query()!
316+ ```
317+
318+ 8 . Update records (note: ` update() ` must be placed last):
319+
320+ ``` v ignore
321+ qb.set('age = ?, title = ?', 71, 'boss')!.where('name = ?','John')!.update()!
322+ ```
323+
324+ 9 . Drop the table:
325+
326+ ``` v ignore
327+ qb.drop()!
328+ ```
329+
330+ 10 . Chainable method calls:
331+ Most Function Call API support chainable calls, allowing easy method chaining:
332+
333+ ``` v ignore
334+ final_users :=
335+ qb
336+ .drop()!
337+ .create()!
338+ .insert_many(users)!
339+ .set('name = ?', 'haha')!.where('name = ?', 'Tom')!.update()!
340+ .where('age >= ?', 30)!.delete()!
341+ .query()!
342+ ```
343+
344+ 11 . Writing complex nested ` WHERE ` clauses:
345+ The API includes a built-in parser to handle intricate ` WHERE ` clause conditions. For example:
346+
347+ ``` v ignore
348+ where('created_at IS NULL && ((salary > ? && age < ?) || (role LIKE ?))', 2000, 30, '%employee%')!
349+ ```
350+
351+ Note the use of placeholders ` ? ` .
352+ The conditional expressions support logical operators including ` AND ` , ` OR ` , ` || ` , and ` && ` .
353+
0 commit comments