( schemaName: string, cursor: CursorV2 )
| 209 | } |
| 210 | |
| 211 | export function parseColumnConstraint( |
| 212 | schemaName: string, |
| 213 | cursor: CursorV2 |
| 214 | ): DatabaseTableColumnConstraint | undefined { |
| 215 | if (cursor.match("CONSTRAINT")) { |
| 216 | cursor.next(); |
| 217 | const constraintName = cursor.consume(); |
| 218 | |
| 219 | return { |
| 220 | ...parseColumnConstraint(schemaName, cursor), |
| 221 | name: constraintName, |
| 222 | }; |
| 223 | } else if (cursor.match("PRIMARY")) { |
| 224 | let primaryKeyOrder: SqlOrder | undefined; |
| 225 | let primaryColumns: string[] | undefined; |
| 226 | let autoIncrement = false; |
| 227 | |
| 228 | cursor.next(); |
| 229 | if (!cursor.match("KEY")) throw new Error("PRIMARY must follow by KEY"); |
| 230 | |
| 231 | cursor.next(); |
| 232 | |
| 233 | if (cursor.match("(")) { |
| 234 | primaryColumns = parseColumnList(cursor.consumeParen()); |
| 235 | } |
| 236 | |
| 237 | if (cursor.match("ASC")) { |
| 238 | primaryKeyOrder = "ASC"; |
| 239 | cursor.next(); |
| 240 | } else if (cursor.match("DESC")) { |
| 241 | primaryKeyOrder = "DESC"; |
| 242 | cursor.next(); |
| 243 | } |
| 244 | |
| 245 | const conflict = parseConstraintConflict(cursor); |
| 246 | |
| 247 | if (cursor.match("AUTOINCREMENT")) { |
| 248 | autoIncrement = true; |
| 249 | cursor.next(); |
| 250 | } |
| 251 | |
| 252 | return { |
| 253 | primaryKey: true, |
| 254 | primaryKeyOrder, |
| 255 | primaryColumns, |
| 256 | autoIncrement, |
| 257 | primaryKeyConflict: conflict, |
| 258 | ...parseColumnConstraint(schemaName, cursor), |
| 259 | }; |
| 260 | } else if (cursor.match("NOT")) { |
| 261 | cursor.next(); |
| 262 | if (!cursor.match("NULL")) throw new Error("NOT should follow by NULL"); |
| 263 | cursor.next(); |
| 264 | |
| 265 | const conflict = parseConstraintConflict(cursor); |
| 266 | return { |
| 267 | notNull: true, |
| 268 | notNullConflict: conflict, |
searching dependent graphs…