Skip to content

Commit 3a482e5

Browse files
committed
Making sure column names are available for MySQL (fixes mauricio#42) and adding tests for time objects on PostgreSQL
1 parent 7c624b5 commit 3a482e5

File tree

4 files changed

+184
-59
lines changed

4 files changed

+184
-59
lines changed

db-async-common/src/main/scala/com/github/mauricio/async/db/general/MutableResultSet.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class MutableResultSet[T <: ColumnData](
3535
( this.columnTypes(index).name, index ) ).toMap
3636

3737

38-
override def columnNames : IndexedSeq[String] = this.columnTypes.map( data => data.name )
38+
val columnNames : IndexedSeq[String] = this.columnMapping.keys.toIndexedSeq
3939

4040
override def length: Int = this.rows.length
4141

mysql-async/src/test/scala/com/github/mauricio/async/db/mysql/QuerySpec.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,30 @@ class QuerySpec extends Specification with ConnectionHelper {
136136
val row = executeQuery(connection, select).rows.get(0)
137137
row("id") === 1
138138
row("some_bytes") === bytes
139+
}
140+
}
141+
142+
"have column names on result set" in {
143+
144+
val create = """CREATE TEMPORARY TABLE posts (
145+
| id INT NOT NULL AUTO_INCREMENT,
146+
| some_bytes BLOB not null,
147+
| primary key (id) )""".stripMargin
139148

149+
val columns = List("id", "some_bytes")
150+
val select = "SELECT * FROM posts"
140151

152+
withConnection {
153+
connection =>
154+
executeQuery(connection, create)
155+
156+
val preparedResult = executePreparedStatement(connection, select).rows.get
157+
preparedResult.columnNames === columns
158+
159+
val result = executeQuery(connection, select).rows.get
160+
result.columnNames === columns
141161
}
162+
142163
}
143164

144165
}

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/PreparedStatementSpec.scala

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -118,64 +118,6 @@ class PreparedStatementSpec extends Specification with DatabaseTestHelper {
118118
}
119119
}
120120

121-
"support timestamp with timezone" in {
122-
withHandler {
123-
handler =>
124-
125-
val create = """CREATE TEMP TABLE messages
126-
(
127-
id bigserial NOT NULL,
128-
moment timestamp with time zone NOT NULL,
129-
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
130-
)"""
131-
132-
executeDdl(handler, create)
133-
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('1999-01-08 04:05:06 -3:00')")
134-
val rows = executePreparedStatement(handler, "SELECT * FROM messages").rows.get
135-
136-
rows.length === 1
137-
138-
val dateTime = rows(0)("moment").asInstanceOf[DateTime]
139-
140-
dateTime.getZone.toTimeZone.getRawOffset === -10800000
141-
142-
}
143-
}
144-
145-
"support timestamp with timezone and microseconds" in {
146-
147-
1.until(6).inclusive.map {
148-
index =>
149-
withHandler {
150-
handler =>
151-
152-
val create = """CREATE TEMP TABLE messages
153-
(
154-
id bigserial NOT NULL,
155-
moment timestamp(%d) with time zone NOT NULL,
156-
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
157-
)""".format(index)
158-
159-
log.debug("create is {}", create)
160-
161-
executeDdl(handler, create)
162-
163-
val seconds = (index.toString * index).toLong
164-
165-
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('1999-01-08 04:05:06.%d -3:00')".format(seconds))
166-
val rows = executePreparedStatement(handler, "SELECT * FROM messages").rows.get
167-
168-
rows.length === 1
169-
170-
val dateTime = rows(0)("moment").asInstanceOf[DateTime]
171-
172-
dateTime.getZone.toTimeZone.getRawOffset === -10800000
173-
}
174-
175-
176-
}
177-
}
178-
179121
}
180122

181123
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2013 Maurício Linhares
3+
*
4+
* Maurício Linhares licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.github.mauricio.async.db.postgresql
18+
19+
import org.specs2.mutable.Specification
20+
import org.joda.time.{LocalTime, DateTime}
21+
22+
class TimeAndDateSpec extends Specification with DatabaseTestHelper {
23+
24+
"when processing times and dates" should {
25+
26+
"support a time object" in {
27+
28+
withHandler {
29+
handler =>
30+
val create = """CREATE TEMP TABLE messages
31+
(
32+
id bigserial NOT NULL,
33+
moment time NOT NULL,
34+
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
35+
)"""
36+
37+
executeDdl(handler, create)
38+
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('04:05:06')")
39+
40+
val rows = executePreparedStatement(handler, "select * from messages").rows.get
41+
42+
val time = rows(0)("moment").asInstanceOf[LocalTime]
43+
44+
time.getHourOfDay === 4
45+
time.getMinuteOfHour === 5
46+
time.getSecondOfMinute === 6
47+
}
48+
49+
}
50+
51+
"support a time object with microseconds" in {
52+
53+
withHandler {
54+
handler =>
55+
val create = """CREATE TEMP TABLE messages
56+
(
57+
id bigserial NOT NULL,
58+
moment time(6) NOT NULL,
59+
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
60+
)"""
61+
62+
executeDdl(handler, create)
63+
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('04:05:06.134')")
64+
65+
val rows = executePreparedStatement(handler, "select * from messages").rows.get
66+
67+
val time = rows(0)("moment").asInstanceOf[LocalTime]
68+
69+
time.getHourOfDay === 4
70+
time.getMinuteOfHour === 5
71+
time.getSecondOfMinute === 6
72+
time.getMillisOfSecond === 134
73+
}
74+
75+
}
76+
77+
"support a time with timezone object" in {
78+
79+
pending("need to find a way to implement this")
80+
81+
withHandler {
82+
handler =>
83+
val create = """CREATE TEMP TABLE messages
84+
(
85+
id bigserial NOT NULL,
86+
moment time with time zone NOT NULL,
87+
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
88+
)"""
89+
90+
executeDdl(handler, create)
91+
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('04:05:06 -3:00')")
92+
93+
val rows = executePreparedStatement(handler, "select * from messages").rows.get
94+
95+
val time = rows(0)("moment").asInstanceOf[LocalTime]
96+
97+
time.getHourOfDay === 4
98+
time.getMinuteOfHour === 5
99+
time.getSecondOfMinute === 6
100+
}
101+
102+
}
103+
104+
"support timestamp with timezone" in {
105+
withHandler {
106+
handler =>
107+
108+
val create = """CREATE TEMP TABLE messages
109+
(
110+
id bigserial NOT NULL,
111+
moment timestamp with time zone NOT NULL,
112+
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
113+
)"""
114+
115+
executeDdl(handler, create)
116+
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('1999-01-08 04:05:06 -3:00')")
117+
val rows = executePreparedStatement(handler, "SELECT * FROM messages").rows.get
118+
119+
rows.length === 1
120+
121+
val dateTime = rows(0)("moment").asInstanceOf[DateTime]
122+
123+
dateTime.getZone.toTimeZone.getRawOffset === -10800000
124+
125+
}
126+
}
127+
128+
"support timestamp with timezone and microseconds" in {
129+
130+
1.until(6).inclusive.map {
131+
index =>
132+
withHandler {
133+
handler =>
134+
135+
val create = """CREATE TEMP TABLE messages
136+
(
137+
id bigserial NOT NULL,
138+
moment timestamp(%d) with time zone NOT NULL,
139+
CONSTRAINT bigserial_column_pkey PRIMARY KEY (id )
140+
)""".format(index)
141+
142+
executeDdl(handler, create)
143+
144+
val seconds = (index.toString * index).toLong
145+
146+
executeQuery(handler, "INSERT INTO messages (moment) VALUES ('1999-01-08 04:05:06.%d -3:00')".format(seconds))
147+
val rows = executePreparedStatement(handler, "SELECT * FROM messages").rows.get
148+
149+
rows.length === 1
150+
151+
val dateTime = rows(0)("moment").asInstanceOf[DateTime]
152+
153+
dateTime.getZone.toTimeZone.getRawOffset === -10800000
154+
}
155+
156+
157+
}
158+
}
159+
160+
}
161+
162+
}

0 commit comments

Comments
 (0)