Skip to content

Instantly share code, notes, and snippets.

@CuberL

CuberL/spanner_create_table.pegjs Secret

Last active Jun 9, 2020
Embed
What would you like to do?
Spanner Create Table
DDL =
CreateTable table_name:(TableName) LeftPara column_defs:(ColumnDefs) RightPara primary_key:(PrimaryKey?) Semi? {
return {
table_name,
column_defs,
primary_key
}
}
CreateTable =
_ "create"i _ "table"i _
Identity =
_ identity:([a-zA-Z][0-9a-zA-Z]+) _ {
return [
identity[0],
...(identity.length === 2 ? identity[1].join('') : [])
].join('')
}
TableName = Identity
ColumnDef =
_ name:(Identity) type:(Type) not_null:(NotNull?) options:(Options?) _ {
return {
name,
type,
not_null: Boolean(not_null),
options
}
}
ColumnDefs =
head:((ColumnDef _ Comma)*) tail:(ColumnDef?) _ {
return [
...head.map(item => item[0]),
...(tail ? [tail] : [])
]
}
Type =
_ "int64"i _ {
return "int64"
} /
_ "bool"i _ {
return "bool"
} /
_ "float64"i _{
return "float64"
} /
_ "date"i _{
return "date"
} /
_ "timestamp"i _{
return "timestamp"
} /
_ "string"i LeftPara length:(Length) RightPara _ {
return `string(${length})`
} /
_ "bytes"i LeftPara length:(Length) RightPara _ {
return `bytes(${length})`
}
NotNull =
_ "not"i _ "null"i _ {
return true
}
DecNumber = _ dec:([0-9]+) _ {
return parseInt(dec.join(''))
}
HexNumber = _ "0x" dec:(DecNumber) _ {
return parseInt(`0x${dec}`, 16);
}
Number =
_ num:(HexNumber) _ {
return num
} /
_ num:(DecNumber) _ {
return num
}
Length =
_ Number _ /
_ "MAX"i _ {
return "MAX"
}
Key =
_ name:(Identity) seq:(("ASC"/ "DESC")?) _ {
return {
name,
seq: seq.toLowerCase() || "asc"
}
}
Keys =
_ head:((Key Comma)*) tail:(Key/_) _ {
return [
...head.map(item => item[0]),
...(tail ? [tail] : [])
]
}
Options =
_ "OPTIONS"i LeftPara "allow_commit_timestamp" Equal allow_commit_timestamp:("true"/"null") RightPara _ {
return {
allow_commit_timestamp: Boolean(allow_commit_timestamp)
}
}
PrimaryKey =
_ "primary"i _ "key"i LeftPara keys:(Keys) RightPara _ {
return {
keys
}
}
LeftPara = _ "(" _
RightPara = _ ")" _
Semi = _ ";" _
Comma = _ "," _
Equal = _ "=" _
WhiteSpace =
[ \r\n\t]
_ =
WhiteSpace* {
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.