Skip to content

Commit

Permalink
fixed queries and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
fredreichbier committed Feb 15, 2009
1 parent e9a58df commit c3e3ed2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
25 changes: 23 additions & 2 deletions iorm/Fields.io
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ Field := Object clone do(
name ::= nil
value := nil
table ::= nil
flags ::= nil

init := method(
setFlags(list())
resend
)

getNameAsSQL := method(
# The name is not quoted. TODO: Fine?
name
)

getFlagsAsSQL := method(
flags join(" ")
)

quote := method(value,
table session quote(value)
Expand All @@ -26,6 +36,17 @@ Field := Object clone do(
Iorm InvalidValueError raise("#{ new_value } is not a #{ ioProto type }" interpolate)
)
)

addFlag := method(flag,
flags append(flag)
)

newFlagSetter := method(sql,
# well, that's an ugly macro. can it be done nicer?
doString("""method(is, if(is, flags appendIfAbsent("#{ sql }"), flags remove("#{ sql }")); self)""" interpolate)
)

setIsPrimaryKey := newFlagSetter("PRIMARY KEY")
)

IntegerField := Field clone do(
Expand All @@ -41,7 +62,7 @@ IntegerField := Field clone do(
)

getCreateQuery := method(
"""#{ quote(name) } #{ quote(typeName) }""" interpolate // TODO: `NOT NULL ...`
"""#{ quote(name) } #{ typeName } #{ getFlagsAsSQL }""" interpolate
)
)

Expand All @@ -59,6 +80,6 @@ VarcharField := Field clone do(
)

getCreateQuery := method(
"""#{ quote(name) } #{ quote(typeName) }(#{ length })""" interpolate // TODO: `NOT NULL ...`
"""#{ quote(name) } #{ typeName }(#{ length }) #{ getFlagsAsSQL }""" interpolate
)
)
6 changes: 3 additions & 3 deletions iorm/Query.io
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ CreateTable := Object clone do(
table fields foreach(field,
queries append(field getCreateQuery)
)
return("""CREATE TABLE #{ table getNameAsSQL } ( #{ queries join(", \n") } );""" interpolate)
return("""CREATE TABLE #{ table getNameAsSQL } ( #{ queries join(", ") } );""" interpolate)
)
)

Expand All @@ -49,8 +49,8 @@ InsertInto := Object clone do(
columns append(field getNameAsSQL)
values append(field getValueAsSQL)
)
return(("""INSERT INTO #{ table getNameAsSQL } (#{ columns join(",") }) """ ..
"""VALUES (#{ values join(",") });""") interpolate)
return(("""INSERT INTO #{ table getNameAsSQL } (#{ columns join(", ") }) """ ..
"""VALUES (#{ values join(", ") });""") interpolate)
)
)

Expand Down
2 changes: 2 additions & 0 deletions iorm/Session.io
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Session := Object clone do(
commit := method(
/* process all statements in `queue`. Return self. */
queue foreach(stmt,
stmt getAsSQL println
self connection execute(stmt getAsSQL)
)
queue = list() # TODO: something like `clear`?
Expand All @@ -31,6 +32,7 @@ Session := Object clone do(

execute := method(query,
commit
query getAsSQL println
executeRaw(query getAsSQL)
)

Expand Down
2 changes: 1 addition & 1 deletion test.io
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ session := Iorm Session withSQLite("./test.sqlite")

Foo := Iorm Model clone do(
setTableName("Foo")
newField("integer", Iorm IntegerField clone)
newField("integer", Iorm IntegerField clone setIsPrimaryKey(true))
newField("string", Iorm VarcharField clone setLength(50))
setPrimaryKey("integer")
) setSession(session)
Expand Down

0 comments on commit c3e3ed2

Please sign in to comment.