SqTable derives from SqEntry. It is used to define database tables. It use SqType to define columns and C structure's members.
SqEntry
│
└─── SqReentry
│
└─── SqTable
SqTable must be used with SqSchema and SqColumn to create a table. You can see below documents to get more information and sample:
Use C language
typedef struct User User;
SqTable *table;
SqType *tableType = NULL;
// Using functions of SqSchema to create tables (Recommend)
table = sq_schema_create(schema, "users", User);
// Using sq_table_new() to create tables
table = sq_table_new("users", tableType);
sq_schema_add(schema, table);
Use C++ language
typedef struct User User;
Sq::Table *table;
Sq::Type *tableType = NULL;
// Using methods of Sq::Schema to create tables (Recommend)
table = schema->create<User>("users");
// Using constructor of Sq::Table to create tables
table = new Sq::Table("users", tableType);
schema->add(table);
Below C++ methods (C functions) are correspond to the different types of columns that you can add to table.
Most methods (functions) set a specific SqType to the column.
C++ methods | C functions | C data type | SQL data type | specified SqType |
---|---|---|---|---|
boolean | sq_table_add_boolean | bool | BOOLEAN | SQ_TYPE_BOOL |
bool_ | sq_table_add_bool | bool | BOOLEAN | SQ_TYPE_BOOL |
integer | sq_table_add_integer | int | INT | SQ_TYPE_INT |
int_ | sq_table_add_int | int | INT | SQ_TYPE_INT |
uint | sq_table_add_uint | unsigned int | INT (UNSIGNED) | SQ_TYPE_UINT |
int64 | sq_table_add_int64 | int64_t | BIGINT | SQ_TYPE_INT64 |
uint64 | sq_table_add_uint64 | uint64_t | BIGINT (UNSIGNED) | SQ_TYPE_UINT64 |
timestamp | sq_table_add_timestamp | time_t | TIMESTAMP | SQ_TYPE_TIME |
timestamps | sq_table_add_timestamps | time_t x 2 | TIMESTAMP x 2 | SQ_TYPE_TIME x 2 |
double_ | sq_table_add_double | double | DOUBLE | SQ_TYPE_DOUBLE |
str | sq_table_add_str | char* | VARCHAR | SQ_TYPE_STR |
string | sq_table_add_string | char* | VARCHAR | SQ_TYPE_STR |
char_ | sq_table_add_char | char* | CHAR | SQ_TYPE_CHAR |
text | sq_table_add_text | char* default | TEXT | SQ_TYPE_STR default |
clob | sq_table_add_clob | char* default | CLOB | SQ_TYPE_STR default |
blob | sq_table_add_blob | SqBuffer | BLOB or BINARY | SQ_TYPE_BUFFER |
binary | sq_table_add_binary | SqBuffer | BLOB or BINARY | SQ_TYPE_BUFFER |
custom | sq_table_add_custom | User define | VARCHAR | User define |
mapping | sq_table_add_mapping | User specify | User specify | User specify |
Below method is for C++ data type only.
C++ methods | C++ data type | SQL data type | specified SqType |
---|---|---|---|
stdstring | std::string | VARCHAR | SQ_TYPE_STD_STRING |
stdstr | std::string | VARCHAR | SQ_TYPE_STD_STRING |
stdvector | std::vector | BLOB or BINARY | SQ_TYPE_STD_VECTOR |
stdvec | std::vector | BLOB or BINARY | SQ_TYPE_STD_VECTOR |
Use custom or JSON type
If you want to store JSON object or array in database column, you can use sq_table_add_custom(), sq_table_add_mapping() or C++ method custom(), mapping() to create column.
When adding a column that uses a custom type, you must specify SqType.
Example: define a C structure that contain struct and array.
struct DemoTable {
// ...
// MyStructure is user-defined C struct
// This will store JSON object in database column.
MyStructure myStruct;
// SqIntArray is integer array that defined in SqArray.h
// This will store JSON integer array in database column.
SqIntArray intArray;
};
In the following example,
SQ_TYPE_MY_STRUCT is the user-defined SqType of MyStructure.
SQ_TYPE_INT_ARRAY is declared in SqType.h and parses JSON integer array from value of database column.
use C language
// JSON object will be stored in VARCHAR column if SQL type is not specified.
column = sq_table_add_custom(table, "myStruct", offsetof(DemoTable, myStruct),
SQ_TYPE_MY_STRUCT, 128);
// JSON integer array will be stored in VARCHAR column if SQL type is not specified.
column = sq_table_add_custom(table, "intArray", offsetof(DemoTable, intArray),
SQ_TYPE_INT_ARRAY, 96);
use C++ language
// JSON object will be stored in VARCHAR column if SQL type is not specified.
column = table->custom("myStruct", offsetof(DemoTable, myStruct),
SQ_TYPE_MY_STRUCT, 128);
// JSON integer array will be stored in VARCHAR column if SQL type is not specified.
column = table->custom("intArray", offsetof(DemoTable, intArray),
SQ_TYPE_INT_ARRAY, 96);
You can also use pointer to member if you don't want to use offsetof().
// use '&DemoTable::myStruct' to replace 'offsetof(DemoTable, myStruct)'
column = table->custom("myStruct", &DemoTable::myStruct,
SQ_TYPE_MY_STRUCT, 128);
// use '&DemoTable::intArray' to replace 'offsetof(DemoTable, intArray)'
column = table->custom("intArray", &DemoTable::intArray,
SQ_TYPE_INT_ARRAY, 96);
Use type mapping
To use type mapping, you must map SqType to the SQL data type when creating column. Of course you can also map to user-defined SqType.
Below are Library-defined common SQL data type for type mapping.
Library-defined SQL data types | Library-provided SqType |
---|---|
SQ_SQL_TYPE_BOOLEAN | SQ_TYPE_BOOL |
SQ_SQL_TYPE_INT | SQ_TYPE_INT |
SQ_SQL_TYPE_INT_UNSIGNED | SQ_TYPE_UINT |
SQ_SQL_TYPE_BIGINT | SQ_TYPE_INT64 |
SQ_SQL_TYPE_BIGINT_UNSIGNED | SQ_TYPE_UINT64 |
SQ_SQL_TYPE_TIMESTAMP | SQ_TYPE_TIME |
SQ_SQL_TYPE_DOUBLE | SQ_TYPE_DOUBLE |
SQ_SQL_TYPE_VARCHAR | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_CHAR | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_TEXT | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_CLOB | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_BLOB | SQ_TYPE_BUFFER, SQ_TYPE_STD_VECTOR |
SQ_SQL_TYPE_DECIMAL | SQ_TYPE_DOUBLE, SQ_TYPE_INT |
SQ_SQL_TYPE_TINYINT | SQ_TYPE_INT |
SQ_SQL_TYPE_TINYINT_UNSIGNED | SQ_TYPE_UINT |
SQ_SQL_TYPE_SMALLINT | SQ_TYPE_INT |
SQ_SQL_TYPE_SMALLINT_UNSIGNED | SQ_TYPE_UINT |
SQ_SQL_TYPE_MEDIUMINT | SQ_TYPE_INT |
SQ_SQL_TYPE_MEDIUMINT_UNSIGNED | SQ_TYPE_UINT |
SQ_SQL_TYPE_TINYTEXT | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_MEDIUMTEXT | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
SQ_SQL_TYPE_LONGTEXT | SQ_TYPE_STRING, SQ_TYPE_STD_STRING |
The following SQL data types are aliases:
Library-defined SQL data types | alias of SQ_SQL_XXXX |
---|---|
SQ_SQL_TYPE_BINARY | alias of SQ_SQL_TYPE_BLOB |
SQ_SQL_TYPE_UNSIGNED_INT | alias of SQ_SQL_TYPE_INT_UNSIGNED |
SQ_SQL_TYPE_UNSIGNED_BIGINT | alias of SQ_SQL_TYPE_BIGINT_UNSIGNED |
SQ_SQL_TYPE_UNSIGNED_TINYINT | alias of SQ_SQL_TYPE_TINYINT_UNSIGNED |
SQ_SQL_TYPE_UNSIGNED_SMALLINT | alias of SQ_SQL_TYPE_SMALLINT_UNSIGNED |
SQ_SQL_TYPE_UNSIGNED_MEDIUMINT | alias of SQ_SQL_TYPE_MEDIUMINT_UNSIGNED |
Example: map SqType SQ_TYPE_INT_ARRAY to SQL data type TEXT
use C language
// JSON integer array will be stored in SQL TEXT column.
column = sq_table_add_mapping(table, "intArray", offsetof(DemoTable, intArray),
SQ_TYPE_INT_ARRAY,
SQ_SQL_TYPE_TEXT);
use C++ language
// JSON integer array will be stored in SQL TEXT column.
column = table->mapping("intArray", offsetof(DemoTable, intArray),
SQ_TYPE_INT_ARRAY,
SQ_SQL_TYPE_TEXT);
C function sq_table_add_column() and sq_table_add_column_ptrs(), C++ method addColumn() can add existed columns.
sq_table_add_column(), addColumn() can add SqColumn array.
sq_table_add_column_ptrs(), addColumn() can add SqColumn pointer array.
You can see schema-builder-constant.md to get more information and sample.
sq_table_drop_column() must specify the column name to drop. This does not drop the column before doing migration because it just add a record of "Remove Column" to the 'table'.
// C function
sq_table_drop_column(table, "columnName");
// C++ method
table->dropColumn("columnName");
Like sq_schema_drop(), sq_schema_rename() does not rename the table immediately.
// C function
sq_table_rename_column(table, "old_name", "new_name");
// C++ method
table->renameColumn("old_name", "new_name");
C function sq_table_find_column(), C++ method findColumn() can find column by name.
// C function
column = sq_table_find_column(table, "columnName");
// C++ method
column = table->findColumn("columnName");
C function sq_table_get_primary(), C++ method getPrimary() can get primary key.
// C function
column = sq_table_get_primary(table, NULL);
// C++ method
column = table->getPrimary();
Create index
To create a composite index, you can pass multiple columns to index() method. If you use C function sq_table_add_index() to create index, the last argument must be NULL.
// C function
column = sq_table_add_index(table, "indexName", "columnName01", "columnName02", NULL);
// C++ method
column = table->index("indexName", "columnName01", "columnName02");
Drop index
To drop an index, you must specify the index's name.
// C function
sq_table_drop_index(table, "indexName");
// C++ method
table->dropIndex("indexName");
Create unique
To create a composite unique, you can pass multiple columns to unique() method. If you use C function sq_table_add_unique() to create unique, the last argument must be NULL.
// C function
column = sq_table_add_unique(table, "uniqueName", "columnName01", "columnName02", NULL);
// C++ method
column = table->unique("uniqueName", "columnName01", "columnName02");
Drop unique
To drop an unique, you must specify the unique's name.
// C function
sq_table_drop_unique(table, "uniqueName");
// C++ method
table->dropUnique("uniqueName");
Create primary key
To create a composite primary key, you can pass multiple columns to primary() method. If you use C function sq_table_add_primary() to create primary key, the last argument must be NULL.
// C function
column = sq_table_add_primary(table, "primaryName", "columnName01", "columnName02", NULL);
// C++ method
column = table->primary("primaryName", "columnName01", "columnName02");
Drop primary key
To drop an primary key, you must specify the primary key's name.
// C function
sq_table_drop_primary(table, "primaryName");
// C++ method
table->dropPrimary("primaryName");
Create foreign key
To create a composite foreign key, you can pass multiple columns to foreign() method. If you use C function sq_table_add_foreign() to create foreign key, the last argument must be NULL.
Because number of columns in foreign key must match the number of columns in the referenced table, number of columns in foreign() and reference() arguments must match.
// C function
// The last argument of sq_table_add_foreign() must be NULL.
column = sq_table_add_foreign(table, "foreignName", "column1", "column2", NULL);
sq_column_reference(column, "foreignTableName", "column1", "column2", NULL);
// C++ method
column = table->foreign("foreignName", "column1", "column2");
column->reference("foreignTableName", "column1", "column2");
Drop foreign key
To drop an foreign key, you must specify the foreign key's name.
// C function
sq_table_drop_foreign(table, "foreignName");
// C++ method
table->dropForeign("foreignName");
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。