ffi-mysql 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.hgignore ADDED
@@ -0,0 +1,7 @@
1
+ syntax: glob
2
+
3
+ .yardoc/
4
+ doc/
5
+ pkg/
6
+ *~
7
+ .hg/
data/.hgtags CHANGED
@@ -1,2 +1,4 @@
1
1
  afd66b4b7cda50c08a79f51574d46e49c4dc083b v0.0.1
2
2
  2bfcf2ddedd5d9589ecc0dc915d0393e218aa603 v0.0.2
3
+ a8bbc2c818cd66f86fbeb43f59bdb21499d58768 v1.0.0
4
+ 84d7cd84016aaade9b5530d183d9d3378d1c0c33 v1.1.0
data/History.markdown CHANGED
@@ -1,9 +1,17 @@
1
+ 1.1.0 / 2012-01-21
2
+ ------------------
3
+
4
+ * add several methods for compatibility with MySQL 5.0 and MySQL/Ruby
5
+ 2.8.2
6
+
7
+
1
8
  1.0.0 / 2010-04-21
2
9
  ------------------
3
10
 
4
11
  * fix Fixnum size bugs
5
12
  * update to mysql-ruby-2.8.2 test-suite
6
13
 
14
+
7
15
  0.0.2 / 2010-03-08
8
16
  ------------------
9
17
 
data/README.markdown CHANGED
@@ -31,7 +31,7 @@ for examples.
31
31
  REQUIREMENTS
32
32
  ------------
33
33
 
34
- * ffi >= 0.6.2
34
+ * ffi ~> 1.0
35
35
 
36
36
  INSTALL
37
37
  -------
@@ -44,6 +44,8 @@ ACKNOWLEDGEMENTS
44
44
  Tomita Masahiro for his MySQL/Ruby and Ruby/MySQL gems from which some
45
45
  of the code has been stolen.
46
46
 
47
+ Norman Meilick for implementing some missing methods.
48
+
47
49
  LICENSE
48
50
  -------
49
51
 
data/announcement.txt ADDED
@@ -0,0 +1,13 @@
1
+ ffi-mysql version 1.1.0
2
+ by Frank Fischer
3
+ http://bitbucket.org/lyro/ffi-mysql
4
+
5
+ == DESCRIPTION
6
+
7
+ Pure Ruby FFI interface to MySQL.
8
+
9
+ == CHANGES
10
+
11
+ * add several methods for compatibility with MySQL 5.0 and MySQL/Ruby
12
+ 2.8.2
13
+
@@ -1,4 +1,11 @@
1
1
  class Mysql
2
2
  class Error < Exception
3
+ alias error message
4
+ attr_reader :errno
5
+
6
+ def initialize(msg = nil, errno = nil)
7
+ super msg
8
+ @errno = errno
9
+ end
3
10
  end
4
11
  end
@@ -63,21 +63,56 @@ class Mysql
63
63
  :type, FieldType)
64
64
  end
65
65
 
66
- StmtAttrType = enum( :update_max_length, :cursor_type, :prefetch_rows )
66
+ def self.maybe_attach_function name, *args
67
+ attach_function name, *args
68
+ rescue FFI::NotFoundError => e
69
+ define_singleton_method(name) {|*a| raise e}
70
+ end
67
71
 
72
+ StmtAttrType = enum( :update_max_length, :cursor_type, :prefetch_rows )
68
73
  attach_function :mysql_init, [:pointer], :pointer
69
74
  attach_function :mysql_close, [:pointer], :void
75
+ attach_function :mysql_errno, [:pointer], :uint
70
76
  attach_function :mysql_error, [:pointer], :string
77
+ attach_function :mysql_warning_count, [:pointer], :uint
78
+ attach_function :mysql_debug, [:string], :void
79
+ attach_function :mysql_dump_debug_info, [:pointer], :int
80
+ attach_function :mysql_ping, [:pointer], :int
81
+ attach_function :mysql_refresh, [:pointer, :uint ], :int
82
+ maybe_attach_function :mysql_reload, [:pointer], :int
83
+ attach_function :mysql_shutdown, [:pointer], :int
84
+ attach_function :mysql_change_user, [:pointer, :string, :string, :string], :char
85
+ attach_function :mysql_select_db, [:pointer, :string], :int
86
+ attach_function :mysql_thread_id, [:pointer], :ulong
87
+ attach_function :mysql_kill, [:pointer, :ulong], :int
88
+ attach_function :mysql_info, [:pointer], :string
89
+ attach_function :mysql_stat, [:pointer], :string
90
+ attach_function :mysql_character_set_name, [:pointer], :string
91
+ attach_function :mysql_set_character_set, [:pointer, :string], :int
71
92
  attach_function :mysql_get_client_version, [], :int
72
93
  attach_function :mysql_get_client_info, [], :string
73
94
  attach_function :mysql_get_server_version, [:pointer], :int
74
95
  attach_function :mysql_get_server_info, [:pointer], :string
96
+ attach_function :mysql_get_host_info, [:pointer], :string
97
+ attach_function :mysql_get_proto_info, [:pointer], :uint
98
+ attach_function :mysql_ssl_set, [:pointer, :string, :string, :string, :string, :string], :int
75
99
  attach_function :mysql_real_connect, [:pointer, :string, :string, :string, :string, :uint, :string, :ulong], :pointer
100
+ attach_function :mysql_ping, [:pointer], :int
76
101
  attach_function :mysql_options, [:pointer, :int, :pointer], :int
77
102
  attach_function :mysql_set_server_option, [:pointer, :int], :int
103
+ attach_function :mysql_real_escape_string, [:pointer, :pointer, :char, :ulong], :ulong
78
104
  attach_function :mysql_real_query, [:pointer, :string, :ulong], :int
105
+ attach_function :mysql_insert_id, [:pointer], :ulong_long
106
+ attach_function :mysql_list_dbs, [:pointer, :string], :pointer
107
+ attach_function :mysql_list_fields, [:pointer, :string, :string], :pointer
108
+ attach_function :mysql_list_processes, [:pointer], :pointer
109
+ attach_function :mysql_list_tables, [:pointer, :string], :pointer
110
+ attach_function :mysql_autocommit, [:pointer, :char], :char
111
+ attach_function :mysql_commit, [:pointer], :char
112
+ attach_function :mysql_rollback, [:pointer], :char
79
113
  attach_function :mysql_field_count, [:pointer], :uint
80
114
  attach_function :mysql_store_result, [:pointer], :pointer
115
+ attach_function :mysql_use_result, [:pointer], :pointer
81
116
  attach_function :mysql_free_result, [:pointer], :void
82
117
  attach_function :mysql_next_result, [:pointer], :int
83
118
  attach_function :mysql_more_results, [:pointer], :bool
@@ -116,6 +151,7 @@ class Mysql
116
151
  attach_function :mysql_stmt_sqlstate, [:pointer], :string
117
152
  attach_function :mysql_stmt_errno, [:pointer], :uint
118
153
  attach_function :mysql_stmt_error, [:pointer], :string
154
+
119
155
  end
120
156
 
121
157
  # Creates a new MySQL object.
@@ -142,12 +178,25 @@ class Mysql
142
178
  def client_version
143
179
  Mysql.client_version
144
180
  end
181
+ alias get_client_version client_version
145
182
 
146
183
  # @return [String] string containing the client's version
147
184
  def self.client_info
148
185
  C::mysql_get_client_info
149
186
  end
150
187
 
188
+ # @return [String] string containing the client's version
189
+ def client_info
190
+ Mysql.client_info
191
+ end
192
+ alias get_client_info client_info
193
+
194
+ # Does a DBUG_PUSH with the given string.
195
+ def self.debug(arg)
196
+ C::mysql_debug(arg)
197
+ self
198
+ end
199
+
151
200
  # Escape special character in MySQL.
152
201
  # === Note
153
202
  # In Ruby 1.8, this is not safe for multibyte charset such as 'SJIS'.
@@ -196,6 +245,22 @@ class Mysql
196
245
  end
197
246
  end
198
247
 
248
+ # Set SSL settings (must be called before real_connect)
249
+ #
250
+ # @param [String] key path to the key file
251
+ # @param [String] cert path to the certificate file
252
+ # @param [String] ca path to the certificate authority file
253
+ # @param [String] capath path name to a directory with SSL CA certificates in pem format
254
+ # @param [String] cipher list of permissible ciphers to use
255
+ #
256
+ # Any unused SSL parameters may be given as NULL.
257
+ def ssl_set(key, cert, ca, capath, cipher)
258
+ if C::mysql_ssl_set(@mysql, key, cert, ca, capath, cipher) != 0
259
+ raise Error.new(error, errno)
260
+ end
261
+ self
262
+ end
263
+
199
264
  # Opens a new connection to a MySQL server.
200
265
  #
201
266
  # @param [String] host the MySQL server
@@ -210,7 +275,7 @@ class Mysql
210
275
  ObjectSpace.define_finalizer( self, Mysql.finalizer(@mysql, @mysql_free))
211
276
 
212
277
  if C::mysql_real_connect( @mysql, host, user, passwd, db, port, sock, flag ).null?
213
- raise Mysql::Error, error_msg
278
+ raise Mysql::Error, error
214
279
  end
215
280
 
216
281
  @connected = true
@@ -224,8 +289,154 @@ class Mysql
224
289
  @mysql = nil
225
290
  @mysql_free[0] = true
226
291
  @connected = false
292
+ self
293
+ end
294
+
295
+ # Checks whether the connection to the server is working.
296
+ def ping
297
+ if C::mysql_ping(@mysql) != 0
298
+ raise Error.new(error, errno)
299
+ end
300
+ self
301
+ end
302
+
303
+ # @return [Integer] error code for the most recently failed API function
304
+ def errno
305
+ C::mysql_errno(@mysql)
306
+ end
307
+
308
+ # @return [String] error message for the most recently failing API function
309
+ def error
310
+ C::mysql_error(@mysql)
311
+ end
312
+
313
+ # @return [Integer] the number of errors, warnings and notes generated during the previous SQL statement
314
+ def warning_count
315
+ C::mysql_warning_count(@mysql)
316
+ end
317
+
318
+ # Write debug information to the log
319
+ # Raises an error when there is a problem, otherwise returns self.
320
+ def dump_debug_info
321
+ if C::mysql_dump_debug_info(@mysql) != 0
322
+ raise Error.new(error, errno)
323
+ end
324
+ self
325
+ end
326
+
327
+ # Check whether the connection to the server is working.
328
+ # Raises an error when there is a problem, otherwise returns self.
329
+ def ping
330
+ if C::mysql_ping(@mysql) != 0
331
+ raise Error.new(error, errno)
332
+ end
333
+ self
334
+ end
335
+
336
+ # Flush tables or caches, or reset replication server information.
337
+ #
338
+ # @param [Integer] options bit mask of REFRESH_... values
339
+ def refresh(options)
340
+ if C::mysql_refresh(@mysql, options) != 0
341
+ raise Error.new(error, errno)
342
+ end
343
+ self
344
+ end
345
+
346
+ # Asks the MySQL server to reload the grant table.
347
+ def reload
348
+ if C::mysql_reload(@mysql) != 0
349
+ raise Error.new(error, errno)
350
+ end
351
+ self
352
+ end
353
+
354
+ # Asks the MySQL server to shut down.
355
+ def shutdown
356
+ if C::mysql_shutdown(@mysql) != 0
357
+ raise Error.new(error, errno)
358
+ end
359
+ self
360
+ end
361
+
362
+ # Change the user and change to a different database
363
+ #
364
+ # @param [String] user user to change to
365
+ # @param [String] password password of user
366
+ # @param [String] db name of database to change to
367
+ #
368
+ # Raises an error when there is a problem, otherwise returns self.
369
+ def change_user(user, password, db)
370
+ if C::mysql_change_user(@mysql, user, password, db) != 0
371
+ raise Error.new(error, errno)
372
+ end
373
+ self
374
+ end
375
+
376
+ # Change the current database
377
+ #
378
+ # @param [String] db name of database to change to
379
+ #
380
+ # Raises an error when there is a problem, otherwise returns self.
381
+ def select_db(db)
382
+ if C::mysql_select_db(@mysql, db) != 0
383
+ raise Error.new(error, errno)
384
+ end
385
+ self
227
386
  end
228
387
 
388
+ # Returns the thread ID of the current connection
389
+ # @return [Integer] thread id of the current connection
390
+ def thread_id
391
+ C::mysql_thread_id(@mysql)
392
+ end
393
+
394
+ # Asks the server to kill the specified thread.
395
+ # @param [Integer] pid thread id to kill
396
+ def kill(pid)
397
+ if C::mysql_kill(@mysql, pid) != 0
398
+ raise Error.new(error, errno)
399
+ end
400
+ self
401
+ end
402
+
403
+ # Return information about the most recently executed statement
404
+ # @return [String] information about statement
405
+ def info
406
+ C::mysql_info(@mysql)
407
+ end
408
+
409
+ # Return information about the server status
410
+ # @return [String] string describing the server status
411
+ def stat
412
+ C::mysql_stat(@mysql)
413
+ end
414
+
415
+ # @return [String] default character set name for the current connection
416
+ def character_set_name
417
+ C::mysql_character_set_name(@mysql)
418
+ end
419
+
420
+ # Set default character set for the current connection.
421
+ # @param [String] csname default character set to use
422
+ def set_character_set(csname)
423
+ if C::mysql_set_character_set(@mysql, csname) != 0
424
+ raise Error.new(error, errno)
425
+ end
426
+ self
427
+ end
428
+
429
+ # Returns a legal SQL string to be used in SQL statements
430
+ # @param [String] str The string to be escaped
431
+ # @return [String] The escaped string
432
+ def real_escape_string(str)
433
+ to = FFI::MemoryPointer.new(str.size * 2 + 1, 1, false)
434
+ len = C::mysql_real_escape_string(@mysql, to, str, str.size)
435
+ to.read_string(len)
436
+ end
437
+ alias :escape_string :real_escape_string
438
+ alias :quote :real_escape_string
439
+
229
440
  # @return [Integer] the version of the server
230
441
  def server_version
231
442
  C::mysql_get_server_version(@mysql)
@@ -238,12 +449,23 @@ class Mysql
238
449
  end
239
450
  alias get_server_info server_info
240
451
 
452
+ # @return [String] string containing the host name and connection type
453
+ def host_info
454
+ C::mysql_get_host_info(@mysql)
455
+ end
456
+ alias get_host_info host_info
457
+
458
+ # @return [Integer] containing the current connection's procol version
459
+ def proto_info
460
+ C::mysql_get_proto_info(@mysql)
461
+ end
462
+ alias get_proto_info proto_info
463
+
241
464
  # @return [String] the SQLSTATE error code for the most recent statement
242
465
  def sqlstate
243
466
  C::mysql_sqlstate(@mysql)
244
467
  end
245
468
 
246
-
247
469
  # Sets extra connection options.
248
470
  #
249
471
  # @param [Integer] option the option to set
@@ -262,7 +484,7 @@ class Mysql
262
484
  else
263
485
  raise ArgumentError, "value must one of [String, Integer, nil, true, false]"
264
486
  end
265
- raise Error, error_msg if result != 0
487
+ raise Error.new(error, errno) if result != 0
266
488
  self
267
489
  end
268
490
 
@@ -271,12 +493,11 @@ class Mysql
271
493
  # @param [Integer] option server option
272
494
  def set_server_option( option )
273
495
  if C::mysql_set_server_option( @mysql, option ) != 0
274
- raise Error, error_msg
496
+ raise Error.new(error, errno)
275
497
  end
276
498
  self
277
499
  end
278
500
 
279
-
280
501
  # Sets the auto-reconnect flag of mysql
281
502
  #
282
503
  # @param [true,false] The new reconnect state.
@@ -291,6 +512,89 @@ class Mysql
291
512
  @reconnect
292
513
  end
293
514
 
515
+ # Returns the value generated for an AUTO_INCREMENT column by
516
+ # the previous INSERT or UPDATE statement.
517
+ def insert_id
518
+ C::mysql_insert_id(@mysql)
519
+ end
520
+
521
+ # Returns a list of all databases on the server matching a pattern
522
+ # @param [String,nil] wild The pattern the databases must match
523
+ def list_dbs(wild = nil)
524
+ res = C::mysql_list_dbs(@mysql, wild)
525
+ if res.null?
526
+ raise Error.new(error, errno)
527
+ end
528
+ res = Result.new(@mysql, res)
529
+ dbs = []
530
+ res.each do |row|
531
+ dbs << row[0]
532
+ end
533
+ dbs
534
+ end
535
+
536
+ # Returns a result consisting the field names in the given table matching a pattern
537
+ # @param [String] table The table whose fields should be returned
538
+ # @param [String,nil] wild The pattern the field names must match
539
+ # @return [Result] A MySQL result of the field names
540
+ def list_fields(table, wild = nil)
541
+ res = C::mysql_list_fields(@mysql, table, wild)
542
+ if res.null?
543
+ raise Error.new(error, errno)
544
+ end
545
+ Result.new(@mysql, res)
546
+ end
547
+
548
+ # Returns a result describing the current server threads
549
+ # @return [Result] A MySQL result of the current server threads
550
+ def list_processes
551
+ res = C::mysql_list_processes(@mysql)
552
+ if res.null?
553
+ raise Error.new(error, errno)
554
+ end
555
+ Result.new(@mysql, res)
556
+ end
557
+
558
+ # Returns a result consisting the table names matching a pattern
559
+ # @param [String,nil] wild The pattern the table names must match
560
+ # @return [Array<String>] The table names
561
+ def list_tables(wild = nil)
562
+ res = C::mysql_list_tables(@mysql, wild)
563
+ if res.null?
564
+ raise Error.new(error, errno)
565
+ end
566
+ res = Result.new(@mysql, res)
567
+ tables = []
568
+ res.each do |row|
569
+ tables << row[0]
570
+ end
571
+ tables
572
+ end
573
+
574
+ # Sets autocommit mode on if mode is true, off if mode is false.
575
+ def autocommit(mode)
576
+ if C::mysql_autocommit(@mysql, mode ? 1 : 0) != 0
577
+ raise Error.new(error, errno)
578
+ end
579
+ self
580
+ end
581
+
582
+ # Commit transaction
583
+ def commit
584
+ if C::mysql_commit(@mysql) != 0
585
+ raise Error.new(error, errno)
586
+ end
587
+ self
588
+ end
589
+
590
+ # Rollback the current transaction
591
+ def rollback
592
+ if C::mysql_rollback(@mysql) != 0
593
+ raise Error.new(error, errno)
594
+ end
595
+ self
596
+ end
597
+
294
598
  # Execute a query statement.
295
599
  #
296
600
  # @param [String] sql the SQL statement
@@ -300,7 +604,7 @@ class Mysql
300
604
  def query(sql)
301
605
  raise Error, "Not connected" unless @connected
302
606
  if C::mysql_real_query(@mysql, sql, sql.size) != 0
303
- raise Error, error_msg
607
+ raise Error.new(error, errno)
304
608
  end
305
609
 
306
610
  if block_given?
@@ -321,11 +625,26 @@ class Mysql
321
625
  self
322
626
  end
323
627
  end
628
+ alias real_query query
324
629
 
325
630
  # Stores the current result in a result set.
326
631
  # @return [Result] the result set
327
632
  def store_result
328
- Result.new(@mysql, C::mysql_store_result(@mysql))
633
+ res = C::mysql_store_result(@mysql)
634
+ if res.null?
635
+ raise Error.new(error, errno)
636
+ end
637
+ Result.new(@mysql, res)
638
+ end
639
+
640
+ # Initialize result set retrieval but do not read data.
641
+ # @return [Result] the result set
642
+ def use_result
643
+ res = C::mysql_use_result(@mysql)
644
+ if res.null?
645
+ raise Error.new(error, errno)
646
+ end
647
+ Result.new(@mysql, res)
329
648
  end
330
649
 
331
650
  # Advances to the next result set.
@@ -337,7 +656,7 @@ class Mysql
337
656
  elsif result < 0
338
657
  false
339
658
  else
340
- raise Error, error_msg
659
+ raise Error.new(error, errno)
341
660
  end
342
661
  end
343
662
 
@@ -368,11 +687,4 @@ class Mysql
368
687
  def prepare( stmt )
369
688
  stmt_init.prepare(stmt)
370
689
  end
371
-
372
- # Returns the current error message.
373
- def error_msg
374
- C::mysql_error(@mysql)
375
- end
376
- private :error_msg
377
-
378
690
  end
@@ -0,0 +1,10 @@
1
+ --- mysql.rb
2
+ +++ mysql.rb
3
+ @@ -74,6 +74,7 @@
4
+ attach_function :mysql_ping, [:pointer], :int
5
+ attach_function :mysql_refresh, [:pointer, :uint ], :int
6
+ attach_function :mysql_reload, [:pointer], :int
7
+ + attach_function :mysql_shutdown, [:pointer], :int
8
+ attach_function :mysql_change_user, [:pointer, :string, :string, :string], :char
9
+ attach_function :mysql_select_db, [:pointer, :string], :int
10
+ attach_function :mysql_thread_id, [:pointer], :ulong
@@ -2,7 +2,7 @@ class Mysql
2
2
  # Major version
3
3
  MAJOR = 1
4
4
  # Minor version
5
- MINOR = 0
5
+ MINOR = 1
6
6
  # Tiny version
7
7
  TINY = 0
8
8
 
data/test/test_mysql.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/local/bin/ruby
2
- # $Id: test.rb 244 2009-02-01 08:43:39Z tommy $
3
2
 
4
3
  require "test/unit"
5
4
  require 'ostruct'
@@ -30,7 +29,7 @@ class TC_Mysql < Test::Unit::TestCase
30
29
  end
31
30
 
32
31
  def test_version()
33
- assert_equal("1.0.0", Mysql::VERSION)
32
+ assert_equal("1.1.0", Mysql::VERSION)
34
33
  end
35
34
 
36
35
  def test_init()
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ffi-mysql
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Frank Fischer
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-21 00:00:00 +02:00
13
+ date: 2012-01-21 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: 3.6.5
35
+ version: 3.7.3
36
36
  type: :development
37
37
  version_requirements: *id002
38
38
  description: Pure Ruby FFI interface to MySQL.
@@ -41,24 +41,27 @@ executables: []
41
41
 
42
42
  extensions: []
43
43
 
44
- extra_rdoc_files: []
45
-
44
+ extra_rdoc_files:
45
+ - announcement.txt
46
+ - lib/ffi-mysql/mysql.rb.rej
46
47
  files:
48
+ - .hgignore
47
49
  - .hgtags
48
50
  - History.markdown
49
51
  - README.markdown
50
52
  - Rakefile
53
+ - announcement.txt
51
54
  - lib/ffi-mysql.rb
52
55
  - lib/ffi-mysql/constants.rb
53
56
  - lib/ffi-mysql/error.rb
54
57
  - lib/ffi-mysql/field.rb
55
58
  - lib/ffi-mysql/mysql.rb
59
+ - lib/ffi-mysql/mysql.rb.rej
56
60
  - lib/ffi-mysql/result.rb
57
61
  - lib/ffi-mysql/stmt.rb
58
62
  - lib/ffi-mysql/time.rb
59
63
  - lib/ffi-mysql/version.rb
60
64
  - test/test_mysql.rb
61
- - test/test_mysql.rb.orig
62
65
  has_rdoc: true
63
66
  homepage: http://bitbucket.org/lyro/ffi-mysql
64
67
  licenses: []