PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/mysql-2.8.1/test/test_mysql.rb

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