Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unique index without space + more fixes #16

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions grammar/13-01-14-create-table.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TABLE_PROPERTIES
props.comment = comment;
return props;
}
/ "IF"i _ "NOT"i _ "EXIST" "S"i? _ props:TABLE_PROPERTIES {
/ "IF"i _ "NOT"i _ "EXIST"i "S"i? _ props:TABLE_PROPERTIES {
props.ifNotExists = true;
return props;
}
Expand Down Expand Up @@ -94,9 +94,13 @@ CREATE_DEFINITION_CONSTRAINT
references: ref
};
}
/ "UNIQUE"i __ type:("KEY"i/"INDEX"i)?
name:(__ name:(ID/STRING) {return name;})? _ "(" _ idlist:ID_LIST ")" _
/ "UNIQUE"i _ type:("KEY"i/"INDEX"i)?
name:(_ name:(ID/STRING) {return name;})? _ "(" _ idlist:INDEX_COL_NAME_LIST ")" _
{
if(!key && name && name.match(/KEY|INDEX/i)) {
key = name
name = undefined
}
var key = {
type: "CONSTRAINT",
constraint: (type ? type.toUpperCase() : 'INDEX'),
Expand All @@ -108,7 +112,7 @@ CREATE_DEFINITION_CONSTRAINT
return key;
}
/ unique:("UNIQUE"i __)? type:("KEY"i/"INDEX"i)
name:(__ name:(ID/STRING) {return name;})? _ "(" _ idlist:ID_LIST ")" _
name:(__ name:(ID/STRING) {return name;})? _ "(" _ idlist:INDEX_COL_NAME_LIST ")" _
{
var key = {
type: "CONSTRAINT",
Expand Down Expand Up @@ -148,6 +152,19 @@ ID_LIST
STRING_ID_LIST
= id:(STRING/ID) _ rest:(',' _ id2:(STRING/ID) _ { return id2; })* { rest.unshift(id); return rest; }

INDEX_COL_NAME_LIST
= index_col_name:INDEX_COL_NAME _ rest:(',' _ index_col_name2:INDEX_COL_NAME _ { return index_col_name2; })* { rest.unshift(index_col_name); return rest; }

INDEX_COL_NAME
= id:ID length:TYPE_LENGTH {
var key = {
id: id,
}
if(length)
key.length = length
return key
}


NUMERIC_TYPE_LENGTH
= _ "(" _ length:POSITIVE_INTEGER _ "," _ decimals:POSITIVE_INTEGER _ ")" {
Expand Down Expand Up @@ -206,6 +223,13 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
if(length) props.length = length;
return props;
}
/ _ ("VARBINARY"i/"VARCHAR"i) length:TYPE_LENGTH _ "BINARY"i _ props:COLUMN_TYPE_PROPERTIES {
if(props.type)
throw new SyntaxError("Ambiguous type");
props.type = 'VARBINARY';
if(length) props.length = length;
return props;
}
/ _ ("VARCHAR"i/"CHARACTER"i __ "VARYING"i) length:TYPE_LENGTH props:COLUMN_TYPE_PROPERTIES {
if(props.type)
throw new SyntaxError("Ambiguous type");
Expand Down Expand Up @@ -241,13 +265,26 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
if(length) props.length = length;
return props;
}
/ _ "BOOLEAN"i _ props:COLUMN_TYPE_PROPERTIES {
if(props.type)
throw new SyntaxError("Ambiguous type");
props.type = 'BOOLEAN';
return props;
}
/ _ "ENUM"i _ "(" values:STRING_ID_LIST ")" _ props:COLUMN_TYPE_PROPERTIES {
if(props.type)
throw new SyntaxError("Ambiguous type");
props.type = 'ENUM';
props.values = values;
return props;
}
/ _ "SET"i _ "(" values:STRING_ID_LIST ")" _ props:COLUMN_TYPE_PROPERTIES {
if(props.type)
throw new SyntaxError("Ambiguous type");
props.type = 'SET';
props.values = values;
return props;
}
/ _ "UNSIGNED"i _ props:COLUMN_TYPE_PROPERTIES {
props.unsigned=true;
return props;
Expand Down Expand Up @@ -287,13 +324,12 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
return props;
}
/ _ "DEFAULT"i __ value:CONSTANT_EXPRESSION props:COLUMN_TYPE_PROPERTIES {
if (value.toSql) { // @@@ FIXME. much ad-hock
value = value.toSql()
}
props.default = value;
return props;
}
/ _ "DEFAULT"i __ CURRENT_TIMESTAMP props:COLUMN_TYPE_PROPERTIES {
props.default = 'CURRENT_TIMESTAMP';
return props;
}
/ _ "ON"i _ "UPDATE"i _ val:CURRENT_TIMESTAMP _ props:COLUMN_TYPE_PROPERTIES {
props.onUpdate = val;
return props;
Expand All @@ -303,4 +339,3 @@ COLUMN_TYPE_PROPERTIES "Column type properties"
return props;
}
/ _ { return {}; }

8 changes: 4 additions & 4 deletions lib/parse-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ ParseOptions.prototype.resolveConstantExpressionObject = function(expr) {
return this.resolveConstantExpressionBinary(expr);
}

throw new Error("Cannot resolve expression as a constant: " + expr);
throw new Error("Cannot resolve expression as a constant: [" + expr + "]");
};


Expand Down Expand Up @@ -206,10 +206,10 @@ ParseOptions.prototype.resolveConstantExpression = function(expr) {
return expr;
case 'string':
return expr;
case 'boolean':
return expr;
case 'object':
return this.resolveConstantExpressionObject(expr);
}
throw new Error("Cannot resolve expression as a constant");
throw new Error("Cannot resolve expression as a constant [" + typeof expr + "][" + expr + "]");
};


Loading