Documentation ¶
Overview ¶
Package attributes analyzes a returned relations attributes, maintaining order. This is useful for determining the relation schema that a query / CTE returns.
For example, given the following query:
WITH satoshi_posts AS ( SELECT id, title, content FROM posts WHERE user_id = ( SELECT id FROM users WHERE username = 'satoshi' LIMIT 1 ) ) SELECT id, title FROM satoshi_posts;
The attributes package will be able to determine that:
- The result of this query is a relation with two attributes: id and title
- The result of the common table expression satoshi_posts is a relation with three attributes: id, title, and content
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidReturnExpression = fmt.Errorf("expression cannot be used as a result column")
ErrInvalidReturnExpression is returned when an expression cannot be used as a result column
Functions ¶
func TableFromAttributes ¶
func TableFromAttributes(tableName string, attrs []*RelationAttribute, withPrimaryKey bool) (*common.Table, error)
TableFromAttributes generates a table structure from a list of relation attributes. It will do it's best to interpret the proper name for the attributes. If a column is given (either as a table.column or just column), it will apply the new table name. If any other expression is given (math on top of a column, aggregate, etc), it will enforce that an alias is given in the ResultColumnExpression of the relation attribute. The types of ambiguous naming supported by SQLite for CTEs is not clear from their docs, so this is to be safe. It takes a boolean to determine if a primary key should be added to the table. If true, the primary key is simply a composite key of all of the columns in the table. If it will return two columns of the same name, it will add a suffix of ":1", ":2", etc.
Types ¶
type RelationAttribute ¶
type RelationAttribute struct { // ResultExpression is the expression that represents the attribute // This can be things like "column_name", "table"."column_name", "sum(column_name)"", "5", etc. ResultExpression *tree.ResultColumnExpression // Type is the data type of the attribute Type common.DataType }
A RelationAttribute is a column or expression that is part of a relation. It contains the logical representation of the attribute, as well as the data type
func GetSelectCoreRelationAttributes ¶
func GetSelectCoreRelationAttributes(selectCore *tree.SelectCore, tables []*common.Table) ([]*RelationAttribute, error)
GetSelectCoreRelationAttributes will analyze the select core and return the identified relation. It returns a list of result column expressions. These can be things like: tbl1.col, col, col AS alias, col*5 AS alias, etc. If a statement has "SELECT * FROM tbl", then the result column expressions will be tbl.col_1, tbl.col_2, etc.