Skip to content

Chained CRUD Operations

This document details the chained query builder of the cDataBase class, providing an elegant ORM-like API without writing complex SQL manually.

Detailed Documentation

TopicDescription
Condition MethodsWhere, OrWhere, WhereIn, WhereLike, WhereNull, etc.
Sort/Group/Join/SelectionOrderBy, GroupBy, Having, Join, Columns, Distinct
Limit & PaginationLimit, Offset, Page
Terminal MethodsRowCreate, RowRead, RowUpdate, RowDelete, Field
Aggregate MethodsRowCount, RowExists, RowPluck, RowMax/Min/Sum/Avg
Examples & Best PracticesPractical examples, SQL comparison, FAQ, best practices

Overview

What is Chained CRUD

Chained CRUD is an elegant database operation approach provided by the cDataBase class. It builds SQL statements progressively through Method Chaining, ultimately executing operations via terminal methods.

Core Advantages:

  • High Readability - Each chained method corresponds to a SQL clause with clear semantics
  • Type Safety - Method parameters are automatically escaped, reducing SQL injection risk
  • Cross-Database Compatibility - Automatically adapts to MySQL / SQL Server / Access syntax differences
  • Zero Handwritten SQL - Common operations like sorting, grouping, pagination, JOINs require no string concatenation

Design Philosophy

Chained Call Flow:

  Starting Method → Condition Methods(stackable) → Modifier Methods(stackable) → Terminal Method
  ─────────────   ───────────────   ───────────────   ──────────
  Table()         Where()           Columns()          RowRead
                  WhereIn()         OrderBy()           RowCreate
                  WhereLike()       GroupBy()           RowUpdate
                  OrWhere()         Join()              RowDelete
                  ...               Limit()             RowCount
                                    Page()              RowExists
                                                        RowPluck
                                                        RowMax/Min/Sum/Avg

Chained Builder Architecture

Internal State Variables

The chained builder accumulates each clause through a set of private variables. Terminal methods automatically reset the builder state after execution (ResetBuilder):

VariableSQL ClauseSetting Method
pvTableNameFROMTable()
pvSelectFieldsSELECT column listColumns()
pvIsDistinctSELECT DISTINCTDistinct()
pvWhereConditionWHERE (AND conditions)Where(), WhereLike()
pvOrWhereConditionWHERE (OR conditions)OrWhere()
pvWhereInClausesWHERE IN/NOT INWhereIn(), WhereNotIn()
pvWhereBetweenClausesWHERE BETWEENWhereBetween()
pvWhereNullClausesWHERE IS NULLWhereNull(), WhereNotNull()
pvOrderByClauseORDER BYOrderBy(), OrderByDesc(), OrderByRaw()
pvGroupByClauseGROUP BYGroupBy()
pvHavingClauseHAVINGHaving()
pvJoinClausesJOINJoin(), LeftJoin(), RightJoin()
pvLimitValueLIMITLimit()
pvOffsetValueOFFSETOffset()
pvPageNum/LimitADO PaginationPage()
pvFieldsINSERT/UPDATE valuesField()

SQL Assembly Logic

Terminal methods call BuildSelectSql() to assemble the complete SQL:

sql
SELECT [DISTINCT] {columns} FROM {table} {JOIN clauses}
WHERE {Where + WhereIn + WhereBetween + WhereNull} [OR (OrWhere)]
GROUP BY {group} HAVING {filter}
ORDER BY {sort}
LIMIT/OFFSET or ADO pagination

Starting Methods

Table - Set Operation Table

Sets the table name for the current operation. The starting point for all chained CRUD operations.

Syntax

vb
Function Table(ByVal TableName As String) As cDataBase

Example

vb
' Basic usage
db.Table("users").RowRead

' With Where query
db.Table("users").Where("age>?", 18).RowRead

' With Field insert
db.Table("users").Field("name", "Zhang San").Field("age", 25).RowCreate

Complete Method Reference

Chained Methods (return cDataBase, allowing continued chaining)

CategoryMethodDescriptionSQL Clause
StartTable(table)Set operation tableFROM
ConditionWhere(cond,params...)AND conditionWHERE
OrWhere(cond,params...)OR conditionWHERE ... OR (...)
WhereIn(field,values)IN conditionWHERE field IN (...)
WhereNotIn(field,values)NOT IN conditionWHERE field NOT IN (...)
WhereBetween(field,V1,V2)BETWEEN conditionWHERE field BETWEEN V1 AND V2
WhereNotBetween(field,V1,V2)NOT BETWEENWHERE field NOT BETWEEN ...
WhereLike(field,pattern)LIKE fuzzy matchWHERE field LIKE 'pattern'
WhereNotLike(field,pattern)NOT LIKEWHERE field NOT LIKE 'pattern'
WhereNull(field)IS NULLWHERE field IS NULL
WhereNotNull(field)IS NOT NULLWHERE field IS NOT NULL
SortOrderBy(field)ASC sortingORDER BY field ASC
OrderByDesc(field)DESC sortingORDER BY field DESC
OrderByRaw(clause)Custom sortingORDER BY clause
GroupGroupBy(field)GroupingGROUP BY field
Having(condition)Group filterHAVING condition
JoinJoin(table,on)INNER JOININNER JOIN table ON on
LeftJoin(table,on)LEFT JOINLEFT JOIN table ON on
RightJoin(table,on)RIGHT JOINRIGHT JOIN table ON on
SelectColumns(cols...)Specify query columnsSELECT columns
Distinct()DistinctSELECT DISTINCT
ValueField(field,value)Set field valueINSERT/UPDATE values
LimitLimit(N)Limit countLIMIT N
Offset(N)OffsetOFFSET N
Page(num,perpage)ADO paginationRs.PageSize + AbsolutePage

Terminal Methods (execute operation, reset builder)

MethodReturnDescription
RowCreatecDataBaseCreate new row
RowReadcDataBaseQuery rows (results in Rs)
RowUpdatecDataBaseUpdate row
RowDeletecDataBaseDelete row
RowCountLongCount rows
RowExistsBooleanCheck if exists
RowPluck(field)CollectionExtract single column values
RowMax(field)VariantAggregate MAX
RowMin(field)VariantAggregate MIN
RowSum(field)VariantAggregate SUM
RowAvg(field)VariantAggregate AVG

Last Updated: 2026-06-26

VB6 and LOGO copyright of Microsoft Corporation