PageRenderTime 75ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/mysql-ruby-2.8.2/test.rb

#
Ruby | 1454 lines | 1444 code | 8 blank | 2 comment | 2 complexity | 4554b5b0d804768dd464e87e0d939727 MD5 | raw file
  1. #!/usr/local/bin/ruby
  2. # $Id: test.rb 250 2010-02-11 10:42:54Z tommy $
  3. require "test/unit"
  4. require "./mysql.o"
  5. class TC_Mysql < Test::Unit::TestCase
  6. def setup()
  7. @host, @user, @pass, db, port, sock, flag = ARGV
  8. @db = db || "test"
  9. @port = port.to_i
  10. @sock = sock.nil? || sock.empty? ? nil : sock
  11. @flag = flag.to_i
  12. end
  13. def teardown()
  14. end
  15. def test_version()
  16. assert_equal(20802, Mysql::VERSION)
  17. end
  18. def test_init()
  19. assert_nothing_raised{@m = Mysql.init}
  20. assert_nothing_raised{@m.close}
  21. end
  22. def test_real_connect()
  23. assert_nothing_raised{@m = Mysql.real_connect(@host, @user, @pass, @db, @port, @sock, @flag)}
  24. assert_nothing_raised{@m.close}
  25. end
  26. def test_connect()
  27. assert_nothing_raised{@m = Mysql.connect(@host, @user, @pass, @db, @port, @sock, @flag)}
  28. assert_nothing_raised{@m.close}
  29. end
  30. def test_new()
  31. assert_nothing_raised{@m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)}
  32. assert_nothing_raised{@m.close}
  33. end
  34. def test_escape_string()
  35. assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.escape_string("abc'def\"ghi\0jkl%mno"))
  36. end
  37. def test_quote()
  38. assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.quote("abc'def\"ghi\0jkl%mno"))
  39. end
  40. def test_get_client_info()
  41. assert_match(/^\d.\d+.\d+[a-z]?(-.*)?$/, Mysql.get_client_info())
  42. end
  43. def test_client_info()
  44. assert_match(/^\d.\d+.\d+[a-z]?(-.*)?$/, Mysql.client_info())
  45. end
  46. def test_options()
  47. @m = Mysql.init
  48. assert_equal(@m, @m.options(Mysql::INIT_COMMAND, "SET AUTOCOMMIT=0"))
  49. assert_equal(@m, @m.options(Mysql::OPT_COMPRESS))
  50. assert_equal(@m, @m.options(Mysql::OPT_CONNECT_TIMEOUT, 10))
  51. assert_equal(@m, @m.options(Mysql::GUESS_CONNECTION)) if defined? Mysql::GUESS_CONNECTION
  52. assert_equal(@m, @m.options(Mysql::OPT_LOCAL_INFILE, true))
  53. # assert_equal(@m, @m.options(Mysql::OPT_NAMED_PIPE))
  54. # assert_equal(@m, @m.options(Mysql::OPT_PROTOCOL, 1))
  55. assert_equal(@m, @m.options(Mysql::OPT_READ_TIMEOUT, 10)) if defined? Mysql::OPT_READ_TIMEOUT
  56. assert_equal(@m, @m.options(Mysql::OPT_USE_EMBEDDED_CONNECTION)) if defined? Mysql::OPT_USE_EMBEDDED_CONNECTION
  57. assert_equal(@m, @m.options(Mysql::OPT_USE_REMOTE_CONNECTION)) if defined? Mysql::OPT_USE_REMOTE_CONNECTION
  58. assert_equal(@m, @m.options(Mysql::OPT_WRITE_TIMEOUT, 10)) if defined? Mysql::OPT_WRITE_TIMEOUT
  59. # assert_equal(@m, @m.options(Mysql::READ_DEFAULT_FILE, "/tmp/hoge"))
  60. assert_equal(@m, @m.options(Mysql::READ_DEFAULT_GROUP, "test"))
  61. assert_equal(@m, @m.options(Mysql::SECURE_AUTH, true)) if defined? Mysql::SECURE_AUTH
  62. # assert_equal(@m, @m.options(Mysql::SET_CHARSET_DIR, "??"))
  63. assert_equal(@m, @m.options(Mysql::SET_CHARSET_NAME, "latin1"))
  64. assert_equal(@m, @m.options(Mysql::SET_CLIENT_IP, "127.0.0.1")) if defined? Mysql::SET_CLIENT_IP
  65. # assert_equal(@m, @m.options(Mysql::SHARED_MEMORY_BASE_NAME, "xxx"))
  66. assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
  67. @m.close
  68. end
  69. def test_real_connect2()
  70. @m = Mysql.init
  71. assert_equal(@m, @m.real_connect(@host, @user, @pass, @db, @port, @sock, @flag))
  72. @m.close
  73. end
  74. def test_connect2()
  75. @m = Mysql.init
  76. assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
  77. @m.close
  78. end
  79. end
  80. class TC_Mysql2 < Test::Unit::TestCase
  81. def setup()
  82. @host, @user, @pass, db, port, sock, flag = ARGV
  83. @db = db || "test"
  84. @port = port.to_i
  85. @sock = sock.nil? || sock.empty? ? nil : sock
  86. @flag = flag.to_i
  87. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  88. end
  89. def teardown()
  90. @m.close if @m
  91. end
  92. def test_affected_rows()
  93. @m.query("create temporary table t (id int)")
  94. @m.query("insert into t values (1)")
  95. assert_equal(1, @m.affected_rows)
  96. end
  97. def test_autocommit()
  98. if @m.methods.include? "autocommit" then
  99. assert_equal(@m, @m.autocommit(true))
  100. assert_equal(@m, @m.autocommit(false))
  101. end
  102. end
  103. # def test_ssl_set()
  104. # end
  105. def test_more_results_next_result()
  106. if @m.server_version >= 40100 then
  107. @m.query_with_result = false
  108. @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON) if defined? Mysql::OPTION_MULTI_STATEMENTS_ON
  109. @m.query("select 1,2,3; select 4,5,6")
  110. res = @m.store_result
  111. assert_equal(["1","2","3"], res.fetch_row)
  112. assert_equal(nil, res.fetch_row)
  113. assert_equal(true, @m.more_results)
  114. assert_equal(true, @m.more_results?)
  115. assert_equal(true, @m.next_result)
  116. res = @m.store_result
  117. assert_equal(["4","5","6"], res.fetch_row)
  118. assert_equal(nil, res.fetch_row)
  119. assert_equal(false, @m.more_results)
  120. assert_equal(false, @m.more_results?)
  121. assert_equal(false, @m.next_result)
  122. end
  123. end if Mysql.client_version >= 40100
  124. def test_query_with_block()
  125. if @m.server_version >= 40100 then
  126. @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
  127. expect = [["1","2","3"], ["4","5","6"]]
  128. @m.query("select 1,2,3; select 4,5,6") {|res|
  129. assert_equal(1, res.num_rows)
  130. assert_equal(expect.shift, res.fetch_row)
  131. }
  132. assert(expect.empty?)
  133. expect = [["1","2","3"], ["4","5","6"]]
  134. assert_raises(Mysql::Error) {
  135. @m.query("select 1,2,3; hoge; select 4,5,6") {|res|
  136. assert_equal(1, res.num_rows)
  137. assert_equal(expect.shift, res.fetch_row)
  138. }
  139. }
  140. assert_equal(1, expect.size)
  141. expect = [["1","2","3"], ["4","5","6"]]
  142. assert_raises(Mysql::Error) {
  143. @m.query("select 1,2,3; select 4,5,6; hoge") {|res|
  144. assert_equal(1, res.num_rows)
  145. assert_equal(expect.shift, res.fetch_row)
  146. }
  147. }
  148. assert(expect.empty?)
  149. end
  150. end
  151. def test_query_with_block_single()
  152. @m.query("select 1,2,3") {|res|
  153. assert_equal(1, res.num_rows)
  154. assert_equal(["1","2","3"], res.fetch_row)
  155. }
  156. end
  157. def test_set_server_option()
  158. if @m.server_version >= 40101 then
  159. assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON))
  160. assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_OFF))
  161. end
  162. end if Mysql.client_version >= 40101
  163. def test_sqlstate()
  164. if @m.server_version >= 40100 then
  165. assert_equal("00000", @m.sqlstate)
  166. assert_raises(Mysql::Error){@m.query("hogehoge")}
  167. assert_equal("42000", @m.sqlstate)
  168. end
  169. end if Mysql.client_version >= 40100
  170. def test_query_with_result()
  171. assert_equal(true, @m.query_with_result)
  172. assert_equal(false, @m.query_with_result = false)
  173. assert_equal(false, @m.query_with_result)
  174. assert_equal(true, @m.query_with_result = true)
  175. assert_equal(true, @m.query_with_result)
  176. end
  177. def test_reconnect()
  178. assert_equal(false, @m.reconnect)
  179. assert_equal(true, @m.reconnect = true)
  180. assert_equal(true, @m.reconnect)
  181. assert_equal(false, @m.reconnect = false)
  182. assert_equal(false, @m.reconnect)
  183. end
  184. end
  185. class TC_MysqlRes < Test::Unit::TestCase
  186. def setup()
  187. @host, @user, @pass, db, port, sock, flag = ARGV
  188. @db = db || "test"
  189. @port = port.to_i
  190. @sock = sock.nil? || sock.empty? ? nil : sock
  191. @flag = flag.to_i
  192. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  193. @m.query("create temporary table t (id int, str char(10), primary key (id))")
  194. @m.query("insert into t values (1, 'abc'), (2, 'defg'), (3, 'hi'), (4, null)")
  195. @res = @m.query("select * from t")
  196. end
  197. def teardown()
  198. @res.free
  199. @m.close
  200. end
  201. def test_num_fields()
  202. assert_equal(2, @res.num_fields)
  203. end
  204. def test_num_rows()
  205. assert_equal(4, @res.num_rows)
  206. end
  207. def test_fetch_row()
  208. assert_equal(["1","abc"], @res.fetch_row)
  209. assert_equal(["2","defg"], @res.fetch_row)
  210. assert_equal(["3","hi"], @res.fetch_row)
  211. assert_equal(["4",nil], @res.fetch_row)
  212. assert_equal(nil, @res.fetch_row)
  213. end
  214. def test_fetch_hash()
  215. assert_equal({"id"=>"1", "str"=>"abc"}, @res.fetch_hash)
  216. assert_equal({"id"=>"2", "str"=>"defg"}, @res.fetch_hash)
  217. assert_equal({"id"=>"3", "str"=>"hi"}, @res.fetch_hash)
  218. assert_equal({"id"=>"4", "str"=>nil}, @res.fetch_hash)
  219. assert_equal(nil, @res.fetch_hash)
  220. end
  221. def test_fetch_hash2()
  222. assert_equal({"t.id"=>"1", "t.str"=>"abc"}, @res.fetch_hash(true))
  223. assert_equal({"t.id"=>"2", "t.str"=>"defg"}, @res.fetch_hash(true))
  224. assert_equal({"t.id"=>"3", "t.str"=>"hi"}, @res.fetch_hash(true))
  225. assert_equal({"t.id"=>"4", "t.str"=>nil}, @res.fetch_hash(true))
  226. assert_equal(nil, @res.fetch_hash)
  227. end
  228. def test_each()
  229. ary = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
  230. @res.each do |a|
  231. assert_equal(ary.shift, a)
  232. end
  233. end
  234. def test_each_hash()
  235. hash = [{"id"=>"1","str"=>"abc"}, {"id"=>"2","str"=>"defg"}, {"id"=>"3","str"=>"hi"}, {"id"=>"4","str"=>nil}]
  236. @res.each_hash do |h|
  237. assert_equal(hash.shift, h)
  238. end
  239. end
  240. def test_data_seek()
  241. assert_equal(["1","abc"], @res.fetch_row)
  242. assert_equal(["2","defg"], @res.fetch_row)
  243. assert_equal(["3","hi"], @res.fetch_row)
  244. @res.data_seek(1)
  245. assert_equal(["2","defg"], @res.fetch_row)
  246. end
  247. def test_row_seek()
  248. assert_equal(["1","abc"], @res.fetch_row)
  249. pos = @res.row_tell
  250. assert_equal(["2","defg"], @res.fetch_row)
  251. assert_equal(["3","hi"], @res.fetch_row)
  252. @res.row_seek(pos)
  253. assert_equal(["2","defg"], @res.fetch_row)
  254. end
  255. def test_field_seek()
  256. assert_equal(0, @res.field_tell)
  257. @res.fetch_field
  258. assert_equal(1, @res.field_tell)
  259. @res.fetch_field
  260. assert_equal(2, @res.field_tell)
  261. @res.field_seek(1)
  262. assert_equal(1, @res.field_tell)
  263. end
  264. def test_fetch_field()
  265. f = @res.fetch_field
  266. assert_equal("id", f.name)
  267. assert_equal("t", f.table)
  268. assert_equal(nil, f.def)
  269. assert_equal(Mysql::Field::TYPE_LONG, f.type)
  270. assert_equal(11, f.length)
  271. assert_equal(1, f.max_length)
  272. assert_equal(Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG, f.flags)
  273. assert_equal(0, f.decimals)
  274. f = @res.fetch_field
  275. assert_equal("str", f.name)
  276. assert_equal("t", f.table)
  277. assert_equal(nil, f.def)
  278. assert_equal(Mysql::Field::TYPE_STRING, f.type)
  279. assert_equal(10, f.length)
  280. assert_equal(4, f.max_length)
  281. assert_equal(0, f.flags)
  282. assert_equal(0, f.decimals)
  283. f = @res.fetch_field
  284. assert_equal(nil, f)
  285. end
  286. def test_fetch_fields()
  287. a = @res.fetch_fields
  288. assert_equal(2, a.size)
  289. assert_equal("id", a[0].name)
  290. assert_equal("str", a[1].name)
  291. end
  292. def test_fetch_field_direct()
  293. f = @res.fetch_field_direct(0)
  294. assert_equal("id", f.name)
  295. f = @res.fetch_field_direct(1)
  296. assert_equal("str", f.name)
  297. assert_raises(Mysql::Error){@res.fetch_field_direct(-1)}
  298. assert_raises(Mysql::Error){@res.fetch_field_direct(2)}
  299. end
  300. def test_fetch_lengths()
  301. assert_equal(nil, @res.fetch_lengths())
  302. @res.fetch_row
  303. assert_equal([1, 3], @res.fetch_lengths())
  304. @res.fetch_row
  305. assert_equal([1, 4], @res.fetch_lengths())
  306. @res.fetch_row
  307. assert_equal([1, 2], @res.fetch_lengths())
  308. @res.fetch_row
  309. assert_equal([1, 0], @res.fetch_lengths())
  310. @res.fetch_row
  311. assert_equal(nil, @res.fetch_lengths())
  312. end
  313. def test_field_hash()
  314. f = @res.fetch_field
  315. h = {
  316. "name" => "id",
  317. "table" => "t",
  318. "def" => nil,
  319. "type" => Mysql::Field::TYPE_LONG,
  320. "length" => 11,
  321. "max_length" => 1,
  322. "flags" => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
  323. "decimals" => 0,
  324. }
  325. assert_equal(h, f.hash)
  326. f = @res.fetch_field
  327. h = {
  328. "name" => "str",
  329. "table" => "t",
  330. "def" => nil,
  331. "type" => Mysql::Field::TYPE_STRING,
  332. "length" => 10,
  333. "max_length" => 4,
  334. "flags" => 0,
  335. "decimals" => 0,
  336. }
  337. assert_equal(h, f.hash)
  338. end
  339. def test_field_inspect()
  340. f = @res.fetch_field
  341. assert_equal("#<Mysql::Field:id>", f.inspect)
  342. f = @res.fetch_field
  343. assert_equal("#<Mysql::Field:str>", f.inspect)
  344. end
  345. def test_is_num()
  346. f = @res.fetch_field
  347. assert_equal(true, f.is_num?)
  348. f = @res.fetch_field
  349. assert_equal(false, f.is_num?)
  350. end
  351. def test_is_not_null()
  352. f = @res.fetch_field
  353. assert_equal(true, f.is_not_null?)
  354. f = @res.fetch_field
  355. assert_equal(false, f.is_not_null?)
  356. end
  357. def test_is_pri_key()
  358. f = @res.fetch_field
  359. assert_equal(true, f.is_pri_key?)
  360. f = @res.fetch_field
  361. assert_equal(false, f.is_pri_key?)
  362. end
  363. end
  364. class TC_MysqlStmt < Test::Unit::TestCase
  365. def setup()
  366. @host, @user, @pass, db, port, sock, flag = ARGV
  367. @db = db || "test"
  368. @port = port.to_i
  369. @sock = sock.nil? || sock.empty? ? nil : sock
  370. @flag = flag.to_i
  371. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  372. end
  373. def teardown()
  374. end
  375. def test_init()
  376. if @m.server_version >= 40100 then
  377. s = @m.stmt_init()
  378. assert_equal(Mysql::Stmt, s.class)
  379. s.close
  380. end
  381. end
  382. def test_prepare()
  383. if @m.server_version >= 40100 then
  384. s = @m.prepare("select 1")
  385. assert_equal(Mysql::Stmt, s.class)
  386. s.close
  387. end
  388. end
  389. end if Mysql.client_version >= 40100
  390. class TC_MysqlStmt2 < Test::Unit::TestCase
  391. def setup()
  392. @host, @user, @pass, db, port, sock, flag = ARGV
  393. @db = db || "test"
  394. @port = port.to_i
  395. @sock = sock.nil? || sock.empty? ? nil : sock
  396. @flag = flag.to_i
  397. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  398. @s = @m.stmt_init()
  399. end
  400. def teardown()
  401. @s.close
  402. @m.close
  403. end
  404. def test_affected_rows()
  405. if @m.server_version >= 40100 then
  406. @m.query("create temporary table t (i int, c char(10))")
  407. @s.prepare("insert into t values (?,?)")
  408. @s.execute(1, "hoge")
  409. assert_equal(1, @s.affected_rows())
  410. @s.execute(2, "hoge")
  411. @s.execute(3, "hoge")
  412. @s.prepare("update t set c=?")
  413. @s.execute("fuga")
  414. assert_equal(3, @s.affected_rows())
  415. end
  416. end
  417. =begin
  418. def test_attr_get()
  419. assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  420. assert_raises(Mysql::Error){@s.attr_get(999)}
  421. end
  422. def test_attr_set()
  423. @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, true)
  424. assert_equal(true, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  425. @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, false)
  426. assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  427. assert_raises(Mysql::Error){@s.attr_set(999, true)}
  428. end
  429. def test_bind_param()
  430. @s.prepare("insert into t values (?,?)")
  431. @s.bind_param(123, "abc")
  432. @s.bind_param(Time.now, nil)
  433. assert_raises(Mysql::Error){@s.bind_param(1, 2, 3)}
  434. b = @s.bind_param(Bind.new(Mysql::TYPE_TINY, 99, false))
  435. @s.bind_param(98.765, b)
  436. end
  437. =end
  438. def test_bind_result_nil()
  439. if @m.server_version >= 40100 then
  440. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  441. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  442. @s.prepare("select * from t")
  443. @s.bind_result(nil,nil,nil,nil)
  444. @s.execute
  445. a = @s.fetch
  446. assert_equal([123, "9abcdefg", 1.2345, Mysql::Time.new(2005,8,2,23,50,11)], a)
  447. end
  448. end
  449. def test_bind_result_numeric()
  450. if @m.server_version >= 40100 then
  451. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  452. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  453. @s.prepare("select * from t")
  454. @s.bind_result(Numeric, Numeric, Numeric, Numeric)
  455. @s.execute
  456. a = @s.fetch
  457. if Mysql.client_version < 50000 then
  458. assert_equal([123, 9, 1, 2005], a)
  459. else
  460. assert_equal([123, 9, 1, 20050802235011], a)
  461. end
  462. end
  463. end
  464. def test_bind_result_integer()
  465. if @m.server_version >= 40100 then
  466. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  467. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  468. @s.prepare("select * from t")
  469. @s.bind_result(Integer, Integer, Integer, Integer)
  470. @s.execute
  471. a = @s.fetch
  472. if Mysql.client_version < 50000 then
  473. assert_equal([123, 9, 1, 2005], a)
  474. else
  475. assert_equal([123, 9, 1, 20050802235011], a)
  476. end
  477. end
  478. end
  479. def test_bind_result_fixnum()
  480. if @m.server_version >= 40100 then
  481. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  482. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  483. @s.prepare("select * from t")
  484. @s.bind_result(Fixnum, Fixnum, Fixnum, Fixnum)
  485. @s.execute
  486. a = @s.fetch
  487. if Mysql.client_version < 50000 then
  488. assert_equal([123, 9, 1, 2005], a)
  489. else
  490. assert_equal([123, 9, 1, 20050802235011.0], a)
  491. end
  492. end
  493. end
  494. def test_bind_result_string()
  495. if @m.server_version >= 40100 then
  496. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  497. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  498. @s.prepare("select * from t")
  499. @s.bind_result(String, String, String, String)
  500. @s.execute
  501. a = @s.fetch
  502. assert_equal(["123", "9abcdefg", "1.2345", "2005-08-02 23:50:11"], a)
  503. end
  504. end
  505. def test_bind_result_float()
  506. if @m.server_version >= 40100 then
  507. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  508. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  509. @s.prepare("select * from t")
  510. @s.bind_result(Float, Float, Float, Float)
  511. @s.execute
  512. a = @s.fetch
  513. if Mysql.client_version < 50000 then
  514. assert_equal([123.0, 9.0, 1.2345, 2005.0], a)
  515. else
  516. assert_equal([123.0, 9.0, 1.2345, 20050802235011.0], a)
  517. end
  518. end
  519. end
  520. def test_bind_result_mysqltime()
  521. if @m.server_version >= 40100 then
  522. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  523. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  524. @s.prepare("select * from t")
  525. @s.bind_result(Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time)
  526. @s.execute
  527. a = @s.fetch
  528. if Mysql.client_version < 50000 then
  529. assert_equal([Mysql::Time.new, Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
  530. else
  531. assert_equal([Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
  532. end
  533. end
  534. end
  535. def test_bind_result_unknown()
  536. if @m.server_version >= 40100 then
  537. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  538. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  539. @s.prepare("select * from t")
  540. assert_raises(TypeError){@s.bind_result(Time, nil, nil, nil)}
  541. end
  542. end
  543. def test_bind_result_unmatch_count()
  544. if @m.server_version >= 40100 then
  545. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  546. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  547. @s.prepare("select * from t")
  548. assert_raises(Mysql::Error){@s.bind_result(nil, nil)}
  549. end
  550. end
  551. def test_data_seek()
  552. if @m.server_version >= 40100 then
  553. @m.query("create temporary table t (i int)")
  554. @m.query("insert into t values (0),(1),(2),(3),(4),(5)")
  555. @s.prepare("select i from t")
  556. @s.execute
  557. assert_equal([0], @s.fetch)
  558. assert_equal([1], @s.fetch)
  559. assert_equal([2], @s.fetch)
  560. @s.data_seek(5)
  561. assert_equal([5], @s.fetch)
  562. @s.data_seek(1)
  563. assert_equal([1], @s.fetch)
  564. end
  565. end
  566. =begin
  567. def test_errno()
  568. @s.errno()
  569. end
  570. def test_error()
  571. @s.error()
  572. end
  573. =end
  574. def test_execute()
  575. if @m.server_version >= 40100 then
  576. @m.query("create temporary table t (i int)")
  577. @s.prepare("insert into t values (123)")
  578. @s.execute()
  579. assert_equal(1, @s.affected_rows)
  580. @s.execute()
  581. assert_equal(1, @s.affected_rows)
  582. assert_equal(2, @m.query("select count(*) from t").fetch_row[0].to_i)
  583. end
  584. end
  585. def test_execute2()
  586. if @m.server_version >= 40100 then
  587. @m.query("create temporary table t (i int)")
  588. @s.prepare("insert into t values (?)")
  589. @s.execute(123)
  590. @s.execute("456")
  591. @s.prepare("select * from t")
  592. @s.execute
  593. assert_equal([123], @s.fetch)
  594. assert_equal([456], @s.fetch)
  595. end
  596. end
  597. def test_execute3()
  598. if @m.server_version >= 40100 then
  599. @m.query("create temporary table t (i int, c char(255), t timestamp)")
  600. @s.prepare("insert into t values (?,?,?)")
  601. @s.execute(123, "hoge", Time.local(2005,7,19,23,53,0));
  602. assert_raises(Mysql::Error){@s.execute(123, "hoge")}
  603. assert_raises(Mysql::Error){@s.execute(123, "hoge", 0, "fuga")}
  604. @s.prepare("select * from t")
  605. @s.execute
  606. assert_equal([123, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
  607. end
  608. end
  609. def test_execute4()
  610. if @m.server_version >= 40100 then
  611. @m.query("create temporary table t (i int, c char(255), t timestamp)")
  612. @s.prepare("insert into t values (?,?,?)")
  613. @s.execute(nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0));
  614. @s.prepare("select * from t")
  615. @s.execute
  616. assert_equal([nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
  617. end
  618. end
  619. def test_execute5()
  620. if @m.server_version >= 40100 then
  621. [30, 31, 32, 62, 63].each do |i|
  622. v, = @m.prepare("select cast(? as signed)").execute(2**i-1).fetch
  623. assert_equal(2**i-1, v)
  624. v, = @m.prepare("select cast(? as signed)").execute(-(2**i)).fetch
  625. assert_equal(-(2**i), v)
  626. end
  627. end
  628. end
  629. def test_fetch()
  630. if @m.server_version >= 40100 then
  631. @s.prepare("select 123, 'abc', null")
  632. @s.execute()
  633. assert_equal([123, "abc", nil], @s.fetch())
  634. end
  635. end
  636. def test_fetch_bit()
  637. if @m.client_version >= 50003 and @m.server_version >= 50003 then
  638. @m.query("create temporary table t (i bit(8))")
  639. @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
  640. @s.prepare("select i from t")
  641. @s.execute
  642. assert_equal(["\x00"], @s.fetch)
  643. assert_equal(["\xff"], @s.fetch)
  644. assert_equal(["\x7f"], @s.fetch)
  645. assert_equal(["\xff"], @s.fetch)
  646. assert_equal(["\xff"], @s.fetch)
  647. assert_equal(["\xff"], @s.fetch)
  648. assert_equal(["\xff"], @s.fetch)
  649. @m.query("create temporary table t2 (i bit(64))")
  650. @m.query("insert into t2 values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)")
  651. @s.prepare("select i from t2")
  652. @s.execute
  653. assert_equal(["\x00\x00\x00\x00\x00\x00\x00\x00"], @s.fetch)
  654. assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
  655. assert_equal(["\x00\x00\x00\x01\x00\x00\x00\x00"], @s.fetch)
  656. assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
  657. assert_equal(["\xff\xff\xff\xff\xff\xff\xff\xff"], @s.fetch)
  658. end
  659. end
  660. def test_fetch_tinyint()
  661. if @m.server_version >= 40100 then
  662. @m.query("create temporary table t (i tinyint)")
  663. @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255)")
  664. @s.prepare("select i from t")
  665. @s.execute
  666. assert_equal([0], @s.fetch)
  667. assert_equal([-1], @s.fetch)
  668. assert_equal([127], @s.fetch)
  669. assert_equal([-128], @s.fetch)
  670. assert_equal([127], @s.fetch)
  671. assert_equal([-128], @s.fetch)
  672. end
  673. end
  674. def test_fetch_tinyint_unsigned()
  675. if @m.server_version >= 40100 then
  676. @m.query("create temporary table t (i tinyint unsigned)")
  677. @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
  678. @s.prepare("select i from t")
  679. @s.execute
  680. assert_equal([0], @s.fetch)
  681. assert_equal([0], @s.fetch)
  682. assert_equal([127], @s.fetch)
  683. assert_equal([0], @s.fetch)
  684. assert_equal([255], @s.fetch)
  685. assert_equal([0], @s.fetch)
  686. assert_equal([255], @s.fetch)
  687. end
  688. end
  689. def test_fetch_smallint()
  690. if @m.server_version >= 40100 then
  691. @m.query("create temporary table t (i smallint)")
  692. @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
  693. @s.prepare("select i from t")
  694. @s.execute
  695. assert_equal([0], @s.fetch)
  696. assert_equal([-1], @s.fetch)
  697. assert_equal([32767], @s.fetch)
  698. assert_equal([-32768], @s.fetch)
  699. assert_equal([32767], @s.fetch)
  700. assert_equal([-32768], @s.fetch)
  701. end
  702. end
  703. def test_fetch_smallint_unsigned()
  704. if @m.server_version >= 40100 then
  705. @m.query("create temporary table t (i smallint unsigned)")
  706. @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
  707. @s.prepare("select i from t")
  708. @s.execute
  709. assert_equal([0], @s.fetch)
  710. assert_equal([0], @s.fetch)
  711. assert_equal([32767], @s.fetch)
  712. assert_equal([0], @s.fetch)
  713. assert_equal([65535], @s.fetch)
  714. assert_equal([0], @s.fetch)
  715. assert_equal([65535], @s.fetch)
  716. end
  717. end
  718. def test_fetch_mediumint()
  719. if @m.server_version >= 40100 then
  720. @m.query("create temporary table t (i mediumint)")
  721. @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
  722. @s.prepare("select i from t")
  723. @s.execute
  724. assert_equal([0], @s.fetch)
  725. assert_equal([-1], @s.fetch)
  726. assert_equal([8388607], @s.fetch)
  727. assert_equal([-8388608], @s.fetch)
  728. assert_equal([8388607], @s.fetch)
  729. assert_equal([-8388608], @s.fetch)
  730. end
  731. end
  732. def test_fetch_mediumint_unsigned()
  733. if @m.server_version >= 40100 then
  734. @m.query("create temporary table t (i mediumint unsigned)")
  735. @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
  736. @s.prepare("select i from t")
  737. @s.execute
  738. assert_equal([0], @s.fetch)
  739. assert_equal([0], @s.fetch)
  740. assert_equal([8388607], @s.fetch)
  741. assert_equal([0], @s.fetch)
  742. assert_equal([16777215], @s.fetch)
  743. assert_equal([0], @s.fetch)
  744. assert_equal([16777215], @s.fetch)
  745. end
  746. end
  747. def test_fetch_int()
  748. if @m.server_version >= 40100 then
  749. @m.query("create temporary table t (i int)")
  750. @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
  751. @s.prepare("select i from t")
  752. @s.execute
  753. assert_equal([0], @s.fetch)
  754. assert_equal([-1], @s.fetch)
  755. assert_equal([2147483647], @s.fetch)
  756. assert_equal([-2147483648], @s.fetch)
  757. assert_equal([2147483647], @s.fetch)
  758. assert_equal([-2147483648], @s.fetch)
  759. end
  760. end
  761. def test_fetch_int_unsigned()
  762. if @m.server_version >= 40100 then
  763. @m.query("create temporary table t (i int unsigned)")
  764. @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
  765. @s.prepare("select i from t")
  766. @s.execute
  767. assert_equal([0], @s.fetch)
  768. assert_equal([0], @s.fetch)
  769. assert_equal([2147483647], @s.fetch)
  770. assert_equal([0], @s.fetch)
  771. assert_equal([4294967295], @s.fetch)
  772. assert_equal([0], @s.fetch)
  773. assert_equal([4294967295], @s.fetch)
  774. end
  775. end
  776. def test_fetch_bigint()
  777. if @m.server_version >= 40100 then
  778. @m.query("create temporary table t (i bigint)")
  779. @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
  780. @s.prepare("select i from t")
  781. @s.execute
  782. assert_equal([0], @s.fetch)
  783. assert_equal([-1], @s.fetch)
  784. assert_equal([9223372036854775807], @s.fetch)
  785. assert_equal([-9223372036854775808], @s.fetch)
  786. if @m.server_version >= 50000 then
  787. assert_equal([9223372036854775807], @s.fetch)
  788. else
  789. assert_equal([-1], @s.fetch) # MySQL problem
  790. end
  791. assert_equal([-9223372036854775808], @s.fetch)
  792. assert_equal([9223372036854775807], @s.fetch)
  793. end
  794. end
  795. def test_fetch_bigint_unsigned()
  796. if @m.server_version >= 40100 then
  797. @m.query("create temporary table t (i bigint unsigned)")
  798. @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
  799. @s.prepare("select i from t")
  800. @s.execute
  801. assert_equal([0], @s.fetch)
  802. if @m.server_version >= 50000 then
  803. assert_equal([0], @s.fetch)
  804. else
  805. assert_equal([18446744073709551615], @s.fetch) # MySQL problem
  806. end
  807. assert_equal([9223372036854775807], @s.fetch)
  808. if @m.server_version >= 50000 then
  809. assert_equal([0], @s.fetch)
  810. else
  811. assert_equal([9223372036854775808], @s.fetch) # MySQL problem
  812. end
  813. assert_equal([18446744073709551615], @s.fetch)
  814. assert_equal([0], @s.fetch)
  815. assert_equal([18446744073709551615], @s.fetch)
  816. end
  817. end
  818. def test_fetch_float()
  819. if @m.server_version >= 40100 then
  820. @m.query("create temporary table t (i float)")
  821. @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
  822. @s.prepare("select i from t")
  823. @s.execute
  824. assert_equal([0], @s.fetch)
  825. assert_in_delta(-3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  826. assert_in_delta(-1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  827. assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  828. assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  829. end
  830. end
  831. def test_fetch_float_unsigned()
  832. if @m.server_version >= 40100 then
  833. @m.query("create temporary table t (i float unsigned)")
  834. @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
  835. @s.prepare("select i from t")
  836. @s.execute
  837. assert_equal([0], @s.fetch)
  838. assert_equal([0], @s.fetch)
  839. assert_equal([0], @s.fetch)
  840. assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  841. assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  842. end
  843. end
  844. def test_fetch_double()
  845. if @m.server_version >= 40100 then
  846. @m.query("create temporary table t (i double)")
  847. @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
  848. @s.prepare("select i from t")
  849. @s.execute
  850. assert_equal([0], @s.fetch)
  851. assert_in_delta(-Float::MAX, @s.fetch[0], Float::EPSILON)
  852. assert_in_delta(-Float::MIN, @s.fetch[0], Float::EPSILON)
  853. assert_in_delta(Float::MIN, @s.fetch[0], Float::EPSILON)
  854. assert_in_delta(Float::MAX, @s.fetch[0], Float::EPSILON)
  855. end
  856. end
  857. def test_fetch_double_unsigned()
  858. if @m.server_version >= 40100 then
  859. @m.query("create temporary table t (i double unsigned)")
  860. @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
  861. @s.prepare("select i from t")
  862. @s.execute
  863. assert_equal([0], @s.fetch)
  864. assert_equal([0], @s.fetch)
  865. assert_equal([0], @s.fetch)
  866. assert_in_delta(Float::MIN, @s.fetch[0], Float::EPSILON)
  867. assert_in_delta(Float::MAX, @s.fetch[0], Float::EPSILON)
  868. end
  869. end
  870. def test_fetch_decimal()
  871. if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
  872. @m.query("create temporary table t (i decimal)")
  873. @m.query("insert into t values (0),(9999999999),(-9999999999),(10000000000),(-10000000000)")
  874. @s.prepare("select i from t")
  875. @s.execute
  876. assert_equal(["0"], @s.fetch)
  877. assert_equal(["9999999999"], @s.fetch)
  878. assert_equal(["-9999999999"], @s.fetch)
  879. if @m.server_version < 50000 then
  880. assert_equal(["10000000000"], @s.fetch) # MySQL problem
  881. else
  882. assert_equal(["9999999999"], @s.fetch)
  883. end
  884. assert_equal(["-9999999999"], @s.fetch)
  885. end
  886. end
  887. def test_fetch_decimal_unsigned()
  888. if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
  889. @m.query("create temporary table t (i decimal unsigned)")
  890. @m.query("insert into t values (0),(9999999998),(9999999999),(-9999999998),(-9999999999),(10000000000),(-10000000000)")
  891. @s.prepare("select i from t")
  892. @s.execute
  893. assert_equal(["0"], @s.fetch)
  894. assert_equal(["9999999998"], @s.fetch)
  895. assert_equal(["9999999999"], @s.fetch)
  896. assert_equal(["0"], @s.fetch)
  897. assert_equal(["0"], @s.fetch)
  898. assert_equal(["9999999999"], @s.fetch)
  899. assert_equal(["0"], @s.fetch)
  900. end
  901. end
  902. def test_fetch_date()
  903. if @m.server_version >= 40100 then
  904. @m.query("create temporary table t (i date)")
  905. @m.query("insert into t values ('0000-00-00'),('1000-01-01'),('9999-12-31')")
  906. @s.prepare("select i from t")
  907. @s.execute
  908. assert_equal([Mysql::Time.new(0,0,0)], @s.fetch)
  909. assert_equal([Mysql::Time.new(1000,1,1)], @s.fetch)
  910. assert_equal([Mysql::Time.new(9999,12,31)], @s.fetch)
  911. end
  912. end
  913. def test_fetch_datetime()
  914. if @m.server_version >= 40100 then
  915. @m.query("create temporary table t (i datetime)")
  916. @m.query("insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')")
  917. @s.prepare("select i from t")
  918. @s.execute
  919. assert_equal([Mysql::Time.new(0,0,0,0,0,0)], @s.fetch)
  920. assert_equal([Mysql::Time.new(1000,1,1,0,0,0)], @s.fetch)
  921. assert_equal([Mysql::Time.new(9999,12,31,23,59,59)], @s.fetch)
  922. end
  923. end
  924. def test_fetch_timestamp()
  925. if @m.server_version >= 40100 then
  926. @m.query("create temporary table t (i timestamp)")
  927. @m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
  928. @s.prepare("select i from t")
  929. @s.execute
  930. assert_equal([Mysql::Time.new(1970,1,2,0,0,0)], @s.fetch)
  931. assert_equal([Mysql::Time.new(2037,12,30,23,59,59)], @s.fetch)
  932. end
  933. end
  934. def test_fetch_time()
  935. if @m.server_version >= 40100 then
  936. @m.query("create temporary table t (i time)")
  937. @m.query("insert into t values ('-838:59:59'),(0),('838:59:59')")
  938. @s.prepare("select i from t")
  939. @s.execute
  940. assert_equal([Mysql::Time.new(0,0,0,838,59,59,true)], @s.fetch)
  941. assert_equal([Mysql::Time.new(0,0,0,0,0,0,false)], @s.fetch)
  942. assert_equal([Mysql::Time.new(0,0,0,838,59,59,false)], @s.fetch)
  943. end
  944. end
  945. def test_fetch_year()
  946. if @m.server_version >= 40100 then
  947. @m.query("create temporary table t (i year)")
  948. @m.query("insert into t values (0),(70),(69),(1901),(2155)")
  949. @s.prepare("select i from t")
  950. @s.execute
  951. assert_equal([0], @s.fetch)
  952. assert_equal([1970], @s.fetch)
  953. assert_equal([2069], @s.fetch)
  954. assert_equal([1901], @s.fetch)
  955. assert_equal([2155], @s.fetch)
  956. end
  957. end
  958. def test_fetch_char()
  959. if @m.server_version >= 40100 then
  960. @m.query("create temporary table t (i char(10))")
  961. @m.query("insert into t values (null),('abc')")
  962. @s.prepare("select i from t")
  963. @s.execute
  964. assert_equal([nil], @s.fetch)
  965. assert_equal(["abc"], @s.fetch)
  966. end
  967. end
  968. def test_fetch_varchar()
  969. if @m.server_version >= 40100 then
  970. @m.query("create temporary table t (i varchar(10))")
  971. @m.query("insert into t values (null),('abc')")
  972. @s.prepare("select i from t")
  973. @s.execute
  974. assert_equal([nil], @s.fetch)
  975. assert_equal(["abc"], @s.fetch)
  976. end
  977. end
  978. def test_fetch_binary()
  979. if @m.server_version >= 40100 then
  980. @m.query("create temporary table t (i binary(10))")
  981. @m.query("insert into t values (null),('abc')")
  982. @s.prepare("select i from t")
  983. @s.execute
  984. assert_equal([nil], @s.fetch)
  985. if @m.server_version >= 50000 then
  986. assert_equal(["abc\0\0\0\0\0\0\0"], @s.fetch)
  987. else
  988. assert_equal(["abc"], @s.fetch)
  989. end
  990. end
  991. end
  992. def test_fetch_varbinary()
  993. if @m.server_version >= 40100 then
  994. @m.query("create temporary table t (i varbinary(10))")
  995. @m.query("insert into t values (null),('abc')")
  996. @s.prepare("select i from t")
  997. @s.execute
  998. assert_equal([nil], @s.fetch)
  999. assert_equal(["abc"], @s.fetch)
  1000. end
  1001. end
  1002. def test_fetch_tinyblob()
  1003. if @m.server_version >= 40100 then
  1004. @m.query("create temporary table t (i tinyblob)")
  1005. @m.query("insert into t values (null),('abc')")
  1006. @s.prepare("select i from t")
  1007. @s.execute
  1008. assert_equal([nil], @s.fetch)
  1009. assert_equal(["abc"], @s.fetch)
  1010. end
  1011. end
  1012. def test_fetch_tinytext()
  1013. if @m.server_version >= 40100 then
  1014. @m.query("create temporary table t (i tinytext)")
  1015. @m.query("insert into t values (null),('abc')")
  1016. @s.prepare("select i from t")
  1017. @s.execute
  1018. assert_equal([nil], @s.fetch)
  1019. assert_equal(["abc"], @s.fetch)
  1020. end
  1021. end
  1022. def test_fetch_blob()
  1023. if @m.server_version >= 40100 then
  1024. @m.query("create temporary table t (i blob)")
  1025. @m.query("insert into t values (null),('abc')")
  1026. @s.prepare("select i from t")
  1027. @s.execute
  1028. assert_equal([nil], @s.fetch)
  1029. assert_equal(["abc"], @s.fetch)
  1030. end
  1031. end
  1032. def test_fetch_text()
  1033. if @m.server_version >= 40100 then
  1034. @m.query("create temporary table t (i text)")
  1035. @m.query("insert into t values (null),('abc')")
  1036. @s.prepare("select i from t")
  1037. @s.execute
  1038. assert_equal([nil], @s.fetch)
  1039. assert_equal(["abc"], @s.fetch)
  1040. end
  1041. end
  1042. def test_fetch_mediumblob()
  1043. if @m.server_version >= 40100 then
  1044. @m.query("create temporary table t (i mediumblob)")
  1045. @m.query("insert into t values (null),('abc')")
  1046. @s.prepare("select i from t")
  1047. @s.execute
  1048. assert_equal([nil], @s.fetch)
  1049. assert_equal(["abc"], @s.fetch)
  1050. end
  1051. end
  1052. def test_fetch_mediumtext()
  1053. if @m.server_version >= 40100 then
  1054. @m.query("create temporary table t (i mediumtext)")
  1055. @m.query("insert into t values (null),('abc')")
  1056. @s.prepare("select i from t")
  1057. @s.execute
  1058. assert_equal([nil], @s.fetch)
  1059. assert_equal(["abc"], @s.fetch)
  1060. end
  1061. end
  1062. def test_fetch_longblob()
  1063. if @m.server_version >= 40100 then
  1064. @m.query("create temporary table t (i longblob)")
  1065. @m.query("insert into t values (null),('abc')")
  1066. @s.prepare("select i from t")
  1067. @s.execute
  1068. assert_equal([nil], @s.fetch)
  1069. assert_equal(["abc"], @s.fetch)
  1070. end
  1071. end
  1072. def test_fetch_longtext()
  1073. if @m.server_version >= 40100 then
  1074. @m.query("create temporary table t (i longtext)")
  1075. @m.query("insert into t values (null),('abc')")
  1076. @s.prepare("select i from t")
  1077. @s.execute
  1078. assert_equal([nil], @s.fetch)
  1079. assert_equal(["abc"], @s.fetch)
  1080. end
  1081. end
  1082. def test_fetch_enum()
  1083. if @m.server_version >= 40100 then
  1084. @m.query("create temporary table t (i enum('abc','def'))")
  1085. @m.query("insert into t values (null),(0),(1),(2),('abc'),('def'),('ghi')")
  1086. @s.prepare("select i from t")
  1087. @s.execute
  1088. assert_equal([nil], @s.fetch)
  1089. assert_equal([""], @s.fetch)
  1090. assert_equal(["abc"], @s.fetch)
  1091. assert_equal(["def"], @s.fetch)
  1092. assert_equal(["abc"], @s.fetch)
  1093. assert_equal(["def"], @s.fetch)
  1094. assert_equal([""], @s.fetch)
  1095. end
  1096. end
  1097. def test_fetch_set()
  1098. if @m.server_version >= 40100 then
  1099. @m.query("create temporary table t (i set('abc','def'))")
  1100. @m.query("insert into t values (null),(0),(1),(2),(3),('abc'),('def'),('abc,def'),('ghi')")
  1101. @s.prepare("select i from t")
  1102. @s.execute
  1103. assert_equal([nil], @s.fetch)
  1104. assert_equal([""], @s.fetch)
  1105. assert_equal(["abc"], @s.fetch)
  1106. assert_equal(["def"], @s.fetch)
  1107. assert_equal(["abc,def"], @s.fetch)
  1108. assert_equal(["abc"], @s.fetch)
  1109. assert_equal(["def"], @s.fetch)
  1110. assert_equal(["abc,def"], @s.fetch)
  1111. assert_equal([""], @s.fetch)
  1112. end
  1113. end
  1114. def test_each()
  1115. if @m.server_version >= 40100 then
  1116. @m.query("create temporary table t (i int, c char(255), d datetime)")
  1117. @m.query("insert into t values (1,'abc','19701224235905'),(2,'def','21120903123456'),(3,'123',null)")
  1118. @s.prepare("select * from t")
  1119. @s.execute
  1120. c = 0
  1121. @s.each do |a|
  1122. case c
  1123. when 0
  1124. assert_equal([1,"abc",Mysql::Time.new(1970,12,24,23,59,05)], a)
  1125. when 1
  1126. assert_equal([2,"def",Mysql::Time.new(2112,9,3,12,34,56)], a)
  1127. when 2
  1128. assert_equal([3,"123",nil], a)
  1129. else
  1130. raise
  1131. end
  1132. c += 1
  1133. end
  1134. end
  1135. end
  1136. def test_field_count()
  1137. if @m.server_version >= 40100 then
  1138. @s.prepare("select 1,2,3")
  1139. @s.execute()
  1140. assert_equal(3, @s.field_count())
  1141. @s.prepare("set @a=1")
  1142. @s.execute()
  1143. assert_equal(0, @s.field_count())
  1144. end
  1145. end
  1146. def test_free_result()
  1147. if @m.server_version >= 40100 then
  1148. @s.free_result()
  1149. @s.prepare("select 1,2,3")
  1150. @s.execute()
  1151. @s.free_result()
  1152. end
  1153. end
  1154. def test_insert_id()
  1155. if @m.server_version >= 40100 then
  1156. @m.query("create temporary table t (i bigint auto_increment, unique(i))")
  1157. @s.prepare("insert into t values (?)")
  1158. @s.execute(0)
  1159. assert_equal(1, @s.insert_id())
  1160. @s.execute(0)
  1161. assert_equal(2, @s.insert_id())
  1162. @s.execute(2**32)
  1163. assert_equal(2**32, @s.insert_id())
  1164. @s.execute(0)
  1165. assert_equal(2**32+1, @s.insert_id())
  1166. end
  1167. end
  1168. def test_num_rows()
  1169. if @m.server_version >= 40100 then
  1170. @m.query("create temporary table t (i int)")
  1171. @m.query("insert into t values (1),(2),(3),(4)")
  1172. @s.prepare("select * from t")
  1173. @s.execute
  1174. assert_equal(4, @s.num_rows())
  1175. end
  1176. end
  1177. def test_param_count()
  1178. if @m.server_version >= 40100 then
  1179. @m.query("create temporary table t (a int, b int, c int)")
  1180. @s.prepare("select * from t")
  1181. assert_equal(0, @s.param_count())
  1182. @s.prepare("insert into t values (?,?,?)")
  1183. assert_equal(3, @s.param_count())
  1184. end
  1185. end
  1186. =begin
  1187. def test_param_metadata()
  1188. @s.param_metadata()
  1189. end
  1190. =end
  1191. def test_prepare()
  1192. if @m.server_version >= 40100 then
  1193. @s.prepare("select 1")
  1194. assert_raises(Mysql::Error){@s.prepare("invalid syntax")}
  1195. end
  1196. end
  1197. =begin
  1198. def test_reset()
  1199. @s.reset()
  1200. end
  1201. =end
  1202. def test_result_metadata()
  1203. if @m.server_version >= 40100 then
  1204. @s.prepare("select 1 foo, 2 bar")
  1205. res = @s.result_metadata()
  1206. f = res.fetch_fields
  1207. assert_equal("foo", f[0].name)
  1208. assert_equal("bar", f[1].name)
  1209. end
  1210. end
  1211. def test_result_metadata_nodata()
  1212. if @m.server_version >= 40100 then
  1213. @m.query("create temporary table t (i int)")
  1214. @s.prepare("insert into t values (1)")
  1215. assert_equal(nil, @s.result_metadata())
  1216. end
  1217. end
  1218. def test_row_seek_tell()
  1219. if @m.server_version >= 40100 then
  1220. @m.query("create temporary table t (i int)")
  1221. @m.query("insert into t values (0),(1),(2),(3),(4)")
  1222. @s.prepare("select * from t")
  1223. @s.execute
  1224. row0 = @s.row_tell
  1225. assert_equal([0], @s.fetch)
  1226. assert_equal([1], @s.fetch)
  1227. row2 = @s.row_seek(row0)
  1228. assert_equal([0], @s.fetch)
  1229. @s.row_seek(row2)
  1230. assert_equal([2], @s.fetch)
  1231. end
  1232. end
  1233. =begin
  1234. def test_send_long_data()
  1235. @m.query("create temporary table t (i int, t text)")
  1236. @s.prepare("insert into t values (?,?)")
  1237. @s.send_long_data(1, "long long data ")
  1238. @s.send_long_data(1, "long long data2")
  1239. assert_raises(Mysql::Error){@s.send_long_data(9, "invalid param number")}
  1240. @s.execute(99, "hoge")
  1241. assert_equal("long long data long long data2", @m.query("select t from t").fetch_row[0])
  1242. end
  1243. =end
  1244. def test_sqlstate()
  1245. if @m.server_version >= 40100 then
  1246. @s.prepare("select 1")
  1247. if @m.client_version >= 50000 then
  1248. assert_equal("00000", @s.sqlstate)
  1249. else
  1250. assert_equal("", @s.sqlstate)
  1251. end
  1252. assert_raises(Mysql::Error){@s.prepare("hogehoge")}
  1253. assert_equal("42000", @s.sqlstate)
  1254. end
  1255. end
  1256. =begin
  1257. def test_store_result()
  1258. @s.store_result()
  1259. end
  1260. =end
  1261. end if Mysql.client_version >= 40100
  1262. class TC_MysqlTime < Test::Unit::TestCase
  1263. def setup()
  1264. end
  1265. def teardown()
  1266. end
  1267. def test_init()
  1268. t = Mysql::Time.new
  1269. assert_equal(0, t.year);
  1270. assert_equal(0, t.month);
  1271. assert_equal(0, t.day);
  1272. assert_equal(0, t.hour);
  1273. assert_equal(0, t.minute);
  1274. assert_equal(0, t.second);
  1275. assert_equal(false, t.neg);
  1276. assert_equal(0, t.second_part);
  1277. end
  1278. def test_year()
  1279. t = Mysql::Time.new
  1280. assert_equal(2005, t.year = 2005)
  1281. assert_equal(2005, t.year)
  1282. end
  1283. def test_month()
  1284. t = Mysql::Time.new
  1285. assert_equal(11, t.month = 11)
  1286. assert_equal(11, t.month)
  1287. end
  1288. def test_day()
  1289. t = Mysql::Time.new
  1290. assert_equal(23, t.day = 23)
  1291. assert_equal(23, t.day)
  1292. end
  1293. def test_hour()
  1294. t = Mysql::Time.new
  1295. assert_equal(15, t.hour = 15)
  1296. assert_equal(15, t.hour)
  1297. end
  1298. def test_minute()
  1299. t = Mysql::Time.new
  1300. assert_equal(58, t.month = 58)
  1301. assert_equal(58, t.month)
  1302. end
  1303. def test_second()
  1304. t = Mysql::Time.new
  1305. assert_equal(34, t.second = 34)
  1306. assert_equal(34, t.second)
  1307. end
  1308. def test_tos()
  1309. t = Mysql::Time.new(2005, 7, 19, 10, 15, 49)
  1310. assert_equal("2005-07-19 10:15:49", t.to_s)
  1311. end
  1312. def test_eql()
  1313. t1 = Mysql::Time.new(2005,7,19,23,56,13)
  1314. t2 = Mysql::Time.new(2005,7,19,23,56,13)
  1315. assert_equal(t1, t2)
  1316. end
  1317. end if Mysql.client_version >= 40100