Skip to content

Commit

Permalink
IDE support for array types - and duplicate null/NULL bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
fegu committed Oct 22, 2020
1 parent e5c2a37 commit d087389
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
10 changes: 8 additions & 2 deletions IHP/IDE/SchemaDesigner/Controller/Columns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ instance Controller ColumnsController where
redirectTo ShowTableAction { .. }
let column = Column
{ name = columnName
, columnType = param "columnType"
, columnType = arrayifytype (param "isArray") (param "columnType")
, defaultValue = defaultValue
, notNull = (not (param "allowNull"))
, isUnique = param "isUnique"
Expand Down Expand Up @@ -82,7 +82,7 @@ instance Controller ColumnsController where
let columnId = param "columnId"
let column = Column
{ name = columnName
, columnType = param "columnType"
, columnType = arrayifytype (param "isArray") (param "columnType")
, defaultValue = defaultValue
, notNull = (not (param "allowNull"))
, isUnique = param "isUnique"
Expand Down Expand Up @@ -228,3 +228,9 @@ isCreateEnumType CreateEnumType {} = True
isCreateEnumType _ = False

nameList statements = map (get #name) statements

arrayifytype :: Bool -> PostgresType -> PostgresType
arrayifytype False (PArray coltype) = coltype
arrayifytype True a@(PArray coltype) = a
arrayifytype False coltype = coltype
arrayifytype True coltype = PArray coltype
17 changes: 14 additions & 3 deletions IHP/IDE/SchemaDesigner/View/Columns/Edit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ instance View EditColumnView ViewContext where
isUniqueCheckbox = if get #isUnique column
then preEscapedToHtml [plain|<input type="checkbox" name="isUnique" class="mr-2" checked/>|]
else preEscapedToHtml [plain|<input type="checkbox" name="isUnique" class="mr-2"/>|]


isArrayTypeCheckbox = if (isArrayType (get #columnType column))
then preEscapedToHtml [plain|<input type="checkbox" name="isArray" class="mr-2" checked/>|]
else preEscapedToHtml [plain|<input type="checkbox" name="isArray" class="mr-2"/>|]

isArrayType (PArray _) = True
isArrayType _ = False

modalContent = [hsx|
<form method="POST" action={UpdateColumnAction}>
<input type="hidden" name="tableName" value={tableName}/>
Expand Down Expand Up @@ -80,6 +87,9 @@ instance View EditColumnView ViewContext where
{isUniqueCheckbox} Unique
</label>
{primaryKeyCheckbox}
<label class="ml-1" style="font-size: 12px">
{isArrayTypeCheckbox} Array Type
</label>
</div>
</div>

Expand All @@ -93,6 +103,7 @@ instance View EditColumnView ViewContext where
<input type="hidden" name="primaryKey" value={inputValue False}/>
<input type="hidden" name="allowNull" value={inputValue False}/>
<input type="hidden" name="isUnique" value={inputValue False}/>
<input type="hidden" name="isArray" value={inputValue False}/>
</form>
|]
modalFooter = mempty
Expand Down Expand Up @@ -128,7 +139,7 @@ typeSelector postgresType enumNames = [hsx|
option selected value text = case selected of
Nothing -> [hsx|<option value={value}>{text}</option>|]
Just selection ->
if selection == value
if selection == value || selection == value <> "[]"
then [hsx|<option value={value} selected="selected">{text}</option>|]
else [hsx|<option value={value}>{text}</option>|]

Expand All @@ -141,7 +152,7 @@ defaultSelector defValue = [hsx|
</div>
|]
where
suggestedValues = [Nothing, Just (TextExpression ""), Just (VarExpression "null"), Just (CallExpression "NOW" [])]
suggestedValues = [Nothing, Just (TextExpression ""), Just (VarExpression "NULL"), Just (CallExpression "NOW" [])]
values = if defValue `elem` suggestedValues then suggestedValues else defValue:suggestedValues

renderValue :: Maybe Expression -> Html
Expand Down
4 changes: 4 additions & 0 deletions IHP/IDE/SchemaDesigner/View/Columns/New.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ instance View NewColumnView ViewContext where
<label class="mx-2" style="font-size: 12px">
<input type="checkbox" name="primaryKey" class="mr-1"/>Primary Key
</label>
<label class="ml-1" style="font-size: 12px">
<input type="checkbox" name="isArray" class="mr-1"/>Array Type
</label>
</div>
</div>

Expand All @@ -73,6 +76,7 @@ instance View NewColumnView ViewContext where
<input type="hidden" name="primaryKey" value={inputValue False}/>
<input type="hidden" name="allowNull" value={inputValue False}/>
<input type="hidden" name="isUnique" value={inputValue False}/>
<input type="hidden" name="isArray" value={inputValue False}/>
<input type="hidden" name="isReference" value={inputValue False}/>
<input type="hidden" name="referenceTable" value=""/>
</form>
Expand Down
3 changes: 2 additions & 1 deletion lib/IHP/static/ihp-schemadesigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function initSchemaDesigner() {
});
$('#allowNull').change(function() {
if ($('#allowNull').is(":checked")) {
$('#defaultSelector').append(new Option("null", "NULL", true, true));
if(0 == $("#defaultSelector option:contains('NULL')").length)
$('#defaultSelector').append(new Option("NULL", "NULL", true, true));
} else {
$("#defaultSelector option[value='NULL']").remove();
}
Expand Down

0 comments on commit d087389

Please sign in to comment.