Class: Mysql::Stmt
- Inherits:
-
Object
- Object
- Mysql::Stmt
- Includes:
- Enumerable
- Defined in:
- lib/mysql.rb,
lib/mysql/constants.rb
Overview
Prepared statement
Constant Summary collapse
- CURSOR_TYPE_NO_CURSOR =
Cursor type
0
- CURSOR_TYPE_READ_ONLY =
1
- CURSOR_TYPE_FOR_UPDATE =
2
- CURSOR_TYPE_SCROLLABLE =
4
Instance Attribute Summary collapse
- #affected_rows ⇒ Integer readonly
- #fields ⇒ Array<Mysql::Field> readonly
- #insert_id ⇒ Integer readonly
- #param_count ⇒ Integer readonly
- #server_status ⇒ Integer readonly
- #sqlstate ⇒ String readonly
- #warning_count ⇒ Integer readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#bind_result(*args) ⇒ Mysql::Stmt
Set retrieve type of value.
-
#close ⇒ void
Close prepared statement.
-
#data_seek(n) ⇒ void
Set record position.
-
#each {|Array| ... } ⇒ Mysql::Stmt, Enumerator
Iterate block with record.
-
#each_hash(with_table = nil) {|Hash| ... } ⇒ Mysql::Stmt, Enumerator
Iterate block with record as Hash.
-
#execute(*values) ⇒ Mysql::Stmt
Execute prepared statement.
-
#fetch ⇒ Array
Current record data.
-
#fetch_hash(with_table = nil) ⇒ Hash
Return data of current record as Hash.
-
#field_count ⇒ Integer
Number of columns for last query.
-
#free_result ⇒ void
ignore.
-
#initialize(protocol, charset) ⇒ Stmt
constructor
A new instance of Stmt.
-
#prepare(str) ⇒ Object
parse prepared-statement and return Stmt object.
-
#result_metadata ⇒ Mysql::Result
Returns Mysql::Result object that is empty.
-
#row_seek(n) ⇒ Integer
Set current position of record.
-
#row_tell ⇒ Integer
Current record position.
-
#size ⇒ Integer
(also: #num_rows)
Number of record.
Constructor Details
#initialize(protocol, charset) ⇒ Stmt
Returns a new instance of Stmt.
835 836 837 838 839 840 841 842 843 |
# File 'lib/mysql.rb', line 835 def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end |
Instance Attribute Details
#affected_rows ⇒ Integer (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#fields ⇒ Array<Mysql::Field> (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#insert_id ⇒ Integer (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#param_count ⇒ Integer (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#server_status ⇒ Integer (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#sqlstate ⇒ String (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
#warning_count ⇒ Integer (readonly)
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/mysql.rb', line 819 class Stmt include Enumerable attr_reader :affected_rows, :insert_id, :server_status, :warning_count attr_reader :param_count, :fields, :sqlstate # @private def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end # @private # @param [Mysql::Protocol] protocol # @param [Mysql::Charset] charset def initialize(protocol, charset) @protocol = protocol @charset = charset @statement_id = nil @affected_rows = @insert_id = @server_status = @warning_count = 0 @sqlstate = "00000" @param_count = nil @bind_result = nil end # @private # parse prepared-statement and return {Mysql::Stmt} object # @param [String] str query string # @return self def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end # Execute prepared statement. # @param [Object] values values passed to query # @return [Mysql::Stmt] self def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end # Close prepared statement # @return [void] def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end # @return [Array] current record data def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end # Return data of current record as Hash. # The hash key is field name. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @return [Hash] record data def fetch_hash(with_table=nil) @result.fetch_hash with_table end # Set retrieve type of value # @param [Numeric / Fixnum / Integer / Float / String / Mysql::Time / nil] args value type # @return [Mysql::Stmt] self def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end # Iterate block with record. # @yield [Array] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end # Iterate block with record as Hash. # @param [Boolean] with_table if true, hash key is "table_name.field_name". # @yield [Hash] record data # @return [Mysql::Stmt] self # @return [Enumerator] If block is not specified def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end # @return [Integer] number of record def size @result.size end alias num_rows size # Set record position # @param [Integer] n record index # @return [void] def data_seek(n) @result.data_seek(n) end # @return [Integer] current record position def row_tell @result.row_tell end # Set current position of record # @param [Integer] n record index # @return [Integer] previous position def row_seek(n) @result.row_seek(n) end # @return [Integer] number of columns for last query def field_count @fields.length end # ignore # @return [void] def free_result end # Returns Mysql::Result object that is empty. # Use fetch_fields to get list of fields. # @return [Mysql::Result] def return nil if @fields.empty? Result.new @fields end end |
Class Method Details
.finalizer(protocol, statement_id) ⇒ Object
826 827 828 829 830 |
# File 'lib/mysql.rb', line 826 def self.finalizer(protocol, statement_id) proc do protocol.gc_stmt statement_id end end |
Instance Method Details
#bind_result(*args) ⇒ Mysql::Stmt
Set retrieve type of value
951 952 953 954 955 956 957 958 959 960 |
# File 'lib/mysql.rb', line 951 def bind_result(*args) if @fields.length != args.length raise ClientError, "bind_result: result value count(#{@fields.length}) != number of argument(#{args.length})" end args.each do |a| raise TypeError unless [Numeric, Fixnum, Integer, Float, String, Mysql::Time, nil].include? a end @bind_result = args self end |
#close ⇒ void
This method returns an undefined value.
Close prepared statement
890 891 892 893 894 |
# File 'lib/mysql.rb', line 890 def close ObjectSpace.undefine_finalizer(self) @protocol.stmt_close_command @statement_id if @statement_id @statement_id = nil end |
#data_seek(n) ⇒ void
This method returns an undefined value.
Set record position
996 997 998 |
# File 'lib/mysql.rb', line 996 def data_seek(n) @result.data_seek(n) end |
#each {|Array| ... } ⇒ Mysql::Stmt, Enumerator
Iterate block with record.
966 967 968 969 970 971 972 |
# File 'lib/mysql.rb', line 966 def each(&block) return enum_for(:each) unless block while rec = fetch block.call rec end self end |
#each_hash(with_table = nil) {|Hash| ... } ⇒ Mysql::Stmt, Enumerator
Iterate block with record as Hash.
979 980 981 982 983 984 985 |
# File 'lib/mysql.rb', line 979 def each_hash(with_table=nil, &block) return enum_for(:each_hash, with_table) unless block while rec = fetch_hash(with_table) block.call rec end self end |
#execute(*values) ⇒ Mysql::Stmt
Execute prepared statement.
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
# File 'lib/mysql.rb', line 866 def execute(*values) raise ClientError, "not prepared" unless @param_count raise ClientError, "parameter count mismatch" if values.length != @param_count values = values.map{|v| @charset.convert v} begin @sqlstate = "00000" nfields = @protocol.stmt_execute_command @statement_id, values if nfields @fields = @protocol.retr_fields nfields @result = StatementResult.new @fields, @protocol, @charset else @affected_rows, @insert_id, @server_status, @warning_count, @info = @protocol.affected_rows, @protocol.insert_id, @protocol.server_status, @protocol.warning_count, @protocol. end return self rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end end |
#fetch ⇒ Array
Returns current record data.
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
# File 'lib/mysql.rb', line 897 def fetch row = @result.fetch return row unless @bind_result row.zip(@bind_result).map do |col, type| if col.nil? nil elsif [Numeric, Integer, Fixnum].include? type col.to_i elsif type == String col.to_s elsif type == Float && !col.is_a?(Float) col.to_i.to_f elsif type == Mysql::Time && !col.is_a?(Mysql::Time) if col.to_s =~ /\A\d+\z/ i = col.to_s.to_i if i < 100000000 y = i/10000 m = i/100%100 d = i%100 h, mm, s = 0 else y = i/10000000000 m = i/100000000%100 d = i/1000000%100 h = i/10000%100 mm= i/100%100 s = i%100 end if y < 70 y += 2000 elsif y < 100 y += 1900 end Mysql::Time.new(y, m, d, h, mm, s) else Mysql::Time.new end else col end end end |
#fetch_hash(with_table = nil) ⇒ Hash
Return data of current record as Hash. The hash key is field name.
944 945 946 |
# File 'lib/mysql.rb', line 944 def fetch_hash(with_table=nil) @result.fetch_hash with_table end |
#field_count ⇒ Integer
Returns number of columns for last query.
1013 1014 1015 |
# File 'lib/mysql.rb', line 1013 def field_count @fields.length end |
#free_result ⇒ void
This method returns an undefined value.
ignore
1019 1020 |
# File 'lib/mysql.rb', line 1019 def free_result end |
#prepare(str) ⇒ Object
parse prepared-statement and return Mysql::Stmt object
849 850 851 852 853 854 855 856 857 858 859 860 861 |
# File 'lib/mysql.rb', line 849 def prepare(str) close begin @sqlstate = "00000" @statement_id, @param_count, @fields = @protocol.stmt_prepare_command(str) rescue ServerError => e @last_error = e @sqlstate = e.sqlstate raise end ObjectSpace.define_finalizer(self, self.class.finalizer(@protocol, @statement_id)) self end |
#result_metadata ⇒ Mysql::Result
Returns Mysql::Result object that is empty. Use fetch_fields to get list of fields.
1025 1026 1027 1028 |
# File 'lib/mysql.rb', line 1025 def return nil if @fields.empty? Result.new @fields end |
#row_seek(n) ⇒ Integer
Set current position of record
1008 1009 1010 |
# File 'lib/mysql.rb', line 1008 def row_seek(n) @result.row_seek(n) end |
#row_tell ⇒ Integer
Returns current record position.
1001 1002 1003 |
# File 'lib/mysql.rb', line 1001 def row_tell @result.row_tell end |
#size ⇒ Integer Also known as: num_rows
Returns number of record.
988 989 990 |
# File 'lib/mysql.rb', line 988 def size @result.size end |