# Sqlserver::Sequence [![Code Climate](https://codeclimate.com/github/zacharywelch/sqlserver-sequence/badges/gpa.svg)](https://codeclimate.com/github/zacharywelch/sqlserver-sequence) [![Issue Count](https://codeclimate.com/github/zacharywelch/sqlserver-sequence/badges/issue_count.svg)](https://codeclimate.com/github/zacharywelch/sqlserver-sequence) [![Gem Version](https://badge.fury.io/rb/sqlserver-sequence.svg)](https://badge.fury.io/rb/sqlserver-sequence) A Rails plugin for SQL Server sequences. ## Installation Add this line to your application's Gemfile: ```ruby gem 'sqlserver-sequence' ``` And then execute: $ bundle Or install it yourself as: $ gem install sqlserver-sequence ## Usage Specify the attribute you'd like to sequence ```ruby class Supplier < ActiveRecord::Base sequence :number end ``` Now when you create a record `number` will be assigned the next value for that sequence. $ supplier = Supplier.create $ supplier.number > 10 # generated by sequence named 'number' $ other_supplier = Supplier.create $ other_supplier.number > 11 You can also define multiple sequences ```ruby class Supplier < ActiveRecord::Base sequence :number sequence :purchase_order end ``` ### Options ###### name By default `sqlserver-sequence` will look for a row in `sys.sequences` with the same `sequence_name` as the attribute. You can use the `name` option if the sequence is named differently. ```ruby class Supplier < ActiveRecord::Base sequence :number, name: 'next_supplier_number' end ``` $ supplier = Supplier.create $ supplier.number > 10 # generated by sequence named 'next_supplier_number' ###### prefix You can use the `prefix` option to prepend every sequence value with a string. ```ruby class Supplier < ActiveRecord::Base sequence :number, prefix: 'S-' end ``` $ supplier = Supplier.create $ supplier.number > S-10 ###### format Pass a lambda to the `format` option if you need more control over the assigned sequence value, for example to assign a fixed length string. ```ruby class Supplier < ActiveRecord::Base sequence :number, prefix: 'S-', format: lambda { |num| num.rjust(10, 0) } end ``` $ supplier = Supplier.create $ supplier.number > S-0000000010 ## Configuration Override any of these defaults in `config/initializers/sqlserver_sequence.rb`: ```ruby Sqlserver::Sequence.configure do |config| config.next_value_strategy = Sqlserver::Sequence::Strategies::NextValueFor end ``` ### next_value_strategy By default `Sqlserver::Sequence` will use a strategy that implements SQL Server's [NEXT VALUE FOR](https://msdn.microsoft.com/en-us/library/ff878370.aspx) function. To change how sequences are generated you can also assign your own strategy: ```ruby # lib/size_strategy.rb module SizeStrategy def next_sequence_value(sequence_name) self.class.size end end # config/initializers/sqlserver_sequence.rb Sqlserver::Sequence.configure do |config| config.next_value_strategy = SizeStrategy end ``` ### Development and Testing without SQL Server If your test or development environment uses something other than SQL Server (e.g. SQLite) a `Simple` strategy is provided to avoid errors. ```ruby Sqlserver::Sequence.configure do |config| config.next_value_strategy = Sqlserver::Sequence::Strategies::Simple end ``` ## Contributing 1. Fork it ( https://github.com/zacharywelch/sqlserver-sequence/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request See the [Running Tests](RUNNING_TESTS.md) guide for details on how to run the test suite on SQL Server.