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

/test/test_mysql.rb

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